InputManager.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <OgreSingleton.h>
  17. #include <OgreString.h>
  18. #include <OIS/OIS.h>
  19. #include <vector>
  20. #include "Event.h"
  21. typedef std::vector<QGears::Event> InputEventArray;
  22. typedef std::vector<int> ButtonList;
  23. class ConfigCmd;
  24. /**
  25. * The input manager.
  26. *
  27. * It handles input events and creates {@see Event}s.
  28. */
  29. class InputManager : public Ogre::Singleton<InputManager>{
  30. public:
  31. /**
  32. * Constructor.
  33. */
  34. InputManager();
  35. /**
  36. * Destructor.
  37. */
  38. virtual ~InputManager();
  39. /**
  40. * Triggered when a keyboard button is pressed or released.
  41. *
  42. * Creates an {@Event}.
  43. *
  44. * @param button[in] Pressed button ID.
  45. * @param text[in] @todo The key code? It gets assigned to parameter 1
  46. * of the generated event.
  47. * @param down[in] True if the button has been pressed, false if it has
  48. * been released. It gets assigned to parameter 2 of the event.
  49. */
  50. void ButtonPressed(int button, char text, bool down);
  51. /**
  52. * Triggered when a mouse button is pressed or released.
  53. *
  54. * Creates an {@Event}.
  55. *
  56. * @param button[in] Pressed button ID.
  57. * @param down[in] True if the button has been pressed, false if it has
  58. * been released. It gets assigned to parameter 1 of the event.
  59. */
  60. void MousePressed(int button, bool down);
  61. /**
  62. * Triggered when a mouse moves.
  63. *
  64. * Creates an {@Event}.
  65. *
  66. * @param x[in] New mouse's X coordinate. It gets assigned to parameter
  67. * 1 of the event.
  68. * @param y[in] New mouse's Y coordinate. It gets assigned to parameter
  69. * 2 of the event.
  70. */
  71. void MouseMoved(int x, int y);
  72. /**
  73. * Triggered when a mouse scrolls.
  74. *
  75. * Creates an {@Event}.
  76. *
  77. * @param value[in] Number of lines scrolled. Positive for scroll down,
  78. * negative for scroll up. It gets assigned to parameter 1 of the
  79. * event.
  80. */
  81. void MouseScrolled(int value);
  82. /**
  83. * Resets all keyboard and mouse events to their default state.
  84. *
  85. * Keyboard and mouse are considered to not pressed, the mouse movement
  86. * and scroll are considered 0.
  87. */
  88. void Reset();
  89. /**
  90. * Update keyboard and mouse buttons, movements and scroll status.
  91. */
  92. void Update();
  93. bool IsButtonPressed(int button) const;
  94. /**
  95. * Retrieves the current input events.
  96. *
  97. * The input event queue will be empty after calling this function.
  98. *
  99. * @param input_events[out] The current events will be loaded here.
  100. */
  101. void GetInputEvents(InputEventArray& input_events);
  102. /**
  103. * Initializes all command bindings.
  104. */
  105. void InitCmd();
  106. /**
  107. * Binds a command to an input event.
  108. *
  109. * @param cmd[in] Command to bind.
  110. * @param params[in] Command arguments.
  111. * @param buttons[in] Buttons to bind to the command.
  112. */
  113. void BindCommand(
  114. ConfigCmd* cmd, const Ogre::StringVector& params,
  115. const ButtonList& buttons
  116. );
  117. /**
  118. * Binds a game event to an input event.
  119. *
  120. * @param event[in] The game event to bind.
  121. * @param buttons[in] Buttons to bind to the event.
  122. */
  123. void BindGameEvent(
  124. const Ogre::String& event, const ButtonList& buttons
  125. );
  126. /**
  127. * Activates all bindings for a button.
  128. *
  129. * @param button[in] ID of the button.
  130. */
  131. void ActivateBinds(const int button);
  132. /**
  133. * @tdo Understand and document.
  134. *
  135. * @param button[in] ID of the button.
  136. * @param type[in] Event type.
  137. */
  138. void AddGameEvents(const int button, const QGears::EventType type);
  139. private:
  140. /**
  141. * The state of eahc button.
  142. */
  143. bool button_state_[256];
  144. /**
  145. * The text of each button.
  146. *
  147. * @todo The text is the keycode?
  148. */
  149. char button_text_[256];
  150. /**
  151. * Indicates if a key holded down is waiting to repeat events.
  152. *
  153. * @todo Verify this.
  154. */
  155. bool repeat_first_wait_;
  156. /**
  157. * The time a key has been pressed.
  158. */
  159. float repeat_timer_;
  160. /**
  161. * The event queue.
  162. */
  163. InputEventArray event_queue_;
  164. /**
  165. * A command binding.
  166. */
  167. struct BindInfo{
  168. /**
  169. * Constructor.
  170. */
  171. BindInfo(): cmd(NULL){}
  172. /**
  173. * The bind command.
  174. */
  175. ConfigCmd* cmd;
  176. /**
  177. * The command parameters.
  178. */
  179. Ogre::StringVector params;
  180. /**
  181. * The buttons bond to the command.
  182. */
  183. ButtonList buttons;
  184. };
  185. /**
  186. * List of command bindings.
  187. */
  188. std::vector<BindInfo> binds_;
  189. /**
  190. * A game event binding
  191. */
  192. struct BindGameEventInfo{
  193. /**
  194. * The game event.
  195. */
  196. Ogre::String event;
  197. /**
  198. * The buttons bond to the command.
  199. */
  200. ButtonList buttons;
  201. };
  202. /**
  203. * List of game event bindings.
  204. */
  205. std::vector<BindGameEventInfo> bind_game_events_;
  206. };