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/entities/radio_group.hpp"
24
25#include <utility>
26
27#include "guillaume/systems/radio_group.hpp"
28
29namespace guillaume::entities
30{
32 ecs::EntityRegistry &entityRegistry)
33 : EntityBuilderBase<RadioGroup>(componentRegistry, entityRegistry)
34 {
35 reset();
36 }
37
38 std::shared_ptr<RadioGroup> RadioGroup::Builder::buildEntity(void)
39 {
40 return RadioGroup::create(this->getComponentRegistry(), _config,
41 _options, _value, _onValueChange);
42 }
43
45 {
46 _config = SurfaceConfig();
47 _options.clear();
48 _value.reset();
49 _onValueChange = {};
50
52 _config.mainAxisAlignment =
54 _config.crossAxisAlignment =
56 _config.spacing = RowSpacing;
57 _config.padding = (RadioButton::TouchTarget - 20.0f) / 2.0f;
58 }
59
61 RadioGroup::Builder::withPose(const utility::graphic::PoseF &pose)
62 {
63 _config.pose = pose;
64 return *this;
65 }
66
68 RadioGroup::Builder::addOption(const std::string &label)
69 {
70 _options.push_back(label);
71 return *this;
72 }
73
75 {
76 _value = index;
77 return *this;
78 }
79
81 std::function<void(std::size_t)> onValueChange)
82 {
83 _onValueChange = std::move(onValueChange);
84 return *this;
85 }
86
87 std::shared_ptr<RadioGroup> RadioGroup::Builder::makeRadioGroup(
88 std::shared_ptr<ecs::Entity> parent,
89 const std::vector<std::string> &options)
90 {
91 for (const auto &option: options) {
92 addOption(option);
93 }
94 return registerEntity(parent);
95 }
96
97 RadioGroup::RadioGroup(ecs::ComponentRegistry &registry,
98 const SurfaceConfig &config,
99 const std::vector<std::string> &options,
100 std::optional<std::size_t> value,
101 std::function<void(std::size_t)> onValueChange)
102 : SurfaceBase<components::RadioGroup>(registry, config)
103 {
106 .setOptions(options)
107 .setValue(value)
108 .setOnValueChange(std::move(onValueChange));
110 }
111
112 RadioGroup &RadioGroup::setValue(std::size_t index)
113 {
115 index);
116 return *this;
117 }
118
119 std::optional<std::size_t> RadioGroup::getValue(void) const
120 {
121 return getComponentRegistry()
123 .getValue();
124 }
125
126 std::size_t RadioGroup::getOptionCount(void) const
127 {
128 return getComponentRegistry()
130 .getOptions()
131 .size();
132 }
133
134 std::shared_ptr<RadioButton> RadioGroup::getOption(std::size_t index) const
135 {
136 const auto &buttons =
139 .getButtons();
140
141 if (index >= buttons.size()) {
142 return nullptr;
143 }
144 // The lookup returns a pointer to const because this getter is
145 // const; the child is shared and callers may modify it, hence the
146 // cast.
147 return std::const_pointer_cast<RadioButton>(
148 getEntity<RadioButton>(buttons[index]));
149 }
150
152 std::function<void(std::size_t)> onValueChange)
153 {
156 .setOnValueChange(std::move(onValueChange));
157 return *this;
158 }
159
160 void RadioGroup::build(void)
161 {
162 auto &registry = getComponentRegistry();
163 // Copy: building children may move the component storage.
164 const std::vector<std::string> options =
166 .getOptions();
167
168 std::vector<ecs::Entity::Identifier> identifiers;
169 Children children;
170 RadioButton::Builder builder(registry, *this);
171 for (const auto &option: options) {
172 const auto button =
173 builder.withLabel(option).registerEntity(shared_from_this());
174 identifiers.push_back(button->getIdentifier());
175 children.push_back(button);
176 }
177
179 .setButtons(identifiers);
180 setChildren(children);
183 }
184
185} // namespace guillaume::entities
@ Start
At the top (row) or left (column).
Definition layout.hpp:100
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Vertical
Column: children placed top to bottom.
Definition layout.hpp:76
State of a radio group: a column of radio buttons of which at most one is selected.
RadioGroup & setValue(std::optional< std::size_t > value)
Store the selected option.
RadioGroup & setOnValueChange(ValueChangeHandler handler)
Set the callback run when the user changes the selection.
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.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Owns entities and gives the systems the list of entities to visit.
std::shared_ptr< EntityType > getEntity(Entity::Identifier identifier)
Find an entity owned directly by this registry, by identifier.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
std::shared_ptr< RadioGroup > registerEntity(std::shared_ptr< ecs::Entity > parent)
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures, creates and registers RadioButton widgets.
static constexpr float TouchTarget
Side of the square that answers input, centered on the ring, in dp.
Configures RadioGroup widgets and registers them in the scene's entity registry.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder holding the defaults of reset().
Builder & withValue(std::size_t index)
Choose the option selected when the group is built.
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the group is.
void reset(void) override
Restore the defaults: default pose, no option, no selected option, no value-change handler,...
Builder & withOnValueChange(std::function< void(std::size_t)> onValueChange)
Set the function called when the user selects another option.
std::shared_ptr< RadioGroup > buildEntity(void) override
Build a group and its radio buttons from the current configuration, without registering it or giving ...
std::shared_ptr< RadioGroup > makeRadioGroup(std::shared_ptr< ecs::Entity > parent, const std::vector< std::string > &options)
Build a group and its radio buttons, register it under parent and reset the builder.
Builder & addOption(const std::string &label)
Append an option at the end of the list.
std::size_t getOptionCount(void) const
Get the number of options.
std::optional< std::size_t > getValue(void) const
Get the selected option.
static std::shared_ptr< RadioGroup > create(Arguments &&...arguments)
Build a group without registering it in an entity registry.
RadioGroup & setOnValueChange(std::function< void(std::size_t)> onValueChange)
Set the function called when the user selects another option.
std::shared_ptr< RadioButton > getOption(std::size_t index) const
Get the radio button of an option, for example to disable it.
RadioGroup & setValue(std::size_t index)
Select an option from code.
static constexpr float RowSpacing
Vertical space between two radio buttons, in dp: enough for the 40 dp touch targets of their 20 dp ri...
std::vector< std::shared_ptr< ecs::Entity > > Children
Definition surface.hpp:245
SurfaceBase(ecs::ComponentRegistry &registry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
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 install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a radio group.
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.
State. Every class here is plain data attached to an entity.
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...
Definition surface.hpp:71