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

104 lines
3.0 KiB

  1. /*
  2. * Copyright (C) 2023 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 "input/input-events.hpp"
  22. #include "event/publisher.hpp"
  23. #include <functional>
  24. namespace input {
  25. /**
  26. * Generates control-related input events on activation state changes.
  27. */
  28. class control
  29. {
  30. public:
  31. /**
  32. * Threshold function type.
  33. *
  34. * Given an input value, returns `true` if the control should be considered active, and `false` otherwise.
  35. */
  36. typedef std::function<bool(float)> threshold_function_type;
  37. /// Constructs a control.
  38. control();
  39. /**
  40. * Sets the threshold function.
  41. *
  42. * @param function Threshold function.
  43. */
  44. void set_threshold_function(const threshold_function_type& function);
  45. /**
  46. * Evaluates the activation state of the control, according to its threshold function and an input value.
  47. *
  48. * @param value Input value.
  49. */
  50. void evaluate(float value);
  51. /// Returns the threshold function.
  52. [[nodiscard]] inline const threshold_function_type& get_threshold_function() const noexcept
  53. {
  54. return threshold_function;
  55. }
  56. /// Returns `true` if the control is active, `false` otherwise.
  57. [[nodiscard]] inline bool is_active() const noexcept
  58. {
  59. return active;
  60. }
  61. /// Returns the channel through which control activated events are published.
  62. [[nodiscard]] inline ::event::channel<control_activated_event>& get_activated_channel() noexcept
  63. {
  64. return activated_publisher.channel();
  65. }
  66. /// Returns the channel through which control active events are published.
  67. [[nodiscard]] inline ::event::channel<control_active_event>& get_active_channel() noexcept
  68. {
  69. return active_publisher.channel();
  70. }
  71. /// Returns the channel through which control deactivated events are published.
  72. [[nodiscard]] inline ::event::channel<control_deactivated_event>& get_deactivated_channel() noexcept
  73. {
  74. return deactivated_publisher.channel();
  75. }
  76. private:
  77. threshold_function_type threshold_function;
  78. bool active;
  79. control_activated_event activated_event;
  80. control_active_event active_event;
  81. control_deactivated_event deactivated_event;
  82. ::event::publisher<control_activated_event> activated_publisher;
  83. ::event::publisher<control_active_event> active_publisher;
  84. ::event::publisher<control_deactivated_event> deactivated_publisher;
  85. };
  86. } // namespace input
  87. #endif // ANTKEEPER_INPUT_CONTROL_HPP