guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
placement_helpers.hpp
Go to the documentation of this file.
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 <cstdint>
26
27#include <utility/graphic/pose.hpp>
28#include <utility/graphic/position.hpp>
29
30#include "guillaume/ecs/component_registry.hpp"
31#include "guillaume/ecs/entity.hpp"
32
33#include "guillaume/components/bound.hpp"
34#include "guillaume/components/layout_extent.hpp"
35#include "guillaume/components/transform.hpp"
36
37#include "guillaume/systems/layout.hpp"
38
60
68{
69
80 inline utility::graphic::PoseF
82 const ecs::Entity::Identifier &identifier)
83 {
84 if (!registry.hasComponent<components::Transform>(identifier)) {
85 return utility::graphic::PoseF();
86 }
87
88 return registry.getComponent<components::Transform>(identifier)
89 .getPose();
90 }
91
117 inline void placeChild(ecs::ComponentRegistry &registry,
118 const ecs::Entity::Identifier &parentIdentifier,
119 const ecs::Entity::Identifier &childIdentifier,
120 const utility::graphic::PositionF &offset,
121 std::uint32_t parentLayer)
122 {
123 if (!registry.hasComponent<components::Transform>(childIdentifier)) {
124 return;
125 }
126
127 const auto origin = poseOf(registry, parentIdentifier);
128
129 const utility::graphic::PositionF basePosition(
130 origin.getPosition().getX() + offset.x,
131 origin.getPosition().getY() - offset.y,
132 origin.getPosition().getZ());
133
134 const auto pose = systems::Layout::contentLayerPose(
135 basePosition, origin.getOrientation(), parentLayer);
136
137 registry.getComponent<components::Transform>(childIdentifier)
138 .setPose(pose);
139 }
140
154 inline void centerChild(ecs::ComponentRegistry &registry,
155 const ecs::Entity::Identifier &parentIdentifier,
156 const ecs::Entity::Identifier &childIdentifier,
157 std::uint32_t parentLayer)
158 {
159 if (!registry.hasComponent<components::Bound>(parentIdentifier)
160 || !registry.hasComponent<components::Bound>(childIdentifier)) {
161 return;
162 }
163
164 const auto &parentBound =
165 registry.getComponent<components::Bound>(parentIdentifier);
166 const auto &childBound =
167 registry.getComponent<components::Bound>(childIdentifier);
168
170 registry, parentIdentifier, childIdentifier,
171 utility::graphic::PositionF(
172 (parentBound.getWidth() - childBound.getWidth()) / 2.0f,
173 (parentBound.getHeight() - childBound.getHeight()) / 2.0f,
174 0.0f),
175 parentLayer);
176 }
177
198 const ecs::Entity::Identifier &parentIdentifier,
199 const ecs::Entity::Identifier &childIdentifier,
200 float normalized, float inset,
201 std::uint32_t parentLayer)
202 {
203 if (!registry.hasComponent<components::Bound>(parentIdentifier)
204 || !registry.hasComponent<components::Bound>(childIdentifier)) {
205 return;
206 }
207
208 const auto &parentBound =
209 registry.getComponent<components::Bound>(parentIdentifier);
210 const auto &childBound =
211 registry.getComponent<components::Bound>(childIdentifier);
212
213 const float travel =
214 parentBound.getWidth() - childBound.getWidth() - 2.0f * inset;
215
217 registry, parentIdentifier, childIdentifier,
218 utility::graphic::PositionF(
219 inset + normalized * (travel > 0.0f ? travel : 0.0f),
220 (parentBound.getHeight() - childBound.getHeight()) / 2.0f,
221 0.0f),
222 parentLayer);
223 }
224
237 const ecs::Entity::Identifier &identifier,
238 float width)
239 {
240 if (!registry.hasComponent<components::LayoutExtent>(identifier)) {
241 if (width <= 0.0f) {
242 return;
243 }
244 registry.addComponent<components::LayoutExtent>(identifier);
245 }
246 registry.getComponent<components::LayoutExtent>(identifier)
247 .setTrailingWidth(width);
248 }
249
250} // namespace guillaume::systems::placement
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
Space a widget takes in its parent's layout beyond its Bound.
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.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
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 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
Helpers that position a child relative to its parent by writing the child's components::Transform.
void centerChild(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, std::uint32_t parentLayer)
Center a child inside the bound of its parent, on both axes.
utility::graphic::PoseF poseOf(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier)
Read the pose (position and orientation) of an entity.
void placeChild(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, const utility::graphic::PositionF &offset, std::uint32_t parentLayer)
Place a child at an offset from the position of its parent.
void reserveTrailingWidth(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &identifier, float width)
Reserve layout room after an entity's Bound.
void placeNormalized(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &parentIdentifier, const ecs::Entity::Identifier &childIdentifier, float normalized, float inset, std::uint32_t parentLayer)
Place a child along the width of its parent from a 0 to 1 value, vertically centered.