Added button handle event

This commit is contained in:
2025-12-13 16:08:11 +08:00
parent 6f5c378cb0
commit be106e3865
7 changed files with 120 additions and 22 deletions

View File

@@ -1,8 +1,9 @@
#include "GameUIManager.h"
GameUIManager::GameUIManager()
#include "utils/CoordinateTools.h"
GameUIManager::GameUIManager(SDL_Renderer* renderer, TextRenderer* textRenderer)
{
m_renderer = renderer;
m_textRenderer = textRenderer;
}
GameUIManager::~GameUIManager() {
@@ -10,14 +11,20 @@ GameUIManager::~GameUIManager() {
}
void GameUIManager::init() {
auto button = std::make_unique<Button>();
auto button = std::make_unique<Button>(m_textRenderer);
button->setBackgroundColor({255, 100, 0, 255});
button->setBorder(2, {0, 0, 0, 255});
button->setPosition(20, 20);
button->setEnabled(true);
button->setVisible(true);
button->setText("hello,world!!", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
m_buttons.push_back(std::move(button));
button->setName("TestButton");
m_buttons.emplace(button->getNameHash(), std::move(button));
auto label = std::make_unique<Label>();
label->setPosition(1200, 20);
label->setText("0 0", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
label->setName("MousePositionLabel");
m_labels.emplace(label->getNameHash(), std::move(label));
}
@@ -32,12 +39,31 @@ 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->isVisible()) {
if(!button.second->isVisible()) {
continue;
}
m_uiRenderData.buttons.push_back(button->getButtonDate());
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);
}