5#include <unordered_map>
11 std::vector<asw::input::ActionBinding>
bindings;
13 bool pressed {
false };
14 bool released {
false };
16 float strength { 0.0F };
19 bool prev_down {
false };
28 return std::hash<std::string_view> { }(name);
32std::unordered_map<std::string, ActionData, NameHash, std::equal_to<>>
action_map;
42 [&out_strength](
const auto& b) ->
bool {
43 using T = std::decay_t<
decltype(b)>;
45 if constexpr (std::is_same_v<T, asw::input::KeyBinding>) {
47 out_strength = std::max(out_strength, 1.0F);
52 }
else if constexpr (std::is_same_v<T, asw::input::MouseButtonBinding>) {
54 out_strength = std::max(out_strength, 1.0F);
59 }
else if constexpr (std::is_same_v<T, asw::input::ControllerButtonBinding>) {
61 out_strength = std::max(out_strength, 1.0F);
66 }
else if constexpr (std::is_same_v<T, asw::input::ControllerAxisBinding>) {
69 const auto check = [&](uint32_t index) {
71 const float effective = b.positive_direction ? val : -val;
72 if (effective >= b.threshold) {
73 out_strength = std::max(out_strength, effective);
80 return check(b.controller_index);
85 for (uint32_t i = 0; i < count; ++i) {
101 [](
const auto& b) ->
bool {
102 using T = std::decay_t<
decltype(b)>;
104 if constexpr (std::is_same_v<T, asw::input::KeyBinding>) {
107 }
else if constexpr (std::is_same_v<T, asw::input::MouseButtonBinding>) {
110 }
else if constexpr (std::is_same_v<T, asw::input::ControllerButtonBinding>) {
125 [](
const auto& b) ->
bool {
126 using T = std::decay_t<
decltype(b)>;
128 if constexpr (std::is_same_v<T, asw::input::KeyBinding>) {
131 }
else if constexpr (std::is_same_v<T, asw::input::MouseButtonBinding>) {
134 }
else if constexpr (std::is_same_v<T, asw::input::ControllerButtonBinding>) {
153 action_map[std::string(name)].bindings.push_back(binding);
171 return it !=
action_map.end() && it->second.pressed;
177 return it !=
action_map.end() && it->second.released;
183 return it !=
action_map.end() && it->second.down;
189 return it !=
action_map.end() ? it->second.strength : 0.0F;
195 bool any_down =
false;
196 bool any_pressed =
false;
197 bool any_released =
false;
198 float max_strength = 0.0F;
200 for (
const auto& binding : action.bindings) {
210 action.pressed = !action.prev_down && (any_down || any_pressed);
211 action.released = !any_down && (action.prev_down || any_released);
212 action.down = any_down;
213 action.strength = max_strength;
214 action.prev_down = any_down;
Action binding system for the ASW library.
bool binding_is_pressed(const asw::input::ActionBinding &binding)
bool binding_is_down(const asw::input::ActionBinding &binding, float &out_strength)
Returns true if the binding is currently active, and updates out_strength.
bool binding_is_released(const asw::input::ActionBinding &binding)
Returns true if the binding was released this frame (digital sources only).
std::unordered_map< std::string, ActionData, NameHash, std::equal_to<> > action_map
std::vector< asw::input::ActionBinding > bindings
std::size_t operator()(std::string_view name) const