guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
overlay.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/systems/overlay.hpp"
24
25#include <algorithm>
26
27namespace guillaume::systems
28{
29 Overlay::Overlay(event::EventBus &eventBus, utility::Engine &engine)
31 components::Bound>(ecs::Phase::Event)
32 , event::EventManager<utility::event::KeyboardEvent>(eventBus)
33 , _engine(engine)
34 , _stack()
35 , _lastTime(std::chrono::steady_clock::now())
36 {
37 }
38
40 {
42
43 const auto now = std::chrono::steady_clock::now();
44 const std::chrono::duration<float> elapsed = now - _lastTime;
45 _lastTime = now;
46 advance(elapsed.count());
47
48 const auto key = getLastEvent();
49 if (key && key->getIsDownEvent()
50 && key->getScancode()
51 == utility::event::KeyboardEvent::ScanCode::Escape) {
53 }
54 }
55
56 void Overlay::update(const ecs::Entity::Identifier &entityIdentifier)
57 {
58 if (!requireComponent<components::Overlay>(entityIdentifier)) {
59 return;
60 }
61
62 auto &overlay = getComponent<components::Overlay>(entityIdentifier);
63
64 if (overlay.isVisible() && !isOpen(entityIdentifier)) {
65 _stack.push_back(entityIdentifier);
66 _engine.setShouldCaptureViewportInput(false);
67 } else if (!overlay.isVisible() && isOpen(entityIdentifier)) {
68 dismiss(entityIdentifier);
69 }
70 }
71
72 void Overlay::show(const ecs::Entity::Identifier &entityIdentifier)
73 {
74 if (isOpen(entityIdentifier)) {
75 return;
76 }
77
78 _stack.push_back(entityIdentifier);
79
80 if (hasComponent<components::Overlay>(entityIdentifier)) {
81 getComponent<components::Overlay>(entityIdentifier)
82 .setVisible(true);
83 }
84
85 _engine.setShouldCaptureViewportInput(false);
86 }
87
89 {
90 if (_stack.empty()) {
91 return;
92 }
93
94 const auto top = _stack.back();
95 _stack.pop_back();
96
98 auto &overlay = getComponent<components::Overlay>(top);
99 overlay.setVisible(false);
100 overlay.setElapsed(0.0f);
101 if (const auto handler = overlay.getOnDismissHandler()) {
102 handler();
103 }
104 }
105
106 if (_stack.empty()) {
107 _engine.setShouldCaptureViewportInput(true);
108 }
109 }
110
111 void Overlay::dismiss(const ecs::Entity::Identifier &entityIdentifier)
112 {
113 const auto iterator =
114 std::find(_stack.begin(), _stack.end(), entityIdentifier);
115 if (iterator == _stack.end()) {
116 return;
117 }
118 _stack.erase(iterator);
119
120 if (hasComponent<components::Overlay>(entityIdentifier)) {
121 auto &overlay = getComponent<components::Overlay>(entityIdentifier);
122 overlay.setVisible(false);
123 overlay.setElapsed(0.0f);
124 if (const auto handler = overlay.getOnDismissHandler()) {
125 handler();
126 }
127 }
128
129 if (_stack.empty()) {
130 _engine.setShouldCaptureViewportInput(true);
131 }
132 }
133
134 bool Overlay::isOpen(const ecs::Entity::Identifier &entityIdentifier) const
135 {
136 return std::find(_stack.begin(), _stack.end(), entityIdentifier)
137 != _stack.end();
138 }
139
141 {
142 return _stack.empty() ? ecs::Entity::InvalidIdentifier : _stack.back();
143 }
144
145 std::size_t Overlay::getDepth(void) const
146 {
147 return _stack.size();
148 }
149
151 {
152 if (_stack.empty()) {
153 return;
154 }
155
156 const auto top = _stack.back();
158 && getComponent<components::Overlay>(top).dismissesOnEscape()) {
159 dismiss();
160 }
161 }
162
163 void Overlay::handleOutsideClick(const utility::math::Vector2F &point)
164 {
165 if (_stack.empty()) {
166 return;
167 }
168
169 const auto top = _stack.back();
173 return;
174 }
175
176 auto &overlay = getComponent<components::Overlay>(top);
177 if (!overlay.dismissesOnOutsideClick()) {
178 return;
179 }
180
181 const auto pose = getComponent<components::Transform>(top).getPose();
182 const auto &bound = getComponent<components::Bound>(top);
183
184 const float x = pose.getPosition().getX();
185 const float y = pose.getPosition().getY();
186 const float width = bound.getWidth();
187 const float height = bound.getHeight();
188
189 // The position is the top-left corner and y points up, so the
190 // rectangle spans [y - height, y].
191 const bool inside = point.x >= x && point.x <= x + width && point.y <= y
192 && point.y >= y - height;
193
194 if (!inside) {
195 dismiss();
196 }
197 }
198
199 void Overlay::advance(float deltaTime)
200 {
201 if (_stack.empty()) {
202 return;
203 }
204
205 const auto top = _stack.back();
207 return;
208 }
209
210 auto &overlay = getComponent<components::Overlay>(top);
211 if (!overlay.hasAutoDismiss()) {
212 return;
213 }
214
215 overlay.setElapsed(overlay.getElapsed() + deltaTime);
216 if (overlay.getElapsed() >= overlay.getAutoDismiss()) {
217 dismiss();
218 }
219 }
220
221} // namespace guillaume::systems
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
bool hasComponent(const ecs::Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component, in the bound registry.
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
bool requireComponent(const ecs::Entity::Identifier &entityIdentifier)
Tell whether an entity has a component, and log a warning naming the entity and the component when it...
Hands each engine event to the listeners subscribed to its type.
Definition event_bus.hpp:67
void handleOutsideClick(const utility::math::Vector2F &point)
Close the top overlay if a click landed outside it and its component says it dismisses on outside cli...
Definition overlay.cpp:163
std::size_t getDepth(void) const
The number of open overlays.
Definition overlay.cpp:145
void show(const ecs::Entity::Identifier &entityIdentifier)
Open an overlay at once: push it on top of the stack, make its component visible and turn viewport in...
Definition overlay.cpp:72
bool isOpen(const ecs::Entity::Identifier &entityIdentifier) const
Whether an overlay is open, that is, in the stack.
Definition overlay.cpp:134
void advance(float deltaTime)
Count down the top overlay and close it when its time is up.
Definition overlay.cpp:199
void handleEscape(void)
Close the top overlay if its component says it dismisses on Escape. Called by prepare() on an Escape ...
Definition overlay.cpp:150
void dismiss(void)
Close the top overlay.
Definition overlay.cpp:88
void update(const ecs::Entity::Identifier &entityIdentifier) override
Make the stack agree with the visibility of one overlay.
Definition overlay.cpp:56
Overlay(event::EventBus &eventBus, utility::Engine &engine)
Create the system, in the Event phase, and subscribe it to keyboard events (for Escape).
Definition overlay.cpp:29
void prepare(void) override
Once per frame: take the next pending keyboard event, count down the top overlay, and handle Escape.
Definition overlay.cpp:39
ecs::Entity::Identifier getTop(void) const
The overlay opened last, the one Escape and the countdown apply to.
Definition overlay.cpp:140
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Delivery of engine events to the systems that subscribed to them.
Definition event_bus.hpp:34
Rules. Every class here runs each frame on the entities that have the components it declares.