guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
switch.hpp
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#pragma once
24
25#include <utility>
26#include <functional>
27#include <memory>
28#include <string>
29
30#include <utility/graphic/color.hpp>
31#include <utility/graphic/pose.hpp>
32
33#include "guillaume/ecs/component_registry.hpp"
34#include "guillaume/ecs/entity_builder.hpp"
35
36#include "guillaume/components/hand_button_interaction.hpp"
37#include "guillaume/components/mouse_button_interaction.hpp"
38#include "guillaume/components/switch.hpp"
39#include "guillaume/components/value.hpp"
40
41#include "guillaume/entities/icon.hpp"
42#include "guillaume/entities/builder_base.hpp"
43#include "guillaume/entities/surface.hpp"
44#include "guillaume/entities/text.hpp"
45
46namespace guillaume::entities
47{
48
94 class Switch:
95 public SurfaceBase<
96 components::Value, components::MouseButtonInteraction,
97 components::HandButtonInteraction, components::Switch>
98 {
99 public:
103
108 class Builder: public EntityBuilderBase<Switch>
109 {
110 private:
111 SurfaceConfig _config;
112 Variant _variant { Variant::Enabled };
113 bool _checked { false };
114 std::string _iconGlyph {};
116 std::string _label {};
117
118 public:
126 Builder(ecs::ComponentRegistry &componentRegistry,
127 ecs::EntityRegistry &entityRegistry);
128
134 std::shared_ptr<Switch> buildEntity(void) override;
135
141 void reset(void) override;
142
149 Builder &withPose(const utility::graphic::PoseF &pose);
150
157 Builder &withVariant(Variant variant);
158
164 Builder &withChecked(bool checked);
165
172 Builder &withIcon(const std::string &iconGlyph);
173
180 Builder &withLabel(const std::string &label);
181
195 std::shared_ptr<Switch>
196 makeSwitch(std::shared_ptr<ecs::Entity> parent, Variant variant,
197 bool checked, const std::string &iconGlyph,
198 const std::string &label);
199 };
200
201 public:
215 template<typename... Arguments>
216 static std::shared_ptr<Switch> create(Arguments &&...arguments)
217 {
218 std::shared_ptr<Switch> entity(
219 new Switch(std::forward<Arguments>(arguments)...));
220 entity->build();
221 return entity;
222 }
223
227 ~Switch(void) override = default;
228
235 Switch &setVariant(Variant variant);
236
241 Variant getVariant(void) const;
242
252 Switch &setChecked(bool checked);
253
258 bool isChecked(void) const;
259
268 Switch &toggle(void);
269
280 Switch &setOnValueChange(std::function<void(bool)> onValueChange);
281
286 bool isDisabled(void) const;
287
291 static constexpr float LabelGap = components::Switch::LabelGap;
292
302 Switch &setLabel(const std::string &label);
303
304 private:
316 Switch(ecs::ComponentRegistry &registry, const SurfaceConfig &config,
317 Variant variant, bool checked, const std::string &iconGlyph,
318 const std::string &label);
319
329 void build(void);
330 };
331
332} // namespace guillaume::entities
static constexpr float LabelGap
Gap between the track and the label, in dp.
Definition switch.hpp:78
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
Owns entities and gives the systems the list of entities to visit.
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Builder & withChecked(bool checked)
Choose whether the switch starts on.
Definition switch.cpp:139
Builder & withVariant(Variant variant)
Choose whether the switch reacts to input.
Definition switch.cpp:133
Builder & withIcon(const std::string &iconGlyph)
Draw an icon on the thumb.
Definition switch.cpp:145
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder holding the defaults of reset().
Definition switch.cpp:92
std::shared_ptr< Switch > makeSwitch(std::shared_ptr< ecs::Entity > parent, Variant variant, bool checked, const std::string &iconGlyph, const std::string &label)
Build a switch and its children, register it under parent and reset the builder.
Definition switch.cpp:157
Builder & withLabel(const std::string &label)
Set the text shown to the right of the track.
Definition switch.cpp:151
void reset(void) override
Restore the defaults: default pose, the Enabled variant, off, no icon, no label, and a 52 x 32 dp tra...
Definition switch.cpp:106
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the switch is.
Definition switch.cpp:127
std::shared_ptr< Switch > buildEntity(void) override
Build a switch and its children from the current configuration, without registering it or giving it a...
Definition switch.cpp:99
Switch & setOnValueChange(std::function< void(bool)> onValueChange)
Set the handler called when the user toggles the switch (Paper onValueChange).
Definition switch.cpp:221
Switch & setVariant(Variant variant)
Enable or disable the switch. Restyles at once.
Definition switch.cpp:186
Switch & toggle(void)
Flip the checked state, as a user activation would.
Definition switch.cpp:215
~Switch(void) override=default
Destructor.
components::Switch::Variant Variant
Definition switch.hpp:102
bool isChecked(void) const
Whether the switch is on.
Definition switch.cpp:209
Variant getVariant(void) const
Get the variant of the switch.
Definition switch.cpp:195
static std::shared_ptr< Switch > create(Arguments &&...arguments)
Build a switch without registering it in an entity registry.
Definition switch.hpp:216
bool isDisabled(void) const
Check whether the switch ignores input.
Definition switch.cpp:229
Switch & setLabel(const std::string &label)
Change the text of the label. Restyles at once.
Definition switch.cpp:235
static constexpr float LabelGap
Gap between the track and the label, in dp.
Definition switch.hpp:291
Switch & setChecked(bool checked)
Turn the switch on or off from code.
Definition switch.cpp:202
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