Antkeeper  0.0.1
action.hpp
Go to the documentation of this file.
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 
20 #ifndef ANTKEEPER_INPUT_ACTION_HPP
21 #define ANTKEEPER_INPUT_ACTION_HPP
22 
25 #include <functional>
26 
27 namespace input {
28 
32 class action
33 {
34 public:
40  using threshold_function_type = std::function<bool(float)>;
41 
43  action();
44 
50  inline void set_threshold_function(const threshold_function_type& function) noexcept
51  {
52  m_threshold_function = function;
53  }
54 
60  void evaluate(float value);
61 
65  void update() const;
66 
70  void reset() noexcept;
71 
73  [[nodiscard]] inline const threshold_function_type& get_threshold_function() const noexcept
74  {
75  return m_threshold_function;
76  }
77 
79  [[nodiscard]] inline bool is_active() const noexcept
80  {
81  return m_active;
82  }
83 
85  [[nodiscard]] inline float get_input_value() const noexcept
86  {
87  return m_active_event.input_value;
88  }
89 
91  [[nodiscard]] inline ::event::channel<action_activated_event>& get_activated_channel() noexcept
92  {
93  return m_activated_publisher.channel();
94  }
95 
97  [[nodiscard]] inline ::event::channel<action_active_event>& get_active_channel() noexcept
98  {
99  return m_active_publisher.channel();
100  }
101 
103  [[nodiscard]] inline ::event::channel<action_deactivated_event>& get_deactivated_channel() noexcept
104  {
105  return m_deactivated_publisher.channel();
106  }
107 
108 private:
109  threshold_function_type m_threshold_function;
110  bool m_active{false};
111 
112  action_activated_event m_activated_event{this};
113  action_active_event m_active_event{this, 0.0f};
114  action_deactivated_event m_deactivated_event{this};
115 
116  ::event::publisher<action_activated_event> m_activated_publisher;
117  ::event::publisher<action_active_event> m_active_publisher;
118  ::event::publisher<action_deactivated_event> m_deactivated_publisher;
119 };
120 
121 } // namespace input
122 
123 #endif // ANTKEEPER_INPUT_ACTION_HPP
Publishes messages to subscribers.
Definition: publisher.hpp:36
Evaluates an activation state given input values and publishes events on activation state changes.
Definition: action.hpp:33
action()
Constructs an action.
Definition: action.cpp:33
void reset() noexcept
Resets the activation state of the action without publishing any events.
Definition: action.cpp:75
::event::channel< action_active_event > & get_active_channel() noexcept
Returns the channel through which action active events are published.
Definition: action.hpp:97
void set_threshold_function(const threshold_function_type &function) noexcept
Sets the threshold function.
Definition: action.hpp:50
bool is_active() const noexcept
Returns true if the action is active, false otherwise.
Definition: action.hpp:79
std::function< bool(float)> threshold_function_type
Threshold function type.
Definition: action.hpp:40
float get_input_value() const noexcept
Returns the most recently evaluated input value.
Definition: action.hpp:85
void evaluate(float value)
Evaluates the activation state of the action, according to its threshold function and an input value.
Definition: action.cpp:37
void update() const
Publishes an action active event if the action is active.
Definition: action.cpp:66
::event::channel< action_deactivated_event > & get_deactivated_channel() noexcept
Returns the channel through which action deactivated events are published.
Definition: action.hpp:103
::event::channel< action_activated_event > & get_activated_channel() noexcept
Returns the channel through which action activated events are published.
Definition: action.hpp:91
const threshold_function_type & get_threshold_function() const noexcept
Returns the threshold function.
Definition: action.hpp:73
Input devices, events, and mapping.
float input_value
Control input value.