From 995c01bd8ca68db43158fdf3e7088e9b2045bea7 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 6 Dec 2025 18:38:36 +0800 Subject: [PATCH] Remove the game logic from InputManager --- src/input/InputManager.cpp | 46 +++++++------------------------------- src/input/InputManager.h | 16 ++++++------- src/input/InputState.h | 6 +++++ 3 files changed, 22 insertions(+), 46 deletions(-) create mode 100644 src/input/InputState.h diff --git a/src/input/InputManager.cpp b/src/input/InputManager.cpp index 33ccbce..0497840 100644 --- a/src/input/InputManager.cpp +++ b/src/input/InputManager.cpp @@ -1,6 +1,6 @@ #include "InputManager.h" -SDL_AppResult InputManager::handleInputEvent(const SDL_Event* event, const Renderer* g_renderer, Game* g_game) { +SDL_AppResult InputManager::handleInputEvent(const SDL_Event* event) { switch (event->type) { // 如果检测到退出 @@ -8,46 +8,16 @@ SDL_AppResult InputManager::handleInputEvent(const SDL_Event* event, const Rende return SDL_APP_SUCCESS; case SDL_EVENT_MOUSE_BUTTON_DOWN: if (event->button.button == SDL_BUTTON_LEFT) { - // 将窗口坐标转为逻辑坐标 - float logicalX, logicalY; - SDL_RenderCoordinatesFromWindow( - g_renderer->getSDLRenderer(), - static_cast(event->button.x), - static_cast(event->button.y), - &logicalX, - &logicalY - ); - auto boardArea = g_renderer->getBoardArea(); - auto click = handleMouseClick(static_cast(logicalX), static_cast(logicalY), boardArea); - if (click) { - auto [row, col] = click.value(); - SDL_Log("click on (%d, %d)", row, col); - g_game->handleCoordinateInput(row, col); - g_game->printBoard(); - } else { - SDL_Log("invail cilck aera!"); - } + // 更新状态 + m_currentInputState.mouseCilckOn = {event->button.x, event->button.y}; + + return SDL_APP_CONTINUE; } } return SDL_APP_CONTINUE; } -std::optional> -InputManager::handleMouseClick(int mouseX, int mouseY, const ui::BoardArea& area) { - // 判断是否点击在棋盘区域内 - if (mouseX < area.x || mouseX >= area.x + area.cellSize * area.cols || - mouseY < area.y || mouseY >= area.y + area.cellSize * area.rows) { - return std::nullopt; // 点击在棋盘外 - } - - // 转换为逻辑坐标 - int col = (mouseX - area.x) / area.cellSize; - int row = (mouseY - area.y) / area.cellSize; - - // 安全检查(通常不需要,但保险) - if (row >= 0 && row < area.rows && col >= 0 && col < area.cols) { - return std::pair{row, col}; - } - return std::nullopt; -} \ No newline at end of file +InputState InputManager::GetInputState() const { + return m_currentInputState; +} diff --git a/src/input/InputManager.h b/src/input/InputManager.h index 104262f..ecef5a7 100644 --- a/src/input/InputManager.h +++ b/src/input/InputManager.h @@ -1,14 +1,14 @@ #pragma once #include -#include -#include -#include "ui/UIType.h" -class Renderer; -class Game; + + +#include "InputState.h" class InputManager { public: - SDL_AppResult handleInputEvent(const SDL_Event* event, const Renderer* g_renderer, Game* g_game); - // 调用前需传入当前棋盘渲染区域(来自 Renderer) - std::optional> handleMouseClick(int mouseX, int mouseY, const ui::BoardArea& boardArea); + SDL_AppResult handleInputEvent(const SDL_Event* event); + + InputState GetInputState() const; +private: + InputState m_currentInputState; }; \ No newline at end of file diff --git a/src/input/InputState.h b/src/input/InputState.h new file mode 100644 index 0000000..f00fa7d --- /dev/null +++ b/src/input/InputState.h @@ -0,0 +1,6 @@ +#pragma once +#include +struct InputState +{ + std::pair mouseCilckOn; +};