mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
refactor: replace pair with vec2
This commit is contained in:
@@ -27,12 +27,12 @@ void DebugManager::initialize() {
|
|||||||
m_isDebugInfoVisible = true;
|
m_isDebugInfoVisible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugManager::updateMousePos(int logicalX, int logicalY, const InputState& inputState) {
|
void DebugManager::updateMousePos(glm::ivec2 logicalPos, const InputState& inputState) {
|
||||||
m_debugData.mousePosition = {
|
m_debugData.mousePosition = {
|
||||||
static_cast<int>(inputState.mouseCurrentPosition.first),
|
static_cast<int>(inputState.mouseCurrentPosition.x),
|
||||||
static_cast<int>(inputState.mouseCurrentPosition.second)
|
static_cast<int>(inputState.mouseCurrentPosition.y)
|
||||||
};
|
};
|
||||||
m_debugData.mouseLogicalPostion = {logicalX, logicalY};
|
m_debugData.mouseLogicalPostion = logicalPos;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public:
|
|||||||
|
|
||||||
void showDebugInfo();
|
void showDebugInfo();
|
||||||
|
|
||||||
void updateMousePos(int logicalX, int logicalY, const InputState& inputState);
|
void updateMousePos(glm::ivec2 logicalPos, const InputState& inputState);
|
||||||
void updateDebugInfo();
|
void updateDebugInfo();
|
||||||
|
|
||||||
DebugData& getDebugData() {
|
DebugData& getDebugData() {
|
||||||
|
|||||||
@@ -74,14 +74,14 @@ SDL_AppResult GameApplication::handleInputEvent(SDL_Event* event) {
|
|||||||
InputState input = m_inputManager->GetInputState();
|
InputState input = m_inputManager->GetInputState();
|
||||||
if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN &&
|
if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN &&
|
||||||
event->button.button == SDL_BUTTON_LEFT) {
|
event->button.button == SDL_BUTTON_LEFT) {
|
||||||
auto pos = Tools::physicalToLogical(input.mouseCilckOn.first, input.mouseCilckOn.second, m_windowManager->getViewport());
|
auto pos = Tools::physicalToLogical(input.mouseCilckOn, m_windowManager->getViewport());
|
||||||
m_sceneManager->handleClickCurrent(pos);
|
m_sceneManager->handleClickCurrent(pos);
|
||||||
}
|
}
|
||||||
auto pos = Tools::physicalToLogical(input.mouseCurrentPosition.first, input.mouseCurrentPosition.second, m_windowManager->getViewport());
|
auto pos = Tools::physicalToLogical(input.mouseCurrentPosition, m_windowManager->getViewport());
|
||||||
|
|
||||||
m_coreData.inputState.mouseCurrentLogicalPosition = pos;
|
m_coreData.inputState.mouseCurrentLogicalPosition = pos;
|
||||||
|
|
||||||
m_debugManager->updateMousePos(pos.first, pos.second, input);
|
m_debugManager->updateMousePos(pos, input);
|
||||||
|
|
||||||
m_windowManager->setFullscreen(input.isFullscreen);
|
m_windowManager->setFullscreen(input.isFullscreen);
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,15 @@ void BoardRenderer::setBoard(const Board* board) {
|
|||||||
m_board = board;
|
m_board = board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void BoardRenderer::update() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void BoardRenderer::drawBackground() {
|
void BoardRenderer::drawBackground() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,6 +108,8 @@ public:
|
|||||||
void renderBlackOverlay();
|
void renderBlackOverlay();
|
||||||
|
|
||||||
void handleGamePieceEvent(GamePieceEvent event, int fromRow, int fromCol, int toRow = -1, int toCol = -1);
|
void handleGamePieceEvent(GamePieceEvent event, int fromRow, int fromCol, int toRow = -1, int toCol = -1);
|
||||||
|
|
||||||
|
void update();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
struct InputState
|
struct InputState
|
||||||
{
|
{
|
||||||
std::pair<float, float> mouseCilckOn;
|
glm::vec2 mouseCilckOn;
|
||||||
bool leftMouseDown = false;
|
bool leftMouseDown = false;
|
||||||
std::pair<float, float> mouseCurrentPosition;
|
glm::vec2 mouseCurrentPosition;
|
||||||
std::pair<int, int> mouseCurrentLogicalPosition;
|
glm::ivec2 mouseCurrentLogicalPosition;
|
||||||
bool isFullscreen = false;
|
bool isFullscreen = false;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "graphics/texture/TextureManager.h"
|
#include "graphics/texture/TextureManager.h"
|
||||||
#include "core/CoreData.h"
|
#include "core/CoreData.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
/**
|
/**
|
||||||
* @class Scene
|
* @class Scene
|
||||||
* @brief 场景基类
|
* @brief 场景基类
|
||||||
|
|||||||
@@ -163,8 +163,9 @@ void SceneManager::changeScene(const std::string& sceneName) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void SceneManager::handleClickCurrent(std::pair<int, int> clickOn) {
|
void SceneManager::handleClickCurrent(glm::ivec2 clickOn) {
|
||||||
auto [logicalX, logicalY] = clickOn;
|
int logicalX = clickOn.x;
|
||||||
|
int logicalY = clickOn.y;
|
||||||
if (m_scene) m_scene->handleClick(logicalX, logicalY);
|
if (m_scene) m_scene->handleClick(logicalX, logicalY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ public:
|
|||||||
* @param clickOn 点击位置坐标对 {x, y}
|
* @param clickOn 点击位置坐标对 {x, y}
|
||||||
* @details 将点击事件转发给当前场景的 handleClick() 方法进行处理
|
* @details 将点击事件转发给当前场景的 handleClick() 方法进行处理
|
||||||
*/
|
*/
|
||||||
void handleClickCurrent(std::pair<int, int> clickOn);
|
void handleClickCurrent(glm::ivec2 clickOn);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -152,7 +152,8 @@ void GameScene::restartGame() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GameScene::updatePieceInfo() {
|
void GameScene::updatePieceInfo() {
|
||||||
auto [mouseX, mouseY] = m_coreData->inputState.mouseCurrentLogicalPosition;
|
int mouseX = m_coreData->inputState.mouseCurrentLogicalPosition.x;
|
||||||
|
int mouseY = m_coreData->inputState.mouseCurrentLogicalPosition.y;
|
||||||
auto click = m_CoordinateConverter->ScreenToBoard(mouseX, mouseY, m_boardRenderer->getBoardArea());
|
auto click = m_CoordinateConverter->ScreenToBoard(mouseX, mouseY, m_boardRenderer->getBoardArea());
|
||||||
|
|
||||||
if (click == std::nullopt) {
|
if (click == std::nullopt) {
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
struct DebugData {
|
struct DebugData {
|
||||||
bool showFPS = true;
|
bool showFPS = true;
|
||||||
bool showMousePosition = true;
|
bool showMousePosition = true;
|
||||||
std::pair<int, int> mousePosition = {0, 0};
|
glm::ivec2 mousePosition = {0, 0};
|
||||||
std::pair<int, int> mouseLogicalPostion = {0, 0};
|
glm::ivec2 mouseLogicalPostion = {0, 0};
|
||||||
int currentFPS = 0;
|
int currentFPS = 0;
|
||||||
int round = 0;
|
int round = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -96,8 +96,8 @@ void DebugOverlay::updateDebugInfo() {
|
|||||||
}
|
}
|
||||||
mousePosLabel->setText(
|
mousePosLabel->setText(
|
||||||
"PhyscialPos: " +
|
"PhyscialPos: " +
|
||||||
std::to_string(m_debugData.mousePosition.first) + ", " +
|
std::to_string(m_debugData.mousePosition.x) + ", " +
|
||||||
std::to_string(m_debugData.mousePosition.second)
|
std::to_string(m_debugData.mousePosition.y)
|
||||||
);
|
);
|
||||||
|
|
||||||
mousePosLabel->setVisible(true);
|
mousePosLabel->setVisible(true);
|
||||||
@@ -121,8 +121,8 @@ void DebugOverlay::updateDebugInfo() {
|
|||||||
}
|
}
|
||||||
mouseLogicalPosLabel->setText(
|
mouseLogicalPosLabel->setText(
|
||||||
"LogicalPos: " +
|
"LogicalPos: " +
|
||||||
std::to_string(m_debugData.mouseLogicalPostion.first) + ", " +
|
std::to_string(m_debugData.mouseLogicalPostion.x) + ", " +
|
||||||
std::to_string(m_debugData.mouseLogicalPostion.second)
|
std::to_string(m_debugData.mouseLogicalPostion.y)
|
||||||
);
|
);
|
||||||
|
|
||||||
mouseLogicalPosLabel->setVisible(true);
|
mouseLogicalPosLabel->setVisible(true);
|
||||||
|
|||||||
@@ -2,20 +2,21 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
static inline float easeInOutSine(float t) {
|
static inline float easeInOutSine(float t) {
|
||||||
return -(cos(M_PI * t) - 1) / 2.0f;
|
return -(cos(M_PI * t) - 1) / 2.0f;
|
||||||
}
|
}
|
||||||
namespace Tools {
|
namespace Tools {
|
||||||
inline std::pair<int, int> physicalToLogical(float physicalX, float physicalY, const Viewport& viewport) {
|
inline glm::ivec2 physicalToLogical(glm::vec2 physicalPos, const Viewport& viewport) {
|
||||||
std::pair<int, int> logicalPoint = {0 , 0};
|
glm::ivec2 logicalPoint = {0 , 0};
|
||||||
|
|
||||||
// 计算相对于目标矩形的坐标
|
// 计算相对于目标矩形的坐标
|
||||||
float normalizedX = (physicalX - viewport.dst.x) / viewport.dst.w;
|
float normalizedX = (physicalPos.x - viewport.dst.x) / viewport.dst.w;
|
||||||
float normalizedY = (physicalY - viewport.dst.y) / viewport.dst.h;
|
float normalizedY = (physicalPos.y - viewport.dst.y) / viewport.dst.h;
|
||||||
|
|
||||||
// 转换为逻辑坐标
|
// 转换为逻辑坐标
|
||||||
logicalPoint.first = static_cast<int>(normalizedX * viewport.logicalWidth);
|
logicalPoint.x = static_cast<int>(normalizedX * viewport.logicalWidth);
|
||||||
logicalPoint.second = static_cast<int>(normalizedY * viewport.logicalHeight);
|
logicalPoint.y = static_cast<int>(normalizedY * viewport.logicalHeight);
|
||||||
|
|
||||||
return logicalPoint;
|
return logicalPoint;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user