guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
view.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 <algorithm>
24#include <memory>
25#include <vector>
26
27#include "guillaume/entities/view.hpp"
28#include "guillaume/entities/builder_base.hpp"
29
30#include "guillaume/systems/layout.hpp"
31
32namespace guillaume::entities
33{
34
36 ecs::EntityRegistry &entityRegistry)
37 : EntityBuilderBase<View>(componentRegistry, entityRegistry)
38 {
39 reset();
40 }
41
42 std::shared_ptr<View> View::Builder::buildEntity(void)
43 {
44 auto entity = View::create(this->getComponentRegistry(), _pose, _color,
45 _borderRadius, _axis, _mainAxisAlignment,
46 _crossAxisAlignment, _spacing, _padding,
47 _hasFixedWidth, _fixedWidth, _hasFixedHeight,
48 _fixedHeight, _entities);
49 return entity;
50 }
51
53 {
54 _pose = utility::graphic::PoseF();
55 _color = { 0, 0, 0, 0 }; // Transparent, like an RN View.
56 _borderRadius = 16.0f;
57 _axis = Axis::Horizontal;
58 _mainAxisAlignment = MainAxisAlignment::Start;
59 _crossAxisAlignment = CrossAxisAlignment::Center;
60 _spacing = 8.0f;
61 _padding = 16.0f;
62 _hasFixedWidth = false;
63 _hasFixedHeight = false;
64 _fixedWidth = 0.0f;
65 _fixedHeight = 0.0f;
66 _entities.clear();
67 }
68
69 View::Builder &View::Builder::withPose(const utility::graphic::PoseF &pose)
70 {
71 _pose = pose;
72 return *this;
73 }
74
76 View::Builder::withColor(const utility::graphic::Color32Bit &color)
77 {
78 _color = color;
79 return *this;
80 }
81
83 {
84 _borderRadius = borderRadius;
85 return *this;
86 }
87
89 {
90 _axis = axis;
91 return *this;
92 }
93
96 {
97 _mainAxisAlignment = alignment;
98 return *this;
99 }
100
103 {
104 _crossAxisAlignment = alignment;
105 return *this;
106 }
107
109 {
110 _spacing = spacing;
111 return *this;
112 }
113
115 {
116 _padding = padding;
117 return *this;
118 }
119
121 {
122 _hasFixedWidth = true;
123 _fixedWidth = width;
124 return *this;
125 }
126
128 {
129 _hasFixedHeight = true;
130 _fixedHeight = height;
131 return *this;
132 }
133
135 const std::vector<std::shared_ptr<ecs::Entity>> &entities)
136 {
137 _entities = entities;
138 return *this;
139 }
140
141 std::shared_ptr<View> View::Builder::makeDefaultView(
142 std::shared_ptr<Entity> parent, const utility::graphic::PoseF &pose,
143 const std::vector<std::shared_ptr<ecs::Entity>> &entities)
144 {
145 return withPose(pose).withEntities(entities).registerEntity(parent);
146 }
147
148 std::shared_ptr<View> View::Builder::makeColorView(
149 std::shared_ptr<Entity> parent, const utility::graphic::PoseF &pose,
150 const utility::graphic::Color32Bit &color,
151 const std::vector<std::shared_ptr<ecs::Entity>> &entities)
152 {
153 return withPose(pose)
154 .withColor(color)
155 .withEntities(entities)
156 .registerEntity(parent);
157 }
158
159 View::View(ecs::ComponentRegistry &registry,
160 const utility::graphic::PoseF &pose,
161 const utility::graphic::Color32Bit &color, float borderRadius,
162 Axis axis, MainAxisAlignment mainAxisAlignment,
163 CrossAxisAlignment crossAxisAlignment, float spacing,
164 float padding, bool hasFixedWidth, float fixedWidth,
165 bool hasFixedHeight, float fixedHeight,
166 const std::vector<std::shared_ptr<ecs::Entity>> &entities)
167 : ecs::ParentEntityFiller<components::Layout, components::Transform,
168 components::Bound, components::Color,
169 components::Borders>(registry)
170 {
171 const auto &identifier = getIdentifier();
172
173 registry.getComponent<components::Transform>(identifier).setPose(pose);
174 registry.getComponent<components::Color>(identifier).setColor(color);
175 registry.getComponent<components::Borders>(identifier)
176 .setBorderRadius(borderRadius);
177
178 auto &layout = registry.getComponent<components::Layout>(identifier);
179 layout.setAxis(axis)
180 .setMainAxisAlignment(mainAxisAlignment)
181 .setCrossAxisAlignment(crossAxisAlignment)
182 .setSpacing(spacing)
183 .setPadding(padding);
184
185 if (hasFixedWidth) {
186 layout.setFixedWidth(fixedWidth);
187 }
188 if (hasFixedHeight) {
189 layout.setFixedHeight(fixedHeight);
190 }
191
192 // Parenting needs shared_from_this(), so it waits for build().
193 // Until then the handles are parked among the direct entities, and
194 // the identifiers are already the layout children.
195 std::vector<ecs::Entity::Identifier> childIdentifiers;
196 childIdentifiers.reserve(entities.size());
197
198 for (const auto &entity: entities) {
199 if (entity != nullptr
200 && entity->getIdentifier() != ecs::Entity::InvalidIdentifier) {
201 accessDirectEntities().push_back(entity);
202 childIdentifiers.push_back(entity->getIdentifier());
203 }
204 }
205
206 layout.setChildren(childIdentifiers);
207 }
208
210 {
211 }
212
213 View &View::setPose(const utility::graphic::PoseF &pose)
214 {
217 .setPose(pose);
218
219 applyGeometry();
220
221 return *this;
222 }
223
224 View &View::setColor(const utility::graphic::Color32Bit &color)
225 {
228 .setColor(color);
229 return *this;
230 }
231
232 View &View::setBorderRadius(float borderRadius)
233 {
236 .setBorderRadius(borderRadius);
237 return *this;
238 }
239
240 View &View::setAxis(Axis axis)
241 {
244 .setAxis(axis);
245
246 applyGeometry();
247
248 return *this;
249 }
250
252 {
255 .setMainAxisAlignment(alignment);
256
257 applyGeometry();
258
259 return *this;
260 }
261
263 {
266 .setCrossAxisAlignment(alignment);
267
268 applyGeometry();
269
270 return *this;
271 }
272
273 View &View::setSpacing(float spacing)
274 {
277 .setSpacing(spacing);
278
279 applyGeometry();
280
281 return *this;
282 }
283
284 View &View::setPadding(float padding)
285 {
288 .setPadding(padding);
289
290 applyGeometry();
291
292 return *this;
293 }
294
295 View &View::setFixedWidth(float width)
296 {
299 .setFixedWidth(width);
300
301 applyGeometry();
302
303 return *this;
304 }
305
307 {
311
312 applyGeometry();
313
314 return *this;
315 }
316
317 View &View::setFixedHeight(float height)
318 {
321 .setFixedHeight(height);
322
323 applyGeometry();
324
325 return *this;
326 }
327
329 {
333
334 applyGeometry();
335
336 return *this;
337 }
338
340 const std::vector<std::shared_ptr<ecs::Entity>> &entities)
341 {
342 std::vector<ecs::Entity::Identifier> childIdentifiers;
343 childIdentifiers.reserve(entities.size());
344
345 for (const auto &entity: entities) {
346 if (entity != nullptr
347 && entity->getIdentifier() != ecs::Entity::InvalidIdentifier) {
348 entity->setParent(shared_from_this());
349 childIdentifiers.push_back(entity->getIdentifier());
350 }
351 }
352
353 // Record the layout children on the component so `systems::Layout`
354 // arranges exactly these.
357 .setChildren(childIdentifiers);
358
359 applyGeometry();
360
361 return *this;
362 }
363
364 void View::build(void)
365 {
366 const auto self = shared_from_this();
367 const auto &children =
370 .getChildren();
371 auto &direct = accessDirectEntities();
372
373 // The entities given to the constructor are layout children that
374 // are not parented yet. They leave the direct entities once
375 // attached: the view does not own what is attached to it, exactly
376 // as with setEntities().
377 std::vector<std::shared_ptr<ecs::Entity>> pending;
378
379 for (auto iterator = direct.begin(); iterator != direct.end();) {
380 const bool isLayoutChild =
381 std::find(children.begin(), children.end(),
382 (*iterator)->getIdentifier())
383 != children.end();
384
385 if (isLayoutChild && (*iterator)->getParent() != self) {
386 pending.push_back(std::move(*iterator));
387 iterator = direct.erase(iterator);
388 } else {
389 ++iterator;
390 }
391 }
392
393 for (const auto &entity: pending) {
394 entity->setParent(self);
395 }
396
397 applyGeometry();
398 }
399
400 void View::applyGeometry(void)
401 {
402 auto &registry = getComponentRegistry();
403 const auto &identifier = getIdentifier();
404
406 registry, identifier,
407 registry.getComponent<components::Layout>(identifier).getChildren(),
408 systems::Layout::layerOf(registry, identifier));
409 }
410
411} // namespace guillaume::entities
Corner radii and outline stroke of a rectangular entity.
Definition borders.hpp:67
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Definition color.hpp:58
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
Layout & setAxis(Axis axis)
Choose between a row and a column.
Definition layout.cpp:32
Layout & setPadding(float padding)
Set the inset between the container's edges and its children.
Definition layout.cpp:92
Layout & setMainAxisAlignment(MainAxisAlignment alignment)
Set where the children go along the main axis.
Definition layout.cpp:47
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
Definition layout.cpp:62
Layout & setSpacing(float spacing)
Set the gap between two consecutive children.
Definition layout.cpp:77
Where an entity is in space: its pose (position and orientation) and its scale.
Definition transform.hpp:64
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.
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
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
std::vector< std::shared_ptr< Entity > > & accessDirectEntities(void) override
Give access to the list of entities this entity owns directly.
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Configures View widgets and registers them in the scene's entity registry.
Definition view.hpp:118
Builder & withMainAxisAlignment(MainAxisAlignment alignment)
Set how the children are distributed along the axis.
Definition view.cpp:95
Builder & withCrossAxisAlignment(CrossAxisAlignment alignment)
Set how each child is aligned across the axis.
Definition view.cpp:102
Builder & withColor(const utility::graphic::Color32Bit &color)
Set the background color.
Definition view.cpp:76
Builder & withEntities(const std::vector< std::shared_ptr< ecs::Entity > > &entities)
Set the entities the view arranges, in layout order.
Definition view.cpp:134
void reset(void) override
Restore the defaults: default pose, transparent background, corner radius 16 dp, horizontal axis,...
Definition view.cpp:52
Builder & withSpacing(float spacing)
Set the gap between two consecutive children.
Definition view.cpp:108
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder holding the defaults of reset().
Definition view.cpp:35
Builder & withBorderRadius(float borderRadius)
Set the corner radius of the background.
Definition view.cpp:82
Builder & withFixedWidth(float width)
Force the width of the view.
Definition view.cpp:120
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the view is.
Definition view.cpp:69
Builder & withFixedHeight(float height)
Force the height of the view.
Definition view.cpp:127
std::shared_ptr< View > buildEntity(void) override
Build a view from the current configuration, without registering it or giving it a parent.
Definition view.cpp:42
Builder & withPadding(float padding)
Set the space between the edges of the view and its children.
Definition view.cpp:114
std::shared_ptr< View > makeDefaultView(std::shared_ptr< Entity > parent, const utility::graphic::PoseF &pose, const std::vector< std::shared_ptr< ecs::Entity > > &entities)
Build a transparent view, register it under parent and reset the builder.
Definition view.cpp:141
Builder & withAxis(Axis axis)
Choose between a row and a column.
Definition view.cpp:88
std::shared_ptr< View > makeColorView(std::shared_ptr< Entity > parent, const utility::graphic::PoseF &pose, const utility::graphic::Color32Bit &color, const std::vector< std::shared_ptr< ecs::Entity > > &entities)
Build a view with a background color, register it under parent and reset the builder.
Definition view.cpp:148
components::Layout::Axis Axis
Definition view.hpp:107
components::Layout::CrossAxisAlignment CrossAxisAlignment
How each child is aligned across the axis.
Definition view.hpp:111
View & setAxis(Axis axis)
Choose between a row and a column. Arranges the children at once.
Definition view.cpp:240
components::Layout::MainAxisAlignment MainAxisAlignment
How the children are distributed along the axis.
Definition view.hpp:109
View & setPadding(float padding)
Set the space between the edges of the view and its children. Arranges the children at once.
Definition view.cpp:284
View & clearFixedWidth(void)
Let the width follow the children again: their extent plus the padding. Arranges the children at once...
Definition view.cpp:306
View & setEntities(const std::vector< std::shared_ptr< ecs::Entity > > &entities)
Replace the entities the view arranges.
Definition view.cpp:339
~View(void)
Destructor.
Definition view.cpp:209
View & setColor(const utility::graphic::Color32Bit &color)
Set the background color.
Definition view.cpp:224
View & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set how each child is aligned across the axis. Arranges the children at once.
Definition view.cpp:262
View & setFixedHeight(float height)
Force the height of the view, whatever its children. Arranges the children at once.
Definition view.cpp:317
View & setFixedWidth(float width)
Force the width of the view, whatever its children. Arranges the children at once.
Definition view.cpp:295
View & setMainAxisAlignment(MainAxisAlignment alignment)
Set how the children are distributed along the axis. Arranges the children at once.
Definition view.cpp:251
View & setPose(const utility::graphic::PoseF &pose)
Move the view. Moves its layout children at once.
Definition view.cpp:213
static std::shared_ptr< View > create(Arguments &&...arguments)
Build a view without registering it in an entity registry.
Definition view.hpp:310
View & setSpacing(float spacing)
Set the gap between two consecutive children. Arranges the children at once.
Definition view.cpp:273
View & clearFixedHeight(void)
Let the height follow the children again: their extent plus the padding. Arranges the children at onc...
Definition view.cpp:328
View & setBorderRadius(float borderRadius)
Set the corner radius of the background.
Definition view.cpp:232
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
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46