Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * InputActions system for Gluon (or anything).
- *
- * This ia s system for implementing generic action bindings to button presses
- * and axis movement. It is based on the Qt signal-and-slots events architecture
- * which dispatches events to anything that subscribes to an InputAction via the
- * standard QObject::connect system.
- *
- * There are currently no concrete implementations of ButtonPressInputAction or
- * AxisMovementInputAction but the basic behavior for binding devices and/or
- * buttons is available.
- *
- * Sample usage:
- *
- * ButtonPressInputAction confirmAction;
- * confirmAction.bindDevice(KeyboardDevice, Z);
- * QObject::connect(
- * &confirmAction,
- * SIGNAL(actionStateChanged(State, int, int)),
- * &eventReceiver,
- * SLOT(actionStateChanged(State, int, int))
- * );
- *
- */
- #include <QtCore/QObject>
- #include <QtCore/QList>
- #include <gluon/core/singleton.h>
- #include <gluon/input/inputmanager.h>
- using namespace GluonInput;
- enum State
- {
- None,
- Up,
- Down,
- Move
- };
- /*
- * An input action component.
- *
- * This defines a base action, such as "jump" or "attack".
- */
- class InputAction : public QObject
- {
- Q_OBJECT
- signals:
- /*
- * This signal notifies listeners that an input state has changed. Depending
- * on the type of input action (axis vs. press), the axis and value
- * parameters may or may not have meaning.
- */
- void actionStateChanged(State state, int axis, int value);
- public:
- /*
- * Emit an event based on the action state chenge.
- *
- * Override in a subclass.
- */
- virtual void update() = 0;
- InputAction(QObject *parent = 0)
- : QObject(parent)
- {
- }
- };
- /*
- * An axis-movement based input action.
- */
- class AxisMovementInputAction : public InputAction
- {
- Q_OBJECT
- QList<DeviceFlag> m_deviceTypes;
- public:
- AxisMovementInputAction(QObject *parent = 0)
- : InputAction(parent)
- {
- this->m_deviceTypes = QList<DeviceFlag>();
- }
- virtual void update()
- {
- // TODO: implement something here using gluon input subsystem
- emit this->actionStateChanged(Move, 0, 0);
- }
- void bindDevice(DeviceFlag deviceType)
- {
- this->m_deviceTypes.append(deviceType);
- }
- };
- /*
- * A button press based input action.
- */
- class ButtonPressInputAction : public InputAction
- {
- Q_OBJECT
- QList<QPair<DeviceFlag, int> > m_buttonBindings;
- public:
- ButtonPressInputAction(QObject *parent = 0)
- : InputAction(parent)
- {
- this->m_buttonBindings = QList<QPair<DeviceFlag, int> >();
- }
- virtual void update()
- {
- // TODO: implement something here using gluon input subsystem
- //
- // also note that the additional parameters for actionStateChanged are
- // meaningless, as pressing a button has no axis or distance moved.
- emit this->actionStateChanged(Down, 0, 0);
- }
- void bindDevice(DeviceFlag deviceType, int button)
- {
- this->m_buttonBindings.append(QPair<DeviceFlag, int>(deviceType, button));
- }
- };
- /*
- * Manager for all input actions.
- *
- * Runs update as necessary.
- */
- class InputActionManager : public GluonCore::Singleton<InputActionManager>
- {
- Q_OBJECT
- QList<InputAction *> m_actions;
- public:
- InputActionManager()
- : GluonCore::Singleton<InputActionManager>()
- {
- this->m_actions = QList<InputAction *>();
- }
- /*
- * Call update on all child actions. Should be called every frame as
- * necessary.
- */
- void update()
- {
- foreach(InputAction *action_p, this->m_actions)
- {
- action_p->update();
- }
- }
- /*
- * Register an action with the InputActionManager.
- */
- void registerAction(InputAction &action)
- {
- this->m_actions.append(&action);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment