Navigation Menu

Skip to content

Commit

Permalink
Refactor|InputSystem|Client: Hide the meaning of InputDevice(Control)…
Browse files Browse the repository at this point in the history
… IDs at API level

Users of the InputDevices should not assume anything from the IDs
assigned to devices and/or their controls.
  • Loading branch information
danij-deng committed Nov 1, 2014
1 parent 7ecd105 commit 0387374
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 311 deletions.
4 changes: 3 additions & 1 deletion doomsday/client/include/ui/b_command.h
Expand Up @@ -24,6 +24,8 @@
#include "b_util.h"
#include "dd_input.h"

struct bcontext_t;

typedef struct evbinding_s {
struct evbinding_s *prev; ///< Previous in list of bindings.
struct evbinding_s *next; ///< Next in list of bindings.
Expand Down Expand Up @@ -86,6 +88,6 @@ evbinding_t *B_FindCommandBinding(evbinding_t const *listRoot, char const *comma
* @return Action to be triggered, or @c nullptr. Caller gets ownership.
*/
de::Action *EventBinding_ActionForEvent(evbinding_t *eb, ddevent_t const *event,
struct bcontext_s *eventClass, bool respectHigherAssociatedContexts);
bcontext_t *eventClass, bool respectHigherAssociatedContexts);

#endif // CLIENT_INPUTSYSTEM_EVENTBINDING_H
5 changes: 3 additions & 2 deletions doomsday/client/include/ui/b_context.h
Expand Up @@ -39,14 +39,15 @@ typedef struct controlbinding_s {
higher-priority contexts override it. */
#define BCF_ACQUIRE_ALL 0x08 ///< Context will acquire all unacquired states.

typedef struct bcontext_s {
struct bcontext_t
{
char *name; ///< Name of the binding context.
byte flags;
evbinding_t commandBinds; ///< List of command bindings.
controlbinding_t controlBinds;
int (*ddFallbackResponder)(ddevent_t const *ddev);
int (*fallbackResponder)(event_t *event);
} bcontext_t;
};

/**
* Marks all device states with the highest-priority binding context to
Expand Down
4 changes: 3 additions & 1 deletion doomsday/client/include/ui/b_device.h
Expand Up @@ -22,6 +22,8 @@

#include "b_util.h"

struct bcontext_t;

typedef enum cbdevtype_e {
CBD_TOGGLE = E_TOGGLE,
CBD_AXIS = E_AXIS,
Expand Down Expand Up @@ -65,7 +67,7 @@ void B_DestroyDeviceBinding(dbinding_t *cb);
void B_DeviceBindingToString(dbinding_t const *b, ddstring_t *str);

void B_EvaluateDeviceBindingList(int localNum, dbinding_t *listRoot, float *pos,
float *relativeOffset, struct bcontext_s *controlClass, dd_bool allowTriggered);
float *relativeOffset, bcontext_t *controlClass, dd_bool allowTriggered);

#endif // CLIENT_INPUTSYSTEM_DEVICEBINDING_H

4 changes: 3 additions & 1 deletion doomsday/client/include/ui/b_main.h
Expand Up @@ -23,6 +23,8 @@
#include <de/types.h>
#include "dd_input.h"

struct bcontext_t;

#define DEFAULT_BINDING_CONTEXT_NAME "game"
#define CONSOLE_BINDING_CONTEXT_NAME "console"
#define UI_BINDING_CONTEXT_NAME "deui"
Expand Down Expand Up @@ -69,7 +71,7 @@ struct evbinding_s *B_BindCommand(char const *eventDesc, char const *command);

struct dbinding_s *B_BindControl(char const *controlDesc, char const *device);

struct dbinding_s *B_GetControlDeviceBindings(int localNum, int control, struct bcontext_s **bContext);
struct dbinding_s *B_GetControlDeviceBindings(int localNum, int control, bcontext_t **bContext);

bool B_UnbindCommand(char const *command);

Expand Down
23 changes: 14 additions & 9 deletions doomsday/client/include/ui/b_util.h
Expand Up @@ -23,8 +23,11 @@
#include "dd_types.h"
#include "dd_input.h"

struct bcontext_t;

// Event Binding Toggle State
typedef enum ebstate_e {
enum ebstate_t
{
EBTOG_UNDEFINED,
EBTOG_DOWN,
EBTOG_REPEAT,
Expand All @@ -34,18 +37,20 @@ typedef enum ebstate_e {
EBAXIS_BEYOND,
EBAXIS_BEYOND_POSITIVE,
EBAXIS_BEYOND_NEGATIVE
} ebstate_t;
};

typedef enum stateconditiontype_e {
enum stateconditiontype_t
{
SCT_STATE, ///< Related to the state of the engine.
SCT_TOGGLE_STATE, ///< Toggle is in a specific state.
SCT_MODIFIER_STATE, ///< Modifier is in a specific state.
SCT_AXIS_BEYOND, ///< Axis is past a specific position.
SCT_ANGLE_AT ///< Angle is pointing to a specific direction.
} stateconditiontype_t;
};

// Device state condition.
typedef struct statecondition_s {
struct statecondition_t
{
uint device; ///< Which device?
stateconditiontype_t type;
int id; ///< Toggle/axis/angle identifier in the device.
Expand All @@ -55,7 +60,7 @@ typedef struct statecondition_s {
uint negate:1; ///< Test the inverse (e.g., not in a specific state).
uint multiplayer:1; ///< Only for multiplayer.
} flags;
} statecondition_t;
};

dd_bool B_ParseToggleState(char const *toggleName, ebstate_t *state);

Expand All @@ -65,7 +70,7 @@ dd_bool B_ParseKeyId(char const *desc, int *id);

dd_bool B_ParseMouseTypeAndId(char const *desc, ddeventtype_t *type, int *id);

dd_bool B_ParseJoystickTypeAndId(int device, char const *desc, ddeventtype_t *type, int *id);
dd_bool B_ParseJoystickTypeAndId(InputDevice const &device, char const *desc, ddeventtype_t *type, int *id);

dd_bool B_ParseAnglePosition(char const *desc, float *pos);

Expand All @@ -78,11 +83,11 @@ dd_bool B_CheckAxisPos(ebstate_t test, float testPos, float pos);
* @param localNum Local player number.
* @param context Relevant binding context, if any (may be @c nullptr).
*/
dd_bool B_CheckCondition(statecondition_t *cond, int localNum, struct bcontext_s *context);
dd_bool B_CheckCondition(statecondition_t *cond, int localNum, bcontext_t *context);

dd_bool B_EqualConditions(statecondition_t const *a, statecondition_t const *b);

void B_AppendDeviceDescToString(int device, ddeventtype_t type, int id, ddstring_t *str);
void B_AppendDeviceDescToString(InputDevice const &device, ddeventtype_t type, int id, ddstring_t *str);

void B_AppendToggleStateToString(ebstate_t state, ddstring_t *str);

Expand Down
121 changes: 94 additions & 27 deletions doomsday/client/include/ui/dd_input.h
Expand Up @@ -20,27 +20,16 @@
#ifndef CLIENT_CORE_INPUT_H
#define CLIENT_CORE_INPUT_H

#include <functional>
#include <QFlags>
#include <de/smoother.h>
#include <de/Error>
#include <de/Event>
#include <de/String>

#include "api_event.h"

// Binding association flags:
#define IDAF_EXPIRED 0x1 /** The state has expired. The device is considered to remain
in default state until the flag gets cleared (which happens when
the real device state returns to its default). */
#define IDAF_TRIGGERED 0x2 /** The state has been triggered. This is cleared when someone checks
the device state. (Only for toggles.) */

// Binding association. How the device control relates to binding contexts.
struct inputdevassoc_t
{
struct bcontext_s *bContext;
struct bcontext_s *prevBContext;
int flags;
};
struct bcontext_t;

class InputDeviceAxisControl;
class InputDeviceButtonControl;
Expand All @@ -66,12 +55,35 @@ class InputDevice
/// No InputDevice is associated with the control. @ingroup errors
DENG2_ERROR(MissingDeviceError);

/**
* How the control state relates to binding contexts.
*/
enum BindContextAssociationFlag {
/// The state has expired. The control is considered to remain in default
/// state until the flag gets cleared (which happens when the real control
/// state returns to its default).
Expired = 0x1,

/// The state has been triggered. This is cleared when someone checks
/// the control state. (Only for toggles).
Triggered = 0x2,

DefaultFlags = 0
};
Q_DECLARE_FLAGS(BindContextAssociation, BindContextAssociationFlag)

public:
explicit Control(InputDevice *device = nullptr);
virtual ~Control() {}
virtual ~Control();

DENG2_AS_IS_METHODS()

/**
* Returns @c true if the control is presently in its default state.
* (e.g., button is not pressed, axis is at center, etc...).
*/
virtual bool inDefaultState() const = 0;

/**
* Reset the control back to its default state. Note that any attributed
* property values (name, device and binding association) are unaffected.
Expand Down Expand Up @@ -127,20 +139,52 @@ class InputDevice
void setDevice(InputDevice *newDevice);

/**
* Provides access to the binding association data of the control.
* Returns the bcontext_t attributed to the control; otherwise @c nullptr.
*
* @see hasBindContext(), setBindContext()
*/
bcontext_t *bindContext() const;

/**
* Returns @c true of a bcontext_t is attributed to the control.
*
* @see bindContext(), setBindContext()
*/
inline bool hasBindContext() const { return bindContext() != nullptr; }

/**
* Change the attributed bcontext_t to @a newContext.
*
* @param newContext bcontext_t to attribute. Ownership is unaffected.
*
* @see hasBindContext(), bindContext()
*/
void setBindContext(bcontext_t *newContext);

/**
* Returns the BindContextAssociation flags for the control.
*/
BindContextAssociation bindContextAssociation() const;

/**
* Change the BindContextAssociation flags for the control.
*
* @param flagsToChange Association flags to change.
* @param op Logical operation to perform.
*/
inputdevassoc_t &association();
inputdevassoc_t const &association() const;
void setBindContextAssociation(BindContextAssociation const &flagsToChange,
de::FlagOp op = de::SetFlags);

void clearBindContextAssociation();
void expireBindContextAssociationIfChanged();

/**
* Register the console commands and variables of the control.
*/
virtual void consoleRegister() {}

private:
InputDevice *_device;
de::String _name; ///< Symbolic
inputdevassoc_t _assoc; ///< Binding association.
DENG2_PRIVATE(d)
};

public:
Expand Down Expand Up @@ -196,7 +240,10 @@ class InputDevice
*/
void reset();

void clearAllControlContextAssociations();
/**
* Iterate through all the controls of the device.
*/
de::LoopResult forAllControls(std::function<de::LoopResult (Control &)> func);

/**
* Translate a symbolic axis @a name to the associated unique axis id.
Expand All @@ -205,6 +252,11 @@ class InputDevice
*/
de::dint toAxisId(de::String const &name) const;

/**
* Returns @c true if @a id is a known axis control.
*/
bool hasAxis(de::dint id) const;

/**
* Lookup an axis control by unique @a id.
*
Expand Down Expand Up @@ -233,6 +285,11 @@ class InputDevice
*/
de::dint toButtonId(de::String const &name) const;

/**
* Returns @c true if @a id is a known button control.
*/
bool hasButton(de::dint id) const;

/**
* Lookup a button control by unique @a id.
*
Expand All @@ -249,6 +306,11 @@ class InputDevice
*/
de::dint buttonCount() const;

/**
* Returns @c true if @a id is a known hat control.
*/
bool hasHat(de::dint id) const;

/**
* Lookup a hat control by unique @a id.
*
Expand All @@ -274,6 +336,8 @@ class InputDevice
DENG2_PRIVATE(d)
};

Q_DECLARE_OPERATORS_FOR_FLAGS(InputDevice::Control::BindContextAssociation)

typedef InputDevice::Control InputDeviceControl;

// Input device axis flags for use with cvars:
Expand Down Expand Up @@ -341,9 +405,9 @@ class InputDeviceAxisControl : public InputDeviceControl
de::duint time() const;

void update(timespan_t ticLength);
void clearContextAssociation();

de::String description() const;
bool inDefaultState() const;
void reset();
void consoleRegister();

Expand Down Expand Up @@ -377,9 +441,8 @@ class InputDeviceButtonControl : public InputDeviceControl
*/
de::duint time() const;

void clearContextAssociation();

de::String description() const;
bool inDefaultState() const;
void reset();

private:
Expand Down Expand Up @@ -413,9 +476,8 @@ class InputDeviceHatControl : public InputDeviceControl
*/
de::duint time() const;

void clearContextAssociation();

de::String description() const;
bool inDefaultState() const;

private:
de::dint _pos = -1; ///< Current position. @c -1= centered.
Expand Down Expand Up @@ -540,6 +602,11 @@ InputDevice &I_Device(int id);
*/
InputDevice *I_DevicePtr(int id);

/**
* Iterate through all the InputDevices.
*/
de::LoopResult I_ForAllDevices(std::function<de::LoopResult (InputDevice &)> func);

/**
* Initializes the key mappings to the default values.
*/
Expand Down

0 comments on commit 0387374

Please sign in to comment.