Toast Engine
Toggle main menu visibility
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1
8
9
#pragma once
10
11
#include "
keycodes.hpp
"
12
13
#include <array>
14
#include <cmath>
15
#include <glm/glm.hpp>
16
17
namespace
input {
18
22
enum class
ValueType
: uint8_t {
23
axis_0d
,
24
axis_1d
,
25
axis_2d
,
26
};
27
31
struct
Value {
32
glm::vec2 data {0.0f, 0.0f};
33
ValueType
type =
ValueType::axis_0d
;
34
35
Value() =
default
;
36
37
Value(
bool
v,
ValueType
t =
ValueType::axis_0d
) : data(v ? 1.0f : 0.0f, 0.0f), type(t) { }
38
39
Value(
float
v,
ValueType
t =
ValueType::axis_1d
) : data(v, 0.0f), type(t) { }
40
41
Value(glm::vec2 v,
ValueType
t =
ValueType::axis_2d
) : data(v), type(t) { }
42
44
template
<
typename
T>
45
[[nodiscard]]
46
auto
as
() const noexcept -> T;
47
49
[[nodiscard]]
50
auto
magnitude
() const noexcept ->
float
{
51
if
(type ==
ValueType::axis_2d
) {
52
return
glm::length(data);
53
}
54
return
std::abs(data.x);
55
}
56
};
57
58
template
<>
59
inline
auto
Value::as
() const noexcept ->
bool
{
60
return
data.x != 0.0f;
61
}
62
63
template
<>
64
inline
auto
Value::as
() const noexcept ->
float
{
65
return
data.x;
66
}
67
68
template
<>
69
inline
auto
Value::as
() const noexcept -> glm::vec2 {
70
return
data;
71
}
72
76
enum class
ActionEvent
: uint8_t {
77
start,
78
hold
,
79
release
,
80
tries
,
81
countdown
,
82
cancelled
,
83
};
84
90
struct
SignalList
{
91
std::array<ActionEvent, 8> items {};
92
uint8_t count = 0;
93
95
void
emit
(
ActionEvent
event)
noexcept
{
96
if
(count < items.size()) {
97
items[count++] = event;
98
}
99
}
100
102
void
merge
(
const
SignalList
& other)
noexcept
{
103
for
(uint8_t i = 0; i < other.count; ++i) {
104
emit
(other.items[i]);
105
}
106
}
107
108
[[nodiscard]]
109
auto
begin() const noexcept {
110
return
items.begin();
111
}
112
113
[[nodiscard]]
114
auto
end() const noexcept {
115
return
items.begin() + count;
116
}
117
118
[[nodiscard]]
119
auto
empty() const noexcept ->
bool
{
120
return
count == 0;
121
}
122
};
123
130
struct
InputSample
{
131
float
scalar = 0.0f;
132
glm::vec2 vector {0.0f, 0.0f};
133
bool
is_vector =
false
;
134
bool
present =
true
;
135
};
136
140
struct
EvalContext
{
141
float
delta
= 0.0f;
142
ModifierKey
mods
= ModifierKey::none;
143
glm::vec2
viewport_size
{0.0f, 0.0f};
144
glm::vec2
viewport_pos
{0.0f, 0.0f};
145
};
146
147
}
keycodes.hpp
input::ModifierKey
ModifierKey
Bit flags for the keyboard modifier keys held during an input.
Definition
keycodes.hpp:28
input::EvalContext
Per-frame context passed down to triggers and modifiers during evaluation.
Definition
value.hpp:140
input::EvalContext::mods
ModifierKey mods
keyboard modifier keys held this frame
Definition
value.hpp:142
input::EvalContext::viewport_size
glm::vec2 viewport_size
size of the active viewport in pixels, for cursor NDC conversion
Definition
value.hpp:143
input::EvalContext::delta
float delta
frame delta time in seconds; currently always zero, see input system
Definition
value.hpp:141
input::EvalContext::viewport_pos
glm::vec2 viewport_pos
top-left of the active viewport in pixels
Definition
value.hpp:144
input::InputSample
One frame of raw input for a single keycode.
Definition
value.hpp:130
input::SignalList
Fixed-capacity buffer of discrete events fired in a single frame.
Definition
value.hpp:90
input::SignalList::emit
void emit(ActionEvent event) noexcept
Appends an event, ignoring overflow past the capacity.
Definition
value.hpp:95
input::SignalList::merge
void merge(const SignalList &other) noexcept
Appends every event from another list.
Definition
value.hpp:102
input::Value::as
auto as() const noexcept -> T
input::Value::magnitude
auto magnitude() const noexcept -> float
Definition
value.hpp:50
input::ValueType
ValueType
Value domain of an action or an input sample.
Definition
value.hpp:22
input::ValueType::axis_0d
@ axis_0d
boolean
Definition
value.hpp:23
input::ValueType::axis_1d
@ axis_1d
float
Definition
value.hpp:24
input::ValueType::axis_2d
@ axis_2d
vector
Definition
value.hpp:25
input::ActionEvent
ActionEvent
Discrete event an action can emit, carried alongside the action in an InputEvent.
Definition
value.hpp:76
input::ActionEvent::release
@ release
the input went from on to off
Definition
value.hpp:79
input::ActionEvent::tries
@ tries
the input was pressed but a countdown must elapse before it starts
Definition
value.hpp:80
input::ActionEvent::cancelled
@ cancelled
the input was released before the countdown finished
Definition
value.hpp:82
input::ActionEvent::countdown
@ countdown
equivalent of hold while the start countdown is running
Definition
value.hpp:81
input::ActionEvent::hold
@ hold
the input is held; sent continuously or on a pulse
Definition
value.hpp:78
engine
src
toast
input
value.hpp
Generated by
1.17.0