From a9a9cc888bbdd1a6f6f8a2c11b49fc6da914aa8f Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 3 Aug 2025 16:42:06 -0400 Subject: [PATCH] Refactor event handling and controller initialization for improved clarity - Moved event handling logic from the Controller class to a new HandleEvents function in the Window class, enhancing separation of concerns. - Simplified the OnEntry method by removing the Initialize function call and directly initializing the editor manager. - Updated the OnLoad method to check for window activity, improving the logic for managing the controller's active state. - Cleaned up the controller header by removing the obsolete Initialize method, streamlining the interface. --- src/app/core/controller.cc | 59 ++++---------------------------------- src/app/core/controller.h | 1 - src/app/core/window.cc | 41 ++++++++++++++++++++++++++ src/app/core/window.h | 3 +- 4 files changed, 49 insertions(+), 55 deletions(-) diff --git a/src/app/core/controller.cc b/src/app/core/controller.cc index a293cc80..11dc82ec 100644 --- a/src/app/core/controller.cc +++ b/src/app/core/controller.cc @@ -16,64 +16,19 @@ absl::Status Controller::OnEntry(std::string filename) { RETURN_IF_ERROR(CreateWindow(window_, SDL_WINDOW_RESIZABLE)); editor_manager_.emulator().set_audio_buffer(window_.audio_buffer_.get()); editor_manager_.emulator().set_audio_device_id(window_.audio_device_); - Initialize(filename); + editor_manager_.Initialize(filename); + active_ = true; return absl::OkStatus(); } -void Controller::Initialize(std::string filename) { - editor_manager_.Initialize(filename); - active_ = true; -} - void Controller::OnInput() { - ImGuiIO &io = ImGui::GetIO(); - SDL_Event event; - - SDL_WaitEvent(&event); - ImGui_ImplSDL2_ProcessEvent(&event); - switch (event.type) { - case SDL_KEYDOWN: - case SDL_KEYUP: { - 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; - 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; - } - - int mouseX; - int mouseY; - const int buttons = SDL_GetMouseState(&mouseX, &mouseY); - - io.DeltaTime = 1.0f / 60.0f; - io.MousePos = ImVec2(static_cast(mouseX), static_cast(mouseY)); - io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT); - io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT); - io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE); - - int wheel = 0; - io.MouseWheel = static_cast(wheel); + PRINT_IF_ERROR(HandleEvents(window_)); } absl::Status Controller::OnLoad() { - if (editor_manager_.quit()) { + if (editor_manager_.quit() || !window_.active_) { active_ = false; + return absl::OkStatus(); } #if TARGET_OS_IPHONE != 1 @@ -120,9 +75,7 @@ void Controller::DoRender() const { SDL_RenderPresent(Renderer::Get().renderer()); } -void Controller::OnExit() { - PRINT_IF_ERROR(ShutdownWindow(window_)); -} +void Controller::OnExit() { PRINT_IF_ERROR(ShutdownWindow(window_)); } } // namespace core } // namespace yaze diff --git a/src/app/core/controller.h b/src/app/core/controller.h index 83513927..d0667c7f 100644 --- a/src/app/core/controller.h +++ b/src/app/core/controller.h @@ -24,7 +24,6 @@ class Controller { public: bool IsActive() const { return active_; } absl::Status OnEntry(std::string filename = ""); - void Initialize(std::string filename = ""); void OnInput(); absl::Status OnLoad(); void DoRender() const; diff --git a/src/app/core/window.cc b/src/app/core/window.cc index 4a30bb10..d41d29db 100644 --- a/src/app/core/window.cc +++ b/src/app/core/window.cc @@ -83,5 +83,46 @@ absl::Status ShutdownWindow(Window& window) { return absl::OkStatus(); } +absl::Status HandleEvents(Window &window) { + SDL_Event event; + ImGuiIO &io = ImGui::GetIO(); + SDL_WaitEvent(&event); + ImGui_ImplSDL2_ProcessEvent(&event); + switch (event.type) { + case SDL_KEYDOWN: + case SDL_KEYUP: { + 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: + window.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; + } + break; + } + int mouseX; + int mouseY; + const int buttons = SDL_GetMouseState(&mouseX, &mouseY); + + io.DeltaTime = 1.0f / 60.0f; + io.MousePos = ImVec2(static_cast(mouseX), static_cast(mouseY)); + io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT); + io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT); + io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE); + + int wheel = 0; + io.MouseWheel = static_cast(wheel); + return absl::OkStatus(); +} + } // namespace core } // namespace yaze \ No newline at end of file diff --git a/src/app/core/window.h b/src/app/core/window.h index 08ff6629..50b1d3b3 100644 --- a/src/app/core/window.h +++ b/src/app/core/window.h @@ -17,10 +17,11 @@ struct Window { std::shared_ptr window_; SDL_AudioDeviceID audio_device_; std::shared_ptr audio_buffer_; + bool active_ = true; }; absl::Status CreateWindow(Window &window, int flags); - +absl::Status HandleEvents(Window &window); absl::Status ShutdownWindow(Window &window); /**