actionMap.h

Engine/source/sim/actionMap.h

More...

Classes:

class

Map raw inputs to a variety of actions.

class

Used to represent a devices.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _ACTIONMAP_H_
 25#define _ACTIONMAP_H_
 26
 27#ifndef _PLATFORM_H_
 28#include "platform/platform.h"
 29#endif
 30#ifndef _TVECTOR_H_
 31#include "core/util/tVector.h"
 32#endif
 33#ifndef _SIMBASE_H_
 34#include "console/simBase.h"
 35#endif
 36
 37struct InputEventInfo;
 38
 39struct EventDescriptor
 40{
 41   U8  flags;      ///< Combination of any modifier flags.
 42   U8  eventType;  ///< SI_KEY, etc.
 43   U16 eventCode;  ///< From event.h
 44};
 45
 46/// Map raw inputs to a variety of actions.  This is used for all keymapping
 47/// in the engine.
 48/// @see ActionMap::Node
 49class ActionMap : public SimObject
 50{
 51   typedef SimObject Parent;
 52
 53  protected:
 54   bool onAdd();
 55
 56   struct Node {
 57      U32 modifiers;
 58      U32 action;
 59
 60      enum Flags {
 61         Ranged      = BIT(0),   ///< Ranged input.
 62         HasScale    = BIT(1),   ///< Scaled input.
 63         HasDeadZone = BIT(2),   ///< Dead zone is present.
 64         Inverted    = BIT(3),   ///< Input is inverted.
 65         NonLinear   = BIT(4),   ///< Input should be re-fit to a non-linear scale
 66         BindCmd     = BIT(5)    ///< Bind a console command to this.
 67      };
 68
 69      U32 flags;           ///< @see Node::Flags
 70      F32 deadZoneBegin;
 71      F32 deadZoneEnd;
 72      F32 scaleFactor;
 73
 74      SimObject* object;                ///< Object to call consoleFunction on.
 75      StringTableEntry consoleFunction; ///< Console function to call with new values.
 76
 77      char *makeConsoleCommand;         ///< Console command to execute when we make this command.
 78      char *breakConsoleCommand;        ///< Console command to execute when we break this command.
 79   };
 80
 81   /// Used to represent a devices.
 82   struct DeviceMap
 83   {
 84      U32 deviceType;
 85      U32 deviceInst;
 86
 87      Vector<Node> nodeMap;
 88      DeviceMap() {
 89         VECTOR_SET_ASSOCIATION(nodeMap);
 90      }
 91      ~DeviceMap();
 92   };
 93   struct BreakEntry
 94   {
 95      U32 deviceType;
 96      U32 deviceInst;
 97      U32 objInst;
 98      SimObject* object;
 99      StringTableEntry consoleFunction;
100      char *breakConsoleCommand;
101
102      // It's possible that the node could be deleted (unlikely, but possible,
103      //  so we replicate the node flags here...
104      //
105      U32 flags;
106      F32 deadZoneBegin;
107      F32 deadZoneEnd;
108      F32 scaleFactor;
109   };
110
111
112   Vector<DeviceMap*>        mDeviceMaps;
113   static Vector<BreakEntry> smBreakTable;
114
115   // Find: return NULL if not found in current map, Get: create if not
116   //  found.
117   const Node* findNode(const U32 inDeviceType, const U32 inDeviceInst,
118                        const U32 inModifiers,  const U32 inAction);
119   bool findBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex );
120   bool nextBoundNode( const char* function, U32 &devMapIndex, U32 &nodeIndex );
121   Node* getNode(const U32 inDeviceType, const U32 inDeviceInst,
122                 const U32 inModifiers,  const U32 inAction,
123                 SimObject* object = NULL);
124
125   void removeNode(const U32 inDeviceType, const U32 inDeviceInst,
126                 const U32 inModifiers,  const U32 inAction,
127                 SimObject* object = NULL);
128
129   void enterBreakEvent(const InputEventInfo* pEvent, const Node* pNode);
130
131   static const char* getModifierString(const U32 modifiers);
132
133   /// Pass index to a break entry, and this function will fire it off.
134   static void fireBreakEvent(U32 idx, F32 value = 0.f);
135
136  public:
137   ActionMap();
138   ~ActionMap();
139
140   void dumpActionMap(const char* fileName, const bool append) const;
141
142   static bool createEventDescriptor(const char* pEventString, EventDescriptor* pDescriptor);
143
144   bool processBind(const U32 argc, const char** argv, SimObject* object = NULL);
145   bool processBindCmd(const char *device, const char *action, const char *makeCmd, const char *breakCmd);
146   bool processUnbind(const char *device, const char *action, SimObject* object = NULL);
147
148   /// @name Console Interface Functions
149   /// @{
150   const char* getBinding ( const char* command );                    ///< Find what the given command is bound to.
151   const char* getCommand ( const char* device, const char* action ); ///< Find what command is bound to the given event descriptor .
152   bool        isInverted ( const char* device, const char* action );
153   F32         getScale   ( const char* device, const char* action );
154   const char* getDeadZone( const char* device, const char* action );
155   /// @}
156
157
158   static bool        getKeyString(const U32 action, char* buffer);
159   static bool        getDeviceName(const U32 deviceType, const U32 deviceInstance, char* buffer);
160   static const char* buildActionString( const InputEventInfo* event );
161
162   bool processAction(const InputEventInfo*);
163   
164   /// Return true if the given event triggers is bound to an action in this map.
165   bool isAction( U32 deviceType, U32 deviceInst, U32 modifiers, U32 action );
166
167   /// Returns the global ActionMap.
168   static ActionMap* getGlobalMap();
169   
170   static bool checkBreakTable(const InputEventInfo*);
171   static bool handleEvent(const InputEventInfo*);
172   static bool handleEventGlobal(const InputEventInfo*);
173
174   /// Called when we lose focus, to make sure we have no dangling inputs.
175   ///
176   /// This fires a break event for every currently pending item in the break
177   /// table.
178   static void clearAllBreaks();
179
180   /// Returns true if the specified key + modifiers are bound to something
181   /// on the global action map.
182   static bool checkAsciiGlobal(U16 key, U32 modifiers);
183
184   static bool getDeviceTypeAndInstance(const char *device, U32 &deviceType, U32 &deviceInstance);
185
186   DECLARE_CONOBJECT(ActionMap);
187};
188
189#endif // _ACTIONMAP_H_
190