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

121 lines
3.3 KiB

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