23#include "guillaume/systems/focus.hpp"
28#include "guillaume/components/accessibility.hpp"
29#include "guillaume/components/style_state.hpp"
44 , _focusedEntity(std::nullopt)
47 this->getLogger().info() <<
"Focus system initialized";
53 if (_focusedEntity.has_value()
54 && _focusedEntity.value() != entityIdentifier) {
55 _lastFocusedEntity = _focusedEntity.value();
59 if (_focusedEntity.has_value()
60 && _focusedEntity.value() != entityIdentifier) {
61 auto previousEntity = _focusedEntity.value();
67 auto handler = previousFocus.getOnFocusLostHandler();
69 this->getLogger().debug()
70 <<
"Triggering focus lost handler for entity "
77 if (_focusedEntity.has_value()
78 && _focusedEntity.value() != entityIdentifier) {
79 markFocused(_focusedEntity.value(),
false);
83 _focusedEntity = entityIdentifier;
84 markFocused(entityIdentifier,
true);
85 this->getLogger().info() <<
"Focus set to entity " << entityIdentifier;
91 auto handler = focus.getOnFocusGainedHandler();
93 this->getLogger().debug()
94 <<
"Triggering focus gained handler for entity "
103 if (_focusedEntity.has_value()) {
105 _lastFocusedEntity = _focusedEntity.value();
107 auto previousEntity = _focusedEntity.value();
110 auto &previousFocus =
113 auto handler = previousFocus.getOnFocusLostHandler();
115 this->getLogger().debug()
116 <<
"Triggering focus lost handler for entity "
121 markFocused(previousEntity,
false);
122 this->getLogger().info()
123 <<
"Focus cleared from entity " << previousEntity;
124 _focusedEntity = std::nullopt;
130 return _focusedEntity;
133 std::optional<ecs::Entity::Identifier>
136 return _lastFocusedEntity;
141 this->getLogger().debug()
142 <<
"Updating Focus system for entity " << entityIdentifier;
146 if (_focusedEntity.has_value()
147 && _focusedEntity.value() == entityIdentifier) {
153 if (isFocusable(entityIdentifier)) {
161 _focusables.emplace_back(entityIdentifier, order);
164 bool entityClicked =
false;
169 auto &mouseInteraction =
172 if (mouseInteraction.isButtonPressed(
173 utility::event::MouseButtonEvent::Button::Left)) {
174 entityClicked =
true;
182 auto &handInteraction =
186 if (handInteraction.isButtonPressed(
187 utility::event::HandButtonEvent::Button::A)
188 || handInteraction.isButtonPressed(
189 utility::event::HandButtonEvent::Button::B)
190 || handInteraction.isButtonPressed(
191 utility::event::HandButtonEvent::Button::X)
192 || handInteraction.isButtonPressed(
193 utility::event::HandButtonEvent::Button::Y)) {
194 entityClicked =
true;
202 auto &pinchInteraction =
205 if (pinchInteraction.isPinch()) {
206 entityClicked =
true;
214 auto &pokeInteraction =
217 if (pokeInteraction.isPoke()) {
218 entityClicked =
true;
225 _engine.setShouldCaptureViewportInput(
false);
230 if (!_focusedEntity.has_value()) {
231 _engine.setShouldCaptureViewportInput(
true);
256 .setFocused(focused);
262 .setFocused(focused);
267 const std::vector<std::pair<ecs::Entity::Identifier, int>> &candidates,
270 if (candidates.empty()) {
274 auto ordered = candidates;
275 std::stable_sort(ordered.begin(), ordered.end(),
276 [](
const auto &lhs,
const auto &rhs) {
278 lhs.second > 0 ? lhs.second : INT_MAX;
280 rhs.second > 0 ? rhs.second : INT_MAX;
284 const auto count =
static_cast<std::ptrdiff_t
>(ordered.size());
285 std::ptrdiff_t next = backward ? count - 1 : 0;
287 if (_focusedEntity.has_value()) {
288 const auto current = std::find_if(
289 ordered.begin(), ordered.end(), [
this](
const auto &candidate) {
290 return candidate.first == _focusedEntity.value();
292 if (current != ordered.end()) {
293 const auto index = std::distance(ordered.begin(), current);
294 next = backward ? (index - 1 + count) % count
295 : (index + 1) % count;
299 setFocus(ordered[
static_cast<std::size_t
>(next)].first);
311 if (!keyboardEvent || !keyboardEvent->getIsDownEvent()
312 || keyboardEvent->getKeycode()
313 != utility::event::KeyboardEvent::KeyCode::Tab) {
317 const bool backward =
318 keyboardEvent->isModifierSet(
319 utility::event::KeyboardEvent::KeyModifiers::LeftShift)
320 || keyboardEvent->isModifierSet(
321 utility::event::KeyboardEvent::KeyModifiers::RightShift);
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
SystemFiller(Phase phase)
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
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.
Hands each engine event to the listeners subscribed to its type.
void consumeNextEvent(void)
EventManager(EventBus &eventBus)
std::shared_ptr< EventType > getLastEvent(void)
Focus(event::EventBus &eventBus, utility::Engine &engine)
Create the system, in the Event phase, and subscribe it to keyboard events (for Tab traversal).
std::optional< ecs::Entity::Identifier > getFocusedEntity(void) const
The entity holding the focus.
void clearFocus(void)
Take the focus away from the focused entity.
void setFocus(const ecs::Entity::Identifier &entityIdentifier)
Give the focus to an entity.
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.
void traverse(const std::vector< std::pair< ecs::Entity::Identifier, int > > &candidates, bool backward)
Move the focus to the next or previous candidate.
void cleanup(void) override
Run Tab traversal once every entity has been visited.
std::optional< ecs::Entity::Identifier > getLastFocusedEntity(void) const
The entity that held the focus before the current one, or before the focus was cleared.
void prepare(void) override
Empty the list of focusable entities and take the next pending keyboard event, once per frame.
Free functions the input systems share to decide whether a pointer or hand ray hits an entity.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Delivery of engine events to the systems that subscribed to them.
Rules. Every class here runs each frame on the entities that have the components it declares.
bool isInteractive(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity accepts input.