guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
badge.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 <memory>
24#include <stdexcept>
25
26#include "guillaume/entities/badge.hpp"
27
28#include "guillaume/entities/builder_base.hpp"
29#include "guillaume/entities/content_helpers.hpp"
31#include "guillaume/systems/badge.hpp"
32#include "guillaume/theme.hpp"
33
34namespace guillaume::entities
35{
37 ecs::EntityRegistry &entityRegistry)
38 : EntityBuilderBase<Badge>(componentRegistry, entityRegistry)
39 {
40 reset();
41 }
42
43 std::shared_ptr<Badge> Badge::Builder::buildEntity(void)
44 {
45 return Badge::create(this->getComponentRegistry(), _config, _variant,
46 _label);
47 }
48
50 {
51 _config = SurfaceConfig();
52 _variant = Variant::Large;
53 _label = "";
55 _config.mainAxisAlignment =
57 _config.crossAxisAlignment =
59 }
60
62 Badge::Builder::withPose(const utility::graphic::PoseF &pose)
63 {
64 _config.pose = pose;
65 return *this;
66 }
67
69 {
70 _variant = variant;
71 return *this;
72 }
73
74 Badge::Builder &Badge::Builder::withLabel(const std::string &label)
75 {
76 _label = label;
77 return *this;
78 }
79
80 std::shared_ptr<Badge>
81 Badge::Builder::makeBadge(std::shared_ptr<ecs::Entity> parent,
82 const std::string &label, Variant variant)
83 {
84 return withLabel(label).withVariant(variant).registerEntity(parent);
85 }
86
87 Badge::Badge(ecs::ComponentRegistry &registry, const SurfaceConfig &config,
88 Variant variant, const std::string &label)
89 : SurfaceBase<components::Animation, components::Badge>(registry,
90 config)
91 {
94 .setVariant(variant)
95 .setLabel(label);
98 }
99
101 {
102 }
103
104 float Badge::getSize(void) const
105 {
107 }
108
117
124
125 Badge &Badge::setLabel(const std::string &label)
126 {
129 .setLabel(label);
131 return *this;
132 }
133
134 Badge &Badge::setSize(float size)
135 {
136 if (size <= 0.0f) {
137 throw std::invalid_argument("Badge size must be positive");
138 }
141 .setSize(size);
143 return *this;
144 }
145
146 Badge &Badge::clearSize(void)
147 {
150 .setSize(std::nullopt);
152 return *this;
153 }
154
155 Badge &Badge::setContainerColor(const utility::graphic::Color32Bit &color)
156 {
159 .setContainerColor(color);
161 return *this;
162 }
163
164 Badge &Badge::setContentColor(const utility::graphic::Color32Bit &color)
165 {
168 .setContentColor(color);
170 return *this;
171 }
172
173 Badge &Badge::setVisible(bool visible)
174 {
176 visible);
177 return *this;
178 }
179
180 bool Badge::isVisible(void) const
181 {
182 return getComponentRegistry()
184 .isVisible();
185 }
186
187 float Badge::getOpacity(void) const
188 {
189 return getComponentRegistry()
191 .getOpacity();
192 }
193
194 void Badge::build(void)
195 {
196 auto &registry = getComponentRegistry();
197 auto &badge = registry.getComponent<components::Badge>(getIdentifier());
198
199 if (systems::Badge::needsLabel(registry, getIdentifier())) {
200 const auto label =
201 buildText(registry, *this, shared_from_this(), badge.getLabel(),
203 schemeColor(SchemeColorRole::OnError));
204
205 if (label != nullptr) {
206 badge.setLabelEntity(label->getIdentifier());
207 setChildren({ label });
208 }
209 }
210
212 }
213
214} // namespace guillaume::entities
State of a badge: a small dot or pill, usually holding a count, drawn over another widget.
Definition badge.hpp:64
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
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.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
Configures Badge widgets and registers them in the scene's entity registry.
Definition badge.hpp:96
std::shared_ptr< Badge > buildEntity(void) override
Build a badge and its label from the current configuration, without registering it or giving it a par...
Definition badge.cpp:43
Builder & withLabel(const std::string &label)
Set the text shown in the badge.
Definition badge.cpp:74
Builder & withVariant(Variant variant)
Choose the size class of the badge.
Definition badge.cpp:68
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder holding the defaults of reset().
Definition badge.cpp:36
void reset(void) override
Restore the defaults: default pose, the Large variant, an empty label, and a horizontal layout center...
Definition badge.cpp:49
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the badge is.
Definition badge.cpp:62
std::shared_ptr< Badge > makeBadge(std::shared_ptr< ecs::Entity > parent, const std::string &label, Variant variant)
Build a badge and its label, register it under parent and reset the builder.
Definition badge.cpp:81
Badge & setSize(float size)
Override the height of the variant (Paper size). Resizes at once.
Definition badge.cpp:134
Badge & setLabel(const std::string &label)
Change the text shown in the badge. Restyles at once.
Definition badge.cpp:125
Badge & setContainerColor(const utility::graphic::Color32Bit &color)
Override the fill color of the pill. Restyles at once.
Definition badge.cpp:155
Badge & clearSize(void)
Drop the height override and use the height of the variant again. Resizes at once.
Definition badge.cpp:146
~Badge(void) override
Destructor.
Definition badge.cpp:100
Badge & setContentColor(const utility::graphic::Color32Bit &color)
Override the color of the label. Restyles at once.
Definition badge.cpp:164
float getOpacity(void) const
Get the current opacity of the fade.
Definition badge.cpp:187
Badge & setVariant(Variant variant)
Change the size class of the badge. Resizes at once.
Definition badge.cpp:109
Variant getVariant(void) const
Get the size class of the badge.
Definition badge.cpp:118
components::Badge::Variant Variant
Definition badge.hpp:89
static std::shared_ptr< Badge > create(Arguments &&...arguments)
Build a badge without registering it in an entity registry.
Definition badge.hpp:179
bool isVisible(void) const
Get the visibility the badge is fading to.
Definition badge.cpp:180
float getSize(void) const
Get the height the badge is drawn with.
Definition badge.cpp:104
Badge & setVisible(bool visible)
Show or hide the badge with a 150 ms fade (Paper visible).
Definition badge.cpp:173
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
SurfaceBase(ecs::ComponentRegistry &registry, const SurfaceConfig &config)
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Size and color one badge and its label.
Definition badge.cpp:166
static void setVisible(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool visible)
Show or hide the badge with a fade.
Definition badge.cpp:138
static float sizeOf(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the height of a badge, in dp.
Definition badge.cpp:109
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Prepare the visibility fade of a badge.
Definition badge.cpp:95
static bool needsLabel(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the badge should have a label child.
Definition badge.cpp:129
Makes the color helpers callable from guillaume::entities without qualification.
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
std::shared_ptr< Text > buildText(ecs::ComponentRegistry &registry, ecs::EntityRegistry &entityRegistry, std::shared_ptr< ecs::Entity > parent, const std::string &content, float fontSize, const utility::graphic::Color32Bit &color)
Create a Text of a given font size as a child of a widget, or nothing when there is no content.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ LabelSmall
11 / 16, medium weight.
Definition typescale.hpp:59
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