mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
181 lines
5.6 KiB
C++
181 lines
5.6 KiB
C++
#include "GameUIManager.h"
|
|
#include "utils/CoordinateTools.h"
|
|
GameUIManager::GameUIManager(SDL_Renderer* renderer, TextRenderer* textRenderer)
|
|
{
|
|
m_renderer = renderer;
|
|
m_textRenderer = textRenderer;
|
|
}
|
|
|
|
GameUIManager::~GameUIManager() {
|
|
|
|
}
|
|
|
|
void GameUIManager::init() {
|
|
//m_restartCallback = restartCallback;
|
|
setupUIComponents();
|
|
}
|
|
|
|
void GameUIManager::setCallback(std::function<void()> restartCallback) {
|
|
m_restartCallback = restartCallback;
|
|
}
|
|
|
|
|
|
|
|
const UIRenderData& GameUIManager::getUIRenderData() {
|
|
CollectRenderData();
|
|
return m_uiRenderData;
|
|
}
|
|
|
|
void GameUIManager::CollectRenderData() {
|
|
|
|
//SDL_Log("CollectRenderData called. buttons count = %zu", m_buttons.size());
|
|
// 清理上一帧的数据
|
|
m_uiRenderData.buttons.clear();
|
|
m_uiRenderData.labels.clear();
|
|
//SDL_Log("collect data\n");
|
|
for (auto& button : m_buttons) {
|
|
if(!button.second->isVisible()) {
|
|
continue;
|
|
|
|
}
|
|
m_uiRenderData.buttons.push_back(button.second->getButtonDate());
|
|
}
|
|
|
|
for (auto& label : m_labels) {
|
|
if(!label.second->isVisible()) {
|
|
continue;
|
|
|
|
}
|
|
m_uiRenderData.labels.push_back(label.second->getLabelDate());
|
|
}
|
|
}
|
|
|
|
void GameUIManager::UpdateMousePositon(float x, float y) {
|
|
|
|
auto logicalPos = physicalToLogical(static_cast<float>(x), static_cast<float>(y), m_renderer);
|
|
int lx = logicalPos.first;
|
|
int ly = logicalPos.second;
|
|
std::string pos = std::to_string(lx) + " " + std::to_string(ly);
|
|
|
|
m_labels[makeHash("MousePositionLabel")]->setText(pos);
|
|
}
|
|
|
|
void GameUIManager::updateActionType(ActionType type) {
|
|
// 根据传入的 ActionType 更新 UI 组件状态
|
|
auto buttonIt = m_buttons.find(makeHash("ActionButton"));
|
|
if (buttonIt != m_buttons.end()) {
|
|
auto& button = buttonIt->second;
|
|
switch (type) {
|
|
case ActionType::GROW:
|
|
button->setBackgroundColor({255, 100, 0, 255}); // 橙色
|
|
button->setText("GROW");
|
|
break;
|
|
case ActionType::MOVE:
|
|
button->setBackgroundColor({0, 255, 0, 255}); // 绿色
|
|
button->setText("MOVE");
|
|
break;
|
|
case ActionType::SPORE:
|
|
button->setBackgroundColor({0, 0, 255, 255}); // 蓝色
|
|
button->setText("SPORE");
|
|
break;
|
|
default:
|
|
button->setBackgroundColor({255, 100, 0, 255}); // 默认橙色
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GameUIManager::updateGameState(GameState state) {
|
|
if (m_currentGameState == state) {
|
|
SDL_Log("State unchanged, returning early\n");
|
|
return;
|
|
}
|
|
// 根据传入的 GameState 更新 UI 组件状态
|
|
{
|
|
auto buttonIt = m_buttons.find(makeHash("RestartButton"));
|
|
if (buttonIt != m_buttons.end()) {
|
|
auto& button = buttonIt->second;
|
|
if (state != GameState::GAME_RUNING) {
|
|
//button->setBackgroundColor({255, 215, 0, 255}); // 金色
|
|
//button->setText("GAME OVER");
|
|
button->setVisible(true);
|
|
button->setEnabled(true);
|
|
} else {
|
|
button->setVisible(false);
|
|
button->setEnabled(false);
|
|
}
|
|
}
|
|
}
|
|
{
|
|
auto actionButtonIt = m_buttons.find(makeHash("ActionButton"));
|
|
if (actionButtonIt != m_buttons.end()) {
|
|
auto& actionButton = actionButtonIt->second;
|
|
if (state != GameState::GAME_RUNING) {
|
|
actionButton->setEnabled(false);
|
|
actionButton->setVisible(false);
|
|
} else {
|
|
actionButton->setEnabled(true);
|
|
actionButton->setVisible(true);
|
|
}
|
|
}
|
|
}
|
|
// 保存当前游戏状态,避免后续相同状态更新被误判为无变化
|
|
m_currentGameState = state;
|
|
}
|
|
|
|
void GameUIManager::setupUIComponents() {
|
|
// 这里可以添加更多的UI组件初始化逻辑
|
|
|
|
auto button = std::make_unique<Button>();
|
|
button->setBackgroundColor({255, 100, 0, 255});
|
|
button->setBorder(2, {0, 0, 0, 255});
|
|
button->setRect(20, 20, 200, 100);
|
|
button->setEnabled(true);
|
|
button->setVisible(true);
|
|
button->setText("Please Choose", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
|
|
button->setName("ActionButton");
|
|
m_buttons.emplace(button->getNameHash(), std::move(button));
|
|
|
|
auto label = std::make_unique<Label>();
|
|
label->setRect(1200, 20, 200, 50);
|
|
label->setText("0 0", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
|
|
label->setName("MousePositionLabel");
|
|
m_labels.emplace(label->getNameHash(), std::move(label));
|
|
|
|
auto restartButton = std::make_unique<Button>(
|
|
"Restart",
|
|
(TextStyle){"unifont.otf", 48, {0, 0, 0, 255}},
|
|
700,
|
|
500
|
|
);
|
|
|
|
restartButton->setCallback([this](){
|
|
if (m_restartCallback) {
|
|
m_restartCallback();
|
|
}
|
|
});
|
|
restartButton->setName("RestartButton");
|
|
restartButton->setVisible(false); // 初始时隐藏
|
|
restartButton->setEnabled(false);
|
|
m_buttons.emplace(restartButton->getNameHash(), std::move(restartButton));
|
|
|
|
|
|
}
|
|
|
|
|
|
bool GameUIManager::handleClick(float x, float y) {
|
|
auto logicalPos = physicalToLogical(static_cast<float>(x), static_cast<float>(y), m_renderer);
|
|
int lx = logicalPos.first;
|
|
int ly = logicalPos.second;
|
|
|
|
// 遍历所有按钮,检查点击位置是否在按钮范围内
|
|
for (auto& [id, button] : m_buttons) {
|
|
if (button->isEnabled() && button->isVisible()) {
|
|
if(button->handleCilck(lx, ly)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|