guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
radio_button.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#include "guillaume/systems/radio_button.hpp"
23
24#include "guillaume/components/accessibility.hpp"
25#include "guillaume/components/bound.hpp"
26#include "guillaume/components/color.hpp"
27#include "guillaume/components/hand_hover_interaction.hpp"
28#include "guillaume/components/layout.hpp"
29#include "guillaume/components/mouse_hover_interaction.hpp"
30#include "guillaume/components/ring.hpp"
31#include "guillaume/components/selection.hpp"
35#include "guillaume/systems/layout.hpp"
36#include "guillaume/theme.hpp"
37
38namespace guillaume::systems
39{
40 namespace
41 {
46 } // namespace
47
52
54 const ecs::Entity::Identifier &entityIdentifier)
55 {
56 ecs::ComponentRegistry *registry = &componentRegistry;
57 const ecs::Entity::Identifier identifier = entityIdentifier;
58
60 componentRegistry, identifier, components::AccessibilityRole::Radio,
61 [registry, identifier]() {
62 press(*registry, identifier);
63 },
64 [registry, identifier]() {
65 apply(*registry, identifier);
66 });
67
68 // Grow the 20 dp ring to the 40 dp target without growing it.
69 const float width =
70 componentRegistry.getComponent<components::Layout>(identifier)
71 .getFixedWidth();
72 const auto margin = static_cast<unsigned int>(
74 componentRegistry
76 .setAccessibilityMargin(utility::graphic::SizeUI(margin, margin));
77 componentRegistry
79 .setAccessibilityMargin(utility::graphic::SizeUI(margin, margin));
80
81 componentRegistry.getComponent<components::Selectable>(identifier)
82 .setOnSelectionChangedHandler([registry, identifier](bool) {
83 apply(*registry, identifier);
84 });
85 }
86
87 bool
89 const ecs::Entity::Identifier &entityIdentifier)
90 {
91 return componentRegistry
92 .getComponent<components::RadioButton>(entityIdentifier)
93 .getVariant()
94 == Variant::Disabled
95 || !interaction::isEnabled(componentRegistry, entityIdentifier);
96 }
97
99 const ecs::Entity::Identifier &entityIdentifier,
100 bool selected)
101 {
102 componentRegistry.getComponent<components::Selectable>(entityIdentifier)
103 .setSelected(selected);
104 apply(componentRegistry, entityIdentifier);
105 }
106
108 const ecs::Entity::Identifier &entityIdentifier)
109 {
110 if (isDisabled(componentRegistry, entityIdentifier)) {
111 return;
112 }
113
114 select(componentRegistry, entityIdentifier, true);
115 // Copy: the callback may replace itself.
116 const auto onPress =
117 componentRegistry
118 .getComponent<components::RadioButton>(entityIdentifier)
119 .getOnPress();
120 if (onPress) {
121 onPress();
122 }
123 }
124
126 const ecs::Entity::Identifier &entityIdentifier)
127 {
128 const auto &radio =
129 componentRegistry.getComponent<components::RadioButton>(
130 entityIdentifier);
131 const Variant variant = radio.getVariant();
132
133 componentRegistry.getComponent<components::Ring>(entityIdentifier)
134 .setThickness(2.0f);
135 interaction::setEnabled(componentRegistry, entityIdentifier,
136 variant != Variant::Disabled);
137
138 const bool disabled = isDisabled(componentRegistry, entityIdentifier);
139 const bool errored = (variant == Variant::Error);
140 const bool selected =
141 componentRegistry
142 .getComponent<components::Selectable>(entityIdentifier)
143 .isSelected();
144
145 auto &color =
146 componentRegistry.getComponent<components::Color>(entityIdentifier);
147 if (disabled) {
148 color.setColor(disabledContentColor());
149 } else if (errored) {
150 color.setColor(schemeColor(SchemeColorRole::Error));
151 } else if (selected) {
152 color.setColor(schemeColor(SchemeColorRole::Primary));
153 } else {
154 color.setColor(schemeColor(SchemeColorRole::OnSurfaceVariant));
155 }
156
157 componentRegistry
158 .getComponent<components::Accessibility>(entityIdentifier)
159 .setLabel(radio.getLabel())
160 .setSelected(selected)
163
164 const auto layer = Layout::layerOf(componentRegistry, entityIdentifier);
165 const auto dot = radio.getDot();
166 if (componentRegistry.hasComponent<components::Color>(dot)) {
167 const auto dotColor = disabled
169 : schemeColor(errored ? SchemeColorRole::Error
170 : SchemeColorRole::Primary);
171 componentRegistry.getComponent<components::Color>(dot).setColor(
172 selected ? dotColor : transparentColor());
173 if (selected) {
174 placement::centerChild(componentRegistry, entityIdentifier, dot,
175 layer);
176 }
177 }
178
179 const auto label = radio.getLabelEntity();
180 if (componentRegistry.hasComponent<components::Color>(label)) {
181 componentRegistry.getComponent<components::Color>(label).setColor(
182 disabled ? disabledContentColor()
183 : schemeColor(errored ? SchemeColorRole::Error
184 : SchemeColorRole::OnSurface));
185 }
186 if (componentRegistry.hasComponent<components::Bound>(label)) {
187 const auto &ring =
188 componentRegistry.getComponent<components::Bound>(
189 entityIdentifier);
190 const auto &labelBound =
191 componentRegistry.getComponent<components::Bound>(label);
192 const float x =
193 (ring.getWidth() + components::RadioButton::TouchTarget) / 2.0f
195 const float y = (ring.getHeight() - labelBound.getHeight()) / 2.0f;
196 placement::placeChild(componentRegistry, entityIdentifier, label,
197 utility::graphic::PositionF(x, y, 0.0f),
198 layer);
199 placement::reserveTrailingWidth(componentRegistry, entityIdentifier,
200 x + labelBound.getWidth()
201 - ring.getWidth());
202 } else {
203 placement::reserveTrailingWidth(componentRegistry, entityIdentifier,
204 0.0f);
205 }
206 }
207
208 void RadioButton::update(const ecs::Entity::Identifier &entityIdentifier)
209 {
210 if (!requireComponent<components::RadioButton>(entityIdentifier)) {
211 return;
212 }
213
214 apply(getComponentRegistry(), entityIdentifier);
215 }
216} // namespace guillaume::systems
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Accessibility & setSelected(bool selected)
Set whether the entity is the selected one of its group.
Accessibility & setChecked(std::optional< CheckedState > checked)
Set whether the entity is ticked.
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
Whether an XR hand (controller) aims at an entity, what to call when the aim enters or leaves,...
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
Whether the mouse pointer is over an entity, what to call when it enters or leaves,...
Configuration of a radio button: its variant, its label and the identifiers of its dot and label chil...
static constexpr float TouchTarget
Edge of the square touch target centered on the ring, in dp.
static constexpr float LabelGap
Gap between the touch target and the label, in dp.
Variant getVariant(void) const noexcept
Get the visual variant.
A full ring: the outline of a radio button, the track of a circular progress indicator.
Definition ring.hpp:53
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
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
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
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...
Arranges the children of a container in a row or a column and sizes the container around them.
Definition layout.hpp:84
static std::uint32_t layerOf(const ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Get the layer of an entity: its depth in the entity tree.
Definition layout.cpp:90
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one radio button.
static void select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool selected)
Select or deselect a radio button from code and show it at once.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Draw one radio button from its components: colors, accessibility, dot and label position.
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the radio button ignores input and is drawn disabled.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a radio button.
RadioButton(void)
Create the system for the Layout phase, matching entities that have a components::RadioButton.
static void press(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Select the radio button as a click does.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
void centerChild(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, std::uint32_t parentLayer)
Center a child inside the bound of its parent, on both axes.
void placeChild(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, const utility::graphic::PositionF &offset, std::uint32_t parentLayer)
Place a child at an offset from the position of its parent.
void reserveTrailingWidth(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier, float width)
Reserve layout room after an entity's Bound.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
Rules. Every class here runs each frame on the entities that have the components it declares.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
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.
Placement helpers for the children systems::Layout does not arrange.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.