HoverRace  2.0
Controller.h
Go to the documentation of this file.
1 
2 // Controller.h
3 //
4 // Copyright (c) 2010 Ryan Curtin.
5 // Copyright (c) 2013-2015 Michael Imamura.
6 //
7 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
8 // you may not use this file except in compliance with the License.
9 //
10 // A copy of the license should have been attached to the package from which
11 // you have taken this file. If you can not find the license you can not use
12 // this file.
13 //
14 //
15 // The author makes no representations about the suitability of
16 // this software for any purpose. It is provided "as is" "AS IS",
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
18 // implied.
19 //
20 // See the License for the specific language governing permissions
21 // and limitations under the License.
22 
23 #pragma once
24 
25 #include <vector>
26 #include <string>
27 
28 #include <SDL2/SDL.h>
29 
30 #include "Action.h"
31 #include "ControlAction.h"
32 
33 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
34 # ifdef MR_ENGINE
35 # define MR_DllDeclare __declspec( dllexport )
36 # else
37 # define MR_DllDeclare __declspec( dllimport )
38 # endif
39 #else
40 # define MR_DllDeclare
41 #endif
42 
43 namespace HoverRace {
44  namespace MainCharacter {
45  class MainCharacter;
46  }
47 }
48 
49 namespace HoverRace {
50 namespace Control {
51 
53 typedef std::shared_ptr<InputHandler> InputHandlerPtr;
54 
55 /***
56  * Tracks actions which are either engaged (@c true) or disengaged (@c false).
57  * In other words, whether the key or button is currently pressed or not.
58  * @todo Analog instead of digital (well, the ones that can, at least).
59  */
61  // TODO: make these inputs analog, not digital
62  bool motorOn;
63  bool jump;
64  bool brake;
65  bool fire;
66  bool weapon;
67  bool lookBack;
68  bool right;
69  bool left;
70 };
71 
107 {
108 public:
111 
112  // Typedef for the maps of hashes to controls
113  using ActionMap = std::map<int, ControlActionPtr>;
114 
115  enum axis_t
116  {
117  AXIS_X = 1,
122  };
123 
124  enum class ActionMapId : size_t
125  {
126  CAMERA = 0x01,
127  CONSOLE = 0x02,
128  CONSOLE_TOGGLE = 0x04,
129  MENU = 0x08,
130  PLAYER = 0x10,
131  };
132 
133  using VoidActionPtr = std::shared_ptr<Action<voidSignal_t>>;
134  using ValueActionPtr = std::shared_ptr<Action<valueSignal_t>>;
135  using StringActionPtr =
136  std::shared_ptr<Action<stringSignal_t, const std::string&>>;
137  using TextControlActionPtr =
138  std::shared_ptr<Action<textControlSignal_t, TextControl::key_t>>;
139  using Vec2ActionPtr = std::shared_ptr<Action<vec2Signal_t, const Vec2&>>;
140  using MouseClickActionPtr =
141  std::shared_ptr<Action<mouseClickSignal_t, const Mouse::Click&>>;
142  using MouseScrollActionPtr =
143  std::shared_ptr<Action<mouseScrollSignal_t, const Mouse::Scroll&>>;
144 
145  // event handlers
146  bool OnKeyPressed(const SDL_KeyboardEvent &arg);
147  bool OnKeyReleased(const SDL_KeyboardEvent &arg);
148  bool OnTextInput(const SDL_TextInputEvent &evt);
149  bool OnMouseMoved(const SDL_MouseMotionEvent &evt);
150  bool OnMousePressed(const SDL_MouseButtonEvent &evt);
151  bool OnMouseReleased(const SDL_MouseButtonEvent &evt);
152  bool OnMouseWheel(const SDL_MouseWheelEvent &evt);
153  /*TODO
154  bool buttonPressed(const JoyStickEvent &arg, int button);
155  bool buttonReleased(const JoyStickEvent &arg, int button);
156  bool axisMoved(const JoyStickEvent &arg, int axis);
157  bool povMoved(const JoyStickEvent &arg, int pov);
158  */
159 
160  void ProcessInputEvent(const SDL_Event &evt);
161  void HandleEvent(int hash, int value);
162 
175  void CaptureNextInput(int oldhash, std::string mapname);
176 
185  bool IsCapturing();
186 
190  void StopCapture();
191 
198  void DisableCaptureInput();
199 
206  void ClearActionMap();
207 
218  bool AddActionMap(const std::string &mapname, ActionMapId mapId);
219 
226  {
227  return (activeMaps | static_cast<size_t>(id)) != 0;
228  }
229 
233  ActionMap& GetActionMap(std::string key);
234 
238  std::vector<std::string> GetAvailableMaps();
239 
250  void AddPlayerMaps(int numPlayers, MainCharacter::MainCharacter** mcs);
251 
252  void AddCameraMaps();
253  void AddMenuMaps();
254  void AddConsoleToggleMaps();
255  void AddConsoleMaps();
256 
257  VoidActionPtr Hotkey(const std::string &key);
258 
259  static std::string HashToString(int hash);
260  SDL_Keycode StringToKey(const std::string &s);
261 
262 public:
263  void LoadCameraMap();
264  void LoadMenuMap();
265  void LoadConsoleToggleMap();
266  void LoadConsoleMap();
267 
268  void SaveConfig();
269  void ReloadConfig();
270  void LoadConfig();
271 
272 private:
273  // Auxiliary functions
274  void RebindKey(std::string mapname, int oldhash, int newhash);
275 
276  // Hashing scheme (we have 32 bits but won't always use them):
277  // disabled control
278  // [000000000000000000][aaaaaaaaaaaa]
279  // a: next available disabled id
280  // keyboard event
281  // [00000000][00][000000][aaaaaaaa][00000000]: keycode
282  // a: int keycode
283  // [00000000][00][01][aaaaaaaaaaaa][00000000]: scancode as keycode
284  // a: int scancode as keycode
285  // mouse event
286  // [00000000][01][00][aaaaaaaa][000000000000]: button press
287  // a: button id
288  // [00000000][01][01][aaaa][bbbb][000000000000]: axis move
289  // a: axis id
290  // b: direction
291  // joystick event
292  // [00000000][10][00][aaaaaaaa][bbbbbbbb][0000]: button press
293  // a: joystick id
294  // b: button id
295  // [00000000][10][01][aaaaaaaa][bbbbbbbb][0000]: slider move
296  // a: joystick id
297  // b: slider id
298  // [00000000][10][10][aaaaaaaa][bbbb][cccc][0000]: pov move
299  // a: joystick id
300  // b: direction
301  // c: pov id
302  // [00000000][10][11][aaaaaaaa][bbbb][cccc][0000]: axis move
303  // a: joystick id
304  // b: axis id
305  // c: direction
306  int GetNextAvailableDisabledHash();
307 
308 public:
309  static int HashKeyboardEvent(const SDL_Keycode& arg);
310  static int HashMouseButtonEvent(const SDL_MouseButtonEvent& arg);
311  static int HashMouseAxisEvent(axis_t axis, int direction);
312  /*TODO
313  static int HashJoystickAxisEvent(const JoyStickEvent& arg, int axis, int direction);
314  static int HashJoystickSliderEvent(const JoyStickEvent& arg, int slider);
315  static int HashJoystickButtonEvent(const JoyStickEvent& arg, int button);
316  static int HashJoystickPovEvent(const JoyStickEvent& arg, int pov, int direction);
317  */
318 
319 private:
325  size_t activeMaps;
326  std::map<std::string, ActionMap> allActionMaps;
327  std::unordered_map<SDL_Keycode, VoidActionPtr> hotkeys;
328 
330 
333  std::string captureMap;
334 
336 
337 public:
338  struct actions_t
339  {
340  struct ui_t
341  {
342  ui_t();
352 
353  // Console scrolling.
360 
361  // Text input mode.
363  TextControlActionPtr control;
365 
366  // Low-level UI widget controls.
371  } ui;
372  struct sys_t
373  {
374  sys_t();
376  } sys;
377  struct camera_t
378  {
379  camera_t();
385  } camera;
386  } actions;
387 };
388 
389 } // namespace Control
390 } // namespace HoverRace
391 
392 #undef MR_DllDeclare
std::shared_ptr< InputHandler > InputHandlerPtr
Definition: Controller.h:52
VoidActionPtr consolePrevCmd
Definition: Controller.h:358
VoidActionPtr menuCancel
Definition: Controller.h:344
VoidActionPtr menuPrev
Definition: Controller.h:351
VoidActionPtr consoleToggle
Definition: Controller.h:375
MouseScrollActionPtr mouseScrolled
Definition: Controller.h:370
bool IsMapActive(ActionMapId id)
Check if a control map is active.
Definition: Controller.h:225
std::shared_ptr< Action< voidSignal_t >> VoidActionPtr
Definition: Controller.h:133
bool jump
Definition: Controller.h:63
VoidActionPtr menuRight
Definition: Controller.h:349
bool right
Definition: Controller.h:68
StringActionPtr text
Text input.
Definition: Controller.h:362
VoidActionPtr consoleTop
Definition: Controller.h:356
int nextAvailableDisabledHash
Definition: Controller.h:329
std::string captureMap
name of the map we are capturing for
Definition: Controller.h:333
Vec2ActionPtr mouseMoved
Definition: Controller.h:367
VoidActionPtr menuNext
Definition: Controller.h:350
MouseClickActionPtr mouseReleased
Definition: Controller.h:369
std::unordered_map< SDL_Keycode, VoidActionPtr > hotkeys
Definition: Controller.h:327
std::shared_ptr< Action< valueSignal_t >> ValueActionPtr
Definition: Controller.h:134
axis_t
Definition: Controller.h:115
int captureOldHash
stores the value of the hash we will be replacing when capturing input
Definition: Controller.h:332
Definition: Vec.h:38
bool captureNextInput
Definition: Controller.h:331
VoidActionPtr zoomOut
Definition: Controller.h:381
VoidActionPtr menuOk
Definition: Controller.h:343
bool motorOn
Definition: Controller.h:62
Vec2 mousePos
Track most-recent mouse position for lookup later.
Definition: Controller.h:335
MouseClickActionPtr mousePressed
Definition: Controller.h:368
ActionMapId
Definition: Controller.h:124
std::shared_ptr< Action< vec2Signal_t, const Vec2 & >> Vec2ActionPtr
Definition: Controller.h:139
VoidActionPtr zoomIn
Definition: Controller.h:380
Definition: Symbol.h:317
VoidActionPtr menuLeft
Definition: Controller.h:348
bool fire
Definition: Controller.h:65
#define MR_DllDeclare
Definition: Controller.h:40
VoidActionPtr panDown
Definition: Controller.h:383
bool weapon
Definition: Controller.h:66
Definition: InputHandler.h:40
VoidActionPtr panUp
Definition: Controller.h:382
size_t activeMaps
Definition: Controller.h:325
VoidActionPtr reset
Definition: Controller.h:384
bool left
Definition: Controller.h:69
VoidActionPtr menuExtra
Scene-specific extra action.
Definition: Controller.h:345
VoidActionPtr consoleBottom
Definition: Controller.h:357
VoidActionPtr menuUp
Definition: Controller.h:346
VoidActionPtr menuDown
Definition: Controller.h:347
VoidActionPtr consoleNextCmd
Definition: Controller.h:359
VoidActionPtr consoleDown
Definition: Controller.h:355
Definition: Announcement.h:24
bool lookBack
Definition: Controller.h:67
std::shared_ptr< Action< mouseClickSignal_t, const Mouse::Click & >> MouseClickActionPtr
Definition: Controller.h:141
std::shared_ptr< Action< stringSignal_t, const std::string & >> StringActionPtr
Definition: Controller.h:136
std::shared_ptr< Action< mouseScrollSignal_t, const Mouse::Scroll & >> MouseScrollActionPtr
Definition: Controller.h:143
std::shared_ptr< Action< textControlSignal_t, TextControl::key_t >> TextControlActionPtr
Definition: Controller.h:138
std::map< int, ControlActionPtr > ActionMap
Definition: Controller.h:113
std::map< std::string, ActionMap > allActionMaps
Definition: Controller.h:326
Definition: Controller.h:60
Definition: MainCharacter.h:62
ActionMap actionMap
We store several different action maps which we can choose from.
Definition: Controller.h:324
Translates input events into actions.
Definition: Controller.h:106
bool brake
Definition: Controller.h:64
VoidActionPtr consoleUp
Definition: Controller.h:354