actionMap.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _ACTIONMAP_H_
  23. #define _ACTIONMAP_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _VECTOR_H_
  28. #include "collection/vector.h"
  29. #endif
  30. #ifndef _SIMBASE_H_
  31. #include "sim/simBase.h"
  32. #endif
  33. struct InputEvent;
  34. struct EventDescriptor
  35. {
  36. U8 flags; ///< Combination of any modifier flags.
  37. U8 eventType; ///< SI_KEY, etc.
  38. U16 eventCode; ///< From event.h
  39. };
  40. /// Map raw inputs to a variety of actions. This is used for all keymapping
  41. /// in the engine.
  42. /// @see ActionMap::Node
  43. class ActionMap : public SimObject
  44. {
  45. typedef SimObject Parent;
  46. protected:
  47. bool onAdd();
  48. struct Node {
  49. U32 modifiers;
  50. U32 action;
  51. enum Flags {
  52. Ranged = BIT(0), ///< Ranged input.
  53. HasScale = BIT(1), ///< Scaled input.
  54. HasDeadZone = BIT(2), ///< Dead zone is present.
  55. Inverted = BIT(3), ///< Input is inverted.
  56. BindCmd = BIT(4) ///< Bind a console command to this.
  57. };
  58. U32 flags; /// @see Node::Flags
  59. F32 deadZoneBegin;
  60. F32 deadZoneEnd;
  61. F32 scaleFactor;
  62. SimObject* object; ///< Object to call consoleFunction on.
  63. StringTableEntry consoleFunction; ///< Console function to call with new values.
  64. char *makeConsoleCommand; ///< Console command to execute when we make this command.
  65. char *breakConsoleCommand; ///< Console command to execute when we break this command.
  66. };
  67. /// Used to represent a devices.
  68. struct DeviceMap
  69. {
  70. U32 deviceType;
  71. U32 deviceInst;
  72. Vector<Node> nodeMap;
  73. DeviceMap() {
  74. VECTOR_SET_ASSOCIATION(nodeMap);
  75. }
  76. ~DeviceMap();
  77. };
  78. struct BreakEntry
  79. {
  80. U32 deviceType;
  81. U32 deviceInst;
  82. U32 objInst;
  83. SimObject* object;
  84. StringTableEntry consoleFunction;
  85. char *breakConsoleCommand;
  86. // It's possible that the node could be deleted (unlikely, but possible,
  87. // so we replicate the node flags here...
  88. //
  89. U32 flags;
  90. F32 deadZoneBegin;
  91. F32 deadZoneEnd;
  92. F32 scaleFactor;
  93. // [neo, 5/7/2007 - #2975]
  94. // object above can be deleted in between a make/break and so object will point
  95. // to turfed memory and crash. To keep things simple we just store id as well so
  96. // we can look it up to validate object ref.
  97. S32 objectId;
  98. };
  99. Vector<DeviceMap*> mDeviceMaps;
  100. static Vector<BreakEntry> smBreakTable;
  101. // Find: return NULL if not found in current map, Get: create if not
  102. // found.
  103. const Node* findNode(const U32 inDeviceType, const U32 inDeviceInst,
  104. const U32 inModifiers, const U32 inAction);
  105. bool findBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex );
  106. bool nextBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex );
  107. Node* getNode(const U32 inDeviceType, const U32 inDeviceInst,
  108. const U32 inModifiers, const U32 inAction,
  109. SimObject* object = NULL);
  110. void removeNode(const U32 inDeviceType, const U32 inDeviceInst,
  111. const U32 inModifiers, const U32 inAction,
  112. SimObject* object = NULL);
  113. void enterBreakEvent(const InputEvent* pEvent, const Node* pNode);
  114. static const char* getModifierString(const U32 modifiers);
  115. public:
  116. ActionMap();
  117. ~ActionMap();
  118. void dumpActionMap(const char* fileName, const bool append) const;
  119. static bool createEventDescriptor(const char* pEventString, EventDescriptor* pDescriptor);
  120. bool processBind(const U32 argc, const char** argv, SimObject* object = NULL);
  121. bool processBindCmd(const char *device, const char *action, const char *makeCmd, const char *breakCmd);
  122. bool processUnbind(const char *device, const char *action, SimObject* object = NULL);
  123. /// @name Console Interface Functions
  124. /// @{
  125. const char* getBinding( const char* command ); ///< Find what the given command is bound to.
  126. const char* getCommand( const char* device, const char* action ); ///< Find what command is bound to the given event descriptor .
  127. bool isInverted( const char* device, const char* action );
  128. F32 getScale( const char* device, const char* action );
  129. const char* getDeadZone( const char* device, const char* action );
  130. /// @}
  131. static bool getKeyString(const U32 action, char* buffer);
  132. static bool getDeviceName(const U32 deviceType, const U32 deviceInstance, char* buffer);
  133. static const char* buildActionString( const InputEvent* event );
  134. bool processAction(const InputEvent*);
  135. static bool checkBreakTable(const InputEvent*);
  136. static bool handleEvent(const InputEvent*);
  137. static bool handleEventGlobal(const InputEvent*);
  138. static bool getDeviceTypeAndInstance(const char *device, U32 &deviceType, U32 &deviceInstance);
  139. DECLARE_CONOBJECT(ActionMap);
  140. };
  141. #endif // _ACTIONMAP_H_