-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.hpp
260 lines (218 loc) · 8.33 KB
/
ui.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (c) Adubbz
* Copyright (c) Ixaruz
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, or any later version, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <array>
#include <memory>
#include <vector>
#include <optional>
#include <nanovg.h>
#include <switch.h>
#include "ui_util.hpp"
#include "util.h"
namespace dbk {
struct Button {
static constexpr u32 InvalidButtonId = -1;
u32 id;
bool selected;
bool enabled;
char text[256];
float x;
float y;
float w;
float h;
inline bool IsPositionInBounds(float x, float y) {
return x >= this->x && y >= this->y && x < (this->x + this->w) && y < (this->y + this->h);
}
};
class LogEntry {
public:
LogEntry(std::string text, TextStyle style) : m_text(text), m_style(style)
{}
virtual void Draw(NVGcontext* vg) {
if (this->m_properties_set) {
DrawTextAlign(vg, this->m_x, this->m_y, this->m_w, this->m_text.c_str(), this->m_style);
}
}
inline bool IsEntryInBounds(float low_y, float high_y) { return this->m_y >= low_y && this->m_y <= high_y; }
void setProperties(float x, float y, float w) {
this->m_x = x;
this->m_y = y;
this->m_w = w;
this->m_properties_set = true;
}
inline std::string getText() { return this->m_text; }
inline void setText(std::string text) { this->m_text = text; }
protected:
std::string m_text = "";
float m_x;
float m_y;
float m_w;
bool m_properties_set = false;
TextStyle m_style;
};
enum class Direction {
Up,
Down,
Left,
Right,
Invalid,
};
class Menu {
protected:
static constexpr size_t MaxButtons = 32;
static constexpr size_t LogBufferSize = 0x1000;
protected:
std::array<std::optional<Button>, MaxButtons> m_buttons;
const std::shared_ptr<Menu> m_prev_menu;
char m_log_buffer[LogBufferSize];
protected:
void AddButton(u32 id, const char *text, float x, float y, float w, float h);
void SetButtonSelected(u32 id, bool selected);
void DeselectAllButtons();
void SetButtonEnabled(u32 id, bool enabled);
Button *GetButton(u32 id);
Button *GetSelectedButton();
Button *GetClosestButtonToSelection(Direction direction);
Button *GetTouchedButton();
Button *GetActivatedButton();
void UpdateButtons();
void DrawButtons(NVGcontext *vg, u64 ns);
public:
Menu(std::shared_ptr<Menu> prev_menu) : m_buttons({}), m_prev_menu(prev_menu), m_log_buffer{} { /* ... */ }
void LogText(const char* format, ...);
std::shared_ptr<Menu> GetPrevMenu();
virtual void Update(u64 ns) = 0;
virtual void Draw(NVGcontext *vg, u64 ns) = 0;
};
class AlertMenu : public Menu {
protected:
static constexpr float WindowWidth = 600.0f;
static constexpr float WindowHeight = 240.0f;
static constexpr float TitleGap = 100.0f;
static constexpr float SubTextHeight = 40.0f;
protected:
char m_text[0x100];
char m_subtext[0x100];
char m_result_text[0x30];
char m_format_text[0x20];
u64 m_rc;
public:
AlertMenu(std::shared_ptr<Menu> prev_menu, const char *text, const char *subtext, const char *format = "Result: 0x%08X", u64 rc = 0);
virtual void Draw(NVGcontext *vg, u64 ns) override;
};
class ErrorMenu : public AlertMenu {
private:
static constexpr u32 ExitButtonId = 0;
public:
ErrorMenu(const char *text, const char *subtext, const char *format = "Result: 0x%08X", u64 rc = 0);
virtual void Update(u64 ns) override;
};
class WarningMenu : public AlertMenu {
private:
static constexpr u32 BackButtonId = 0;
static constexpr u32 ContinueButtonId = 1;
private:
const std::shared_ptr<Menu> m_next_menu;
public:
WarningMenu(std::shared_ptr<Menu> prev_menu, std::shared_ptr<Menu> next_menu, const char *text, const char *subtext, const char *format = "Result: 0x%08X", u64 rc = 0);
virtual void Update(u64 ns) override;
};
class MainMenu : public Menu {
private:
static constexpr u32 DumpButtonId = 0;
static constexpr u32 ExitButtonId = 1;
static constexpr float WindowWidth = 400.0f;
static constexpr float WindowHeight = 240.0f;
static constexpr float TitleGap = 90.0f;
protected:
void gotoNextMenu(u64 ns);
public:
MainMenu(const char* islandname = "");
virtual void Update(u64 ns) override;
virtual void Draw(NVGcontext *vg, u64 ns) override;
};
class DumpingMenu : public Menu {
private:
static constexpr u32 ExitButtonId = 0;
static constexpr size_t MaxFileRows = 11;
static constexpr float WindowWidth = 600.0f;
static constexpr float WindowHeight = 600.0f;
static constexpr float TitleGap = 90.0f;
static constexpr float ProgressTextHeight = 20.0f;
static constexpr float ProgressBarHeight = 30.0f;
static constexpr float LogPadding = 10.0f;
static constexpr float TextAreaHeight = 348.0f;
static constexpr float LogRowHeight = 20.0f;
static constexpr float LogRowGap = 6.0f;
static constexpr float LogRowHorizontalInset = 10.0f;
static constexpr float LogListHeight = MaxFileRows * (LogRowHeight + LogRowGap);
static constexpr size_t UpdateTaskBufferSize = 0x100000;
private:
void InterpretInput();
Result TransitionDumpState(u64 ns);
float getTopBound();
float getBottomBound();
float getLeftBound();
float getRightBound();
public:
enum class DumpState {
NeedsDraw,
NeedsSetup,
NeedsWait,
NeedsDecrypt,
NeedsRW,
NeedsFix,
NeedsSave,
End,
};
std::vector<LogEntry*> m_log_entries;
//offset of entire log
float m_offset = 0, m_nextOffset = 0;
float m_logHeight = 0;
float m_speed = 0;
float m_speedmax = 0.4f;
float m_speedmin = 0.1f;
bool m_scrolling = false;
void EnableButtons();
int LogAddLine(std::string text, int index = -1, bool scroll = true, TextStyle style = TextStyle::LeftAlign);
void LogEditLastElement(std::string text);
void ScrollTo(float offset) {
this->m_nextOffset = offset;
}
void ScrollDown() {
this->m_nextOffset = this->m_logHeight - this->TextAreaHeight + this->LogRowHorizontalInset;
}
void ScrollUp() {
this->m_nextOffset = 0;
}
DumpState m_dumping_state;
AsyncResult m_prepare_result;
Debugger* m_debugger = nullptr;
float m_progress_percent = 0.0f;
float m_progress_nextPercent = 0.0f;
bool m_enable_buttons = false;
DumpingMenu(std::shared_ptr<Menu> prev_menu);
virtual void Update(u64 ns) override;
virtual void Draw(NVGcontext *vg, u64 ns) override;
};
void showErrorMenu(Error error, std::string additional_info = "");
void StarRandomizer();
bool InitializeMenu(u32 screen_width, u32 screen_height);
void UpdateMenu(u64 ns);
void RenderMenu(NVGcontext *vg, u64 ns);
bool IsExitRequested();
}