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

136 lines
3.4 KiB

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