23#include "guillaume/systems/menu.hpp"
28#include "guillaume/components/accessibility.hpp"
29#include "guillaume/components/borders.hpp"
30#include "guillaume/components/bound.hpp"
31#include "guillaume/components/color.hpp"
32#include "guillaume/components/elevation.hpp"
33#include "guillaume/components/layout.hpp"
34#include "guillaume/components/overlay.hpp"
35#include "guillaume/components/transform.hpp"
38#include "guillaume/systems/layout.hpp"
39#include "guillaume/theme.hpp"
47 using Row = components::Menu::Row;
53 constexpr float ItemHeight = 48.0f;
56 constexpr float DenseItemHeight = 32.0f;
65 constexpr float RowRadius = 16.0f;
76 void colorChild(ecs::ComponentRegistry ®istry,
78 const utility::graphic::Color32Bit &color)
80 if (registry.hasComponent<components::Color>(child)) {
81 registry.getComponent<components::Color>(child).setColor(color);
91 float contentWidthOf(
const ecs::ComponentRegistry ®istry,
94 float width = 2.0f * ItemPadding;
95 std::size_t count = 0;
97 for (
const auto child: row.content) {
98 if (!registry.hasComponent<components::Bound>(child)) {
102 registry.getComponent<components::Bound>(child).getWidth();
106 width += ItemSpacing *
static_cast<float>(count - 1);
116 void arrangeRows(ecs::ComponentRegistry ®istry,
117 const std::vector<Row> &rows)
119 for (
const auto &row: rows) {
131 void applyAnchor(ecs::ComponentRegistry ®istry,
135 const auto anchor = menu.getAnchor();
138 || !registry.hasComponent<components::Transform>(anchor)
139 || !registry.hasComponent<components::Bound>(anchor)) {
143 const auto anchorPose =
144 registry.getComponent<components::Transform>(anchor).getPose();
145 const auto &anchorBound =
146 registry.getComponent<components::Bound>(anchor);
148 registry.getComponent<components::Bound>(identifier);
150 const float offsetY =
151 menu.getAnchorPosition() == AnchorPosition::Bottom
152 ? anchorBound.getHeight() + AnchorGap
153 : -(bound.getHeight() + AnchorGap);
155 const utility::graphic::PositionF position(
156 anchorPose.getPosition().getX(),
158 anchorPose.getPosition().getY() - offsetY,
159 anchorPose.getPosition().getZ());
160 registry.getComponent<components::Transform>(identifier)
161 .setPose(utility::graphic::PoseF(position,
162 anchorPose.getOrientation()));
179 const std::size_t count =
183 for (std::size_t index = 0; index < count; ++index) {
184 installRow(componentRegistry, entityIdentifier, index);
197 if (index >= menu.getRows().size()) {
201 const auto row = menu.
getRows()[index].view;
204 [registry, identifier, index]() {
207 [registry, identifier, index]() {
208 styleRow(*registry, identifier, index);
210 if (index < menu.getItems().size()) {
211 const auto &item = menu.getItems()[index];
213 .setLabel(item.label);
236 if (index >= rows.size()) {
240 const auto &row = rows[index];
246 if (!
isOpen(componentRegistry, entityIdentifier)) {
249 for (
const auto icon: row.icons) {
255 colorChild(componentRegistry, row.view,
259 colorChild(componentRegistry, row.label,
262 for (
const auto icon: row.icons) {
263 colorChild(componentRegistry, icon,
264 enabled ?
schemeColor(SchemeColorRole::OnSurfaceVariant)
277 if (index >= items.size() || items[index].disabled
278 || !
isOpen(componentRegistry, entityIdentifier)) {
283 const auto onPress = items[index].onPress;
291 std::size_t index,
bool disabled)
295 apply(componentRegistry, entityIdentifier);
303 apply(componentRegistry, entityIdentifier);
311 apply(componentRegistry, entityIdentifier);
320 if (!overlay.isVisible()) {
323 hide(componentRegistry, entityIdentifier);
326 const auto onDismiss = overlay.getOnDismissHandler();
337 const auto &rows = menu.
getRows();
338 const auto &items = menu.getItems();
339 const bool elevated = menu.getMode() == Mode::Elevated;
341 std::vector<ecs::Entity::Identifier> children;
342 children.reserve(rows.size());
343 float contentWidth = 0.0f;
344 for (
const auto &row: rows) {
345 children.push_back(row.view);
347 std::max(contentWidth, contentWidthOf(componentRegistry, row));
367 const bool open =
isOpen(componentRegistry, entityIdentifier);
369 .setElevationLevel(elevated && open
384 for (std::size_t index = 0; index < rows.size(); ++index) {
385 const auto &row = rows[index];
386 const bool dense = index < items.size() && items[index].dense;
401 .setBorderRadius(RowRadius);
403 if (index < items.size()) {
405 !items[index].disabled);
407 styleRow(componentRegistry, entityIdentifier, index);
412 const auto layer =
Layout::layerOf(componentRegistry, entityIdentifier);
413 arrangeRows(componentRegistry, rows);
414 Layout::apply(componentRegistry, entityIdentifier, children, layer);
415 applyAnchor(componentRegistry, entityIdentifier, menu);
416 Layout::apply(componentRegistry, entityIdentifier, children, layer);
417 arrangeRows(componentRegistry, rows);
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Accessibility & setFocusable(bool focusable)
Include the entity in the Tab traversal or leave it out.
Corner radii and outline stroke of a rectangular entity.
Main color of an entity: the fill of a shape, or the ink of a text or an icon.
Soft shadow drawn around an elevated surface.
How a container arranges its children: in a row or a column, with which alignment,...
Layout & setFixedWidth(float width)
Force the container width instead of wrapping the children.
Layout & setChildren(const std::vector< ecs::Entity::Identifier > &children)
Replace the list of children to arrange.
Layout & setAxis(Axis axis)
Choose between a row and a column.
Layout & setPadding(float padding)
Set the inset between the container's edges and its children.
Layout & setMainAxisAlignment(MainAxisAlignment alignment)
Set where the children go along the main axis.
@ Start
At the top (row) or left (column).
Layout & setFixedHeight(float height)
Force the container height instead of wrapping the children.
Layout & setCrossAxisAlignment(CrossAxisAlignment alignment)
Set where each child goes along the cross axis.
@ Start
Packed at the left (row) or top (column).
@ Vertical
Column: children placed top to bottom.
@ Horizontal
Row: children placed left to right.
Layout & setSpacing(float spacing)
Set the gap between two consecutive children.
Marks an entity as something shown temporarily above the rest of the interface (a dialog,...
Overlay & setDismissOnOutsideClick(bool dismiss)
Choose whether a click outside the overlay dismisses it.
Overlay & setDismissOnEscape(bool dismiss)
Choose whether the Escape key dismisses the overlay.
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...
Arranges the children of a container in a row or a column and sizes the container around them.
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 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.
static constexpr float get(unsigned int steps)
Get a spacing as a number of grid units.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
Rules. Every class here runs each frame on the entities that have the components it declares.
utility::graphic::Color32Bit transparentColor(void)
The color written to hide a fill or an outline without removing its component.
utility::graphic::Color32Bit disabledContentColor(void)
Color of the icon, label or outline of a disabled widget.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Level2
Second raised level.
@ Level0
Flat: no shadow, no tint.
@ ExtraSmall
4 dp (text fields, menus, snackbars).
utility::graphic::Color32Bit getElevatedSurfaceColor(theme::ElevationLevel level)
Get the background color of a surface raised to an elevation level.
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
bool isEnabled(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity accepts input.
void makeInteractive(ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier, components::AccessibilityRole role, std::function< void(void)> onPress, std::function< void(void)> onStateChange={})
Make an entity hoverable, pressable and focusable.
void setEnabled(ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier, bool enabled)
Enable or disable an entity.
utility::graphic::Color32Bit stateLayerColor(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier, const utility::graphic::Color32Bit &base, const utility::graphic::Color32Bit &content)
Container color of a widget in its current input state.
Color helpers shared by the widget systems: scheme lookup, state layers and the disabled colors.