Skip to main content

MenuComponent.ixx File

Component for managing UI menu structures. More...

Included Headers

#include <span> #include <vector> #include <helios.engine.ecs.EntityHandle> #include <helios.engine.ecs.GameObject> #include <helios.engine.modules.ui.widgets.types.MenuId>

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacemodules

Domain-specific components and systems. More...

namespaceui

User interface components and systems for game entities. More...

namespacewidgets

UI widget components and systems. More...

namespacecomponents

UI widget state components. More...

Classes Index

classMenuComponent

Manages a collection of menu items for UI navigation. More...

Description

Component for managing UI menu structures.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file MenuComponent.ixx
3 * @brief Component for managing UI menu structures.
4 */
5module;
6
7#include <span>
8#include <vector>
9
10export module helios.engine.modules.ui.widgets.components.MenuComponent;
11
12import helios.engine.modules.ui.widgets.types.MenuId;
13import helios.engine.ecs.GameObject;
14import helios.engine.ecs.EntityHandle;
15
17
18/**
19 * @brief Manages a collection of menu items for UI navigation.
20 *
21 * @details Stores entity handles to menu items and tracks the currently
22 * selected item index. Supports dirty tracking for efficient updates.
23 *
24 * @see MenuDisplaySystem
25 * @see MenuNavigationSystem
26 */
28
29 /**
30 * @brief Entity handles of the menu items.
31 */
32 std::vector<helios::engine::ecs::EntityHandle> menuItems_;
33
34 /**
35 * @brief Unique identifier for this menu.
36 */
38
39 /**
40 * @brief Currently selected item index.
41 */
42 size_t selectedIndex_ = 0;
43
44 /**
45 * @brief Previously selected item index.
46 */
47 size_t previousSelectedIndex_ = 0;
48
49 /**
50 * @brief Default selection index when menu is focused.
51 */
52 size_t defaultSelectedIndex_ = 0;
53
54 /**
55 * @brief Whether the menu state has changed.
56 */
57 bool isDirty_ = true;
58
59public:
60
61 /**
62 * @brief Sets the menu identifier.
63 *
64 * @param id The menu ID.
65 */
67 menuId_ = id;
68 }
69
70 /**
71 * @brief Returns the menu identifier.
72 *
73 * @return The MenuId for this menu.
74 */
76 return menuId_;
77 }
78
79 /**
80 * @brief Sets the currently selected item index.
81 *
82 * @details Marks the menu as dirty if the index changes.
83 *
84 * @param index The index to select.
85 */
86 void setSelectedIndex(const size_t index) noexcept {
87 if (index != selectedIndex_) {
88 markDirty();
89 }
90 previousSelectedIndex_ = selectedIndex_;
91 selectedIndex_ = index;;
92 }
93
94 /**
95 * @brief Resets selection to the default index.
96 */
98 setSelectedIndex(defaultSelectedIndex_);
99 }
100
101 /**
102 * @brief Returns the currently selected item index.
103 *
104 * @return The selected index.
105 */
106 [[nodiscard]] size_t selectedIndex() const noexcept {
107 return selectedIndex_;
108 }
109
110
111 /**
112 * @brief Adds a menu item to the end of the list.
113 *
114 * @param menuItem The GameObject to add as a menu item.
115 */
117 markDirty();
118 menuItems_.push_back(menuItem.entityHandle());
119 }
120
121 /**
122 * @brief Returns a span of all menu item handles.
123 *
124 * @return Span of EntityHandle for all menu items.
125 */
126 [[nodiscard]] std::span<helios::engine::ecs::EntityHandle> menuItems() noexcept {
127 return menuItems_;
128 }
129
130 /**
131 * @brief Clears the dirty flag.
132 */
133 void clearDirty() {
134 isDirty_ = false;
135 }
136
137 /**
138 * @brief Marks the menu as dirty.
139 */
140 void markDirty() {
141 isDirty_ = true;
142 }
143
144 /**
145 * @brief Returns whether the menu state has changed.
146 *
147 * @return True if dirty.
148 */
149 [[nodiscard]] bool isDirty() const noexcept {
150 return isDirty_;
151 }
152
153 /**
154 * @brief Returns the index of the previous selected menu index.
155 *
156 * @return The index of the previous selected menu item.
157 */
158 [[nodiscard]] size_t previousSelectedIndex() const noexcept {
159 return previousSelectedIndex_;
160 }
161
162 /**
163 * @brief Inserts a menu item at a specific index.
164 *
165 * @details Resizes the internal vector if necessary. If the item
166 * already exists at the index, no action is taken.
167 *
168 * @param menuItem The GameObject to insert.
169 * @param index The index at which to insert.
170 */
171 void insert(const helios::engine::ecs::GameObject menuItem, const size_t index) {
172 if (index >= menuItems_.size()) {
173 menuItems_.resize(index + 1);
174 }
175
176 for (size_t i = 0; i < menuItems_.size(); i++) {
177 if (i == index && menuItems_[i] == menuItem.entityHandle()) {
178 return;
179 }
180 }
181
182 const auto old = menuItems_[index];
183 menuItems_[index] = menuItem.entityHandle();
184 menuItems_.push_back(old);
185 }
186};
187
188} // namespace helios::engine::modules::ui::widgets::components

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.