le2d v0.4.7
2D game engine framework
 
Loading...
Searching...
No Matches
controls.hpp
Go to the documentation of this file.
1#pragma once
2#include "le2d/event.hpp"
3#include <GLFW/glfw3.h>
4#include <bitset>
5
6namespace le::input {
7class Chord {
8 public:
9 struct Entry {
10 int actor{};
11 int action{};
12 int mods{};
13
14 auto operator==(Entry const&) const -> bool = default;
15 };
16
17 Chord() = default;
18
19 explicit constexpr Chord(Entry const& match) : m_match(match) {}
20
21 [[nodiscard]] constexpr auto is_engaged(Entry const& entry) const -> bool { return entry == m_match; }
22
23 private:
24 Entry m_match{};
25};
26
27class KeyChord : public Chord {
28 public:
29 KeyChord() = default;
30
31 explicit constexpr KeyChord(int const key, int const action = GLFW_PRESS, int const mods = 0)
32 : Chord(Entry{.actor = key, .action = action, .mods = mods}) {}
33
34 [[nodiscard]] constexpr auto is_engaged(event::Key const& key) const -> bool {
35 return Chord::is_engaged(Entry{.actor = key.key, .action = key.action, .mods = key.mods});
36 }
37};
38
39class MouseButtonChord : public Chord {
40 public:
41 MouseButtonChord() = default;
42
43 explicit constexpr MouseButtonChord(int const button, int const action = GLFW_PRESS, int const mods = 0)
44 : Chord(Entry{.actor = button, .action = action, .mods = mods}) {}
45
46 [[nodiscard]] constexpr auto is_engaged(event::MouseButton const& button) const -> bool {
47 return Chord::is_engaged(Entry{.actor = button.button, .action = button.action, .mods = button.mods});
48 }
49};
50
51class Trigger {
52 public:
53 Trigger() = default;
54
55 explicit constexpr Trigger(int const actor) : m_actor(actor) {}
56
57 constexpr auto on_action(int const actor, int const action) -> bool {
58 if (actor != m_actor) { return false; }
59 switch (action) {
60 case GLFW_PRESS: m_engaged = true; return true;
61 case GLFW_RELEASE: m_engaged = false; return true;
62 default: return false;
63 }
64 }
65
66 [[nodiscard]] constexpr auto is_engaged() const -> bool { return m_engaged; }
67 constexpr void disengage() { m_engaged = false; }
68
69 private:
70 int m_actor{};
71 bool m_engaged{};
72};
73
74class KeyTrigger : public Trigger {
75 public:
76 KeyTrigger() = default;
77
78 explicit constexpr KeyTrigger(int const key) : Trigger(key) {}
79
80 constexpr auto on_event(event::Key const& key) -> bool { return on_action(key.key, key.action); }
81};
82
84 public:
85 MouseButtonTrigger() = default;
86
87 explicit constexpr MouseButtonTrigger(int const button) : Trigger(button) {}
88
89 constexpr auto on_event(event::MouseButton const& button) -> bool { return on_action(button.button, button.action); }
90};
91
93 public:
94 DigitalAxis() = default;
95
96 explicit constexpr DigitalAxis(int lo, int hi) : m_lo(lo), m_hi(hi) {}
97
98 constexpr auto on_action(int const actor, int const action) -> bool {
99 if (actor != m_lo && actor != m_hi) { return false; }
100 switch (action) {
101 case GLFW_PRESS: on_press(actor); return true;
102 case GLFW_RELEASE: on_release(actor); return true;
103 default: return false;
104 }
105 }
106
107 [[nodiscard]] constexpr auto value() const -> float {
108 auto ret = 0.0f;
109 if (m_held.test(0)) { ret -= 1.0f; }
110 if (m_held.test(1)) { ret += 1.0f; }
111 return ret;
112 }
113
114 constexpr void disengage() { m_held.reset(); }
115
116 private:
117 constexpr void on_press(int const signal) {
118 if (signal == m_lo) {
119 m_held.set(0);
120 } else if (signal == m_hi) {
121 m_held.set(1);
122 }
123 }
124
125 constexpr void on_release(int const signal) {
126 if (signal == m_lo) {
127 m_held.reset(0);
128 } else if (signal == m_hi) {
129 m_held.reset(1);
130 }
131 }
132
133 int m_lo{};
134 int m_hi{};
135
136 std::bitset<2> m_held{};
137};
138
139class KeyAxis : public DigitalAxis {
140 public:
141 KeyAxis() = default;
142
143 explicit constexpr KeyAxis(int const key_lo, int const key_hi) : DigitalAxis(key_lo, key_hi) {}
144
145 constexpr auto on_event(event::Key const& key) -> bool { return on_action(key.key, key.action); }
146};
147
149 public:
150 MouseButtonAxis() = default;
151
152 explicit constexpr MouseButtonAxis(int const button_lo, int const button_hi) : DigitalAxis(button_lo, button_hi) {}
153
154 constexpr auto on_event(event::MouseButton const& button) -> bool { return on_action(button.button, button.action); }
155};
156} // namespace le::input
Definition controls.hpp:7
constexpr Chord(Entry const &match)
Definition controls.hpp:19
constexpr auto is_engaged(Entry const &entry) const -> bool
Definition controls.hpp:21
Definition controls.hpp:92
constexpr DigitalAxis(int lo, int hi)
Definition controls.hpp:96
constexpr auto value() const -> float
Definition controls.hpp:107
constexpr void disengage()
Definition controls.hpp:114
constexpr auto on_action(int const actor, int const action) -> bool
Definition controls.hpp:98
Definition controls.hpp:139
constexpr KeyAxis(int const key_lo, int const key_hi)
Definition controls.hpp:143
constexpr auto on_event(event::Key const &key) -> bool
Definition controls.hpp:145
Definition controls.hpp:27
constexpr KeyChord(int const key, int const action=GLFW_PRESS, int const mods=0)
Definition controls.hpp:31
constexpr auto is_engaged(event::Key const &key) const -> bool
Definition controls.hpp:34
Definition controls.hpp:74
constexpr KeyTrigger(int const key)
Definition controls.hpp:78
constexpr auto on_event(event::Key const &key) -> bool
Definition controls.hpp:80
Definition controls.hpp:148
constexpr auto on_event(event::MouseButton const &button) -> bool
Definition controls.hpp:154
constexpr MouseButtonAxis(int const button_lo, int const button_hi)
Definition controls.hpp:152
Definition controls.hpp:39
constexpr MouseButtonChord(int const button, int const action=GLFW_PRESS, int const mods=0)
Definition controls.hpp:43
constexpr auto is_engaged(event::MouseButton const &button) const -> bool
Definition controls.hpp:46
Definition controls.hpp:83
constexpr MouseButtonTrigger(int const button)
Definition controls.hpp:87
constexpr auto on_event(event::MouseButton const &button) -> bool
Definition controls.hpp:89
Definition controls.hpp:51
constexpr void disengage()
Definition controls.hpp:67
constexpr Trigger(int const actor)
Definition controls.hpp:55
constexpr auto is_engaged() const -> bool
Definition controls.hpp:66
constexpr auto on_action(int const actor, int const action) -> bool
Definition controls.hpp:57
Definition action.hpp:4
Definition event.hpp:39
Definition event.hpp:47
Definition controls.hpp:9
int actor
Definition controls.hpp:10
auto operator==(Entry const &) const -> bool=default
int mods
Definition controls.hpp:12
int action
Definition controls.hpp:11