Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Input.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_INPUT_HANDLER
19#define INCLUDED_INPUT_HANDLER
20
21#include "ps/Input.h"
22
23#include <array>
24#include <concepts>
25#include <cstddef>
26#include <queue>
27#include <memory>
28#include <optional>
29#include <type_traits>
30
31class ScriptRequest;
32union SDL_Event;
33
34namespace Input
35{
36class HandlerBase;
37
38enum class Reaction
39{
40 // Pass the event to the next handler in the chain.
41 PASS,
42
43 // We handled it. No other handlers will receive this event.
45};
46
47// A slot for each handler. Numbers are in invocation order. A handler can discard events. The first handler
48// is the only which gets all events.
49namespace Slot
50{
51constexpr std::integral_constant<std::size_t, 0> PRIMARY;
52constexpr std::integral_constant<std::size_t, 1> WINDOW;
53constexpr std::integral_constant<std::size_t, 2> SCREENSHOT;
54
55// These two must be called first `globalsInput` deals with some important global state, such as which
56// scancodes are being pressed, mouse buttons pressed, etc. while hotkeyStateChange updates the map of
57// active hotkeys.
58constexpr std::integral_constant<std::size_t, 3> HOTKEY_STATE_CHANGE;
59constexpr std::integral_constant<std::size_t, 4> GLOBAL;
60
61// Should be called after scancode map update (i.e. after the global input, but before UI). This never
62// blocks the event, but it does some processing necessary for hotkeys, which are triggered later down the
63// input chain. (by calling this before the UI, we can use `EventWouldTriggerHotkey` in the UI).
64constexpr std::integral_constant<std::size_t, 5> HOTKEY_INPUT_PREPARATION;
65
66constexpr std::integral_constant<std::size_t, 6> TOUCH_INPUT;
67
68// The console handler needs to be called before the hotkey handler so that text can be typed in without
69// setting off hotkeys.
70constexpr std::integral_constant<std::size_t, 7> CONSOLE;
71// Likewise for gui.
72constexpr std::integral_constant<std::size_t, 8> GUI;
73constexpr std::integral_constant<std::size_t, 9> HOTKEY_INPUT;
74constexpr std::integral_constant<std::size_t, 10> PROFILE_VIEWER;
75constexpr std::integral_constant<std::size_t, 11> GAME_VIEW;
76}
77
78/**
79 * Holds a pointer to all registered `HandlerBase`s.
80 * It does unregister all registered `Handler`s in the destructor.
81 */
83{
84public:
85 Manager();
86 // The `Manager` needs to have a constant memory location (the `Handler`s hold a pointer to it).
87 Manager(Manager&) = delete;
89 Manager(Manager&&) = delete;
92
93 void PushPriorityEvent(const SDL_Event& event);
94 void DispatchEvent(const SDL_Event& event);
95
96 template<std::size_t slot>
97 HandlerBase*& Get() noexcept
98 {
99 return std::get<slot>(m_Handlers);
100 }
101
103 {
104 public:
106 {
107 public:
108 using difference_type = std::ptrdiff_t;
109 using value_type = SDL_Event;
112 using iterator_category = std::input_iterator_tag;
113
114 EventIterator() = default;
115 explicit EventIterator(PollEventsResult& range);
116
119
122
123 bool operator==(const EventIterator& other) const;
124
125 private:
127 };
128
129 explicit PollEventsResult(std::queue<SDL_Event>& priorityEvents);
130
131 EventIterator begin();
132 EventIterator end();
133
134 private:
135 std::queue<SDL_Event>& m_PriorityEvents;
136 const std::unique_ptr<SDL_Event> m_Event;
137 };
138
140
141private:
142 std::array<HandlerBase*, 12> m_Handlers{{}};
143 const std::unique_ptr<std::queue<SDL_Event>> m_PriorityEvents;
144};
145
146/**
147 * Type-erased callable
148 */
150{
151protected:
152 /**
153 * Can't not be constructed themself, use `Handler` instead.
154 * @param pos On construction the pointer is set to @c this. On
155 * destruction the pointer will be set to nullptr.
156 */
157 explicit HandlerBase(HandlerBase*& pos) noexcept;
158 virtual ~HandlerBase();
159
160 // A `HandlerBase` needs to have a constant memory location (the `Manager` does hold a pointer to it).
165
166public:
167 virtual Input::Reaction operator()(const SDL_Event& event) = 0;
168
169private:
170 // When the handler is destructed this position han to be changed to
171 // `nullptr`, so that it's not invoked again.
173};
174
175/**
176 * Usable type to register a handler to the associated `Manager`.
177 */
178template<std::invocable<const SDL_Event&> Callback>
179class Handler final : private HandlerBase
180{
181public:
182 // `slot` specifies when(in which order) the `callback` of this `Handler` is executed.
183 template<std::size_t slot>
184 explicit Handler(Manager& manager, std::integral_constant<std::size_t, slot>, Callback func) :
185 HandlerBase{manager.Get<slot>()},
186 m_Callback{std::move(func)}
187 {
188 }
189 ~Handler() final = default;
190
191private:
192 Input::Reaction operator()(const SDL_Event& event) final
193 {
194 return m_Callback(event);
195 }
196
197 Callback m_Callback;
198};
199}
200
201#endif // INCLUDED_INPUT_HANDLER
Type-erased callable.
Definition: Input.h:150
HandlerBase *& m_ToReset
Definition: Input.h:172
HandlerBase & operator=(HandlerBase &)=delete
HandlerBase & operator=(HandlerBase &&)=delete
HandlerBase(HandlerBase *&pos) noexcept
Can't not be constructed themself, use Handler instead.
Definition: Input.cpp:119
virtual Input::Reaction operator()(const SDL_Event &event)=0
HandlerBase(HandlerBase &)=delete
virtual ~HandlerBase()
Definition: Input.cpp:126
HandlerBase(HandlerBase &&)=delete
Usable type to register a handler to the associated Manager.
Definition: Input.h:180
Callback m_Callback
Definition: Input.h:197
~Handler() final=default
Handler(Manager &manager, std::integral_constant< std::size_t, slot >, Callback func)
Definition: Input.h:184
value_type * pointer
Definition: Input.h:110
bool operator==(const EventIterator &other) const
Definition: Input.cpp:92
PollEventsResult * m_Storage
Definition: Input.h:126
pointer operator->()
Definition: Input.cpp:67
reference operator*()
Definition: Input.cpp:62
std::input_iterator_tag iterator_category
Definition: Input.h:112
EventIterator & operator++()
Definition: Input.cpp:72
SDL_Event value_type
Definition: Input.h:109
std::ptrdiff_t difference_type
Definition: Input.h:108
value_type & reference
Definition: Input.h:111
Definition: Input.h:103
EventIterator begin()
Definition: Input.cpp:103
const std::unique_ptr< SDL_Event > m_Event
Definition: Input.h:136
EventIterator end()
Definition: Input.cpp:108
std::queue< SDL_Event > & m_PriorityEvents
Definition: Input.h:135
PollEventsResult(std::queue< SDL_Event > &priorityEvents)
Definition: Input.cpp:97
Holds a pointer to all registered HandlerBases.
Definition: Input.h:83
const std::unique_ptr< std::queue< SDL_Event > > m_PriorityEvents
Definition: Input.h:143
std::array< HandlerBase *, 12 > m_Handlers
Definition: Input.h:142
Manager & operator=(Manager &&)=delete
PollEventsResult PollEvents()
Definition: Input.cpp:113
Manager()
Definition: Input.cpp:34
Manager & operator=(Manager &)=delete
void DispatchEvent(const SDL_Event &event)
Definition: Input.cpp:46
void PushPriorityEvent(const SDL_Event &event)
Definition: Input.cpp:41
Manager(Manager &)=delete
HandlerBase *& Get() noexcept
Definition: Input.h:97
Manager(Manager &&)=delete
constexpr std::integral_constant< std::size_t, 6 > TOUCH_INPUT
Definition: Input.h:66
constexpr std::integral_constant< std::size_t, 0 > PRIMARY
Definition: Input.h:51
constexpr std::integral_constant< std::size_t, 5 > HOTKEY_INPUT_PREPARATION
Definition: Input.h:64
constexpr std::integral_constant< std::size_t, 7 > CONSOLE
Definition: Input.h:70
constexpr std::integral_constant< std::size_t, 9 > HOTKEY_INPUT
Definition: Input.h:73
constexpr std::integral_constant< std::size_t, 4 > GLOBAL
Definition: Input.h:59
constexpr std::integral_constant< std::size_t, 8 > GUI
Definition: Input.h:72
constexpr std::integral_constant< std::size_t, 11 > GAME_VIEW
Definition: Input.h:75
constexpr std::integral_constant< std::size_t, 1 > WINDOW
Definition: Input.h:52
constexpr std::integral_constant< std::size_t, 2 > SCREENSHOT
Definition: Input.h:53
constexpr std::integral_constant< std::size_t, 10 > PROFILE_VIEWER
Definition: Input.h:74
constexpr std::integral_constant< std::size_t, 3 > HOTKEY_STATE_CHANGE
Definition: Input.h:58
Definition: Input.cpp:33
Reaction
Definition: Input.h:39
void Get(const CStr &value, T &ret)
Definition: ConfigDB.cpp:71
Definition: ShaderDefines.cpp:43