Input.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_DC_INPUT
34 #define H_DC_INPUT
35 
36 #include <Dynacoe/Backends/InputManager/InputTypes.h>
37 #include <Dynacoe/Backends/InputManager/InputDevice.h>
38 #include <Dynacoe/Backends/InputManager/InputManager.h>
39 #include <Dynacoe/Util/Table.h>
40 #include <Dynacoe/Modules/Module.h>
41 #include <string>
42 #include <map>
43 #include <set>
44 
45 
46 
47 
48 namespace Dynacoe {
49 
52 using PadID = Dynacoe::LookupID;
53 
58  public:
61  virtual void OnPress(){};
62 
64  virtual void OnHold(){};
65 
68  virtual void OnRelease(){};
69 };
70 
74  public:
77  virtual void OnNewUnicode(int unicodeValue){};
78 };
79 
82 class Input : public Module {
83  public:
84 
85 
86 
91  static bool IsPressed(Keyboard);
92  static bool IsPressed(MouseButtons);
93  static bool IsPressed(PadID, PadButtons);
94  static bool IsPressed(const std::string &);
96 
102  static bool GetState(Keyboard);
103  static bool GetState(MouseButtons);
104  static bool GetState(PadID, PadButtons);
105  static bool GetState(const std::string &);
107 
112  static int GetLastUnicode();
113 
114  static void AddUnicodeListener(UnicodeListener *);
115  static void RemoveUnicodeListener(UnicodeListener *);
116 
121  static bool IsHeld(Keyboard);
122  static bool IsHeld(MouseButtons);
123  static bool IsHeld(PadID, PadButtons);
124  static bool IsHeld(const std::string &);
126 
127  // \name IsReleased
128  // Returns whether or not the input was released this update.
129  //
130  static bool IsReleased(Keyboard);
131  static bool IsReleased(MouseButtons);
132  static bool IsReleased(PadID, PadButtons);
133  static bool IsReleased(const std::string &);
134 
137  static std::vector<PadID> QueryPads();
138 
142  static void MapInput(const std::string & id, Keyboard);
143  static void MapInput(const std::string & id, MouseButtons);
144  static void MapInput(const std::string & id, PadID, PadButtons);
146 
149  static void UnmapInput(const std::string & id);
150 
155  static void AddListener(ButtonListener *, Keyboard);
156  static void AddListener(ButtonListener *, MouseButtons);
157  static void AddListener(ButtonListener *, PadID, PadButtons);
158  static void AddListener(ButtonListener *, const std::string &);
159 
160 
163  static void RemoveListener(ButtonListener *);
164 
165 
168 
171  static int MouseX();
172 
174  static int MouseY();
175 
178  static int MouseXDelta();
179 
181  static int MouseYDelta();
182 
187  static int MouseWheel();
189 
190 
194  static float GetPadAxis(PadID, PadAxes);
195 
196 
199  static bool LockInput(float sec);
200 
201 
202 
205  static void Update();
206 
207 
210  static InputManager * GetManager();
211 
212  private:
213 
214 
215  static void initBase();
216  static bool IsShiftMod();
217 
218  static void getUnicode();
219 
220  struct InputState {
221 
222 
223  InputDevice ** devices;
224  int numDevices;
225  };
226 
227 
228 
229  static InputManager * manager;
230  static int lastUnicode;
231 
232 
233  // Aggregate button state
234  struct ButtonList {
235  std::vector<Keyboard> keys;
236  std::vector<MouseButtons> mouseButtons;
237  std::vector<std::pair<PadID, std::vector<PadButtons>>> padButtons;
238 
239  void addButton(Keyboard);
240  void addButton(MouseButtons);
241  void addButton(PadID, PadButtons);
242  bool GetState();
243  bool IsPressed();
244  bool IsHeld();
245  bool IsReleased();
246  };
247 
248  //static std::map<std::string, ButtonList*> stringMap;
249  static std::map<std::string, Keyboard> stringMapKeyboard;
250  static std::map<std::string, MouseButtons> stringMapMouse;
251  static std::map<std::string, std::pair<PadID, PadButtons>> stringMapPad;
252  static std::vector<ButtonList*> buttonLists;
253 
254 
255 
256 
257 
258 
259  static bool inputLocked;
260 
261  static InputState thisState;
262  static InputState prevState;
263 
264 
265 
266 
267 
268 
269 
270 
271 
272  public:
273  std::string GetName() {return "Input";}
274  void Init(); void InitAfter(); void RunBefore(); void RunAfter(); void DrawBefore(); void DrawAfter();
275  Backend * GetBackend();
276 };
277 }
278 
279 
280 #endif
MouseButtons
Mouse Button inputs.
Definition: InputTypes.h:144
Backend.
Definition: Backend.h:56
virtual void OnHold()
Function called upon holding the button.
Definition: Input.h:64
static int MouseY()
Current Y position of the Mouse.
static void Update()
Update the state of input. This is done for you.
static int MouseXDelta()
Returns the change in X position since last update.
static float GetPadAxis(PadID, PadAxes)
Returns the axis tilt amount. The range is from -1 to 1.
static bool LockInput(float sec)
Prevents the updating of Input until the time expires.
Definition: AssetID.h:37
Callback functor base class. To implement a callback signal, create a class that inherits from this a...
Definition: Input.h:57
static int GetLastUnicode()
Returns the last Ascii code from keyboard.
virtual void OnPress()
Function called upon pressing of the button.
Definition: Input.h:61
static void UnmapInput(const std::string &id)
Disassociates a string with its corresponding InputID if any.
static InputManager * GetManager()
Returns the system-dependent object that handles input requests.
virtual void OnRelease()
Function called upon releasing the button.
Definition: Input.h:68
static std::vector< PadID > QueryPads()
Retrieves the a list of all valid Gamepads.
static int MouseYDelta()
Returns the change in Y position since the last update.
Keyboard
Enum containing all valid keyboard key inputs.
Definition: InputTypes.h:45
virtual void OnNewUnicode(int unicodeValue)
An incoming unicode value.
Definition: Input.h:77
static int MouseX()
Current X position of the Mouse.
Callbacck functor for unicode values from a user's keyboard.
Definition: Input.h:73
Singleton class for querying input devices.
Definition: Input.h:82
static void AddListener(ButtonListener *, Keyboard)
Maps an InputCallback to the specifed key.
static int MouseWheel()
Returns the current state of the mouse wheel.
static void RemoveListener(ButtonListener *)
Disassociates the corresponding InputCallback if any.
PadButtons
Valid inputs for an arbitrary input pad.
Definition: InputTypes.h:163
PadAxes
All the valid axes that an inputad can enter.
Definition: InputTypes.h:203