Guest User

Untitled

a guest
Dec 1st, 2010
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. /*
  2.  * InputActions system for Gluon (or anything).
  3.  *
  4.  * This ia s system for implementing generic action bindings to button presses
  5.  * and axis movement. It is based on the Qt signal-and-slots events architecture
  6.  * which dispatches events to anything that subscribes to an InputAction via the
  7.  * standard QObject::connect system.
  8.  *
  9.  * There are currently no concrete implementations of ButtonPressInputAction or
  10.  * AxisMovementInputAction but the basic behavior for binding devices and/or
  11.  * buttons is available.
  12.  *
  13.  * Sample usage:
  14.  *
  15.  * ButtonPressInputAction confirmAction;
  16.  * confirmAction.bindDevice(KeyboardDevice, Z);
  17.  * QObject::connect(
  18.  *    &confirmAction,
  19.  *    SIGNAL(actionStateChanged(State, int, int)),
  20.  *    &eventReceiver,
  21.  *    SLOT(actionStateChanged(State, int, int))
  22.  * );
  23.  *
  24.  */
  25.  
  26. #include <QtCore/QObject>
  27. #include <QtCore/QList>
  28.  
  29. #include <gluon/core/singleton.h>
  30. #include <gluon/input/inputmanager.h>
  31.  
  32. using namespace GluonInput;
  33.  
  34. enum State
  35. {
  36.     None,
  37.     Up,
  38.     Down,
  39.     Move
  40. };
  41.  
  42. /*
  43.  * An input action component.
  44.  *
  45.  * This defines a base action, such as "jump" or "attack".
  46.  */
  47. class InputAction : public QObject
  48. {
  49.     Q_OBJECT
  50.  
  51. signals:
  52.     /*
  53.      * This signal notifies listeners that an input state has changed. Depending
  54.      * on the type of input action (axis vs. press), the axis and value
  55.      * parameters may or may not have meaning.
  56.      */
  57.     void actionStateChanged(State state, int axis, int value);
  58.  
  59. public:
  60.     /*
  61.      * Emit an event based on the action state chenge.
  62.      *
  63.      * Override in a subclass.
  64.      */
  65.     virtual void update() = 0;
  66.  
  67.     InputAction(QObject *parent = 0)
  68.         : QObject(parent)
  69.     {
  70.     }
  71. };
  72.  
  73. /*
  74.  * An axis-movement based input action.
  75.  */
  76. class AxisMovementInputAction : public InputAction
  77. {
  78.     Q_OBJECT
  79.    
  80.     QList<DeviceFlag> m_deviceTypes;
  81.  
  82. public:
  83.     AxisMovementInputAction(QObject *parent = 0)
  84.         : InputAction(parent)
  85.     {
  86.         this->m_deviceTypes = QList<DeviceFlag>();
  87.     }
  88.  
  89.     virtual void update()
  90.     {
  91.         // TODO: implement something here using gluon input subsystem
  92.         emit this->actionStateChanged(Move, 0, 0);
  93.     }
  94.    
  95.     void bindDevice(DeviceFlag deviceType)
  96.     {
  97.         this->m_deviceTypes.append(deviceType);
  98.     }
  99. };
  100.  
  101. /*
  102.  * A button press based input action.
  103.  */
  104. class ButtonPressInputAction : public InputAction
  105. {
  106.     Q_OBJECT
  107.    
  108.     QList<QPair<DeviceFlag, int> > m_buttonBindings;
  109.  
  110. public:
  111.     ButtonPressInputAction(QObject *parent = 0)
  112.         : InputAction(parent)
  113.     {
  114.         this->m_buttonBindings = QList<QPair<DeviceFlag, int> >();
  115.     }
  116.  
  117.     virtual void update()
  118.     {
  119.         // TODO: implement something here using gluon input subsystem
  120.         //
  121.         // also note that the additional parameters for actionStateChanged are
  122.         // meaningless, as pressing a button has no axis or distance moved.
  123.         emit this->actionStateChanged(Down, 0, 0);
  124.     }
  125.    
  126.     void bindDevice(DeviceFlag deviceType, int button)
  127.     {
  128.         this->m_buttonBindings.append(QPair<DeviceFlag, int>(deviceType, button));
  129.     }
  130. };
  131.  
  132. /*
  133.  * Manager for all input actions.
  134.  *
  135.  * Runs update as necessary.
  136.  */
  137. class InputActionManager : public GluonCore::Singleton<InputActionManager>
  138. {
  139.     Q_OBJECT
  140.    
  141.     QList<InputAction *> m_actions;
  142.    
  143. public:
  144.     InputActionManager()
  145.         : GluonCore::Singleton<InputActionManager>()
  146.     {
  147.         this->m_actions = QList<InputAction *>();
  148.     }
  149.  
  150.     /*
  151.      * Call update on all child actions. Should be called every frame as
  152.      * necessary.
  153.      */
  154.     void update()
  155.     {
  156.         foreach(InputAction *action_p, this->m_actions)
  157.         {
  158.             action_p->update();
  159.         }
  160.     }
  161.  
  162.     /*
  163.      * Register an action with the InputActionManager.
  164.      */
  165.     void registerAction(InputAction &action)
  166.     {
  167.         this->m_actions.append(&action);
  168.     }
  169. };
Advertisement
Add Comment
Please, Sign In to add comment