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

138 lines
3.4 KiB

  1. /*
  2. * Copyright (C) 2021 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 ANTKEEPER_INPUT_CONTROL_HPP
  20. #define ANTKEEPER_INPUT_CONTROL_HPP
  21. #include <functional>
  22. namespace input {
  23. /**
  24. * A control can be bound to multiple types of input events.
  25. */
  26. class control
  27. {
  28. public:
  29. /// Creates a control.
  30. control();
  31. /// Destroys a control.
  32. virtual ~control();
  33. /**
  34. * Performs callbacks then sets the previous value equal to the current value.
  35. */
  36. void update();
  37. /**
  38. * Sets the current value of the control.
  39. *
  40. * @param value control value.
  41. */
  42. void set_current_value(float value);
  43. /**
  44. * This works the same as setting the current value, but causes the value to be reset on the next call to update.
  45. */
  46. void set_temporary_value(float value);
  47. /**
  48. * Sets the deadzone value. If the current value of the control is not greater than the deadzone value, the control will not be considered active.
  49. *
  50. * @param value Deadzone value.
  51. */
  52. void set_deadzone(float value);
  53. /// Sets the callback for when the control is activated.
  54. void set_activated_callback(std::function<void()> callback);
  55. /// Sets the callback for when the control is deactivated.
  56. void set_deactivated_callback(std::function<void()> callback);
  57. /// Sets the callback for when the control value is changed.
  58. void set_value_changed_callback(std::function<void(float)> callback);
  59. /**
  60. * Enables or disables callbacks.
  61. *
  62. * @param enabled Whether to enable or disable callbacks.
  63. */
  64. void set_callbacks_enabled(bool enabled);
  65. /// Returns the deadzone value. The default value is 0.0.
  66. float get_deadzone() const;
  67. /// Returns the current value of the control.
  68. float get_current_value() const;
  69. /// Returns the previous value of the control.
  70. float get_previous_value() const;
  71. /// Returns true if the control is currently active.
  72. bool is_active() const;
  73. /// Returns true if the control was previously active when update() was last called.
  74. bool was_active() const;
  75. private:
  76. float deadzone;
  77. float current_value;
  78. float previous_value;
  79. bool reset;
  80. std::function<void()> activated_callback;
  81. std::function<void()> deactivated_callback;
  82. std::function<void(float)> value_changed_callback;
  83. bool callbacks_enabled;
  84. };
  85. inline void control::set_callbacks_enabled(bool enabled)
  86. {
  87. this->callbacks_enabled = enabled;
  88. }
  89. inline float control::get_deadzone() const
  90. {
  91. return deadzone;
  92. }
  93. inline float control::get_current_value() const
  94. {
  95. return current_value;
  96. }
  97. inline float control::get_previous_value() const
  98. {
  99. return previous_value;
  100. }
  101. inline bool control::is_active() const
  102. {
  103. return current_value > deadzone;
  104. }
  105. inline bool control::was_active() const
  106. {
  107. return previous_value > deadzone;
  108. }
  109. } // namespace input
  110. #endif // ANTKEEPER_INPUT_CONTROL_HPP