23#include "guillaume/systems/layout.hpp"
29#include "guillaume/components/hierarchy.hpp"
30#include "guillaume/components/layout_extent.hpp"
31#include "guillaume/ecs/entity.hpp"
55 float padding,
float crossRegion,
float childCross)
61 return padding + crossRegion - childCross;
64 return padding + (crossRegion - childCross) / 2.0f;
98 return static_cast<std::uint32_t
>(
104 const utility::graphic::PositionF &position,
105 const utility::graphic::OrientationF &orientation,
106 const std::uint32_t &layer)
108 const auto forwardVector = orientation.getForward();
109 utility::graphic::PositionF forwardPosition = position;
111 forwardPosition.translate(utility::graphic::PositionF(
115 return utility::graphic::PoseF(forwardPosition, orientation);
119 const utility::graphic::PositionF &position,
120 const utility::graphic::OrientationF &orientation,
121 const std::uint32_t &containerLayer)
129 const std::vector<ecs::Entity::Identifier> &childIdentifiers,
134 const auto &parentTransform =
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();
145 const bool horizontal =
148 std::vector<LayoutChild> children;
149 children.reserve(childIdentifiers.size());
151 for (
const auto &childIdentifier: childIdentifiers) {
159 const auto &childBound =
163 const float trailing =
169 children.push_back(LayoutChild {
170 .identifier = childIdentifier,
171 .width = childBound.getWidth() + trailing,
172 .height = childBound.getHeight(),
176 float rawMain = 0.0f;
177 float rawCross = 0.0f;
179 for (
const auto &child: children) {
180 rawMain += horizontal ? child.width : child.height;
182 std::max(rawCross, horizontal ? child.height : child.width);
185 const float spacing = layout.getSpacing();
186 const float padding = layout.getPadding();
188 const float gapsTotal = children.size() > 1
189 ? spacing *
static_cast<float>(children.size() - 1)
191 const float mainContent = rawMain + gapsTotal;
193 const bool fixedWidth = layout.hasFixedWidth();
194 const bool fixedHeight = layout.hasFixedHeight();
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;
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;
208 const float mainRegion = availableMain - 2.0f * padding;
209 const float crossRegion = availableCross - 2.0f * padding;
211 float cursor = padding;
214 switch (layout.getMainAxisAlignment()) {
216 cursor = padding + (mainRegion - mainContent) / 2.0f;
219 cursor = padding + (mainRegion - mainContent);
223 gap = children.size() > 1 ? (mainRegion - rawMain)
224 /
static_cast<float>(children.size() - 1)
233 for (
const auto &child: children) {
234 const float childMain = horizontal ? child.width : child.height;
235 const float childCross = horizontal ? child.height : child.width;
237 const float crossPosition =
238 crossOffset(layout.getCrossAxisAlignment(), padding,
239 crossRegion, childCross);
241 float childX = originX;
242 float childY = originY;
245 childX = originX + cursor;
246 childY = originY - crossPosition;
248 childX = originX + crossPosition;
249 childY = originY - cursor;
252 auto &childTransform =
254 auto childPose = childTransform.
getPose();
256 const utility::graphic::PositionF childBasePosition(childX, childY,
258 const auto layeredPose =
261 childPose.setPosition(layeredPose.getPosition());
262 childTransform.
setPose(childPose);
264 cursor += childMain + gap;
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;
274 parentBound.setWidth(width).setHeight(height);
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Place of an entity in the entity tree: its parent, its direct children and its depth.
std::int32_t getLayer(void) const noexcept
Get the depth of the entity in the tree.
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,...
CrossAxisAlignment
Where each child goes along the cross axis, inside the padding. In a container that wraps its childre...
@ End
At the bottom (row) or right (column).
@ Start
At the top (row) or left (column).
@ Center
Packed in the middle.
@ End
Packed at the right (row) or bottom (column).
@ Start
Packed at the left (row) or top (column).
@ Horizontal
Row: children placed left to right.
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...
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
SystemFiller(Phase phase)
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
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 ®istry, 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.
static constexpr std::uint32_t ContentLayerOffset
Number of layers added between a container and what is placed on it.
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.
static std::uint32_t layerOf(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &identifier)
Get the layer of an entity: its depth in the entity tree.
Layout(void)
Create the system in the Layout phase.
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...
static constexpr float LayerDepthStep
Depth of one layer: the distance a child is moved towards the viewer, against the forward axis of its...
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.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Rules. Every class here runs each frame on the entities that have the components it declares.