guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
selection.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/systems/selection.hpp"
24
25#include "guillaume/components/hierarchy.hpp"
26#include "guillaume/ecs/entity.hpp"
27
28namespace guillaume::systems
29{
31 : ecs::SystemFiller<components::SelectionGroup, components::Transform,
32 components::Bound>(ecs::Phase::Event)
33 {
34 }
35
36 void Selection::update(const ecs::Entity::Identifier &entityIdentifier)
37 {
38 if (!requireComponent<components::SelectionGroup>(entityIdentifier)) {
39 return;
40 }
41
42 // The group's members are its tree children, read from the Hierarchy
43 // component: no entity object is needed.
45 entityIdentifier)) {
46 return;
47 }
48 const auto childIdentifiers =
50 .getChildIdentifiers();
51
52 reconcile(getComponentRegistry(), entityIdentifier, childIdentifiers);
53 }
54
56 ecs::ComponentRegistry &registry,
57 const ecs::Entity::Identifier &groupIdentifier,
58 const std::vector<ecs::Entity::Identifier> &childIdentifiers)
59 {
60 auto &group =
61 registry.getComponent<components::SelectionGroup>(groupIdentifier);
62
64
65 for (const auto &childIdentifier: childIdentifiers) {
67 childIdentifier)) {
68 continue;
69 }
70
71 auto &selectable =
72 registry.getComponent<components::Selectable>(childIdentifier);
73
74 if (!selectable.isSelected()) {
75 continue;
76 }
77
78 if (selected == ecs::Entity::InvalidIdentifier) {
79 selected = childIdentifier;
80 } else {
81 selectable.setSelected(false);
82 }
83 }
84
85 if (selected != ecs::Entity::InvalidIdentifier) {
86 group.setSelected(selected);
87 return group.getSelected();
88 }
89
90 // No child is selected. Restore the previous selection when it is still
91 // part of the group, otherwise select the first selectable child when
92 // the group does not allow an empty selection.
93 const auto previous = group.getSelected();
94 if (previous != ecs::Entity::InvalidIdentifier
95 && registry.hasComponent<components::Selectable>(previous)) {
96 registry.getComponent<components::Selectable>(previous).setSelected(
97 true);
98 return previous;
99 }
100
101 if (!group.isEmptyAllowed()) {
102 for (const auto &childIdentifier: childIdentifiers) {
104 childIdentifier)) {
105 registry
106 .getComponent<components::Selectable>(childIdentifier)
107 .setSelected(true);
108 group.setSelected(childIdentifier);
109 return childIdentifier;
110 }
111 }
112 }
113
114 group.clearSelection();
116 }
117
119 ecs::ComponentRegistry &registry,
120 const ecs::Entity::Identifier &groupIdentifier,
121 const std::vector<ecs::Entity::Identifier> &childIdentifiers,
122 ecs::Entity::Identifier selectedIdentifier)
123 {
124 for (const auto &childIdentifier: childIdentifiers) {
126 childIdentifier)) {
127 continue;
128 }
129
130 registry.getComponent<components::Selectable>(childIdentifier)
131 .setSelected(childIdentifier == selectedIdentifier);
132 }
133
134 return reconcile(registry, groupIdentifier, childIdentifiers);
135 }
136
137} // namespace guillaume::systems
Selected/unselected flag of an entity, with a callback run when it changes.
Definition selection.hpp:57
Marks an entity whose Selectable children are mutually exclusive, and remembers which one is selected...
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...
Definition entity.hpp:86
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
bool hasComponent(const ecs::Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component, in the bound registry.
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...
Selection(void)
Create the system, in the Event phase.
Definition selection.cpp:30
static ecs::Entity::Identifier select(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &groupIdentifier, const std::vector< ecs::Entity::Identifier > &childIdentifiers, ecs::Entity::Identifier selectedIdentifier)
Select one child of a group, at once.
static ecs::Entity::Identifier reconcile(ecs::ComponentRegistry &registry, const ecs::Entity::Identifier &groupIdentifier, const std::vector< ecs::Entity::Identifier > &childIdentifiers)
Make a group and its children agree on which child is selected.
Definition selection.cpp:55
void update(const ecs::Entity::Identifier &entityIdentifier) override
Reconcile one group with the children listed in its components::Hierarchy.
Definition selection.cpp:36
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Rules. Every class here runs each frame on the entities that have the components it declares.