From 646db9607a6352004e5bd1f2bfdc3e0170e5ec1a Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 26 Jan 2025 19:28:40 -0500 Subject: [PATCH] Refactor Controller and File Dialog: streamline platform handling, remove redundant parameters, and enhance initialization logic --- src/app/core/controller.cc | 81 +++++++++++++--------------- src/app/core/controller.h | 11 +--- src/app/core/platform/file_dialog.cc | 23 ++++++-- src/app/core/platform/file_dialog.h | 6 +-- src/app/gui/zeml.h | 13 ++--- src/ios/main.mm | 29 +++++----- 6 files changed, 76 insertions(+), 87 deletions(-) diff --git a/src/app/core/controller.cc b/src/app/core/controller.cc index 1280ab1c..f2b07b22 100644 --- a/src/app/core/controller.cc +++ b/src/app/core/controller.cc @@ -7,10 +7,10 @@ #include "absl/status/status.h" #include "absl/strings/str_format.h" +#include "app/core/platform/file_dialog.h" #include "app/core/platform/font_loader.h" #include "app/editor/editor_manager.h" #include "app/gui/style.h" -#include "app/core/platform/file_dialog.h" #include "imgui/backends/imgui_impl_sdl2.h" #include "imgui/backends/imgui_impl_sdlrenderer2.h" #include "imgui/imgui.h" @@ -19,26 +19,17 @@ namespace yaze { namespace core { absl::Status Controller::OnEntry(std::string filename) { -#if defined(__APPLE__) && defined(__MACH__) -#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1 - platform_ = Platform::kiOS; -#elif TARGET_OS_MAC == 1 - platform_ = Platform::kMacOS; -#endif -#elif defined(_WIN32) - platform_ = Platform::kWindows; -#elif defined(__linux__) - platform_ = Platform::kLinux; -#else - platform_ = Platform::kUnknown; -#endif RETURN_IF_ERROR(CreateWindow()) RETURN_IF_ERROR(CreateRenderer()) RETURN_IF_ERROR(CreateGuiContext()) RETURN_IF_ERROR(LoadAudioDevice()) + Initialize(filename); + return absl::OkStatus(); +} + +void Controller::Initialize(std::string filename) { editor_manager_.Initialize(filename); active_ = true; - return absl::OkStatus(); } void Controller::OnInput() { @@ -49,30 +40,30 @@ void Controller::OnInput() { while (SDL_PollEvent(&event)) { ImGui_ImplSDL2_ProcessEvent(&event); switch (event.type) { - case SDL_KEYDOWN: - case SDL_KEYUP: { - ImGuiIO &io = ImGui::GetIO(); - io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); - io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); - io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); - io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); - break; - } - case SDL_WINDOWEVENT: - switch (event.window.event) { - case SDL_WINDOWEVENT_CLOSE: - active_ = false; + case SDL_KEYDOWN: + case SDL_KEYUP: { + ImGuiIO &io = ImGui::GetIO(); + io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); + io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); + io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); + io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); break; - case SDL_WINDOWEVENT_SIZE_CHANGED: - io.DisplaySize.x = static_cast(event.window.data1); - io.DisplaySize.y = static_cast(event.window.data2); + } + case SDL_WINDOWEVENT: + switch (event.window.event) { + case SDL_WINDOWEVENT_CLOSE: + active_ = false; + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: + io.DisplaySize.x = static_cast(event.window.data1); + io.DisplaySize.y = static_cast(event.window.data2); + break; + default: + break; + } break; default: break; - } - break; - default: - break; } } @@ -153,12 +144,12 @@ absl::Status Controller::CreateWindow() { int screen_height = display_mode.h * 0.8; window_ = std::unique_ptr( - SDL_CreateWindow("Yet Another Zelda3 Editor", // window title - SDL_WINDOWPOS_UNDEFINED, // initial x position - SDL_WINDOWPOS_UNDEFINED, // initial y position - screen_width, // width, in pixels - screen_height, // height, in pixels - SDL_WINDOW_RESIZABLE), + SDL_CreateWindow("Yet Another Zelda3 Editor", // window title + SDL_WINDOWPOS_UNDEFINED, // initial x position + SDL_WINDOWPOS_UNDEFINED, // initial y position + screen_width, // width, in pixels + screen_height, // height, in pixels + SDL_WINDOW_RESIZABLE), core::SDL_Deleter()); if (window_ == nullptr) { return absl::InternalError( @@ -208,7 +199,7 @@ absl::Status Controller::LoadAudioDevice() { want.format = AUDIO_S16; want.channels = 2; want.samples = 2048; - want.callback = NULL; // Uses the queue + want.callback = NULL; // Uses the queue audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); if (audio_device_ == 0) { return absl::InternalError( @@ -225,7 +216,7 @@ absl::Status Controller::LoadAudioDevice() { absl::Status Controller::LoadConfigFiles() { // Create and load a dotfile for the application // This will store the user's preferences and settings - std::string config_directory = GetConfigDirectory(platform_); + std::string config_directory = GetConfigDirectory(); // Create the directory if it doesn't exist if (!std::filesystem::exists(config_directory)) { @@ -250,5 +241,5 @@ absl::Status Controller::LoadConfigFiles() { return absl::OkStatus(); } -} // namespace core -} // namespace yaze +} // namespace core +} // namespace yaze diff --git a/src/app/core/controller.h b/src/app/core/controller.h index 21602eb3..2eb1e30f 100644 --- a/src/app/core/controller.h +++ b/src/app/core/controller.h @@ -6,8 +6,8 @@ #include #include "absl/status/status.h" -#include "app/core/platform/renderer.h" #include "app/core/platform/file_dialog.h" +#include "app/core/platform/renderer.h" #include "app/editor/editor.h" #include "app/editor/editor_manager.h" #include "imgui/backends/imgui_impl_sdl2.h" @@ -30,6 +30,7 @@ class Controller { public: bool IsActive() const { return active_; } absl::Status OnEntry(std::string filename = ""); + void Initialize(std::string filename = ""); void OnInput(); absl::Status OnLoad(); absl::Status OnTestLoad(); @@ -43,13 +44,6 @@ class Controller { absl::Status LoadAudioDevice(); absl::Status LoadConfigFiles(); - void SetupScreen(std::string filename = "") { - editor_manager_.Initialize(filename); - } - auto editor_manager() -> editor::EditorManager & { return editor_manager_; } - auto renderer() -> SDL_Renderer * { - return Renderer::GetInstance().renderer(); - } auto window() -> SDL_Window * { return window_.get(); } void init_test_editor(editor::Editor *editor) { test_editor_ = editor; } void set_active(bool active) { active_ = active; } @@ -59,7 +53,6 @@ class Controller { friend int ::main(int argc, char **argv); bool active_ = false; - Platform platform_ = Platform::kUnknown; editor::Editor *test_editor_ = nullptr; editor::EditorManager editor_manager_; diff --git a/src/app/core/platform/file_dialog.cc b/src/app/core/platform/file_dialog.cc index 9565340f..637bd4a9 100644 --- a/src/app/core/platform/file_dialog.cc +++ b/src/app/core/platform/file_dialog.cc @@ -56,7 +56,7 @@ std::string LoadConfigFile(const std::string &filename) { #else platform = Platform::kLinux; #endif - std::string filepath = GetConfigDirectory(platform) + "/" + filename; + std::string filepath = GetConfigDirectory() + "/" + filename; std::ifstream file(filepath); if (file.is_open()) { std::stringstream buffer; @@ -67,9 +67,8 @@ std::string LoadConfigFile(const std::string &filename) { return contents; } -void SaveFile(const std::string &filename, const std::string &contents, - Platform platform) { - std::string filepath = GetConfigDirectory(platform) + "/" + filename; +void SaveFile(const std::string &filename, const std::string &contents) { + std::string filepath = GetConfigDirectory() + "/" + filename; std::ofstream file(filepath); if (file.is_open()) { file << contents; @@ -77,8 +76,22 @@ void SaveFile(const std::string &filename, const std::string &contents, } } -std::string GetConfigDirectory(Platform platform) { +std::string GetConfigDirectory() { std::string config_directory = ".yaze"; + Platform platform; +#if defined(__APPLE__) && defined(__MACH__) +#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1 + platform = Platform::kiOS; +#elif TARGET_OS_MAC == 1 + platform = Platform::kMacOS; +#endif +#elif defined(_WIN32) + platform = Platform::kWindows; +#elif defined(__linux__) + platform = Platform::kLinux; +#else + platform = Platform::kUnknown; +#endif switch (platform) { case Platform::kWindows: config_directory = "~/AppData/Roaming/yaze"; diff --git a/src/app/core/platform/file_dialog.h b/src/app/core/platform/file_dialog.h index d9c43738..fcbde379 100644 --- a/src/app/core/platform/file_dialog.h +++ b/src/app/core/platform/file_dialog.h @@ -38,10 +38,8 @@ std::string GetFileExtension(const std::string &filename); std::string GetFileName(const std::string &filename); std::string LoadFile(const std::string &filename); std::string LoadConfigFile(const std::string &filename); -std::string GetConfigDirectory(Platform platform); - -void SaveFile(const std::string &filename, const std::string &data, - Platform platform); +std::string GetConfigDirectory(); +void SaveFile(const std::string &filename, const std::string &data); } // namespace core } // namespace yaze diff --git a/src/app/gui/zeml.h b/src/app/gui/zeml.h index 1ddbaac1..79c68976 100644 --- a/src/app/gui/zeml.h +++ b/src/app/gui/zeml.h @@ -1,8 +1,6 @@ #ifndef YAZE_APP_GUI_ZEML_H #define YAZE_APP_GUI_ZEML_H -#include "imgui/imgui.h" - #include #include #include @@ -10,6 +8,8 @@ #include #include +#include "imgui/imgui.h" + namespace yaze { namespace gui { @@ -163,12 +163,6 @@ void BindSelectable(Node* node, bool* selected, std::function callback); */ WidgetType MapType(const std::string& type); -/** - * @brief Parse a zeml definition - */ -void ParseDefinitions(const std::vector& tokens, size_t& index, - std::map& definitions); - void ParseFlags(const WidgetType& type, const std::string& flags, WidgetAttributes& flags_ptr); @@ -206,7 +200,6 @@ std::string LoadFile(const std::string& filename); } // namespace zeml } // namespace gui - } // namespace yaze -#endif // YAZE_APP_GUI_YAZON_H_ +#endif // YAZE_APP_GUI_ZEML_H diff --git a/src/ios/main.mm b/src/ios/main.mm index 45715e97..a80a05cc 100644 --- a/src/ios/main.mm +++ b/src/ios/main.mm @@ -46,7 +46,17 @@ abort(); } - _controller = new yaze::app::core::Controller(); + _controller = new yaze::core::Controller(); + + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO &io = ImGui::GetIO(); + (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + + yaze::gui::ColorsYaze(); SDL_SetMainReady(); SDL_iOSSetEventPump(SDL_TRUE); @@ -76,23 +86,14 @@ abort(); } - // Setup Dear ImGui context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO &io = ImGui::GetIO(); - (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - - yaze::app::gui::ColorsYaze(); - - ImGui_ImplSDL2_InitForSDLRenderer(_controller->window(), _controller->renderer()); - ImGui_ImplSDLRenderer2_Init(_controller->renderer()); + ImGui_ImplSDL2_InitForSDLRenderer(_controller->window(), + core::Renderer::GetInstance().renderer()); + ImGui_ImplSDLRenderer2_Init(core::Renderer::GetInstance().renderer()); if (!_controller->LoadFontFamilies().ok()) { abort(); } - _controller->SetupScreen(rom_filename); + _controller->Initialize(""); _controller->set_active(true); _hoverGestureRecognizer =