PixelLightAPI  .
Controller.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Controller.h                                   *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLINPUT_CONTROLLER_H__
00024 #define __PLINPUT_CONTROLLER_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLCore/Base/Object.h>
00032 #include "PLInput/PLInput.h"
00033 #include "PLInput/PLInputDefinitions.h"
00034 
00035 
00036 //[-------------------------------------------------------]
00037 //[ Namespace                                             ]
00038 //[-------------------------------------------------------]
00039 namespace PLInput {
00040 
00041 
00042 //[-------------------------------------------------------]
00043 //[ Forward declarations                                  ]
00044 //[-------------------------------------------------------]
00045 class Control;
00046 class Button;
00047 class Axis;
00048 class Connection;
00049 
00050 
00051 //[-------------------------------------------------------]
00052 //[ Classes                                               ]
00053 //[-------------------------------------------------------]
00054 /**
00055 *  @brief
00056 *    Input controller
00057 *
00058 *  @remarks
00059 *    A controller represents an input device, which can either be a real device like e.g. a mouse or joystick,
00060 *    or a virtual device that is used to map real input devices to virtual axes and keys. A controller consists
00061 *    of a list of controls, e.g. buttons or axes and provides methods to obtain the status.
00062 */
00063 class Controller : public PLCore::Object {
00064 
00065 
00066     //[-------------------------------------------------------]
00067     //[ Friends                                               ]
00068     //[-------------------------------------------------------]
00069     friend class InputManager;
00070     friend class Provider;
00071     friend class Control;
00072 
00073 
00074     //[-------------------------------------------------------]
00075     //[ Class definition                                      ]
00076     //[-------------------------------------------------------]
00077     pl_class(PLINPUT_RTTI_EXPORT, Controller, "PLInput", PLCore::Object, "Input controller base class")
00078         // Attributes
00079         pl_attribute(Type,          pl_enum_type(EControllerType),  ControllerVirtual,  ReadOnly,   GetSet, "Controller type",          "")
00080         pl_attribute(Name,          PLCore::String,                 "",                 ReadOnly,   GetSet, "Controller name",          "")
00081         pl_attribute(Description,   PLCore::String,                 "",                 ReadOnly,   GetSet, "Controller description",   "")
00082         pl_attribute(Active,        bool,                           true,               ReadWrite,  GetSet, "State of controller",      "")
00083         // Signals
00084         pl_signal_1(SignalOnActivate,   bool,       "Controller has been activated or deactivated. 'true' as parameter if the controller has been activated, else 'false'.",    "")
00085         pl_signal_1(SignalOnControl,    Control&,   "Control event has occurred, control as parameter",                                                                         "")
00086         pl_signal_0(SignalOnChanged,                "Controller state has changed",                                                                                             "")
00087     pl_class_end
00088 
00089 
00090     //[-------------------------------------------------------]
00091     //[ Public functions                                      ]
00092     //[-------------------------------------------------------]
00093     public:
00094         /**
00095         *  @brief
00096         *    Constructor
00097         *
00098         *  @param[in] nType
00099         *    Controller type
00100         *  @param[in] sName
00101         *    Controller name
00102         *  @param[in] sDescription
00103         *    Controller description
00104         */
00105         PLINPUT_API Controller(EControllerType nType, const PLCore::String &sName, const PLCore::String &sDescription);
00106 
00107         /**
00108         *  @brief
00109         *    Destructor
00110         */
00111         PLINPUT_API virtual ~Controller();
00112 
00113         /**
00114         *  @brief
00115         *    Get controller type
00116         *
00117         *  @return
00118         *    Controller type
00119         */
00120         PLINPUT_API EControllerType GetType() const;
00121 
00122         /**
00123         *  @brief
00124         *    Get controller name
00125         *
00126         *  @return
00127         *    Name
00128         */
00129         PLINPUT_API PLCore::String GetName() const;
00130 
00131         /**
00132         *  @brief
00133         *    Get controller description
00134         *
00135         *  @return
00136         *    Description
00137         */
00138         PLINPUT_API PLCore::String GetDescription() const;
00139 
00140         /**
00141         *  @brief
00142         *    Check if controller is active
00143         *
00144         *  @return
00145         *    'true' if controller is active, else 'false'
00146         *
00147         *  @remarks
00148         *    If a controller is active, it sends out signals when the state of it's controls has changed.
00149         *    If a controller is not active, no state changes will occur and all input events from connected
00150         *    devices will be discarded.
00151         */
00152         PLINPUT_API bool GetActive() const;
00153 
00154         /**
00155         *  @brief
00156         *    Activate or deactivate controller
00157         *
00158         *  @param[in] bActive
00159         *    'true' if controller is active, else 'false'
00160         *
00161         *  @remarks
00162         *    Virtual controllers can be activated or deactivated, real input devices
00163         *    are always active and can not be deactivated.
00164         */
00165         PLINPUT_API void SetActive(bool bActive);
00166 
00167         /**
00168         *  @brief
00169         *    Check if the controller's state has changed (for polling)
00170         *
00171         *  @return
00172         *    'true', if the state has changed, else 'false'
00173         */
00174         PLINPUT_API bool HasChanged() const;
00175 
00176         /**
00177         *  @brief
00178         *    Get all controls of the controller
00179         *
00180         *  @return
00181         *    List of controls
00182         */
00183         PLINPUT_API const PLCore::List<Control*> &GetControls() const;
00184 
00185         /**
00186         *  @brief
00187         *    Get all buttons
00188         *
00189         *  @return
00190         *    List of buttons
00191         */
00192         PLINPUT_API const PLCore::List<Button*> &GetButtons() const;
00193 
00194         /**
00195         *  @brief
00196         *    Get all axes
00197         *
00198         *  @return
00199         *    List of axes
00200         */
00201         PLINPUT_API const PLCore::List<Axis*> &GetAxes() const;
00202 
00203         /**
00204         *  @brief
00205         *    Get control with a specific name
00206         *
00207         *  @param[in] sName
00208         *    Name of control
00209         *
00210         *  @return
00211         *    Control, or a null pointer if no control with that name could be found
00212         */
00213         PLINPUT_API Control *GetControl(const PLCore::String &sName) const;
00214 
00215         /**
00216         *  @brief
00217         *    Get character of last button that was hit
00218         *
00219         *  @return
00220         *    Button character (ASCII), can be '\0'
00221         *
00222         *  @remarks
00223         *    This function returns the character code of the last button that was hit (not pressed!).
00224         *    The character will then be reset to '\0', so the next call will return '\0', until
00225         *    a new button is first pressed and then released.
00226         */
00227         PLINPUT_API char GetChar();
00228 
00229         /**
00230         *  @brief
00231         *    Get connections
00232         *
00233         *  @return
00234         *    List of connections (both incoming and outgoing), do not destroy the returned connection instances!
00235         *
00236         *  @remarks
00237         *    To determine whether a connection is incoming or outgoing, you can check e.g.
00238         *    GetOutputControl()->GetController() == this or something similar.
00239         */
00240         PLINPUT_API const PLCore::List<Connection*> &GetConnections();
00241 
00242         /**
00243         *  @brief
00244         *    Connect to another controller
00245         *
00246         *  @param[in] sControl
00247         *    Name of control of this controller (output control)
00248         *  @param[in] pControl
00249         *    Pointer to control (input control), shouldn't be a null pointer (but a null pointer is caught internally)
00250         *  @param[in] fScale
00251         *    Scale factor
00252         */
00253         PLINPUT_API void Connect(const PLCore::String &sControl, Control *pControl, float fScale = 1.0f);
00254 
00255         /**
00256         *  @brief
00257         *    Connect to another controller
00258         *
00259         *  @param[in] pController
00260         *    Pointer to controller containing the input controls, shouldn't be a null pointer (but a null pointer is caught internally)
00261         *  @param[in] sPrefixOut
00262         *    Prefix for controls of this controller
00263         *  @param[in] sPrefixIn
00264         *    Prefix for controls of the other controller
00265         *
00266         *  @remarks
00267         *    This connects all controls of the input controller (pController) to the controls of the output
00268         *    controller (this), if their names are equal, e.g. pController->"Left" will be connected to this->"Left".
00269         *    You can also provide a prefix for either or both sides, e.g.: ConnectAll(pOtherController, "", "Camera")
00270         *    will connect pController->"CameraLeft" to this->"Left".
00271         */
00272         PLINPUT_API void ConnectAll(Controller *pController, const PLCore::String &sPrefixOut, const PLCore::String &sPrefixIn);
00273 
00274         /**
00275         *  @brief
00276         *    Disconnect connection
00277         *
00278         *  @param[in] pConnection
00279         *    Connection (must be valid!), on successful disconnect, the given "pConnection" instance becomes invalid
00280         */
00281         PLINPUT_API void Disconnect(Connection *pConnection);
00282 
00283 
00284     //[-------------------------------------------------------]
00285     //[ Public virtual Controller functions                   ]
00286     //[-------------------------------------------------------]
00287     public:
00288         /**
00289         *  @brief
00290         *    Update device once per frame
00291         *
00292         *  @remarks
00293         *    This function can be used e.g. to reset any data of a device once per frame. Usually this is
00294         *    not needed, but some devices (e.g. RawInput mice etc.) need to reset their data once per frame.
00295         *
00296         *  @note
00297         *    - The default implementation is empty
00298         */
00299         PLINPUT_API virtual void Update();
00300 
00301         /**
00302         *  @brief
00303         *    Update output controls (LEDs, effects etc.)
00304         *
00305         *  @param[in] pControl
00306         *    Output control that has been changed, must be valid!
00307         *
00308         *  @remarks
00309         *    This function is called whenever an output control such as LED or Effect has been changed.
00310         *    A device should use this function to update the specific control state on the device (or update
00311         *    all output controls at the same time)
00312         *
00313         *  @note
00314         *    - The default implementation is empty
00315         */
00316         PLINPUT_API virtual void UpdateOutputControl(Control *pControl);
00317 
00318 
00319     //[-------------------------------------------------------]
00320     //[ Protected functions                                   ]
00321     //[-------------------------------------------------------]
00322     protected:
00323         /**
00324         *  @brief
00325         *    Add control
00326         *
00327         *  @param[in] pControl
00328         *    Pointer to control, must be valid!
00329         */
00330         PLINPUT_API void AddControl(Control *pControl);
00331 
00332         /**
00333         *  @brief
00334         *    Inform controller that a control has changed it's state
00335         *
00336         *  @param[in] pControl
00337         *    Pointer to control, must be valid!
00338         */
00339         PLINPUT_API void InformControl(Control *pControl);
00340 
00341         /**
00342         *  @brief
00343         *    Init control list
00344         *
00345         *  @param[in] nType
00346         *    Type of list that is to be filled
00347         */
00348         PLINPUT_API void InitControlList(EControlType nType) const;
00349 
00350         /**
00351         *  @brief
00352         *    Add connection
00353         *
00354         *  @param[in] pConnection
00355         *    Connection (must be valid!)
00356         */
00357         PLINPUT_API void AddConnection(Connection *pConnection);
00358 
00359         /**
00360         *  @brief
00361         *    Remove connection
00362         *
00363         *  @param[in] pConnection
00364         *    Connection (must be valid!)
00365         */
00366         PLINPUT_API void RemoveConnection(Connection *pConnection);
00367 
00368 
00369     //[-------------------------------------------------------]
00370     //[ Protected data                                        ]
00371     //[-------------------------------------------------------]
00372     protected:
00373         // Controller information and state
00374         EControllerType                             m_nType;            /**< Controller type */
00375         PLCore::String                              m_sName;            /**< Controller name */
00376         PLCore::String                              m_sDescription;     /**< Controller description */
00377         bool                                        m_bConfirmed;       /**< Confirmation flag for DetectDevices() */
00378         bool                                        m_bActive;          /**< Is the controller active? */
00379         mutable bool                                m_bChanged;         /**< Has the controller's state changed? */
00380 
00381         // Controls
00382         PLCore::List<Control*>                      m_lstControls;      /**< List of all controls */
00383         PLCore::HashMap<PLCore::String, Control*>   m_mapControls;      /**< Hash map of name -> control */
00384         mutable PLCore::List<Button*>               m_lstButtons;       /**< List of buttons (filled on use) */
00385         mutable PLCore::List<Axis*>                 m_lstAxes;          /**< List of absolute axes (filled on use) */
00386         char                                        m_nChar;            /**< Last hit key character */
00387 
00388         // Connections
00389         PLCore::List<Connection*>                   m_lstConnections;   /**< List of connections */
00390 
00391 
00392 };
00393 
00394 
00395 //[-------------------------------------------------------]
00396 //[ Namespace                                             ]
00397 //[-------------------------------------------------------]
00398 } // PLInput
00399 
00400 
00401 #endif // __PLINPUT_CONTROLLER_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:52
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported