le2d v0.4.7
2D game engine framework
 
Loading...
Searching...
No Matches
action_types.hpp
Go to the documentation of this file.
1#pragma once
2#include "le2d/event.hpp"
4#include <GLFW/glfw3.h>
5#include <glm/vec2.hpp>
6#include <klib/base_types.hpp>
7#include <bitset>
8#include <optional>
9
10namespace le::input {
11namespace action {
12class Value;
13} // namespace action
14
16class IAction : public klib::Polymorphic {
17 public:
18 virtual void on_key(event::Key const& key) = 0;
19 virtual void on_mouse_button(event::MouseButton const& mb) = 0;
20 virtual void on_scroll(event::Scroll const& scroll) = 0;
21 virtual void on_cursor_pos(event::CursorPos const& cursor_pos) = 0;
22 virtual void update_gamepad(Gamepad const& gamepad) = 0;
23 virtual void disengage() = 0;
24 [[nodiscard]] virtual auto should_dispatch() const -> bool = 0;
25 [[nodiscard]] virtual auto get_gamepad_binding() const -> std::optional<Gamepad::Binding> = 0;
26 [[nodiscard]] virtual auto get_value() const -> action::Value = 0;
27};
28
29namespace action {
31template <typename Type>
32concept ValueTypeT = std::same_as<Type, bool> || std::same_as<Type, float> || std::same_as<Type, glm::vec2>;
33
35class Value {
36 public:
37 Value() = default;
38
39 explicit(false) Value(bool const digital) : m_value(digital ? 1.0f : 0.0f, 0.0f) {}
40 explicit(false) Value(float const axis_1d) : m_value(axis_1d, 0.0f) {}
41 explicit(false) Value(glm::vec2 const axis_2d) : m_value(axis_2d) {}
42
44 template <ValueTypeT Type>
45 [[nodiscard]] auto get() const -> Type {
46 if constexpr (std::same_as<Type, bool>) {
47 return m_value[0] != 0.0f;
48 } else if constexpr (std::same_as<Type, float>) {
49 return m_value[0];
50 } else {
51 return m_value;
52 }
53 }
54
55 private:
56 glm::vec2 m_value{};
57};
58
60template <std::size_t Count>
61using Bits = std::bitset<Count>;
62
65class IDigital : public IAction {
66 public:
67 IDigital() = default;
68
69 explicit IDigital(int const match) : match(match) {}
70
72 int match{};
73
74 protected:
75 void on_input(int actor, int action);
76
77 private:
78 void disengage() final;
79 [[nodiscard]] auto should_dispatch() const -> bool final;
80 [[nodiscard]] auto get_value() const -> action::Value final { return m_state[0]; }
81
82 Bits<1> m_state{};
83 mutable bool m_changed{};
84};
85
88class IBinaryAxis1D : public IAction {
89 public:
90 IBinaryAxis1D() = default;
91
94 explicit IBinaryAxis1D(int const lo, int const hi) : lo(lo), hi(hi) {}
95
97 int lo{};
99 int hi{};
100
101 protected:
102 void on_input(int actor, int action);
103
104 private:
105 void disengage() final;
106 [[nodiscard]] auto should_dispatch() const -> bool final { return true; }
107 [[nodiscard]] auto get_value() const -> action::Value final;
108
109 Bits<2> m_state{};
110};
111
114class IBinaryAxis2D : public IAction {
115 public:
116 IBinaryAxis2D() = default;
117
120 explicit IBinaryAxis2D(glm::ivec2 const horz, glm::ivec2 const vert) : horz(horz), vert(vert) {}
121
123 glm::ivec2 horz{};
125 glm::ivec2 vert{};
126
127 protected:
128 void on_input(int actor, int action);
129
130 private:
131 void disengage() final;
132 [[nodiscard]] auto should_dispatch() const -> bool final { return true; }
133 [[nodiscard]] auto get_value() const -> action::Value final;
134
135 Bits<4> m_state{};
136};
137
138class IGamepadAxis : public IAction {
139 public:
140 static constexpr auto dead_zone_v = 0.05f;
141
142 IGamepadAxis() = default;
143
144 explicit IGamepadAxis(float const dead_zone) : dead_zone(dead_zone) {}
145
147 float dead_zone{dead_zone_v};
148
151
152 private:
153 void on_key(event::Key const& /*key*/) final {}
154 void on_mouse_button(event::MouseButton const& /*mb*/) final {}
155 void on_scroll(event::Scroll const& /*scroll*/) final {}
156 void on_cursor_pos(event::CursorPos const& /*cursor_pos*/) final {}
157 [[nodiscard]] auto should_dispatch() const -> bool final { return true; }
158 [[nodiscard]] auto get_gamepad_binding() const -> std::optional<Gamepad::Binding> final { return binding; }
159};
160
162template <std::derived_from<IAction> BaseT>
163class KeyBase : public BaseT {
164 public:
165 using BaseT::BaseT;
166
167 private:
168 void on_key(event::Key const& key) final { this->on_input(key.key, key.action); }
169 void on_mouse_button(event::MouseButton const& /*mb*/) final {}
170 void on_scroll(event::Scroll const& /*scroll*/) final {}
171 void on_cursor_pos(event::CursorPos const& /*cursor_pos*/) final {}
172 void update_gamepad(Gamepad const& /*gamepad*/) final {}
173 [[nodiscard]] auto get_gamepad_binding() const -> std::optional<Gamepad::Binding> final { return {}; }
174};
175
177template <std::derived_from<IAction> BaseT>
178class MouseButtonBase : public BaseT {
179 public:
180 using BaseT::BaseT;
181
182 private:
183 void on_key(event::Key const& /*mb*/) final {}
184 void on_mouse_button(event::MouseButton const& mb) final { this->on_input(mb.button, mb.action); }
185 void on_scroll(event::Scroll const& /*scroll*/) final {}
186 void on_cursor_pos(event::CursorPos const& /*cursor_pos*/) final {}
187 void update_gamepad(Gamepad const& /*gamepad*/) final {}
188 [[nodiscard]] auto get_gamepad_binding() const -> std::optional<Gamepad::Binding> final { return {}; }
189};
190
192class MouseScrollBase : public IAction {
193 protected:
194 enum class Dim : std::int8_t { One, Two };
195
196 explicit MouseScrollBase(Dim const dim) : m_dim(dim) {}
197
198 private:
199 void on_key(event::Key const& /*mb*/) final {}
200 void on_mouse_button(event::MouseButton const& /*mb*/) final {}
201 void on_scroll(event::Scroll const& scroll) final;
202 void on_cursor_pos(event::CursorPos const& /*cursor_pos*/) final {}
203 void update_gamepad(Gamepad const& /*gamepad*/) final {}
204 void disengage() final { m_value = {}; }
205 [[nodiscard]] auto should_dispatch() const -> bool final;
206 [[nodiscard]] auto get_gamepad_binding() const -> std::optional<Gamepad::Binding> final { return {}; }
207 [[nodiscard]] auto get_value() const -> action::Value final;
208
209 Dim m_dim;
210 mutable glm::vec2 m_value{};
211};
212} // namespace action
213} // namespace le::input
Snapshot of gamepad / game controller state.
Definition gamepad.hpp:14
std::variant< FirstUsed, LastUsed, Id > Binding
Variant of selectors.
Definition gamepad.hpp:28
Base class for all actions.
Definition action_types.hpp:16
virtual auto should_dispatch() const -> bool=0
virtual void on_key(event::Key const &key)=0
virtual void disengage()=0
virtual void update_gamepad(Gamepad const &gamepad)=0
virtual auto get_value() const -> action::Value=0
virtual void on_mouse_button(event::MouseButton const &mb)=0
virtual auto get_gamepad_binding() const -> std::optional< Gamepad::Binding >=0
virtual void on_scroll(event::Scroll const &scroll)=0
virtual void on_cursor_pos(event::CursorPos const &cursor_pos)=0
Interface for binary 1D axis actions.
Definition action_types.hpp:88
void on_input(int actor, int action)
IBinaryAxis1D(int const lo, int const hi)
Definition action_types.hpp:94
Interface for binary 2D axis actions.
Definition action_types.hpp:114
void on_input(int actor, int action)
IBinaryAxis2D(glm::ivec2 const horz, glm::ivec2 const vert)
Definition action_types.hpp:120
Interface for digital actions.
Definition action_types.hpp:65
void on_input(int actor, int action)
IDigital(int const match)
Definition action_types.hpp:69
Definition action_types.hpp:138
IGamepadAxis(float const dead_zone)
Definition action_types.hpp:144
Class template for actions using key matches.
Definition action_types.hpp:163
Class template for actions using mouse button matches.
Definition action_types.hpp:178
Base class for mouse scroll actions.
Definition action_types.hpp:192
MouseScrollBase(Dim const dim)
Definition action_types.hpp:196
Dim
Definition action_types.hpp:194
Value associated with an action callback.
Definition action_types.hpp:35
auto get() const -> Type
Definition action_types.hpp:45
Concept for gettable Value types.
Definition action_types.hpp:32
std::bitset< Count > Bits
Alias for Count binary states.
Definition action_types.hpp:61
Definition action.hpp:4
Definition event.hpp:32
Definition event.hpp:39
int action
Definition event.hpp:41
int key
Definition event.hpp:40
Definition event.hpp:47
int button
Definition event.hpp:48
int action
Definition event.hpp:49
Definition event.hpp:55
Selector for first used gamepad.
Definition gamepad.hpp:24