guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
radio_group.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
23#include "guillaume/systems/radio_group.hpp"
24
25#include <stdexcept>
26
27#include "guillaume/components/accessibility.hpp"
28#include "guillaume/components/layout.hpp"
29#include "guillaume/components/radio_button.hpp"
30#include "guillaume/components/selection.hpp"
31#include "guillaume/systems/layout.hpp"
32#include "guillaume/systems/radio_button.hpp"
33
34namespace guillaume::systems
35{
40
42 const ecs::Entity::Identifier &entityIdentifier)
43 {
44 ecs::ComponentRegistry *registry = &componentRegistry;
45 const ecs::Entity::Identifier identifier = entityIdentifier;
46
47 if (!componentRegistry.hasComponent<components::Accessibility>(
48 identifier)) {
49 componentRegistry.addComponent<components::Accessibility>(
50 identifier);
51 }
52 componentRegistry.getComponent<components::Accessibility>(identifier)
54 .setFocusable(false);
55
56 const auto &buttons =
57 componentRegistry.getComponent<components::RadioGroup>(identifier)
58 .getButtons();
59 for (std::size_t index = 0; index < buttons.size(); ++index) {
60 if (!componentRegistry.hasComponent<components::RadioButton>(
61 buttons[index])) {
62 continue;
63 }
64 componentRegistry
66 .setOnPress([registry, identifier, index]() {
67 press(*registry, identifier, index);
68 });
69 }
70 }
71
73 const ecs::Entity::Identifier &entityIdentifier,
74 std::size_t index)
75 {
76 auto &group = componentRegistry.getComponent<components::RadioGroup>(
77 entityIdentifier);
78
79 if (index >= group.getOptions().size()) {
80 throw std::out_of_range("RadioGroup option index out of range");
81 }
82 group.setValue(index);
83 apply(componentRegistry, entityIdentifier);
84 }
85
87 const ecs::Entity::Identifier &entityIdentifier,
88 std::size_t index)
89 {
90 auto &group = componentRegistry.getComponent<components::RadioGroup>(
91 entityIdentifier);
92
93 if (index >= group.getButtons().size()) {
94 return;
95 }
96
97 const bool changed =
98 !group.getValue().has_value() || *group.getValue() != index;
99 group.setValue(index);
100 apply(componentRegistry, entityIdentifier);
101 // Copy: the callback may replace itself.
102 const auto onValueChange = group.getOnValueChange();
103 if (changed && onValueChange) {
104 onValueChange(index);
105 }
106 }
107
109 const ecs::Entity::Identifier &entityIdentifier)
110 {
111 const auto &group =
112 componentRegistry.getComponent<components::RadioGroup>(
113 entityIdentifier);
114 const auto value = group.getValue();
115 const auto &buttons = group.getButtons();
116
117 for (std::size_t index = 0; index < buttons.size(); ++index) {
118 if (!componentRegistry.hasComponent<components::Selectable>(
119 buttons[index])
120 || !componentRegistry.hasComponent<components::RadioButton>(
121 buttons[index])) {
122 continue;
123 }
124
125 const bool selected = value.has_value() && *value == index;
126 if (componentRegistry
128 .isSelected()
129 != selected) {
130 RadioButton::select(componentRegistry, buttons[index],
131 selected);
132 }
133 }
134
135 if (!componentRegistry.hasComponent<components::Layout>(
136 entityIdentifier)) {
137 return;
138 }
139
140 // The radio buttons are the children the Layout system stacks.
141 auto &layout = componentRegistry.getComponent<components::Layout>(
142 entityIdentifier);
143 layout.setChildren(buttons);
144 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
145 Layout::layerOf(componentRegistry, entityIdentifier));
146 }
147
148 void RadioGroup::update(const ecs::Entity::Identifier &entityIdentifier)
149 {
150 if (!requireComponent<components::RadioGroup>(entityIdentifier)) {
151 return;
152 }
153
154 apply(getComponentRegistry(), entityIdentifier);
155 }
156} // 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 & setFocusable(bool focusable)
Include the entity in the Tab traversal or leave it out.
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
Layout & setChildren(const std::vector< ecs::Entity::Identifier > &children)
Replace the list of children to arrange.
Definition layout.cpp:171
Configuration of a radio button: its variant, its label and the identifiers of its dot and label chil...
State of a radio group: a column of radio buttons of which at most one is selected.
std::optional< std::size_t > getValue(void) const noexcept
Get the selected option.
RadioGroup & setValue(std::optional< std::size_t > value)
Store the selected option.
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.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
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
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
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 void apply(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const std::vector< ecs::Entity::Identifier > &childIdentifiers, std::uint32_t layer=0)
Arrange children inside a container and size the container.
Definition layout.cpp:126
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
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 select(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Select an option from code and show it at once.
static void press(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, std::size_t index)
Select an option as a click on its radio button does.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a radio group.
RadioGroup(void)
Create the system for the Layout phase, matching entities that have a components::RadioGroup.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make the radio buttons agree with the value of the group, then stack them.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Frame entry point: call apply on one radio group.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Rules. Every class here runs each frame on the entities that have the components it declares.