From d2a2a879c279e32a54b2fcad38dde2ff3dc50a3e Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Sun, 4 Jan 2026 14:29:17 +0000 Subject: [PATCH] feat: Integrate GUI input processing into input service and script service --- src/app/service_based_app.cpp | 7 +++ src/controllers/application_controller.cpp | 11 +++++ src/services/impl/sdl_input_service.cpp | 54 +++++++++++++++++++++ src/services/impl/sdl_input_service.hpp | 9 +++- src/services/interfaces/i_input_service.hpp | 23 +++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) diff --git a/src/app/service_based_app.cpp b/src/app/service_based_app.cpp index 6544924..3928443 100644 --- a/src/app/service_based_app.cpp +++ b/src/app/service_based_app.cpp @@ -215,6 +215,13 @@ void ServiceBasedApp::RegisterServices() { // Script service registry_.RegisterService(scriptPath_); + // Connect input service to script service for GUI input processing + auto inputService = registry_.GetService(); + auto scriptService = registry_.GetService(); + if (inputService && scriptService) { + inputService->SetScriptService(scriptService.get()); + } + // Scene service registry_.RegisterService( registry_.GetService()); diff --git a/src/controllers/application_controller.cpp b/src/controllers/application_controller.cpp index 9044c70..d5724a0 100644 --- a/src/controllers/application_controller.cpp +++ b/src/controllers/application_controller.cpp @@ -74,6 +74,12 @@ void ApplicationController::HandleEvents() { void ApplicationController::ProcessFrame(float deltaTime) { logger_->Trace("ApplicationController", "ProcessFrame", "deltaTime=" + std::to_string(deltaTime), "Entering"); + // Reset input frame state + auto inputService = registry_.GetService(); + if (inputService) { + inputService->ResetFrameState(); + } + // Update physics auto physicsService = registry_.GetService(); if (physicsService) { @@ -86,6 +92,11 @@ void ApplicationController::ProcessFrame(float deltaTime) { sceneService->UpdateScene(deltaTime); } + // Update GUI input to script service + if (inputService) { + inputService->UpdateGuiInput(); + } + // Render frame // Temporarily disabled for testing // auto renderController = std::make_unique(registry_); diff --git a/src/services/impl/sdl_input_service.cpp b/src/services/impl/sdl_input_service.cpp index f8d6f75..17d2029 100644 --- a/src/services/impl/sdl_input_service.cpp +++ b/src/services/impl/sdl_input_service.cpp @@ -1,8 +1,19 @@ #include "sdl_input_service.hpp" +#include "../interfaces/i_script_service.hpp" #include "../../logging/logger.hpp" namespace sdl3cpp::services::impl { +// GUI key mapping extracted from old Sdl3App +const std::unordered_map SdlInputService::kGuiKeyNames = { + {SDLK_LEFT, "left"}, {SDLK_RIGHT, "right"}, {SDLK_UP, "up"}, {SDLK_DOWN, "down"}, + {SDLK_HOME, "home"}, {SDLK_END, "end"}, {SDLK_BACKSPACE, "backspace"}, + {SDLK_DELETE, "delete"}, {SDLK_RETURN, "return"}, {SDLK_TAB, "tab"}, + {SDLK_ESCAPE, "escape"}, {SDLK_LCTRL, "lctrl"}, {SDLK_RCTRL, "rctrl"}, + {SDLK_LSHIFT, "lshift"}, {SDLK_RSHIFT, "rshift"}, {SDLK_LALT, "lalt"}, + {SDLK_RALT, "ralt"} +}; + SdlInputService::SdlInputService(std::shared_ptr eventBus) : eventBus_(std::move(eventBus)) { @@ -42,32 +53,61 @@ void SdlInputService::ProcessEvent(const SDL_Event& event) { switch (event.type) { case SDL_EVENT_KEY_DOWN: state_.keysPressed.insert(event.key.key); + // GUI input processing + { + auto it = kGuiKeyNames.find(event.key.key); + if (it != kGuiKeyNames.end()) { + guiInputSnapshot_.keyStates[it->second] = true; + } + } break; case SDL_EVENT_KEY_UP: state_.keysPressed.erase(event.key.key); + // GUI input processing + { + auto it = kGuiKeyNames.find(event.key.key); + if (it != kGuiKeyNames.end()) { + guiInputSnapshot_.keyStates[it->second] = false; + } + } break; case SDL_EVENT_MOUSE_MOTION: state_.mouseX = event.motion.x; state_.mouseY = event.motion.y; + // GUI input processing + guiInputSnapshot_.mouseX = static_cast(event.motion.x); + guiInputSnapshot_.mouseY = static_cast(event.motion.y); break; case SDL_EVENT_MOUSE_BUTTON_DOWN: state_.mouseButtonsPressed.insert(event.button.button); + // GUI input processing + if (event.button.button == SDL_BUTTON_LEFT) { + guiInputSnapshot_.mouseDown = true; + } break; case SDL_EVENT_MOUSE_BUTTON_UP: state_.mouseButtonsPressed.erase(event.button.button); + // GUI input processing + if (event.button.button == SDL_BUTTON_LEFT) { + guiInputSnapshot_.mouseDown = false; + } break; case SDL_EVENT_MOUSE_WHEEL: state_.mouseWheelDeltaX = event.wheel.x; state_.mouseWheelDeltaY = event.wheel.y; + // GUI input processing + guiInputSnapshot_.wheel += static_cast(event.wheel.y); break; case SDL_EVENT_TEXT_INPUT: state_.textInput += event.text.text; + // GUI input processing + guiInputSnapshot_.textInput.append(event.text.text); break; default: @@ -80,6 +120,10 @@ void SdlInputService::ResetFrameState() { state_.mouseWheelDeltaX = 0.0f; state_.mouseWheelDeltaY = 0.0f; state_.textInput.clear(); + + // Reset GUI per-frame state + guiInputSnapshot_.wheel = 0.0f; + guiInputSnapshot_.textInput.clear(); } bool SdlInputService::IsKeyPressed(SDL_Keycode key) const { @@ -131,4 +175,14 @@ void SdlInputService::OnTextInput(const events::Event& event) { state_.textInput += textEvent.text; } +void SdlInputService::SetScriptService(IScriptService* scriptService) { + scriptService_ = scriptService; +} + +void SdlInputService::UpdateGuiInput() { + if (scriptService_) { + scriptService_->UpdateGuiInput(guiInputSnapshot_); + } +} + } // namespace sdl3cpp::services::impl diff --git a/src/services/impl/sdl_input_service.hpp b/src/services/impl/sdl_input_service.hpp index dbc51d3..d2d3885 100644 --- a/src/services/impl/sdl_input_service.hpp +++ b/src/services/impl/sdl_input_service.hpp @@ -10,7 +10,7 @@ namespace sdl3cpp::services::impl { * @brief SDL3-based input service implementation. * * Subscribes to input events from the event bus and maintains - * the current input state for queries. + * the current input state for queries. Also handles GUI input processing. */ class SdlInputService : public IInputService { public: @@ -30,10 +30,14 @@ public: bool IsKeyPressed(SDL_Keycode key) const override; bool IsMouseButtonPressed(uint8_t button) const override; std::pair GetMousePosition() const override; + void SetScriptService(IScriptService* scriptService) override; + void UpdateGuiInput() override; private: std::shared_ptr eventBus_; InputState state_; + script::GuiInputSnapshot guiInputSnapshot_; + IScriptService* scriptService_ = nullptr; // Event bus listeners void OnKeyPressed(const events::Event& event); @@ -43,6 +47,9 @@ private: void OnMouseButtonReleased(const events::Event& event); void OnMouseWheel(const events::Event& event); void OnTextInput(const events::Event& event); + + // GUI key mapping (extracted from old Sdl3App) + static const std::unordered_map kGuiKeyNames; }; } // namespace sdl3cpp::services::impl diff --git a/src/services/interfaces/i_input_service.hpp b/src/services/interfaces/i_input_service.hpp index 5d93e7d..2fc10c8 100644 --- a/src/services/interfaces/i_input_service.hpp +++ b/src/services/interfaces/i_input_service.hpp @@ -3,9 +3,13 @@ #include #include #include +#include "../../script/gui_types.hpp" namespace sdl3cpp::services { +// Forward declaration +class IScriptService; + /** * @brief Input state snapshot for a single frame. */ @@ -24,6 +28,7 @@ struct InputState { * * Subscribes to input events from the event bus and maintains * the current input state for queries by other services. + * Also handles GUI input processing for script integration. */ class IInputService { public: @@ -76,6 +81,24 @@ public: * @return Pair of (x, y) coordinates in pixels */ virtual std::pair GetMousePosition() const = 0; + + /** + * @brief Set the script service for GUI input processing. + * + * The input service will update GUI input state to the script service + * when events are processed. + * + * @param scriptService Pointer to the script service, or nullptr to disable + */ + virtual void SetScriptService(IScriptService* scriptService) = 0; + + /** + * @brief Update GUI input state to the script service. + * + * Called at the end of each frame to send accumulated GUI input + * to the script engine for processing. + */ + virtual void UpdateGuiInput() = 0; }; } // namespace sdl3cpp::services