actionMap.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _SIMBASE_H_
  31. #include "console/simBase.h"
  32. #endif
  33. struct InputEventInfo;
  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. NonLinear = BIT(4), ///< Input should be re-fit to a non-linear scale
  57. BindCmd = BIT(5) ///< Bind a console command to this.
  58. };
  59. U32 flags; ///< @see Node::Flags
  60. F32 deadZoneBegin;
  61. F32 deadZoneEnd;
  62. F32 scaleFactor;
  63. SimObject* object; ///< Object to call consoleFunction on.
  64. StringTableEntry consoleFunction; ///< Console function to call with new values.
  65. char *makeConsoleCommand; ///< Console command to execute when we make this command.
  66. char *breakConsoleCommand; ///< Console command to execute when we break this command.
  67. };
  68. /// Used to represent a devices.
  69. struct DeviceMap
  70. {
  71. U32 deviceType;
  72. U32 deviceInst;
  73. Vector<Node> nodeMap;
  74. DeviceMap() {
  75. VECTOR_SET_ASSOCIATION(nodeMap);
  76. }
  77. ~DeviceMap();
  78. };
  79. struct BreakEntry
  80. {
  81. U32 deviceType;
  82. U32 deviceInst;
  83. U32 objInst;
  84. SimObject* object;
  85. StringTableEntry consoleFunction;
  86. char *breakConsoleCommand;
  87. // It's possible that the node could be deleted (unlikely, but possible,
  88. // so we replicate the node flags here...
  89. //
  90. U32 flags;
  91. F32 deadZoneBegin;
  92. F32 deadZoneEnd;
  93. F32 scaleFactor;
  94. };
  95. Vector<DeviceMap*> mDeviceMaps;
  96. static Vector<BreakEntry> smBreakTable;
  97. // Find: return NULL if not found in current map, Get: create if not
  98. // found.
  99. const Node* findNode(const U32 inDeviceType, const U32 inDeviceInst,
  100. const U32 inModifiers, const U32 inAction);
  101. bool findBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex );
  102. bool nextBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex );
  103. Node* getNode(const U32 inDeviceType, const U32 inDeviceInst,
  104. const U32 inModifiers, const U32 inAction,
  105. SimObject* object = NULL);
  106. void removeNode(const U32 inDeviceType, const U32 inDeviceInst,
  107. const U32 inModifiers, const U32 inAction,
  108. SimObject* object = NULL);
  109. void enterBreakEvent(const InputEventInfo* pEvent, const Node* pNode);
  110. static const char* getModifierString(const U32 modifiers);
  111. /// Pass index to a break entry, and this function will fire it off.
  112. static void fireBreakEvent(U32 idx, F32 value = 0.f);
  113. public:
  114. ActionMap();
  115. ~ActionMap();
  116. void dumpActionMap(const char* fileName, const bool append) const;
  117. static bool createEventDescriptor(const char* pEventString, EventDescriptor* pDescriptor);
  118. bool processBind(const U32 argc, const char** argv, SimObject* object = NULL);
  119. bool processBindCmd(const char *device, const char *action, const char *makeCmd, const char *breakCmd);
  120. bool processUnbind(const char *device, const char *action, SimObject* object = NULL);
  121. /// @name Console Interface Functions
  122. /// @{
  123. const char* getBinding ( const char* command ); ///< Find what the given command is bound to.
  124. const char* getCommand ( const char* device, const char* action ); ///< Find what command is bound to the given event descriptor .
  125. bool isInverted ( const char* device, const char* action );
  126. F32 getScale ( const char* device, const char* action );
  127. const char* getDeadZone( const char* device, const char* action );
  128. /// @}
  129. static bool getKeyString(const U32 action, char* buffer);
  130. static bool getDeviceName(const U32 deviceType, const U32 deviceInstance, char* buffer);
  131. static const char* buildActionString( const InputEventInfo* event );
  132. bool processAction(const InputEventInfo*);
  133. /// Return true if the given event triggers is bound to an action in this map.
  134. bool isAction( U32 deviceType, U32 deviceInst, U32 modifiers, U32 action );
  135. /// Returns the global ActionMap.
  136. static ActionMap* getGlobalMap();
  137. static bool checkBreakTable(const InputEventInfo*);
  138. static bool handleEvent(const InputEventInfo*);
  139. static bool handleEventGlobal(const InputEventInfo*);
  140. /// Called when we lose focus, to make sure we have no dangling inputs.
  141. ///
  142. /// This fires a break event for every currently pending item in the break
  143. /// table.
  144. static void clearAllBreaks();
  145. /// Returns true if the specified key + modifiers are bound to something
  146. /// on the global action map.
  147. static bool checkAsciiGlobal(U16 key, U32 modifiers);
  148. static bool getDeviceTypeAndInstance(const char *device, U32 &deviceType, U32 &deviceInstance);
  149. DECLARE_CONOBJECT(ActionMap);
  150. };
  151. #endif // _ACTIONMAP_H_