guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
layout.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/layout.hpp"
24
25#include <algorithm>
26#include <cstdint>
27#include <utility>
28
29#include "guillaume/components/hierarchy.hpp"
30#include "guillaume/components/layout_extent.hpp"
31#include "guillaume/ecs/entity.hpp"
32
33namespace guillaume::systems
34{
35 namespace
36 {
40 struct LayoutChild {
41 ecs::Entity::Identifier identifier;
42 float width;
43 float height;
44 };
45
54 float crossOffset(components::Layout::CrossAxisAlignment alignment,
55 float padding, float crossRegion, float childCross)
56 {
57 switch (alignment) {
59 return padding;
61 return padding + crossRegion - childCross;
63 default:
64 return padding + (crossRegion - childCross) / 2.0f;
65 }
66 }
67 } // namespace
68
71 components::Bound>(ecs::Phase::Layout)
72 {
73 }
74
75 void Layout::update(const ecs::Entity::Identifier &entityIdentifier)
76 {
77 if (!requireComponent<components::Layout>(entityIdentifier)) {
78 return;
79 }
80
81 const auto &layout = getComponent<components::Layout>(entityIdentifier);
82
83 // Only the container's registered layout children are arranged; any
84 // child the container positions manually (a switch thumb, a slider
85 // fill, ...) is deliberately absent from the list and keeps its pose.
86 apply(getComponentRegistry(), entityIdentifier, layout.getChildren(),
87 layerOf(getComponentRegistry(), entityIdentifier));
88 }
89
90 std::uint32_t Layout::layerOf(const ecs::ComponentRegistry &registry,
91 const ecs::Entity::Identifier &identifier)
92 {
93 // The layer comes from the Hierarchy component, so systems need no
94 // entity object.
95 if (!registry.hasComponent<components::Hierarchy>(identifier)) {
96 return 0;
97 }
98 return static_cast<std::uint32_t>(
99 registry.getComponent<components::Hierarchy>(identifier)
100 .getLayer());
101 }
102
103 const utility::graphic::PoseF Layout::applyLayerToPosition(
104 const utility::graphic::PositionF &position,
105 const utility::graphic::OrientationF &orientation,
106 const std::uint32_t &layer)
107 {
108 const auto forwardVector = orientation.getForward();
109 utility::graphic::PositionF forwardPosition = position;
110
111 forwardPosition.translate(utility::graphic::PositionF(
112 -forwardVector * static_cast<float>(layer + ContentLayerOffset)
113 * LayerDepthStep));
114
115 return utility::graphic::PoseF(forwardPosition, orientation);
116 }
117
118 const utility::graphic::PoseF Layout::contentLayerPose(
119 const utility::graphic::PositionF &position,
120 const utility::graphic::OrientationF &orientation,
121 const std::uint32_t &containerLayer)
122 {
123 return applyLayerToPosition(position, orientation, containerLayer);
124 }
125
127 ecs::ComponentRegistry &registry,
128 const ecs::Entity::Identifier &parentIdentifier,
129 const std::vector<ecs::Entity::Identifier> &childIdentifiers,
130 std::uint32_t layer)
131 {
132 const auto &layout =
133 registry.getComponent<components::Layout>(parentIdentifier);
134 const auto &parentTransform =
135 registry.getComponent<components::Transform>(parentIdentifier);
136 auto &parentBound =
137 registry.getComponent<components::Bound>(parentIdentifier);
138
139 const auto parentPose = parentTransform.getPose();
140 const auto orientation = parentPose.getOrientation();
141 const float originX = parentPose.getPosition().getX();
142 const float originY = parentPose.getPosition().getY();
143 const float originZ = parentPose.getPosition().getZ();
144
145 const bool horizontal =
146 layout.getAxis() == components::Layout::Axis::Horizontal;
147
148 std::vector<LayoutChild> children;
149 children.reserve(childIdentifiers.size());
150
151 for (const auto &childIdentifier: childIdentifiers) {
152 if (childIdentifier == ecs::Entity::InvalidIdentifier
154 childIdentifier)
155 || !registry.hasComponent<components::Bound>(childIdentifier)) {
156 continue;
157 }
158
159 const auto &childBound =
160 registry.getComponent<components::Bound>(childIdentifier);
161 // A child may reserve room beyond what it draws (a checkbox and
162 // its label).
163 const float trailing =
164 registry.hasComponent<components::LayoutExtent>(childIdentifier)
165 ? registry
167 .getTrailingWidth()
168 : 0.0f;
169 children.push_back(LayoutChild {
170 .identifier = childIdentifier,
171 .width = childBound.getWidth() + trailing,
172 .height = childBound.getHeight(),
173 });
174 }
175
176 float rawMain = 0.0f;
177 float rawCross = 0.0f;
178
179 for (const auto &child: children) {
180 rawMain += horizontal ? child.width : child.height;
181 rawCross =
182 std::max(rawCross, horizontal ? child.height : child.width);
183 }
184
185 const float spacing = layout.getSpacing();
186 const float padding = layout.getPadding();
187
188 const float gapsTotal = children.size() > 1
189 ? spacing * static_cast<float>(children.size() - 1)
190 : 0.0f;
191 const float mainContent = rawMain + gapsTotal;
192
193 const bool fixedWidth = layout.hasFixedWidth();
194 const bool fixedHeight = layout.hasFixedHeight();
195
196 const bool hasFixedMain = horizontal ? fixedWidth : fixedHeight;
197 const float fixedMain =
198 horizontal ? layout.getFixedWidth() : layout.getFixedHeight();
199 const float availableMain =
200 hasFixedMain ? fixedMain : mainContent + 2.0f * padding;
201
202 const bool hasFixedCross = horizontal ? fixedHeight : fixedWidth;
203 const float fixedCross =
204 horizontal ? layout.getFixedHeight() : layout.getFixedWidth();
205 const float availableCross =
206 hasFixedCross ? fixedCross : rawCross + 2.0f * padding;
207
208 const float mainRegion = availableMain - 2.0f * padding;
209 const float crossRegion = availableCross - 2.0f * padding;
210
211 float cursor = padding;
212 float gap = spacing;
213
214 switch (layout.getMainAxisAlignment()) {
216 cursor = padding + (mainRegion - mainContent) / 2.0f;
217 break;
219 cursor = padding + (mainRegion - mainContent);
220 break;
222 cursor = padding;
223 gap = children.size() > 1 ? (mainRegion - rawMain)
224 / static_cast<float>(children.size() - 1)
225 : 0.0f;
226 break;
228 default:
229 cursor = padding;
230 break;
231 }
232
233 for (const auto &child: children) {
234 const float childMain = horizontal ? child.width : child.height;
235 const float childCross = horizontal ? child.height : child.width;
236
237 const float crossPosition =
238 crossOffset(layout.getCrossAxisAlignment(), padding,
239 crossRegion, childCross);
240
241 float childX = originX;
242 float childY = originY;
243
244 if (horizontal) {
245 childX = originX + cursor;
246 childY = originY - crossPosition;
247 } else {
248 childX = originX + crossPosition;
249 childY = originY - cursor;
250 }
251
252 auto &childTransform =
253 registry.getComponent<components::Transform>(child.identifier);
254 auto childPose = childTransform.getPose();
255
256 const utility::graphic::PositionF childBasePosition(childX, childY,
257 originZ);
258 const auto layeredPose =
259 applyLayerToPosition(childBasePosition, orientation, layer);
260
261 childPose.setPosition(layeredPose.getPosition());
262 childTransform.setPose(childPose);
263
264 cursor += childMain + gap;
265 }
266
267 const float width = fixedWidth
268 ? layout.getFixedWidth()
269 : (horizontal ? mainContent : rawCross) + 2.0f * padding;
270 const float height = fixedHeight
271 ? layout.getFixedHeight()
272 : (horizontal ? rawCross : mainContent) + 2.0f * padding;
273
274 parentBound.setWidth(width).setHeight(height);
275 }
276
277} // namespace guillaume::systems
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Place of an entity in the entity tree: its parent, its direct children and its depth.
Definition hierarchy.hpp:61
std::int32_t getLayer(void) const noexcept
Get the depth of the entity in the tree.
Definition hierarchy.cpp:83
Space a widget takes in its parent's layout beyond its Bound.
How a container arranges its children: in a row or a column, with which alignment,...
Definition layout.hpp:69
CrossAxisAlignment
Where each child goes along the cross axis, inside the padding. In a container that wraps its childre...
Definition layout.hpp:99
@ End
At the bottom (row) or right (column).
Definition layout.hpp:102
@ Start
At the top (row) or left (column).
Definition layout.hpp:100
@ End
Packed at the right (row) or bottom (column).
Definition layout.hpp:89
@ Start
Packed at the left (row) or top (column).
Definition layout.hpp:87
@ Horizontal
Row: children placed left to right.
Definition layout.hpp:75
Where an entity is in space: its pose (position and orientation) and its scale.
Definition transform.hpp:64
utility::graphic::PoseF getPose(void) const
Get where the entity is.
Definition transform.cpp:47
Transform & setPose(const utility::graphic::PoseF &pose)
Move and orient the entity.
Definition transform.cpp:27
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.
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
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
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...
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 constexpr std::uint32_t ContentLayerOffset
Number of layers added between a container and what is placed on it.
Definition layout.hpp:101
static const utility::graphic::PoseF contentLayerPose(const utility::graphic::PositionF &position, const utility::graphic::OrientationF &orientation, const std::uint32_t &containerLayer)
Compute the pose of content placed by hand on a container.
Definition layout.cpp:118
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
Layout(void)
Create the system in the Layout phase.
Definition layout.cpp:69
void update(const ecs::Entity::Identifier &entityIdentifier) override
Arrange the children listed in the components::Layout of one container, using the layer of its compon...
Definition layout.cpp:75
static constexpr float LayerDepthStep
Depth of one layer: the distance a child is moved towards the viewer, against the forward axis of its...
Definition layout.hpp:91
static const utility::graphic::PoseF applyLayerToPosition(const utility::graphic::PositionF &position, const utility::graphic::OrientationF &orientation, const std::uint32_t &layer)
Move a position in front of a container.
Definition layout.cpp:103
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.