InputManager.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <vector>
  17. #include <OgreSingleton.h>
  18. #include <OgreString.h>
  19. #include <OgreStringVector.h>
  20. #include <OIS/OIS.h>
  21. #include "Event.h"
  22. #include "Manager.h"
  23. typedef std::vector<VGears::Event> InputEventArray;
  24. typedef std::vector<int> ButtonList;
  25. class ConfigCmd;
  26. /**
  27. * The input manager.
  28. *
  29. * It handles input events and creates {@see Event}s.
  30. */
  31. class InputManager : public Manager, public Ogre::Singleton<InputManager>{
  32. public:
  33. /**
  34. * Constructor.
  35. */
  36. InputManager();
  37. /**
  38. * Destructor.
  39. */
  40. virtual ~InputManager();
  41. /**
  42. * Makes the input manager itself handles an input event.
  43. *
  44. * @param[in] event Event to handle.
  45. */
  46. void Input(const VGears::Event& event) override;
  47. /**
  48. * Update keyboard and mouse buttons, movements and scroll status.
  49. */
  50. void Update();
  51. /**
  52. * Updates the input manager with debug information.
  53. */
  54. void UpdateDebug() override;
  55. /**
  56. * Handles resizing events
  57. */
  58. void OnResize() override;
  59. /**
  60. * Clears all field information in the input manager.
  61. *
  62. * It does nothing.
  63. */
  64. void ClearField() override;
  65. /**
  66. * Clears all battle information in the input manager.
  67. *
  68. * It does nothing.
  69. */
  70. void ClearBattle() override;
  71. /**
  72. * Clears all world map information in the input manager.
  73. *
  74. * It does nothing.
  75. */
  76. void ClearWorld() override;
  77. /**
  78. * Triggered when a keyboard button is pressed or released.
  79. *
  80. * Creates an {@see Event}.
  81. *
  82. * @param[in] button Pressed button ID.
  83. * @param[in] text @todo The key code? It gets assigned to parameter 1 of the generated
  84. * event.
  85. * @param[in] down True if the button has been pressed, false if it has been released. It
  86. * gets assigned to parameter 2 of the event.
  87. */
  88. void ButtonPressed(int button, char text, bool down);
  89. /**
  90. * Triggered when a mouse button is pressed or released.
  91. *
  92. * Creates an {@see Event}.
  93. *
  94. * @param[in] button Pressed button ID.
  95. * @param[in] down True if the button has been pressed, false if it has been released. It
  96. * gets assigned to parameter 1 of the event.
  97. */
  98. void MousePressed(int button, bool down);
  99. /**
  100. * Triggered when a mouse moves.
  101. *
  102. * Creates an {@see Event}.
  103. *
  104. * @param[in] x New mouse's X coordinate. It gets assigned to parameter 1 of the event.
  105. * @param[in] y New mouse's Y coordinate. It gets assigned to parameter 2 of the event.
  106. */
  107. void MouseMoved(int x, int y);
  108. /**
  109. * Triggered when a mouse scrolls.
  110. *
  111. * Creates an {@see Event}.
  112. *
  113. * @param[in] value Number of lines scrolled. Positive for scroll down, negative for scroll
  114. * up. It gets assigned to parameter 1 of the event.
  115. */
  116. void MouseScrolled(int value);
  117. /**
  118. * Resets all keyboard and mouse events to their default state.
  119. *
  120. * Keyboard and mouse are considered to not pressed, the mouse movement
  121. * and scroll are considered 0.
  122. */
  123. void Reset();
  124. /**
  125. * Checks if a button is being pressed.
  126. *
  127. * @param[in] button Button code.
  128. * @return true if the button is being pressed, false otherwise.
  129. */
  130. bool IsButtonPressed(int button) const;
  131. /**
  132. * Retrieves the current input events.
  133. *
  134. * The input event queue will be empty after calling this function.
  135. *
  136. * @param[out] input_events The current events will be loaded here.
  137. */
  138. void GetInputEvents(InputEventArray& input_events);
  139. /**
  140. * Initializes all command bindings.
  141. */
  142. void InitCmd();
  143. /**
  144. * Binds a command to an input event.
  145. *
  146. * @param[in] cmd Command to bind.
  147. * @param[in] params Command arguments.
  148. * @param[in] buttons Buttons to bind to the command.
  149. */
  150. void BindCommand(
  151. ConfigCmd* cmd, const Ogre::StringVector& params, const ButtonList& buttons
  152. );
  153. /**
  154. * Binds a game event to an input event.
  155. *
  156. * @param[in] event The game event to bind.
  157. * @param[in] buttons Buttons to bind to the event.
  158. */
  159. void BindGameEvent(const Ogre::String& event, const ButtonList& buttons);
  160. /**
  161. * Activates all bindings for a button.
  162. *
  163. * @param[in] button ID of the button.
  164. */
  165. void ActivateBinds(const int button);
  166. /**
  167. * Adds a game event associated to a button.
  168. *
  169. * @param[in] button ID of the button.
  170. * @param[in] type Event type.
  171. */
  172. void AddGameEvents(const int button, const VGears::EventType type);
  173. private:
  174. /**
  175. * Updates the input manager while on the fields.
  176. *
  177. * It just calls the generic {@see Update()}.
  178. */
  179. void UpdateField() override;
  180. /**
  181. * Updates the input manager while on a battle.
  182. *
  183. * It just calls the generic {@see Update()}.
  184. */
  185. void UpdateBattle() override;
  186. /**
  187. * Updates the input manager while on the world map.
  188. *
  189. * It just calls the generic {@see Update()}.
  190. */
  191. void UpdateWorld() override;
  192. /**
  193. * The state of eahc button.
  194. */
  195. bool button_state_[256];
  196. /**
  197. * The text of each button.
  198. *
  199. * @todo The text is the keycode?
  200. */
  201. char button_text_[256];
  202. /**
  203. * Indicates if a key held down is waiting to repeat events.
  204. *
  205. * @todo Verify this.
  206. */
  207. bool repeat_first_wait_;
  208. /**
  209. * The time a key has been pressed.
  210. */
  211. float repeat_timer_;
  212. /**
  213. * The event queue.
  214. */
  215. InputEventArray event_queue_;
  216. /**
  217. * A command binding.
  218. */
  219. struct BindInfo{
  220. /**
  221. * Constructor.
  222. */
  223. BindInfo(): cmd(NULL){}
  224. /**
  225. * The bind command.
  226. */
  227. ConfigCmd* cmd;
  228. /**
  229. * The command parameters.
  230. */
  231. Ogre::StringVector params;
  232. /**
  233. * The buttons bond to the command.
  234. */
  235. ButtonList buttons;
  236. };
  237. /**
  238. * List of command bindings.
  239. */
  240. std::vector<BindInfo> binds_;
  241. /**
  242. * A game event binding
  243. */
  244. struct BindGameEventInfo{
  245. /**
  246. * The game event.
  247. */
  248. Ogre::String event;
  249. /**
  250. * The buttons bond to the command.
  251. */
  252. ButtonList buttons;
  253. };
  254. /**
  255. * List of game event bindings.
  256. */
  257. std::vector<BindGameEventInfo> bind_game_events_;
  258. };