Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
menu.hpp
Go to the documentation of this file.
1
3#pragma once
4
9#include <aegis2d/ui/export.hpp>
11
12#include <cstddef>
13#include <cstdint>
14#include <optional>
15#include <string>
16#include <vector>
17
18namespace aegis2d::ui {
19
21enum class WidgetState : std::uint8_t {
23 Normal,
25 Focused,
27 Pressed,
30};
31
58
73class AEGIS2D_UI_EXPORT Menu {
74public:
77 explicit Menu(const MenuStyle& style = {});
78
85 WidgetId add_button(std::string text, render::Rect bounds, WidgetId group = root);
86
91 std::size_t add_label(std::string text, Vec2 position);
92
97 void set_text(WidgetId button, std::string text);
98
103 void set_label(std::size_t label, std::string text);
104
109 [[nodiscard]] const std::string& text(WidgetId button) const;
110
113 [[nodiscard]] WidgetTree& tree() noexcept;
114
117 [[nodiscard]] const WidgetTree& tree() const noexcept;
118
122 std::optional<WidgetId> update(const Navigation& navigation);
123
129 [[nodiscard]] WidgetState state(WidgetId button) const;
130
134 void draw(render::IRenderer& renderer, const render::BitmapFont& font) const;
135
136private:
137 struct Button {
138 WidgetId id;
139 std::string text;
140 };
141 struct Label {
142 std::string text;
143 Vec2 position;
144 };
145
146 MenuStyle style_;
147 WidgetTree tree_;
148 std::vector<Button> buttons_;
149 std::vector<Label> labels_;
150 bool held_ = false;
151};
152
153} // namespace aegis2d::ui
Text drawn with a bitmap font, through the sprite batcher.
Buttons and labels of a menu, on a WidgetTree.
Definition menu.hpp:73
WidgetTree & tree() noexcept
The tree of the buttons: focus, enabled buttons, scale.
Menu(const MenuStyle &style={})
Creates an empty menu.
WidgetId add_button(std::string text, render::Rect bounds, WidgetId group=root)
Adds a button.
const std::string & text(WidgetId button) const
Text of a button.
void set_label(std::size_t label, std::string text)
Changes the text of a label.
std::size_t add_label(std::string text, Vec2 position)
Adds a label, a text that never takes the focus.
void set_text(WidgetId button, std::string text)
Changes the text of a button.
Widgets of a menu or a form, in groups, and which one has the focus.
Definition widget_tree.hpp:72
RGBA color used by the renderer.
Vector and matrix types of the engine (GLM behind engine names).
constexpr Color rgb(std::uint32_t hex, float alpha=1.0F) noexcept
Color from its hexadecimal sRGB code, as design tools and palettes give it.
Definition color.hpp:25
Menus and forms: widgets and their focus, moved by keyboard, gamepad and mouse (client only).
Definition console.hpp:21
WidgetState
How a button looks, from what the player does with it.
Definition menu.hpp:21
@ Focused
Focused: activate uses it.
@ Disabled
Cannot be focused or used.
@ Pressed
Focused, with the activate key, button or the main mouse button held down.
std::size_t WidgetId
Identifier of a widget in its WidgetTree, given by WidgetTree::add().
Definition widget_tree.hpp:18
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
Axis-aligned rectangle.
Definition renderer.hpp:20
Colors, text size and layer of a menu.
Definition menu.hpp:36
render::Color pressed
Background of the pressed button.
Definition menu.hpp:42
render::Color disabled
Background of a disabled button.
Definition menu.hpp:44
render::Color focused
Background of the focused button.
Definition menu.hpp:40
std::int32_t layer
Layer of the backgrounds; the text goes one layer above.
Definition menu.hpp:56
float outline_width
Width of the outline, in logical pixels at 100%.
Definition menu.hpp:52
render::Color button
Background of a button in the Normal state.
Definition menu.hpp:38
render::Color disabled_text
Text of a disabled button.
Definition menu.hpp:48
float text_scale
Scale of the text at 100%, multiplied by the tree's scale.
Definition menu.hpp:54
render::Color outline
Outline around the focused button.
Definition menu.hpp:50
render::Color text
Text of the buttons and labels.
Definition menu.hpp:46
What the player asked of the focus during a frame.
Definition widget_tree.hpp:37
Widgets of a menu or a form, and which one has the focus.