guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
interaction_helpers.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
24
25#include <memory>
26#include <utility>
27
28#include "guillaume/components/focus.hpp"
29#include "guillaume/components/hand_button_interaction.hpp"
30#include "guillaume/components/hand_hover_interaction.hpp"
31#include "guillaume/components/mouse_button_interaction.hpp"
32#include "guillaume/components/mouse_hover_interaction.hpp"
33#include "guillaume/components/style_state.hpp"
35
36namespace guillaume::systems::interaction
37{
38 namespace
39 {
40 template<typename Component>
41 Component &ensure(ecs::ComponentRegistry &registry,
42 const ecs::Entity::Identifier &entityIdentifier)
43 {
44 if (!registry.hasComponent<Component>(entityIdentifier)) {
45 registry.addComponent<Component>(entityIdentifier);
46 }
47 return registry.getComponent<Component>(entityIdentifier);
48 }
49 } // namespace
50
52 const ecs::Entity::Identifier &entityIdentifier,
54 std::function<void(void)> onPress,
55 std::function<void(void)> onStateChange)
56 {
57 ensure<components::StyleState>(registry, entityIdentifier);
58 ensure<components::Focus>(registry, entityIdentifier);
59 ensure<components::Accessibility>(registry, entityIdentifier)
60 .setRole(role);
61
62 auto press =
63 std::make_shared<std::function<void(void)>>(std::move(onPress));
64 auto restyle = std::make_shared<std::function<void(void)>>(
65 std::move(onStateChange));
66 ecs::ComponentRegistry *registryPtr = &registry;
67 const ecs::Entity::Identifier identifier = entityIdentifier;
68
69 const auto changed = [restyle]() {
70 if (*restyle) {
71 (*restyle)();
72 }
73 };
74 const auto released = [press, restyle, registryPtr, identifier]() {
75 if (!isEnabled(*registryPtr, identifier)) {
76 return;
77 }
78 if (*press) {
79 (*press)();
80 }
81 if (*restyle) {
82 (*restyle)();
83 }
84 };
85
86 ensure<components::MouseHoverInteraction>(registry, entityIdentifier)
87 .setOnHoverHandler(changed)
88 .setOnUnhoverHandler(changed);
89 ensure<components::HandHoverInteraction>(registry, entityIdentifier)
90 .setOnHoverHandler(changed)
91 .setOnUnhoverHandler(changed);
92 ensure<components::MouseButtonInteraction>(registry, entityIdentifier)
93 .setOnButtonPressHandler(
94 utility::event::MouseButtonEvent::Button::Left, changed)
95 .setOnButtonReleaseHandler(
96 utility::event::MouseButtonEvent::Button::Left, released);
97 ensure<components::HandButtonInteraction>(registry, entityIdentifier)
98 .setOnButtonPressHandler(utility::event::HandButtonEvent::Button::A,
99 changed)
100 .setOnButtonReleaseHandler(
101 utility::event::HandButtonEvent::Button::A, released);
102 }
103
105 const ecs::Entity::Identifier &entityIdentifier,
106 bool enabled)
107 {
108 ensure<components::StyleState>(registry, entityIdentifier)
109 .setEnabled(enabled);
110 }
111
112 bool isEnabled(const ecs::ComponentRegistry &registry,
113 const ecs::Entity::Identifier &entityIdentifier)
114 {
115 return !registry.hasComponent<components::StyleState>(entityIdentifier)
116 || registry.getComponent<components::StyleState>(entityIdentifier)
117 .isEnabled();
118 }
119
120 bool isHovered(const ecs::ComponentRegistry &registry,
121 const ecs::Entity::Identifier &entityIdentifier)
122 {
123 if (!isEnabled(registry, entityIdentifier)) {
124 return false;
125 }
127 entityIdentifier)
128 && registry
130 entityIdentifier)
131 .isHovered()) {
132 return true;
133 }
135 entityIdentifier)
136 && registry
137 .getComponent<components::HandHoverInteraction>(
138 entityIdentifier)
139 .isHovered();
140 }
141
142 bool isPressed(const ecs::ComponentRegistry &registry,
143 const ecs::Entity::Identifier &entityIdentifier)
144 {
145 if (!isEnabled(registry, entityIdentifier)) {
146 return false;
147 }
149 entityIdentifier)
150 && registry
152 entityIdentifier)
153 .isButtonPressed(
154 utility::event::MouseButtonEvent::Button::Left)) {
155 return true;
156 }
158 entityIdentifier)
159 && registry
160 .getComponent<components::HandButtonInteraction>(
161 entityIdentifier)
162 .isButtonPressed(utility::event::HandButtonEvent::Button::A);
163 }
164
165 utility::graphic::Color32Bit
167 const ecs::Entity::Identifier &entityIdentifier,
168 const utility::graphic::Color32Bit &base,
169 const utility::graphic::Color32Bit &content)
170 {
171 if (!isEnabled(registry, entityIdentifier)) {
172 return base;
173 }
174
175 std::optional<theme::StateLayer> layer;
176 if (registry.hasComponent<components::StyleState>(entityIdentifier)) {
177 layer =
178 registry.getComponent<components::StyleState>(entityIdentifier)
179 .getActiveLayer();
180 }
181 // The interaction components are authoritative within the frame the
182 // StyleState system has not yet mirrored.
183 if (isPressed(registry, entityIdentifier)) {
185 } else if (!layer.has_value()
186 && isHovered(registry, entityIdentifier)) {
188 }
189
190 if (!layer.has_value()) {
191 return base;
192 }
193
194 // A transparent container (text/outlined styles) shows the layer over
195 // nothing, so the result is the content color at the layer opacity.
196 if (base.getAlpha() == 0) {
197 return style::withAlpha(content, getStateLayerAlpha(*layer));
198 }
199 return style::stateLayer(base, content, *layer);
200 }
201
202} // namespace guillaume::systems::interaction
Whether an XR hand (controller) aims at an entity, what to call when the aim enters or leaves,...
Whether the mouse pointer is over an entity, what to call when it enters or leaves,...
bool isHovered(void) const
Check whether the pointer is over the entity.
The interaction state of an entity in one place: enabled or disabled, and whether it is hovered,...
bool isEnabled(void) const
Check whether the entity accepts interaction.
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
ComponentType & getComponent(const Entity::Identifier &entityIdentifier)
Get a component of an entity, to read or modify it.
bool hasComponent(const Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component of a given type.
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
ButtonInteraction< utility::event::MouseButtonEvent, utility::event::MouseButtonEvent::Button, MouseButtons.size(), MouseButtons > MouseButtonInteraction
Which mouse buttons are held on an entity, with a press and a release handler per button.
AccessibilityRole
What an entity is for a screen reader or another assistive technology. The values mirror react-native...
ButtonInteraction< utility::event::HandButtonEvent, utility::event::HandButtonEvent::Button, HandButtons.size(), HandButtons > HandButtonInteraction
Which XR controller buttons are held on an entity, with a press and a release handler per button.
utility::graphic::Color32Bit stateLayer(const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &overlay, std::uint8_t alpha)
Blend a translucent tint over a base color.
utility::graphic::Color32Bit withAlpha(const utility::graphic::Color32Bit &color, std::uint8_t alpha)
Copy a color with another alpha channel.
@ Hovered
0.08 overlay of the content color.
Definition tokens.hpp:224
@ Pressed
0.12 overlay of the content color.
Definition tokens.hpp:226
std::uint8_t getStateLayerAlpha(theme::StateLayer layer)
Get the opacity the active theme gives to a state layer, as an alpha channel value.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
bool isPressed(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity is being pressed.
bool isHovered(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier)
Whether the mouse pointer or a hand is over an entity.
bool isEnabled(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity accepts input.
void makeInteractive(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, components::AccessibilityRole role, std::function< void(void)> onPress, std::function< void(void)> onStateChange={})
Make an entity hoverable, pressable and focusable.
void setEnabled(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, bool enabled)
Enable or disable an entity.
utility::graphic::Color32Bit stateLayerColor(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &entityIdentifier, const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &content)
Container color of a widget in its current input state.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.