Toast Engine
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
17namespace input {
18
22enum class ValueType : uint8_t {
26};
27
31struct Value {
32 glm::vec2 data {0.0f, 0.0f};
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
58template<>
59inline auto Value::as() const noexcept -> bool {
60 return data.x != 0.0f;
61}
62
63template<>
64inline auto Value::as() const noexcept -> float {
65 return data.x;
66}
67
68template<>
69inline auto Value::as() const noexcept -> glm::vec2 {
70 return data;
71}
72
76enum class ActionEvent : uint8_t {
77 start,
83};
84
90struct 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
131 float scalar = 0.0f;
132 glm::vec2 vector {0.0f, 0.0f};
133 bool is_vector = false;
134 bool present = true;
135};
136
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}
ModifierKey
Bit flags for the keyboard modifier keys held during an input.
Definition keycodes.hpp:28
Per-frame context passed down to triggers and modifiers during evaluation.
Definition value.hpp:140
ModifierKey mods
keyboard modifier keys held this frame
Definition value.hpp:142
glm::vec2 viewport_size
size of the active viewport in pixels, for cursor NDC conversion
Definition value.hpp:143
float delta
frame delta time in seconds; currently always zero, see input system
Definition value.hpp:141
glm::vec2 viewport_pos
top-left of the active viewport in pixels
Definition value.hpp:144
One frame of raw input for a single keycode.
Definition value.hpp:130
Fixed-capacity buffer of discrete events fired in a single frame.
Definition value.hpp:90
void emit(ActionEvent event) noexcept
Appends an event, ignoring overflow past the capacity.
Definition value.hpp:95
void merge(const SignalList &other) noexcept
Appends every event from another list.
Definition value.hpp:102
auto as() const noexcept -> T
auto magnitude() const noexcept -> float
Definition value.hpp:50
ValueType
Value domain of an action or an input sample.
Definition value.hpp:22
@ axis_0d
boolean
Definition value.hpp:23
@ axis_1d
float
Definition value.hpp:24
@ axis_2d
vector
Definition value.hpp:25
ActionEvent
Discrete event an action can emit, carried alongside the action in an InputEvent.
Definition value.hpp:76
@ release
the input went from on to off
Definition value.hpp:79
@ tries
the input was pressed but a countdown must elapse before it starts
Definition value.hpp:80
@ cancelled
the input was released before the countdown finished
Definition value.hpp:82
@ countdown
equivalent of hold while the start countdown is running
Definition value.hpp:81
@ hold
the input is held; sent continuously or on a pulse
Definition value.hpp:78