Antkeeper  0.0.1
action.cpp
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 #include <engine/input/action.hpp>
21 
22 namespace input {
23 
24 namespace {
25 
26 inline bool default_threshold_function(float x) noexcept
27 {
28  return x > 0.0f;
29 }
30 
31 } // namespace
32 
34  m_threshold_function(default_threshold_function)
35 {}
36 
37 void action::evaluate(float value)
38 {
39  // Update input value
40  m_active_event.input_value = value;
41 
42  // Store activation state
43  const bool was_active = m_active;
44 
45  // Re-evaluate activation state
46  m_active = m_threshold_function(value);
47 
48  if (m_active)
49  {
50  if (!was_active)
51  {
52  // Publish activated event
53  m_activated_publisher.publish(m_activated_event);
54  }
55  }
56  else
57  {
58  if (was_active)
59  {
60  // Publish deactivated event
61  m_deactivated_publisher.publish(m_deactivated_event);
62  }
63  }
64 }
65 
66 void action::update() const
67 {
68  if (m_active)
69  {
70  // Publish active event
71  m_active_publisher.publish(m_active_event);
72  }
73 }
74 
75 void action::reset() noexcept
76 {
77  m_active = false;
78  m_active_event.input_value = 0.0f;
79 }
80 
81 } // namespace input
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
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
Input devices, events, and mapping.
float input_value
Control input value.