-
Notifications
You must be signed in to change notification settings - Fork 16
/
root.cpp
369 lines (309 loc) · 9.35 KB
/
root.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <core/3d/gl.hpp>
#include "EditorFactory.hpp"
#include "IEditor.hpp"
#include "SettingsMenu.hpp"
#include "root.hpp"
#include <core/util/timestamp.hpp>
#include <frontend/Fonts.hpp>
#include <frontend/Localization.hpp>
#include <frontend/widgets/UnsavedProgress.hpp>
#include <frontend/widgets/fps.hpp>
#include <imcxx/Widgets.hpp>
#include <imgui_markdown.h>
#include <oishii/reader/binary_reader.hxx>
#include <oishii/writer/binary_writer.hxx>
#include <rsl/Discord.hpp>
#include <rsl/FsDialog.hpp>
#include <rsl/LeakDebug.hpp>
#include <rsl/Stb.hpp>
namespace llvm {
int EnableABIBreakingChecks;
int DisableABIBreakingChecks;
} // namespace llvm
bool gIsAdvancedMode = false;
bool IsAdvancedMode() { return gIsAdvancedMode; }
namespace libcube::UI {
void InstallCrate();
void ImageActionsInstaller();
} // namespace libcube::UI
namespace riistudio::frontend {
void DrawLocaleMenu() {
static int locale = 0;
int locale_opt = locale;
ImGui::RadioButton("English", &locale_opt, 0);
ImGui::RadioButton("日本語", &locale_opt, 1);
if (locale_opt != locale) {
locale = locale_opt;
const char* id_str = "English";
switch (locale) {
default:
case 0:
id_str = "English";
break;
case 1:
id_str = "Japanese";
break;
}
riistudio::SetLocale(id_str);
}
if (ImGui::Button("Dump Untranslated Strings"_j)) {
riistudio::DebugDumpLocaleMisses();
}
if (ImGui::Button("Reload Language .csv Files"_j)) {
riistudio::DebugReloadLocales();
}
}
RootWindow* RootWindow::spInstance;
void SetWindowIcon(void* platform_window, const char* path) {
#ifdef RII_BACKEND_GLFW
if (platform_window == nullptr) {
rsl::error("Failed to set icon: platform_window is NULL");
return;
}
GLFWwindow* window = reinterpret_cast<GLFWwindow*>(platform_window);
auto x = rsl::stb::load(path);
if (!x.has_value()) {
rsl::error("Failed to load icon");
return;
}
rsl::info("Setting icon: width={},height={},data={}", x->width, x->height,
reinterpret_cast<void*>(x->data.data()));
GLFWimage image;
image.width = x->width;
image.height = x->height;
image.pixels = x->data.data();
glfwSetWindowIcon(window, 1, &image);
#endif
}
static void MSVCWarningWindow() {
#if !defined(HAS_RUST_TRY)
ImGui::SetNextItemWidth(400);
if (ImGui::Begin("Warning", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
auto back = ImGui::GetCurrentWindow()->FontWindowScale;
ImGui::SetWindowFontScale(1.3f);
util::Markdown(
"Warning: This version of RiiStudio is built with Microsoft's Visual\n"
"C++ Compiler (MSVC). MSVC does not support a C++ language extention\n"
"called \"Statement Expressions\" that RiiStudio depends on. As such,\n"
"error handling is severely hampered and some features may just not\n"
"work. It's recommended you compile with the Clang compiler on\n"
"Windows. In Visual Studio: Tools > Get Tools and Features. On the\n"
"right-hand panel check \"C++ Clang tools for Windows\" and click\n"
"\"Install while downloading.\" When installed, at the top bar switch\n"
"the preset from msvc-x64-Debug to Clang-x64-debug (or clang-x64-DIST\n"
"for a final build).");
ImGui::SetWindowFontScale(back);
}
ImGui::End();
#endif // !defined(HAS_RUST_TRY)
#ifdef __EMSCRIPTEN__
ImGui::SetNextItemWidth(400);
static bool okp = true;
if (okp && ImGui::Begin("Warning", &okp, ImGuiWindowFlags_AlwaysAutoResize)) {
auto back = ImGui::GetCurrentWindow()->FontWindowScale;
ImGui::SetWindowFontScale(1.3f);
util::PushErrorSyle();
util::Markdown("Warning: The web version of RiiStudio is just a preview "
"and less frequently updated than the desktop builds "
"published on the Releases section.\n\nAccess the last "
"version at https://riidefi.github.io/RiiStudio/old (this "
"version's importer is not fully functional)");
util::PopErrorStyle();
ImGui::SetWindowFontScale(back);
}
ImGui::End();
#endif
}
void RootWindow::draw() {
rsl::DoLeakCheck();
auto on_file_open = [&](FileData x, OpenFilePolicy y) {
onFileOpen(std::move(x), y);
};
mFileHost.fileHostProcess(on_file_open);
if (auto* a = getActive()) {
if (auto* b = dynamic_cast<IEditor*>(a)) {
auto status = b->discordStatus();
mDiscordRpc.setStatus(std::move(status));
}
}
auto& io = ImGui::GetIO();
#ifdef RETINA_DEBUG
ImGui::Text("DEBUG: Display: %f, %f", io.DisplaySize.x, io.DisplaySize.y);
ImGui::Text("DEBUG: Apple Retina scaling: %f, %f",
io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
#endif
io.FontGlobalScale = mThemeData.mFontGlobalScale;
if (mThemeUpdated) {
// Reset theme; otherwise scale factors could compound later
ImGui::GetStyle() = {};
mTheme.setThemeEx(mThemeData.mTheme);
mThemeUpdated = false;
ImGui::GetStyle().ScaleAllSizes(mThemeData.mGlobalScale);
}
util::IDScope g(0);
if (imcxx::BeginFullscreenWindow("##RootWindow", getOpen())) {
drawStatusBar();
const u32 dockspace_id = ImGui::GetID("DockSpaceWidget");
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), 0);
drawMenuBar();
MSVCWarningWindow();
if (bDemo)
ImGui::ShowDemoWindow(&bDemo);
if (mCheckUpdate && mUpdater.IsOnline())
mUpdater.draw();
drawChildren();
if (mShowMkwDebug) {
mMkwDebugWindow.draw();
}
if (mShowSettings) {
DrawSettingsWindow();
}
}
imcxx::EndFullscreenWindow();
}
void RootWindow::drawStatusBar() {
ImGui::SetWindowFontScale(1.1f);
#ifndef NDEBUG
{
ImVec4 clr{1.0f, 0.0f, 0.0f, 1.0f};
ImGui::TextColored(
clr, "Warning: This is a Debug build with optimizations disabled");
}
#endif
if (!hasChildren()) {
ImGui::TextUnformatted("Drop a file to edit."_j);
}
ImGui::SetWindowFontScale(1.0f);
}
void RootWindow::drawMenuBar() {
// [File] [Settings] [Lang] (---> WindowWidth-60) [FPS]
if (ImGui::BeginMenuBar()) {
drawFileMenu();
drawSettingsMenu();
if (ImGui::BeginMenu("Tools"_j)) {
ImGui::Checkbox("Build Info"_j, &mShowSettings);
ImGui::Checkbox("MKW Debug (Dolphin)"_j, &mShowMkwDebug);
ImGui::EndMenu();
}
if (Fonts::IsJapaneseSupported())
drawLangMenu();
ImGui::SameLine(ImGui::GetWindowWidth() - 60);
DrawFps();
ImGui::EndMenuBar();
}
}
void RootWindow::drawLangMenu() {
if (ImGui::BeginMenu("日本語")) {
DrawLocaleMenu();
ImGui::EndMenu();
}
}
void RootWindow::drawSettingsMenu() {
if (ImGui::BeginMenu("Settings"_j)) {
bool _vsync = vsync;
ImGui::Checkbox("VSync"_j, &_vsync);
if (_vsync != vsync) {
setVsync(_vsync);
vsync = _vsync;
}
mThemeUpdated |= DrawThemeEditor(mThemeData, nullptr);
#ifdef BUILD_DEBUG
ImGui::Checkbox("ImGui Demo", &bDemo);
#endif
ImGui::Checkbox("Advanced Mode"_j, &gIsAdvancedMode);
ImGui::EndMenu();
}
}
void RootWindow::drawFileMenu() {
if (ImGui::BeginMenu("File"_j)) {
#if !defined(__EMSCRIPTEN__)
if (ImGui::MenuItem("Open"_j)) {
mFileHost.openFile();
}
#endif
if (ImGui::MenuItem("Save"_j)) {
saveButton();
}
#if !defined(__EMSCRIPTEN__)
if (ImGui::MenuItem("Save As"_j)) {
saveAsButton();
}
#endif
ImGui::EndMenu();
}
}
void RootWindow::onFileOpen(FileData data, OpenFilePolicy policy) {
rsl::info("Opening file: {}", data.mPath.c_str());
auto w = MakeEditor(data);
if (w) {
attachWindow(std::move(w));
return;
}
if (mWantFile) {
mReqData = std::move(data);
mGotFile = true;
return;
}
}
RootWindow::RootWindow()
: Applet(std::string("RiiStudio "_j) + RII_TIME_STAMP) {
spInstance = this;
// Loads the plugins for file formats / importers
libcube::UI::InstallCrate();
libcube::UI::ImageActionsInstaller();
// Without this, clicking in the viewport with a mouse would move the window
// when undocked.
ImGui::GetIO().ConfigWindowsMoveFromTitleBarOnly = true;
SetWindowIcon(getPlatformWindow(), "icon.png");
mDiscordRpc.connect();
{
auto brres = LoadLuigiCircuitSample();
if (brres) {
mFileHost.dropDirect(std::move(*brres), "./samples/luigi_circuit.brres");
}
}
#if 0
// iOS has 3:1 DPI
mThemeData.mGlobalScale = .534f;
mThemeData.mFontGlobalScale = .534f;
#endif
}
RootWindow::~RootWindow() = default;
void RootWindow::saveButton() {
if (getActive() == nullptr) {
rsl::ErrorDialog("Failed to save. Nothing is open");
return;
}
if (IEditor* b = dynamic_cast<IEditor*>(getActive())) {
b->saveButton();
return;
}
// In theory, you could handle custom types here implementing a different API.
rsl::ErrorDialog("Failed to save. Not saveable");
}
void RootWindow::saveAsButton() {
if (getActive() == nullptr) {
rsl::ErrorDialog("Failed to save. Nothing is open");
return;
}
if (IEditor* b = dynamic_cast<IEditor*>(getActive())) {
b->saveAsButton();
return;
}
// In theory, you could handle custom types here implementing a different API.
rsl::ErrorDialog("Failed to save. Not saveable");
}
bool RootWindow::shouldClose() {
if (!hasChildren()) {
return true;
}
auto choice = unsavedProgressBox();
if (choice == UnsavedProgressResult::CancelClose) {
return false;
}
if (choice == UnsavedProgressResult::Save) {
saveButton();
}
return true;
}
} // namespace riistudio::frontend