💿🐜 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.

134 lines
3.6 KiB

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 CONTROLS_HPP
  20. #define CONTROLS_HPP
  21. #include "input.hpp"
  22. #include <list>
  23. #include <utility>
  24. #include <tuple>
  25. #include <map>
  26. enum class MouseWheelAxis
  27. {
  28. POSITIVE_X,
  29. NEGATIVE_X,
  30. POSITIVE_Y,
  31. NEGATIVE_Y
  32. };
  33. class Control:
  34. public KeyObserver,
  35. public MouseButtonObserver,
  36. public MouseWheelObserver,
  37. public GamepadButtonObserver,
  38. public GamepadAxisObserver
  39. {
  40. public:
  41. Control();
  42. virtual ~Control();
  43. void setDeadzone(float value);
  44. void update();
  45. float getDeadzone() const;
  46. float getCurrentValue() const;
  47. float getPreviousValue() const;
  48. bool isTriggered() const;
  49. bool wasTriggered() const;
  50. bool isUnbound() const;
  51. void bindKey(Keyboard* keyboard, int scancode);
  52. void bindMouseButton(Mouse* mouse, int button);
  53. void bindMouseWheelAxis(Mouse* mouse, MouseWheelAxis axis);
  54. void bindGamepadButton(Gamepad* gamepad, int button);
  55. void bindGamepadAxis(Gamepad* gamepad, int axis, bool negative);
  56. void bind(const InputEvent& event);
  57. void unbind();
  58. virtual void keyPressed(int scancode);
  59. virtual void keyReleased(int scancode);
  60. virtual void mouseButtonPressed(int button, int x, int y);
  61. virtual void mouseButtonReleased(int button, int x, int y);
  62. virtual void mouseWheelScrolled(int x, int y);
  63. virtual void gamepadButtonPressed(int button);
  64. virtual void gamepadButtonReleased(int button);
  65. virtual void gamepadAxisMoved(int axis, bool negative, float value);
  66. const std::list<std::pair<Keyboard*, int>>* getBoundKeys() const;
  67. const std::list<std::pair<Mouse*, int>>* getBoundMouseButtons() const;
  68. const std::list<std::pair<Mouse*, MouseWheelAxis>>* getBoundMouseWheelAxes() const;
  69. const std::list<std::pair<Gamepad*, int>>* getBoundGamepadButtons() const;
  70. const std::list<std::tuple<Gamepad*, int, bool>>* getBoundGamepadAxes() const;
  71. private:
  72. float deadzone;
  73. float currentValue;
  74. float previousValue;
  75. std::list<std::pair<Keyboard*, int>> boundKeys;
  76. std::list<std::pair<Mouse*, int>> boundMouseButtons;
  77. std::list<std::pair<Mouse*, MouseWheelAxis>> boundMouseWheelAxes;
  78. std::list<std::pair<Gamepad*, int>> boundGamepadButtons;
  79. std::list<std::tuple<Gamepad*, int, bool>> boundGamepadAxes;
  80. };
  81. inline float Control::getDeadzone() const
  82. {
  83. return deadzone;
  84. }
  85. inline float Control::getCurrentValue() const
  86. {
  87. return currentValue;
  88. }
  89. inline float Control::getPreviousValue() const
  90. {
  91. return previousValue;
  92. }
  93. class ControlProfile
  94. {
  95. public:
  96. ControlProfile(InputManager* inputManager);
  97. void registerControl(const std::string& name, Control* control);
  98. bool save(const std::string& filename);
  99. bool load(const std::string& filename);
  100. // Calls Control::update() on each control registered with this profile
  101. void update();
  102. const std::map<std::string, Control*>* getControlMap() const;
  103. private:
  104. InputManager* inputManager;
  105. std::map<std::string, Control*> controls;
  106. };
  107. inline const std::map<std::string, Control*>* ControlProfile::getControlMap() const
  108. {
  109. return &controls;
  110. }
  111. #endif