mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-24 13:44:58 +00:00
feat: Integrate GUI input processing into input service and script service
This commit is contained in:
@@ -215,6 +215,13 @@ void ServiceBasedApp::RegisterServices() {
|
||||
// Script service
|
||||
registry_.RegisterService<services::IScriptService, services::impl::LuaScriptService>(scriptPath_);
|
||||
|
||||
// Connect input service to script service for GUI input processing
|
||||
auto inputService = registry_.GetService<services::IInputService>();
|
||||
auto scriptService = registry_.GetService<services::IScriptService>();
|
||||
if (inputService && scriptService) {
|
||||
inputService->SetScriptService(scriptService.get());
|
||||
}
|
||||
|
||||
// Scene service
|
||||
registry_.RegisterService<services::ISceneService, services::impl::SceneService>(
|
||||
registry_.GetService<services::IScriptService>());
|
||||
|
||||
@@ -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<services::IInputService>();
|
||||
if (inputService) {
|
||||
inputService->ResetFrameState();
|
||||
}
|
||||
|
||||
// Update physics
|
||||
auto physicsService = registry_.GetService<services::IPhysicsService>();
|
||||
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<RenderController>(registry_);
|
||||
|
||||
@@ -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<SDL_Keycode, std::string> 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<events::EventBus> 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<float>(event.motion.x);
|
||||
guiInputSnapshot_.mouseY = static_cast<float>(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<float>(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
|
||||
|
||||
@@ -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<float, float> GetMousePosition() const override;
|
||||
void SetScriptService(IScriptService* scriptService) override;
|
||||
void UpdateGuiInput() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<events::EventBus> 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<SDL_Keycode, std::string> kGuiKeyNames;
|
||||
};
|
||||
|
||||
} // namespace sdl3cpp::services::impl
|
||||
|
||||
@@ -3,9 +3,13 @@
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <SDL3/SDL.h>
|
||||
#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<float, float> 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
|
||||
|
||||
Reference in New Issue
Block a user