💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
7.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef INPUT_HPP
  20. #define INPUT_HPP
  21. #include <emergent/emergent.hpp>
  22. using namespace Emergent;
  23. #include <string>
  24. #include <iostream>
  25. #include <map>
  26. #include <list>
  27. #include <SDL2/SDL.h>
  28. class KeyObserver
  29. {
  30. public:
  31. virtual void keyPressed(int scancode) = 0;
  32. virtual void keyReleased(int scancode) = 0;
  33. };
  34. class MouseMotionObserver
  35. {
  36. public:
  37. virtual void mouseMoved(int x, int y) = 0;
  38. };
  39. class MouseButtonObserver
  40. {
  41. public:
  42. virtual void mouseButtonPressed(int button, int x, int y) = 0;
  43. virtual void mouseButtonReleased(int button, int x, int y) = 0;
  44. };
  45. class MouseWheelObserver
  46. {
  47. public:
  48. virtual void mouseWheelScrolled(int x, int y) = 0;
  49. };
  50. class GamepadButtonObserver
  51. {
  52. public:
  53. virtual void gamepadButtonPressed(int button) = 0;
  54. virtual void gamepadButtonReleased(int button) = 0;
  55. };
  56. class GamepadAxisObserver
  57. {
  58. public:
  59. virtual void gamepadAxisMoved(int axis, bool negative, float value) = 0;
  60. };
  61. class WindowObserver
  62. {
  63. public:
  64. virtual void windowClosed() = 0;
  65. virtual void windowResized(int width, int height) = 0;
  66. };
  67. class InputDevice
  68. {
  69. public:
  70. enum class Type
  71. {
  72. KEYBOARD,
  73. MOUSE,
  74. GAMEPAD
  75. };
  76. InputDevice(const std::string& name);
  77. const std::string& getName() const;
  78. virtual InputDevice::Type getType() const = 0;
  79. void setDisconnected(bool disconnected);
  80. bool isDisconnected() const;
  81. private:
  82. std::string name;
  83. bool disconnected;
  84. };
  85. inline const std::string& InputDevice::getName() const
  86. {
  87. return name;
  88. }
  89. inline bool InputDevice::isDisconnected() const
  90. {
  91. return disconnected;
  92. }
  93. class Keyboard: public InputDevice
  94. {
  95. public:
  96. Keyboard(const std::string& name);
  97. virtual ~Keyboard();
  98. InputDevice::Type getType() const;
  99. void addKeyObserver(KeyObserver* observer);
  100. void removeKeyObserver(KeyObserver* observer);
  101. void removeKeyObservers();
  102. void press(int scancode);
  103. void release(int scancode);
  104. private:
  105. std::list<KeyObserver*> keyObservers;
  106. };
  107. inline InputDevice::Type Keyboard::getType() const
  108. {
  109. return InputDevice::Type::KEYBOARD;
  110. }
  111. class Mouse: public InputDevice
  112. {
  113. public:
  114. Mouse(const std::string& name);
  115. virtual ~Mouse();
  116. InputDevice::Type getType() const;
  117. void addMouseMotionObserver(MouseMotionObserver* observer);
  118. void addMouseButtonObserver(MouseButtonObserver* observer);
  119. void addMouseWheelObserver(MouseWheelObserver* observer);
  120. void removeMouseMotionObserver(MouseMotionObserver* observer);
  121. void removeMouseButtonObserver(MouseButtonObserver* observer);
  122. void removeMouseWheelObserver(MouseWheelObserver* observer);
  123. void removeMouseMotionObservers();
  124. void removeMouseButtonObservers();
  125. void removeMouseWheelObservers();
  126. void press(int button, int x, int y);
  127. void release(int button, int x, int y);
  128. void move(int x, int y);
  129. void scroll(int x, int y);
  130. const glm::ivec2& getCurrentPosition() const;
  131. const glm::ivec2& getPreviousPosition() const;
  132. private:
  133. void processFlaggedMotionObservers();
  134. void processFlaggedButtonObservers();
  135. void processFlaggedWheelObservers();
  136. glm::ivec2 currentPosition;
  137. glm::ivec2 previousPosition;
  138. std::list<MouseMotionObserver*> motionObservers;
  139. std::list<MouseButtonObserver*> buttonObservers;
  140. std::list<MouseWheelObserver*> wheelObservers;
  141. bool notifyingMotionObservers;
  142. bool notifyingButtonObservers;
  143. bool notifyingWheelObservers;
  144. std::list<MouseMotionObserver*> additionFlaggedMotionObservers;
  145. std::list<MouseButtonObserver*> additionFlaggedButtonObservers;
  146. std::list<MouseWheelObserver*> additionFlaggedWheelObservers;
  147. std::list<MouseMotionObserver*> removalFlaggedMotionObservers;
  148. std::list<MouseButtonObserver*> removalFlaggedButtonObservers;
  149. std::list<MouseWheelObserver*> removalFlaggedWheelObservers;
  150. };
  151. inline InputDevice::Type Mouse::getType() const
  152. {
  153. return InputDevice::Type::MOUSE;
  154. }
  155. inline const glm::ivec2& Mouse::getCurrentPosition() const
  156. {
  157. return currentPosition;
  158. }
  159. inline const glm::ivec2& Mouse::getPreviousPosition() const
  160. {
  161. return previousPosition;
  162. }
  163. class Gamepad: public InputDevice
  164. {
  165. public:
  166. Gamepad(const std::string& name);
  167. virtual ~Gamepad();
  168. InputDevice::Type getType() const;
  169. void addGamepadButtonObserver(GamepadButtonObserver* observer);
  170. void removeGamepadButtonObserver(GamepadButtonObserver* observer);
  171. void removeGamepadButtonObservers();
  172. void addGamepadAxisObserver(GamepadAxisObserver* observer);
  173. void removeGamepadAxisObserver(GamepadAxisObserver* observer);
  174. void removeGamepadAxisObservers();
  175. void press(int button);
  176. void release(int button);
  177. void move(int axis, bool negative, float value);
  178. private:
  179. std::list<GamepadButtonObserver*> buttonObservers;
  180. std::list<GamepadAxisObserver*> axisObservers;
  181. };
  182. inline InputDevice::Type Gamepad::getType() const
  183. {
  184. return InputDevice::Type::GAMEPAD;
  185. }
  186. struct InputEvent
  187. {
  188. public:
  189. enum class Type
  190. {
  191. NONE,
  192. KEY,
  193. MOUSE_BUTTON,
  194. MOUSE_WHEEL,
  195. GAMEPAD_BUTTON,
  196. GAMEPAD_AXIS
  197. };
  198. InputEvent();
  199. InputEvent::Type type;
  200. std::pair<Keyboard*, int> key;
  201. std::pair<Mouse*, int> mouseButton;
  202. std::tuple<Mouse*, int, int> mouseWheel;
  203. std::pair<Gamepad*, int> gamepadButton;
  204. std::tuple<Gamepad*, int, int> gamepadAxis;
  205. };
  206. class InputManager
  207. {
  208. public:
  209. InputManager();
  210. // Processes input events
  211. virtual void update() = 0;
  212. // Listens for the next input event, should be called BEFORE update()
  213. virtual void listen(InputEvent* inputEvent) = 0;
  214. bool wasClosed() const;
  215. void addWindowObserver(WindowObserver* observer);
  216. void removeWindowObserver(WindowObserver* observer);
  217. void removeWindowObservers();
  218. void registerKeyboard(Keyboard* keyboard);
  219. void registerMouse(Mouse* mouse);
  220. void registerGamepad(Gamepad* gamepad);
  221. void unregisterKeyboard(Keyboard* keyboard);
  222. void unregisterMouse(Mouse* mouse);
  223. void unregisterGamepad(Gamepad* gamepad);
  224. bool isRegistered(const Keyboard* keyboard) const;
  225. bool isRegistered(const Mouse* mouse) const;
  226. bool isRegistered(const Gamepad* gamepad) const;
  227. const Gamepad* getGamepad(const std::string& name) const;
  228. Gamepad* getGamepad(const std::string& name);
  229. const std::list<Keyboard*>* getKeyboards() const;
  230. const std::list<Mouse*>* getMice() const;
  231. const std::list<Gamepad*>* getGamepads() const;
  232. protected:
  233. bool closed;
  234. std::list<WindowObserver*> windowObservers;
  235. private:
  236. std::list<Keyboard*> keyboards;
  237. std::list<Mouse*> mice;
  238. std::list<Gamepad*> gamepads;
  239. };
  240. inline bool InputManager::wasClosed() const
  241. {
  242. return closed;
  243. }
  244. inline const std::list<Keyboard*>* InputManager::getKeyboards() const
  245. {
  246. return &keyboards;
  247. }
  248. inline const std::list<Mouse*>* InputManager::getMice() const
  249. {
  250. return &mice;
  251. }
  252. inline const std::list<Gamepad*>* InputManager::getGamepads() const
  253. {
  254. return &gamepads;
  255. }
  256. class SDLInputManager: public InputManager
  257. {
  258. public:
  259. SDLInputManager();
  260. ~SDLInputManager();
  261. virtual void update();
  262. virtual void listen(InputEvent* event);
  263. private:
  264. Keyboard* keyboard;
  265. Mouse* mouse;
  266. std::map<int, Gamepad*> gamepadMap;
  267. SDL_Event event;
  268. std::list<Gamepad*> allocatedGamepads;
  269. };
  270. #endif