23#include "guillaume/entities/switch.hpp"
30#include <utility/event/hand_button_event.hpp>
31#include <utility/event/mouse_button_event.hpp>
33#include "guillaume/ecs/entity_filler.hpp"
35#include "guillaume/components/bound.hpp"
36#include "guillaume/components/color.hpp"
37#include "guillaume/components/ellipse.hpp"
38#include "guillaume/components/selection.hpp"
39#include "guillaume/components/transform.hpp"
41#include "guillaume/entities/content_helpers.hpp"
43#include "guillaume/entities/builder_base.hpp"
45#include "guillaume/systems/switch.hpp"
46#include "guillaume/theme.hpp"
52 constexpr float TrackWidth = 52.0f;
53 constexpr float TrackHeight = 32.0f;
60 public ecs::EntityFiller<components::Transform, components::Bound,
61 components::Color, components::Ellipse>
69 SwitchThumb(ecs::ComponentRegistry ®istry,
70 const utility::graphic::Color32Bit &color)
71 : ecs::EntityFiller<components::Transform, components::Bound,
72 components::Color, components::Ellipse>(
75 getComponentRegistry()
76 .getComponent<components::Bound>(getIdentifier())
78 .setHeight(ThumbSize);
80 getComponentRegistry()
81 .getComponent<components::Color>(getIdentifier())
88 ~SwitchThumb(
void)
override =
default;
102 _variant, _checked, _iconGlyph, _label);
109 _variant = Variant::Enabled;
114 _config.borderRadius = TrackHeight * 0.5f;
116 _config.mainAxisAlignment =
118 _config.crossAxisAlignment =
120 _config.spacing = 8.0f;
121 _config.padding = 0.0f;
122 _config.setFixedWidth(TrackWidth);
123 _config.setFixedHeight(TrackHeight);
147 _iconGlyph = iconGlyph;
158 std::shared_ptr<ecs::Entity> parent,
Variant variant,
bool checked,
159 const std::string &iconGlyph,
const std::string &label)
162 .withChecked(checked)
165 .registerEntity(parent);
170 const std::string &iconGlyph,
const std::string &label)
178 .setIconGlyph(iconGlyph)
241 config.setLabel(label);
250 void Switch::build(
void)
256 const auto thumb = std::make_shared<SwitchThumb>(
258 thumb->setParent(shared_from_this());
260 config.setThumb(thumb->getIdentifier());
262 if (!config.getIconGlyph().empty()) {
264 buildIcon(registry, *
this, thumb, config.getIconGlyph(), 16.0f,
266 if (icon !=
nullptr) {
267 config.setIcon(icon->getIdentifier());
271 if (!config.getLabel().empty()) {
273 buildText(registry, *
this, shared_from_this(),
276 if (label !=
nullptr) {
277 config.setLabelEntity(label->getIdentifier());
@ Start
Packed at the left (row) or top (column).
@ Horizontal
Row: children placed left to right.
Configuration of a switch: its variant, its thumb icon, its label and the identifiers of its thumb,...
static constexpr float ThumbSize
Diameter of the thumb, in dp.
A run of text: its content, its font metrics and the position of the editing cursor.
Text & setContent(const std::string &content)
Replace the text.
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.
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.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
ComponentRegistry & getComponentRegistry(void)
EntityBuilderBase(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
SurfaceBase(ecs::ComponentRegistry ®istry, const SurfaceConfig &config)
SurfaceBase & setChildren(const Children &children)
Configures Switch widgets and registers them in the scene's entity registry.
Builder & withChecked(bool checked)
Choose whether the switch starts on.
Builder & withVariant(Variant variant)
Choose whether the switch reacts to input.
Builder & withIcon(const std::string &iconGlyph)
Draw an icon on the thumb.
Builder(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry)
Construct a builder holding the defaults of reset().
std::shared_ptr< Switch > makeSwitch(std::shared_ptr< ecs::Entity > parent, Variant variant, bool checked, const std::string &iconGlyph, const std::string &label)
Build a switch and its children, register it under parent and reset the builder.
Builder & withLabel(const std::string &label)
Set the text shown to the right of the track.
void reset(void) override
Restore the defaults: default pose, the Enabled variant, off, no icon, no label, and a 52 x 32 dp tra...
Builder & withPose(const utility::graphic::PoseF &pose)
Set where the switch is.
std::shared_ptr< Switch > buildEntity(void) override
Build a switch and its children from the current configuration, without registering it or giving it a...
Switch & setOnValueChange(std::function< void(bool)> onValueChange)
Set the handler called when the user toggles the switch (Paper onValueChange).
Switch & setVariant(Variant variant)
Enable or disable the switch. Restyles at once.
Switch & toggle(void)
Flip the checked state, as a user activation would.
components::Switch::Variant Variant
bool isChecked(void) const
Whether the switch is on.
Variant getVariant(void) const
Get the variant of the switch.
static std::shared_ptr< Switch > create(Arguments &&...arguments)
Build a switch without registering it in an entity registry.
bool isDisabled(void) const
Check whether the switch ignores input.
Switch & setLabel(const std::string &label)
Change the text of the label. Restyles at once.
Switch & setChecked(bool checked)
Turn the switch on or off from code.
static void setChecked(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier, bool checked)
Turn the switch on or off from code.
static bool isChecked(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the switch is on.
static void install(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Make an entity behave as a switch.
static bool isDisabled(const ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Whether the switch ignores input and is drawn disabled.
static void toggle(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Flip the switch as a click does.
static void apply(ecs::ComponentRegistry &componentRegistry, const ecs::Entity::Identifier &entityIdentifier)
Draw one switch from its components: thumb position, colors and accessibility.
Makes the interaction helpers callable from guillaume::entities without qualification.
Makes the placement helpers callable from guillaume::entities without qualification.
Makes the color helpers callable from guillaume::entities without qualification.
State. Every class here is plain data attached to an entity.
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.
std::shared_ptr< Icon > buildIcon(ecs::ComponentRegistry ®istry, ecs::EntityRegistry &entityRegistry, std::shared_ptr< ecs::Entity > parent, const std::string &glyphName, float fontSize, const utility::graphic::Color32Bit &color, components::Glyph::Style style=components::Glyph::Style::Outlined)
Create an Icon as a child of a widget, or nothing when there is no glyph to show.
@ BodyLarge
16 / 24, regular.
Everything needed to set up a surface in one value: where it is, how it looks and how it arranges its...