mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
Added UI class and UI rendering class
This commit is contained in:
80
src/ui/base/UIComponent.h
Normal file
80
src/ui/base/UIComponent.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
/**
|
||||
* @brief UI组件抽象基类
|
||||
*
|
||||
* 所有UI组件(按钮、标签、面板等)都应继承此类
|
||||
* 提供统一的接口用于渲染、更新和事件处理
|
||||
*/
|
||||
|
||||
|
||||
|
||||
class UIComponent {
|
||||
public:
|
||||
virtual ~UIComponent() = default;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 更新组件状态
|
||||
* @param deltaTime 距离上一帧的时间(秒),用于动画
|
||||
*/
|
||||
|
||||
virtual void update(float deltaTime) = 0;
|
||||
|
||||
/**
|
||||
* @brief 处理SDL事件
|
||||
* @param event SDL事件引用
|
||||
*/
|
||||
|
||||
//virtual void handleEvent
|
||||
|
||||
/**
|
||||
* @brief 设置组件位置
|
||||
* @param x X坐标(屏幕像素)
|
||||
* @param y Y坐标(屏幕像素)
|
||||
*/
|
||||
|
||||
virtual void setPosition(int x, int y) {
|
||||
m_rect.x = static_cast<float>(x);
|
||||
m_rect.y = static_cast<float>(y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取组件边界矩形
|
||||
* @return SDL_FRect 浮点矩形
|
||||
*/
|
||||
SDL_FRect getBounds() const { return m_rect; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否可见
|
||||
* @param visible 可见性标志
|
||||
*/
|
||||
void setVisible(bool visible) { m_isVisible = visible; }
|
||||
|
||||
/**
|
||||
* @brief 检查是否可见
|
||||
* @return true 如果组件可见
|
||||
*/
|
||||
bool isVisible() const { return m_isVisible; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用交互
|
||||
* @param enabled 启用标志
|
||||
*/
|
||||
void setEnabled(bool enabled) { m_isEnabled = enabled; }
|
||||
|
||||
/**
|
||||
* @brief 检查是否启用
|
||||
* @return true 如果组件启用
|
||||
*/
|
||||
bool isEnabled() const { return m_isEnabled; }
|
||||
|
||||
protected:
|
||||
UIComponent() = default;
|
||||
SDL_FRect m_rect = {0.0f, 0.0f, 0.0f, 0.0f}; ///< 组件位置和尺寸
|
||||
bool m_isVisible = true; ///< 可见性标志
|
||||
bool m_isEnabled = true; ///< 启用标志
|
||||
|
||||
};
|
||||
20
src/ui/base/UIRenderData.h
Normal file
20
src/ui/base/UIRenderData.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include "graphics/font/Textstyle.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
struct ButtonData
|
||||
{
|
||||
std::string text;
|
||||
TextStyle textstytle;
|
||||
SDL_FRect rect;
|
||||
SDL_Color backgroundColor;
|
||||
|
||||
int borderThickness;
|
||||
SDL_Color borderColor;
|
||||
};
|
||||
|
||||
// 可以通过索引方式实现,及直接传递Button指针, 在UIRender里面调用getData
|
||||
struct UIRenderData
|
||||
{
|
||||
std::vector<ButtonData> buttons;
|
||||
};
|
||||
33
src/ui/components/Button.cpp
Normal file
33
src/ui/components/Button.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "Button.h"
|
||||
|
||||
Button::Button()
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Button::setText(const std::string& text, TextStyle style) {
|
||||
m_buttonData.text = text;
|
||||
m_buttonData.textstytle = style;
|
||||
}
|
||||
|
||||
void Button::setBackgroundColor(SDL_Color normal) {
|
||||
m_buttonData.backgroundColor = normal;
|
||||
}
|
||||
|
||||
void Button::setBorder(int thickness, SDL_Color color) {
|
||||
m_buttonData.borderThickness = thickness;
|
||||
m_buttonData.borderColor = color;
|
||||
}
|
||||
|
||||
|
||||
void Button::update(float deltaTime) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
48
src/ui/components/Button.h
Normal file
48
src/ui/components/Button.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include "ui/base/UIRenderData.h"
|
||||
#include "ui/base/UIComponent.h"
|
||||
class Button : public UIComponent{
|
||||
public:
|
||||
Button();
|
||||
~Button() override = default;
|
||||
|
||||
// 实现UIComponent接口
|
||||
|
||||
void update(float deltaTime) override;
|
||||
|
||||
/**
|
||||
* @brief 设置按钮文本
|
||||
* @param text 按钮文本
|
||||
* @param style 文本样式
|
||||
*/
|
||||
void setText(const std::string& text, TextStyle style);
|
||||
|
||||
/**
|
||||
* @brief 设置背景颜色
|
||||
* @param normal 正常状态颜色
|
||||
* @param hovered 悬停状态颜色(可选,默认透明)
|
||||
* @param pressed 按下状态颜色(可选,默认透明)
|
||||
*/
|
||||
void setBackgroundColor(SDL_Color normal);
|
||||
/**
|
||||
* @brief 设置边框
|
||||
* @param thickness 边框厚度(像素)
|
||||
* @param color 边框颜色
|
||||
*/
|
||||
void setBorder(int thickness, SDL_Color color);
|
||||
|
||||
|
||||
ButtonData& getButtonDate() {
|
||||
m_buttonData.rect = m_rect;
|
||||
return m_buttonData;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
|
||||
|
||||
|
||||
ButtonData m_buttonData;
|
||||
|
||||
|
||||
};
|
||||
43
src/ui/managers/GameUIManager.cpp
Normal file
43
src/ui/managers/GameUIManager.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "GameUIManager.h"
|
||||
|
||||
GameUIManager::GameUIManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
GameUIManager::~GameUIManager() {
|
||||
|
||||
}
|
||||
|
||||
void GameUIManager::init() {
|
||||
auto button = std::make_unique<Button>();
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
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();
|
||||
//SDL_Log("collect data\n");
|
||||
for (auto& button : m_buttons) {
|
||||
if(!button->isVisible()) {
|
||||
continue;
|
||||
|
||||
}
|
||||
m_uiRenderData.buttons.push_back(button->getButtonDate());
|
||||
}
|
||||
}
|
||||
22
src/ui/managers/GameUIManager.h
Normal file
22
src/ui/managers/GameUIManager.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include "ui/components/Button.h"
|
||||
#include "ui/base/UIRenderData.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
class GameUIManager {
|
||||
private:
|
||||
std::vector<std::unique_ptr<Button>> m_buttons;
|
||||
UIRenderData m_uiRenderData;
|
||||
|
||||
public:
|
||||
GameUIManager();
|
||||
~GameUIManager();
|
||||
|
||||
void init();
|
||||
|
||||
const UIRenderData& getUIRenderData();
|
||||
// 收集渲染数据
|
||||
void CollectRenderData();
|
||||
};
|
||||
Reference in New Issue
Block a user