Plutonium framework API 0.3.0
UI framework libraries for libnx
Loading...
Searching...
No Matches
elm_Menu.hpp
Go to the documentation of this file.
1
2/*
3
4 Plutonium library
5
6 @file Menu.hpp
7 @brief A Menu is a very useful Element for option browsing or selecting.
8 @author XorTroll
9
10 @copyright Plutonium project - an easy-to-use UI framework for Nintendo Switch homebrew
11
12*/
13
14#pragma once
15#include <pu/ui/elm/elm_Element.hpp>
16#include <chrono>
17#include <functional>
18
19namespace pu::ui::elm {
20
21 class MenuItem {
22 public:
23 using OnKeyCallback = std::function<void()>;
24 static constexpr Color DefaultColor = { 10, 10, 10, 0xFF };
25
26 private:
27 std::string name;
28 Color clr;
29 sdl2::TextureHandle::Ref icon;
30 std::vector<OnKeyCallback> on_key_cbs;
31 std::vector<u64> on_key_cb_keys;
32
33 public:
34 MenuItem(const std::string &name) : name(name), clr(DefaultColor) {}
36
37 inline std::string GetName() {
38 return this->name;
39 }
40
41 inline void SetName(const std::string &name) {
42 this->name = name;
43 }
44
45 PU_CLASS_POD_GETSET(Color, clr, Color)
46
47 void AddOnKey(OnKeyCallback on_key_cb, const u64 key = HidNpadButton_A);
48
50 return this->on_key_cbs.size();
51 }
52
53 inline OnKeyCallback GetOnKeyCallback(const u32 idx) {
54 if(idx < this->on_key_cbs.size()) {
55 return this->on_key_cbs.at(idx);
56 }
57 else {
58 return {};
59 }
60 }
61
62 inline u64 GetOnKeyCallbackKey(const u32 idx) {
63 if(idx < this->on_key_cb_keys.size()) {
64 return this->on_key_cb_keys.at(idx);
65 }
66 else {
67 return {};
68 }
69 }
70
72 return this->icon;
73 }
74
75 void SetIcon(sdl2::TextureHandle::Ref icon);
76
77 inline bool HasIcon() {
78 return this->icon != nullptr;
79 }
80 };
81
82 class Menu : public Element {
83 public:
84 static constexpr Color DefaultScrollbarColor = { 110, 110, 110, 0xFF };
85
86 static constexpr u8 DefaultItemAlphaIncrementSteps = 15;
87
88 static constexpr float DefaultIconItemSizesFactor = 0.8f;
89
90 static constexpr u32 DefaultIconMargin = 37;
91 static constexpr u32 DefaultTextMargin = 37;
92
93 static constexpr u8 DefaultLightScrollbarColorFactor = 30;
94
95 static constexpr u32 DefaultScrollbarWidth = 30;
96
97 static constexpr u32 DefaultShadowHeight = 7;
98 static constexpr u8 DefaultShadowBaseAlpha = 160;
99
100 static constexpr s64 DefaultMoveWaitTimeMs = 150;
101
102 enum class MoveStatus : u8 {
103 None = 0,
104 WaitingUp = 1,
105 WaitingDown = 2
106 };
107
108 using OnSelectionChangedCallback = std::function<void()>;
109
110 private:
111 i32 x;
112 i32 y;
113 i32 w;
114 i32 items_h;
115 u32 items_to_show;
116 u32 selected_item_idx;
117 i32 selected_item_alpha;
118 SigmoidIncrementer<i32> selected_item_alpha_incr;
119 i32 prev_selected_item_idx;
120 i32 prev_selected_item_alpha;
121 SigmoidIncrementer<i32> prev_selected_item_alpha_incr;
122 u32 advanced_item_count;
123 Color scrollbar_clr;
124 Color items_clr;
125 Color items_focus_clr;
126 bool cooldown_enabled;
127 bool item_touched;
128 MoveStatus move_status;
129 std::chrono::time_point<std::chrono::steady_clock> move_start_time;
130 OnSelectionChangedCallback on_selection_changed_cb;
131 std::vector<MenuItem::Ref> items;
132 std::string font_name;
133 std::vector<sdl2::Texture> loaded_name_texs;
134 u8 item_alpha_incr_steps;
135 float icon_item_sizes_factor;
136 u32 icon_margin;
137 u32 text_margin;
138 u8 light_scrollbar_color_factor;
139 u32 scrollbar_width;
140 u32 shadow_height;
141 u8 shadow_base_alpha;
142 s64 move_wait_time_ms;
143
144 void ReloadItemRenders();
145 void MoveUp();
146 void MoveDown();
147
148 inline Color MakeItemsFocusColor(const u8 alpha) {
149 return this->items_focus_clr.WithAlpha(alpha);
150 }
151
152 inline constexpr Color MakeLighterScrollbarColor() {
153 i32 base_r = this->scrollbar_clr.r - this->light_scrollbar_color_factor;
154 if(base_r < 0) {
155 base_r = 0;
156 }
157 i32 base_g = this->scrollbar_clr.g - this->light_scrollbar_color_factor;
158 if(base_g < 0) {
159 base_g = 0;
160 }
161 i32 base_b = this->scrollbar_clr.b - this->light_scrollbar_color_factor;
162 if(base_b < 0) {
163 base_b = 0;
164 }
165
166 return { static_cast<u8>(base_r), static_cast<u8>(base_g), static_cast<u8>(base_b), this->scrollbar_clr.a };
167 }
168
169 inline void HandleOnSelectionChanged() {
170 if(this->on_selection_changed_cb) {
171 (this->on_selection_changed_cb)();
172 }
173 }
174
175 inline void RunSelectedItemCallback(const u64 keys) {
176 auto item = this->items.at(this->selected_item_idx);
177 const auto cb_count = item->GetOnKeyCallbackCount();
178 for(u32 i = 0; i < cb_count; i++) {
179 if(keys & item->GetOnKeyCallbackKey(i)) {
180 if(!this->cooldown_enabled) {
181 auto cb = item->GetOnKeyCallback(i);
182 if(cb) {
183 cb();
184 }
185 }
186 }
187 }
188 this->cooldown_enabled = false;
189 }
190
191 inline u32 GetItemCount() {
192 auto item_count = this->items_to_show;
193 if(item_count > this->items.size()) {
194 item_count = this->items.size();
195 }
196 if((item_count + this->advanced_item_count) > this->items.size()) {
197 item_count = this->items.size() - this->advanced_item_count;
198 }
199 return item_count;
200 }
201
202 public:
203 Menu(const i32 x, const i32 y, const i32 width, const Color items_clr, const Color items_focus_clr, const i32 items_height, const u32 items_to_show);
205
206 inline i32 GetX() override {
207 return this->x;
208 }
209
210 inline void SetX(const i32 x) {
211 this->x = x;
212 }
213
214 inline i32 GetY() override {
215 return this->y;
216 }
217
218 inline void SetY(const i32 y) {
219 this->y = y;
220 }
221
222 inline i32 GetWidth() override {
223 return this->w;
224 }
225
226 inline void SetWidth(const i32 width) {
227 this->w = width;
228 }
229
230 inline i32 GetHeight() override {
231 return this->items_h * this->items_to_show;
232 }
233
234 PU_CLASS_POD_GETSET(ItemsHeight, items_h, i32)
235 PU_CLASS_POD_GETSET(NumberOfItemsToShow, items_to_show, i32)
236 PU_CLASS_POD_GETSET(ItemsFocusColor, items_focus_clr, Color)
237 PU_CLASS_POD_GETSET(ItemsColor, items_clr, Color)
238 PU_CLASS_POD_GETSET(ScrollbarColor, scrollbar_clr, Color)
239 PU_CLASS_POD_GETSET(ItemAlphaIncrementSteps, item_alpha_incr_steps, u8)
240 PU_CLASS_POD_GETSET(IconItemSizesFactor, icon_item_sizes_factor, float)
241 PU_CLASS_POD_GETSET(IconMargin, icon_margin, u32)
242 PU_CLASS_POD_GETSET(TextMargin, text_margin, u32)
243 PU_CLASS_POD_GETSET(LightScrollbarColorFactor, light_scrollbar_color_factor, u8)
244 PU_CLASS_POD_GETSET(ScrollbarWidth, scrollbar_width, u32)
245 PU_CLASS_POD_GETSET(ShadowHeight, shadow_height, u32)
246 PU_CLASS_POD_GETSET(ShadowBaseAlpha, shadow_base_alpha, u8)
247 PU_CLASS_POD_GETSET(MoveWaitTimeMs, move_wait_time_ms, s64)
248
249 inline void SetOnSelectionChanged(OnSelectionChangedCallback on_selection_changed_cb) {
250 this->on_selection_changed_cb = on_selection_changed_cb;
251 }
252
253 inline void AddItem(MenuItem::Ref &item) {
254 this->items.push_back(item);
255 }
256
258
259 inline void ForceReloadItems() {
260 this->ReloadItemRenders();
261 }
262
263 PU_CLASS_POD_SET(CooldownEnabled, cooldown_enabled, bool)
264
265 inline MenuItem::Ref &GetSelectedItem() {
266 return this->items.at(this->selected_item_idx);
267 }
268
270 return this->items;
271 }
272
273 PU_CLASS_POD_GET(SelectedIndex, selected_item_idx, i32)
274
275 void SetSelectedIndex(const u32 idx);
276
277 void OnRender(render::Renderer::Ref &drawer, const i32 x, const i32 y) override;
278 void OnInput(const u64 keys_down, const u64 keys_up, const u64 keys_held, const TouchPoint touch_pos) override;
279 };
280}
Definition sdl2_Types.hpp:18
Definition elm_Element.hpp:37
Definition elm_Menu.hpp:82
void SetY(const i32 y)
Definition elm_Menu.hpp:218
MenuItem::Ref & GetSelectedItem()
Definition elm_Menu.hpp:265
static constexpr u32 DefaultScrollbarWidth
Definition elm_Menu.hpp:95
void AddItem(MenuItem::Ref &item)
Definition elm_Menu.hpp:253
static constexpr float DefaultIconItemSizesFactor
Definition elm_Menu.hpp:88
static constexpr u32 DefaultShadowHeight
Definition elm_Menu.hpp:97
static constexpr u32 DefaultIconMargin
Definition elm_Menu.hpp:90
MoveStatus
Definition elm_Menu.hpp:102
static constexpr Color DefaultScrollbarColor
Definition elm_Menu.hpp:84
void SetX(const i32 x)
Definition elm_Menu.hpp:210
i32 GetHeight() override
Definition elm_Menu.hpp:230
i32 GetWidth() override
Definition elm_Menu.hpp:222
void SetOnSelectionChanged(OnSelectionChangedCallback on_selection_changed_cb)
Definition elm_Menu.hpp:249
static constexpr u8 DefaultShadowBaseAlpha
Definition elm_Menu.hpp:98
static constexpr u32 DefaultTextMargin
Definition elm_Menu.hpp:91
static constexpr s64 DefaultMoveWaitTimeMs
Definition elm_Menu.hpp:100
void OnInput(const u64 keys_down, const u64 keys_up, const u64 keys_held, const TouchPoint touch_pos) override
std::vector< MenuItem::Ref > & GetItems()
Definition elm_Menu.hpp:269
void SetWidth(const i32 width)
Definition elm_Menu.hpp:226
void OnRender(render::Renderer::Ref &drawer, const i32 x, const i32 y) override
static constexpr u8 DefaultItemAlphaIncrementSteps
Definition elm_Menu.hpp:86
i32 GetY() override
Definition elm_Menu.hpp:214
static constexpr u8 DefaultLightScrollbarColorFactor
Definition elm_Menu.hpp:93
void ForceReloadItems()
Definition elm_Menu.hpp:259
i32 GetX() override
Definition elm_Menu.hpp:206
void SetSelectedIndex(const u32 idx)
Menu(const i32 x, const i32 y, const i32 width, const Color items_clr, const Color items_focus_clr, const i32 items_height, const u32 items_to_show)
Definition elm_Menu.hpp:21
OnKeyCallback GetOnKeyCallback(const u32 idx)
Definition elm_Menu.hpp:53
void SetName(const std::string &name)
Definition elm_Menu.hpp:41
bool HasIcon()
Definition elm_Menu.hpp:77
u32 GetOnKeyCallbackCount()
Definition elm_Menu.hpp:49
sdl2::TextureHandle::Ref GetIconTexture()
Definition elm_Menu.hpp:71
void AddOnKey(OnKeyCallback on_key_cb, const u64 key=HidNpadButton_A)
static constexpr Color DefaultColor
Definition elm_Menu.hpp:24
u64 GetOnKeyCallbackKey(const u32 idx)
Definition elm_Menu.hpp:62
void SetIcon(sdl2::TextureHandle::Ref icon)
MenuItem(const std::string &name)
Definition elm_Menu.hpp:34
std::string GetName()
Definition elm_Menu.hpp:37
Definition render_Renderer.hpp:127
Definition sdl2_Types.hpp:10
Definition render_Renderer.hpp:20
Definition elm_Button.hpp:18
#define PU_SMART_CTOR(type)
Definition pu_Include.hpp:20
#define PU_CLASS_POD_GETSET(fn_name, var_name, type)
Definition pu_Include.hpp:37
#define PU_CLASS_POD_SET(fn_name, var_name, type)
Definition pu_Include.hpp:32
#define PU_CLASS_POD_GET(fn_name, var_name, type)
Definition pu_Include.hpp:27
Definition ui_Types.hpp:44
Definition ui_Types.hpp:67