28#include "guillaume/entities/card.hpp"
30#include "guillaume/components/accessibility.hpp"
31#include "guillaume/entities/builder_base.hpp"
32#include "guillaume/entities/button.hpp"
33#include "guillaume/entities/content_helpers.hpp"
34#include "guillaume/entities/image.hpp"
36#include "guillaume/entities/text.hpp"
37#include "guillaume/systems/card.hpp"
38#include "guillaume/theme.hpp"
55 CardSection(ecs::ComponentRegistry ®istry,
56 const SurfaceConfig &config)
57 : SurfaceBase<>(registry, config)
64 ~CardSection(
void)
override =
default;
67 constexpr float DefaultWidth = 240.0f;
82 entity->setOnPress(_onPress);
84 entity->setDisabled(_disabled);
91 _variant = Variant::Elevated;
95 _config.padding = 16.0f;
96 _config.spacing = 8.0f;
98 _config.setFixedWidth(DefaultWidth);
115 _config.hasFixedWidth =
true;
116 _config.fixedWidth = width;
122 _config.hasFixedHeight =
true;
123 _config.fixedHeight = height;
129 _config.borderRadius = radius;
135 _onPress = std::move(onPress);
141 _disabled = disabled;
145 std::shared_ptr<Card>
149 return withVariant(variant).registerEntity(parent);
215 .setCoverPath(texturePath);
227 .setSubtitle(subtitle);
238 .setContentText(content);
246 std::function<
void(
void)> onPress)
278 return std::const_pointer_cast<Text>(
281 .getContentEntity()));
298 std::vector<std::shared_ptr<Button>> actions;
300 if (row ==
nullptr) {
304 for (
const auto identifier: card.getActionButtons()) {
305 if (
auto button = std::const_pointer_cast<Button>(
306 row->getEntity<
Button>(identifier))) {
307 actions.push_back(std::move(button));
320 void Card::buildComposition(
void)
323 if (weak_from_this().expired()) {
328 const auto self = shared_from_this();
334 && !card.getCoverPath().empty()) {
335 Image::Builder builder(registry, *
this);
336 builder.withTexturePath(card.getCoverPath());
340 const auto cover = builder.registerEntity(self);
341 if (cover !=
nullptr) {
342 card.setCover(cover->getIdentifier());
347 && !card.getTitle().empty()) {
348 SurfaceConfig sectionConfig;
350 sectionConfig.mainAxisAlignment =
352 sectionConfig.crossAxisAlignment =
356 sectionConfig.setFixedWidth(inner);
359 std::make_shared<CardSection>(registry, sectionConfig);
360 section->setParent(self);
362 card.setTitleSection(section->getIdentifier());
364 const auto titleText =
365 buildText(registry, *section, section, card.getTitle(),
368 const auto subtitleText =
369 buildText(registry, *section, section, card.getSubtitle(),
373 std::vector<std::shared_ptr<ecs::Entity>> texts;
374 if (titleText !=
nullptr) {
375 card.setTitleText(titleText->getIdentifier());
376 texts.push_back(titleText);
378 if (subtitleText !=
nullptr) {
379 card.setSubtitleText(subtitleText->getIdentifier());
380 texts.push_back(subtitleText);
382 section->setChildren(texts);
384 section->getIdentifier());
387 section->getIdentifier())
389 .setLabel(card.getTitle());
393 && !card.getContentText().empty()) {
395 buildText(registry, *
this, self, card.getContentText(),
398 if (content !=
nullptr) {
399 card.setContentEntity(content->getIdentifier());
403 if (!card.getActions().empty()) {
405 SurfaceConfig rowConfig;
407 rowConfig.mainAxisAlignment =
409 rowConfig.crossAxisAlignment =
411 rowConfig.spacing = 8.0f;
413 rowConfig.setFixedWidth(inner);
416 std::make_shared<CardSection>(registry, rowConfig);
417 section->setParent(self);
419 card.setActionsSection(section->getIdentifier());
423 auto identifiers = card.getActionButtons();
424 while (row !=
nullptr
425 && identifiers.size() < card.getActions().size()) {
426 const auto &action = card.getActions()[identifiers.size()];
427 Button::Builder builder(registry, *row);
428 const auto button = builder.withLabel(action.label)
429 .withOnClick(action.onPress)
430 .withColorStyle(Button::Color::Text)
431 .registerEntity(row);
432 identifiers.push_back(button !=
nullptr
433 ? button->getIdentifier()
436 card.setActionButtons(identifiers);
438 if (row !=
nullptr) {
439 std::vector<std::shared_ptr<ecs::Entity>> buttons;
440 for (
const auto identifier: identifiers) {
441 if (
auto button = row->getEntity<Button>(identifier)) {
442 buttons.push_back(std::move(button));
445 row->setChildren(buttons);
450 std::vector<std::shared_ptr<ecs::Entity>> children;
451 for (
const auto identifier:
452 { card.getCover(), card.getTitleSection(), card.getContentEntity(),
453 card.getActionsSection() }) {
455 children.push_back(std::move(child));
461 void Card::build(
void)
State of a card: a surface grouping an optional cover image, title, text and row of action buttons.
@ Start
At the top (row) or left (column).
@ Center
Packed in the middle.
@ End
Packed at the right (row) or bottom (column).
@ Vertical
Column: children placed top to bottom.
@ 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.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Owns entities and gives the systems the list of entities to visit.
void addEntity(std::shared_ptr< Entity > entity)
Make this registry own an entity, so that it stays alive and systems visit it.
std::shared_ptr< EntityType > getEntity(Entity::Identifier identifier)
Find an entity owned directly by this registry, by identifier.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
ComponentRegistry & getComponentRegistry(void)
Configures Card widgets and registers them in the scene's entity registry.
Builder & withWidth(float width)
Set the width of the card.
void reset(void) override
Restore the defaults: default pose, the Elevated variant, no press handler, enabled,...
std::shared_ptr< Card > makeCard(std::shared_ptr< ecs::Entity > parent, Variant variant)
Build an empty card, register it under parent and reset the builder.
Builder & withDisabled(bool disabled)
Build the card disabled or enabled.
Builder & withOnPress(std::function< void(void)> onPress)
Make the built card pressable.
Builder & withBorderRadius(float radius)
Set the corner radius.
std::shared_ptr< Card > buildEntity(void) override
Build an empty card from the current configuration, without registering it or giving it a parent.
Builder & withHeight(float height)
Force the height of the card.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder holding the defaults of reset().
Builder & withVariant(Variant variant)
Choose the look of the card.
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the card is.
static constexpr float CoverHeight
Height given to the cover image, in dp (Paper Card.Cover: 195).
Card & setOnPress(std::function< void(void)> onPress)
Make the card pressable and set what a press does (Paper onPress).
static std::shared_ptr< Card > create(Arguments &&...arguments)
Build a card without registering it in an entity registry.
Card & addAction(const std::string &label, std::function< void(void)> onPress)
Append a text button to the row at the bottom of the card (Paper Card.Actions).
Card & setContent(const std::string &content)
Show a text under the title (Paper Card.Content).
Card & setDisabled(bool disabled)
Disable or enable the card (Paper disabled). Restyles at once.
Card & setCover(const std::string &texturePath)
Show an image at the top of the card (Paper Card.Cover).
bool isInteractive(void) const
Check whether the card reacts to input.
std::shared_ptr< ecs::Entity > getActionsSection(void) const
Get the row holding the action buttons.
Variant getVariant(void) const
Get the look of the card.
Card & setVariant(Variant variant)
Change the look of the card. Restyles at once.
static constexpr float TitleMinHeight
Height given to the title section, in dp (the minimum height of Paper Card.Title: 72).
float getPressProgress(void) const
Get how far the press elevation has progressed.
std::shared_ptr< Image > getCover(void) const
Get the cover image.
bool isDisabled(void) const
Check whether the card ignores presses.
std::shared_ptr< Text > getContent(void) const
Get the content text.
components::Card::Variant Variant
Look of the card: Elevated, Filled or Outlined.
std::vector< std::shared_ptr< Button > > getActions(void) const
Get the action buttons.
~Card(void) override
Destructor.
Card & setTitle(const std::string &title, const std::string &subtitle="")
Show a title, and optionally a subtitle under it (Paper Card.Title).
std::shared_ptr< ecs::Entity > getTitleSection(void) const
Get the section holding the title and subtitle texts.
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Base of the widgets that are a rectangle containing other widgets (Card, Dialog, Checkbox,...
SurfaceBase(ecs::ComponentRegistry ®istry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
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 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.
Makes the color helpers callable from guillaume::entities without qualification.
State. Every class here is plain data attached to an entity.
@ Header
Heading (app bar title, list subheader).
Widgets: the classes used to create entities with the right components and children.
std::shared_ptr< Text > buildText(ecs::ComponentRegistry ®istry, ecs::EntityRegistry &entityRegistry, std::shared_ptr< ecs::Entity > parent, const std::string &content, float fontSize, const utility::graphic::Color32Bit &color)
Create a Text of a given font size as a child of a widget, or nothing when there is no content.
utility::graphic::Color32Bit schemeColor(SchemeColorRole role)
Look up a color role in the color scheme of the active theme.
@ BodyLarge
16 / 24, regular.
@ BodyMedium
14 / 20, regular.
float getShapeRadius(theme::ShapeRole role)
Get the corner radius the active theme gives to a shape role.
One button of the actions row: its label and what it does.
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...