23#include "guillaume/systems/card.hpp"
31#include "guillaume/components/accessibility.hpp"
32#include "guillaume/components/animation.hpp"
33#include "guillaume/components/borders.hpp"
34#include "guillaume/components/color.hpp"
35#include "guillaume/components/elevation.hpp"
36#include "guillaume/components/image.hpp"
37#include "guillaume/components/layout.hpp"
38#include "guillaume/components/text.hpp"
41#include "guillaume/systems/layout.hpp"
42#include "guillaume/theme.hpp"
59 utility::graphic::Color32Bit
60 lerpColor(
const utility::graphic::Color32Bit &from,
61 const utility::graphic::Color32Bit &to,
float t)
63 const auto mix = [t](std::uint8_t a, std::uint8_t b) {
64 return static_cast<std::uint8_t
>(
66 + (
static_cast<float>(b) -
static_cast<float>(a)) * t
69 return utility::graphic::Color32Bit(
70 mix(from.getRed(), to.getRed()),
71 mix(from.getGreen(), to.getGreen()),
72 mix(from.getBlue(), to.getBlue()),
73 mix(from.getAlpha(), to.getAlpha()));
82 void writeText(ecs::ComponentRegistry ®istry,
84 const std::string &content)
100 arrangeSection(ecs::ComponentRegistry ®istry,
102 const std::vector<ecs::Entity::Identifier> &children)
104 if (!registry.hasComponent<components::Layout>(section)
105 || !registry.hasComponent<components::Transform>(section)
106 || !registry.hasComponent<components::Bound>(section)) {
109 registry.getComponent<components::Layout>(section).setChildren(
120 void arrangeSections(ecs::ComponentRegistry ®istry,
121 const components::Card &card)
124 std::vector<ecs::Entity::Identifier> texts;
125 std::vector<ecs::Entity::Identifier> buttons;
127 if (card.getTitleText() != invalid) {
128 texts.push_back(card.getTitleText());
130 if (card.getSubtitleText() != invalid) {
131 texts.push_back(card.getSubtitleText());
133 for (
const auto button: card.getActionButtons()) {
134 if (button != invalid) {
135 buttons.push_back(button);
138 arrangeSection(registry, card.getTitleSection(), texts);
139 arrangeSection(registry, card.getActionsSection(), buttons);
147 void applyStyle(ecs::ComponentRegistry ®istry,
151 registry.getComponent<components::Card>(identifier);
153 registry.getComponent<components::Borders>(identifier);
155 registry.getComponent<components::Elevation>(identifier);
156 utility::graphic::Color32Bit container;
158 switch (card.getVariant()) {
159 case Variant::Elevated: {
167 const float t = card.getPressProgress();
168 container = lerpColor(
173 const auto level = t >= 0.5f
176 const float spread = rest.shadowSpread
177 + (raised.shadowSpread - rest.shadowSpread) * t;
178 const utility::graphic::Color32Bit shadow(
180 static_cast<std::uint8_t
>(
181 static_cast<float>(rest.shadowAlpha)
182 + (
static_cast<float>(raised.shadowAlpha)
183 -
static_cast<float>(rest.shadowAlpha))
188 if (elevation.getElevationLevel() != level
189 || elevation.getLevel() != 1.0f
190 || elevation.getSpread() != spread
191 || elevation.getColor() != shadow) {
192 elevation.setElevationLevel(level)
199 case Variant::Filled: {
200 container =
schemeColor(SchemeColorRole::SurfaceVariant);
205 case Variant::Outlined:
209 .setColor(
schemeColor(SchemeColorRole::OutlineVariant))
216 if (card.isInteractive()) {
218 registry, identifier, container,
221 registry.getComponent<components::Color>(identifier)
222 .setColor(container);
243 const float target = card.
isPressed() ? 1.0f : 0.0f;
244 const float from = card.getPressFrom();
246 card.setPressProgress(from + (target - from) * progress);
247 apply(*registry, identifier);
261 if (!card.isInteractive()) {
262 card.setInteractive(
true);
264 componentRegistry, identifier,
266 [registry, identifier]() {
267 press(*registry, identifier);
269 [registry, identifier]() {
273 apply(componentRegistry, identifier);
279 if (
isDisabled(componentRegistry, entityIdentifier)) {
298 const bool pressed = card.
getVariant() == Variant::Elevated
301 if (pressed != card.isPressed()) {
302 card.setPressed(pressed).setPressFrom(card.getPressProgress());
307 apply(componentRegistry, entityIdentifier);
318 if (disabled && card.isPressed()) {
319 card.setPressed(
false).setPressProgress(0.0f);
321 apply(componentRegistry, entityIdentifier);
338 return std::max(0.0f, width - 2.0f * layout.getPadding());
349 const auto cover = card.getCover();
350 const auto titleSection = card.getTitleSection();
351 const auto content = card.getContentEntity();
352 const auto actionsSection = card.getActionsSection();
354 applyStyle(componentRegistry, entityIdentifier);
358 .setTexturePath(card.getCoverPath());
360 writeText(componentRegistry, card.getTitleText(), card.getTitle());
361 writeText(componentRegistry, card.getSubtitleText(),
363 writeText(componentRegistry, content, card.getContentText());
368 .setLabel(card.getTitle());
373 arrangeSections(componentRegistry, card);
378 if (cover != invalid || titleSection != invalid || content != invalid
379 || actionsSection != invalid) {
380 std::vector<ecs::Entity::Identifier> children;
381 for (
const auto child:
382 { cover, titleSection, content, actionsSection }) {
383 if (child != invalid) {
384 children.push_back(child);
387 layout.setChildren(children);
389 Layout::apply(componentRegistry, entityIdentifier, layout.getChildren(),
393 arrangeSections(componentRegistry, card);
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
A timer that runs from 0 to a duration and reports its eased progress, from 0 to 1,...
Animation & setEasing(motion::Easing easing)
Set the curve that turns the linear progress into the value passed to the sample handler.
Animation & setOnSampleHandler(const SampleHandler &handler)
Store the callback that applies the animation.
State of a card: a surface grouping an optional cover image, title, text and row of action buttons.
bool isPressed(void) const noexcept
Check whether the press motion is heading to the raised state.
std::function< void(void)> Handler
Callback run when the card is pressed.
Variant
Look of the card surface.
Card & setOnPress(Handler handler)
Store the callback run when the card is pressed.
Variant getVariant(void) const noexcept
Get the look of the card surface.
A picture drawn in the entity's rectangle: the texture to show, a fallback texture,...
How a container arranges its children: in a row or a column, with which alignment,...
bool hasFixedWidth(void) const
Check whether the container width is forced.
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.
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...
void update(const ecs::Entity::Identifier &entityIdentifier) override
Run apply() on one card; called by the frame loop.
static void press(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Activate the card as a click or a key press would.
Card(void)
Create the system in the Layout phase.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Prepare the press-elevation motion of a card.
static void setDisabled(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool disabled)
Enable or disable the card and restyle it.
static float contentWidth(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Get the width a section of the card can use, in dp.
static void interactionChanged(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
React to a change of the hover or press state of the card.
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the card ignores input.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Style one card and arrange its sections.
static void setOnPress(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, components::Card::Handler handler)
Set the press handler and make the card pressable.
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 motion::Easing standard(void)
Get the easing for elements that move or change while staying on screen.
State. Every class here is plain data attached to an entity.
@ Button
Pressable button.
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 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 schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ Level2
Second raised level.
@ Level1
Lowest raised level.
@ Level0
Flat: no shadow, no tint.
utility::graphic::Color32Bit getElevatedSurfaceColor(theme::ElevationLevel level)
Get the background color of a surface raised to an elevation level.
const theme::ElevationToken & getElevationToken(theme::ElevationLevel level)
Get the shadow and tint the active theme gives to an elevation level.
float getMotionDuration(theme::MotionDuration duration)
Get the length the active theme gives to a duration token.
Helpers of the namespace systems::interaction: what "pressable" means for a widget,...
bool isPressed(const ecs::ComponentRegistry ®istry, const ecs::Entity::Identifier &entityIdentifier)
Whether an entity is being pressed.
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.