guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
focus.hpp
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#pragma once
24
25#include <optional>
26
27#include <utility/engine.hpp>
28
29#include "guillaume/ecs/system_filler.hpp"
30#include "guillaume/event/event_bus.hpp"
31#include "guillaume/event/event_manager.hpp"
32
33#include <utility/event/keyboard_event.hpp>
34
35#include <vector>
36
37#include "guillaume/components/focus.hpp"
38#include "guillaume/components/transform.hpp"
39#include "guillaume/components/bound.hpp"
40#include "guillaume/components/mouse_button_interaction.hpp"
41#include "guillaume/components/hand_button_interaction.hpp"
42#include "guillaume/components/hand_pinch_interaction.hpp"
43#include "guillaume/components/hand_poke_interaction.hpp"
44
45namespace guillaume::systems
46{
47
84 class Focus:
85 public ecs::SystemFiller<
86 components::Focus, components::Transform, components::Bound,
87 components::MouseButtonInteraction,
88 components::HandButtonInteraction, components::HandPinchInteraction,
89 components::HandPokeInteraction>,
90 public event::EventManager<utility::event::KeyboardEvent>
91 {
92 private:
93 utility::Engine &_engine;
95 std::optional<ecs::Entity::Identifier>
96 _focusedEntity;
97 std::optional<ecs::Entity::Identifier>
98 _lastFocusedEntity;
100 std::vector<std::pair<ecs::Entity::Identifier, int>>
101 _focusables;
104
112 bool isFocusable(const ecs::Entity::Identifier &entityIdentifier);
113
120 void markFocused(const ecs::Entity::Identifier &entityIdentifier,
121 bool focused);
122
123 public:
131 Focus(event::EventBus &eventBus, utility::Engine &engine);
132
144 void setFocus(const ecs::Entity::Identifier &entityIdentifier);
145
153 void clearFocus(void);
154
160 std::optional<ecs::Entity::Identifier> getFocusedEntity(void) const;
161
168 std::optional<ecs::Entity::Identifier> getLastFocusedEntity(void) const;
169
185 void traverse(const std::vector<std::pair<ecs::Entity::Identifier, int>>
186 &candidates,
187 bool backward);
188
193 void prepare(void) override;
194
202 void cleanup(void) override;
203
214 virtual void
215 update(const ecs::Entity::Identifier &entityIdentifier) override;
216 };
217
218} // 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
The base class to derive a system from: a System whose signature is the list of component types given...
Hands each engine event to the listeners subscribed to its type.
Definition event_bus.hpp:67
Base class that gives a system the input events of one type, one event per frame.
Focus(event::EventBus &eventBus, utility::Engine &engine)
Create the system, in the Event phase, and subscribe it to keyboard events (for Tab traversal).
Definition focus.cpp:35
std::optional< ecs::Entity::Identifier > getFocusedEntity(void) const
The entity holding the focus.
Definition focus.cpp:128
void clearFocus(void)
Take the focus away from the focused entity.
Definition focus.cpp:101
void setFocus(const ecs::Entity::Identifier &entityIdentifier)
Give the focus to an entity.
Definition focus.cpp:50
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Focus one entity if it is being pressed, and record it as a Tab candidate if it is focusable.
Definition focus.cpp:139
void traverse(const std::vector< std::pair< ecs::Entity::Identifier, int > > &candidates, bool backward)
Move the focus to the next or previous candidate.
Definition focus.cpp:266
void cleanup(void) override
Run Tab traversal once every entity has been visited.
Definition focus.cpp:308
std::optional< ecs::Entity::Identifier > getLastFocusedEntity(void) const
The entity that held the focus before the current one, or before the focus was cleared.
Definition focus.cpp:134
void prepare(void) override
Empty the list of focusable entities and take the next pending keyboard event, once per frame.
Definition focus.cpp:302
Rules. Every class here runs each frame on the entities that have the components it declares.