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

142 lines
3.7 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 activation threshold. If the current value of the control is not greater than the activation threshold, the control will not be considered active.
  49. *
  50. * @param threshold Activation threshold.
  51. */
  52. void set_activation_threshold(float threshold);
  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. /// Sets the callback for while the control is active.
  60. void set_active_callback(std::function<void(float)> callback);
  61. /**
  62. * Enables or disables callbacks.
  63. *
  64. * @param enabled Whether to enable or disable callbacks.
  65. */
  66. void set_callbacks_enabled(bool enabled);
  67. /// Returns the activation threshold. The default value is 0.0.
  68. float get_activation_threshold() const;
  69. /// Returns the current value of the control.
  70. float get_current_value() const;
  71. /// Returns the previous value of the control.
  72. float get_previous_value() const;
  73. /// Returns true if the control is currently active.
  74. bool is_active() const;
  75. /// Returns true if the control was previously active when update() was last called.
  76. bool was_active() const;
  77. private:
  78. float activation_threshold;
  79. float current_value;
  80. float previous_value;
  81. bool reset;
  82. std::function<void()> activated_callback;
  83. std::function<void()> deactivated_callback;
  84. std::function<void(float)> value_changed_callback;
  85. std::function<void(float)> active_callback;
  86. bool callbacks_enabled;
  87. };
  88. inline void control::set_callbacks_enabled(bool enabled)
  89. {
  90. this->callbacks_enabled = enabled;
  91. }
  92. inline float control::get_activation_threshold() const
  93. {
  94. return activation_threshold;
  95. }
  96. inline float control::get_current_value() const
  97. {
  98. return current_value;
  99. }
  100. inline float control::get_previous_value() const
  101. {
  102. return previous_value;
  103. }
  104. inline bool control::is_active() const
  105. {
  106. return current_value > activation_threshold;
  107. }
  108. inline bool control::was_active() const
  109. {
  110. return previous_value > activation_threshold;
  111. }
  112. } // namespace input
  113. #endif // ANTKEEPER_INPUT_CONTROL_HPP