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"
36namespace guillaume::systems::interaction
40 template<
typename Component>
41 Component &ensure(ecs::ComponentRegistry ®istry,
44 if (!registry.hasComponent<Component>(entityIdentifier)) {
45 registry.addComponent<Component>(entityIdentifier);
47 return registry.getComponent<Component>(entityIdentifier);
54 std::function<
void(
void)> onPress,
55 std::function<
void(
void)> onStateChange)
57 ensure<components::StyleState>(registry, entityIdentifier);
58 ensure<components::Focus>(registry, entityIdentifier);
59 ensure<components::Accessibility>(registry, entityIdentifier)
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));
69 const auto changed = [restyle]() {
74 const auto released = [press, restyle, registryPtr, identifier]() {
75 if (!
isEnabled(*registryPtr, identifier)) {
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,
100 .setOnButtonReleaseHandler(
101 utility::event::HandButtonEvent::Button::A, released);
108 ensure<components::StyleState>(registry, entityIdentifier)
109 .setEnabled(enabled);
123 if (!
isEnabled(registry, entityIdentifier)) {
137 .getComponent<components::HandHoverInteraction>(
145 if (!
isEnabled(registry, entityIdentifier)) {
154 utility::event::MouseButtonEvent::Button::Left)) {
160 .getComponent<components::HandButtonInteraction>(
162 .isButtonPressed(utility::event::HandButtonEvent::Button::A);
165 utility::graphic::Color32Bit
168 const utility::graphic::Color32Bit &base,
169 const utility::graphic::Color32Bit &content)
171 if (!
isEnabled(registry, entityIdentifier)) {
175 std::optional<theme::StateLayer> layer;
183 if (
isPressed(registry, entityIdentifier)) {
185 }
else if (!layer.has_value()
186 &&
isHovered(registry, entityIdentifier)) {
190 if (!layer.has_value()) {
196 if (base.getAlpha() == 0) {
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...
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.
@ Pressed
0.12 overlay of the content color.
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 ®istry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity is being pressed.
bool isHovered(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier)
Whether the mouse pointer or a hand is over an entity.
bool isEnabled(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity accepts input.
void makeInteractive(ecs::ComponentRegistry ®istry, 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 ®istry, const ecs::Entity::Identifier &entityIdentifier, bool enabled)
Enable or disable an entity.
utility::graphic::Color32Bit stateLayerColor(const ecs::ComponentRegistry ®istry, 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.