refactor: 统一边界框方法名称为 boundingBox
将 getBoundingBox 方法重命名为 boundingBox,以遵循更简洁的命名规范 同时更新相关文档和示例代码中的调用
This commit is contained in:
parent
377ec373b0
commit
ea5ecd383f
|
|
@ -316,8 +316,8 @@ if (buttonFrame) {
|
|||
**按钮使用世界坐标空间(World Space)**:
|
||||
|
||||
```cpp
|
||||
// Button::getBoundingBox() 返回世界坐标系的包围盒
|
||||
Rect Button::getBoundingBox() const {
|
||||
// Button::boundingBox() 返回世界坐标系的包围盒
|
||||
Rect Button::boundingBox() const {
|
||||
auto pos = getRenderPosition(); // 获取世界坐标位置
|
||||
// ... 计算包围盒
|
||||
return Rect(x0, y0, w, h); // 世界坐标
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public:
|
|||
}
|
||||
|
||||
// 必须实现 getBoundingBox 方法
|
||||
Rect getBoundingBox() const override {
|
||||
Rect boundingBox() const override {
|
||||
Vec2 pos = getPosition();
|
||||
return Rect(pos.x - width_ / 2, pos.y - height_ / 2, width_, height_);
|
||||
}
|
||||
|
|
@ -212,7 +212,7 @@ private:
|
|||
## 关键要点
|
||||
|
||||
1. **必须调用 `setSpatialIndexed(true)`** - 启用节点的空间索引
|
||||
2. **必须实现 `getBoundingBox()`** - 返回准确的边界框
|
||||
2. **必须实现 `boundingBox()`** - 返回准确的边界框
|
||||
3. **在 `onEnter()` 中调用 `Scene::onEnter()`** - 确保节点正确注册到空间索引
|
||||
4. **使用 `queryCollisions()`** - 自动利用空间索引优化检测
|
||||
|
||||
|
|
|
|||
|
|
@ -16,10 +16,11 @@ public:
|
|||
|
||||
void setColliding(bool colliding) { isColliding_ = colliding; }
|
||||
|
||||
Rect getBoundingBox() const override {
|
||||
Rect boundingBox() const override {
|
||||
// 返回实际的矩形边界
|
||||
Vec2 position = pos();
|
||||
return Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_);
|
||||
return Rect(position.x - width_ / 2, position.y - height_ / 2, width_,
|
||||
height_);
|
||||
}
|
||||
|
||||
void onRender(RenderBackend &renderer) override {
|
||||
|
|
@ -27,16 +28,16 @@ public:
|
|||
|
||||
// 绘制填充矩形
|
||||
Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.8f) : color_;
|
||||
renderer.fillRect(
|
||||
Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_),
|
||||
renderer.fillRect(Rect(position.x - width_ / 2, position.y - height_ / 2,
|
||||
width_, height_),
|
||||
fillColor);
|
||||
|
||||
// 绘制边框
|
||||
Color borderColor = isColliding_ ? Color(1.0f, 0.0f, 0.0f, 1.0f)
|
||||
: Color(1.0f, 1.0f, 1.0f, 0.5f);
|
||||
float borderWidth = isColliding_ ? 3.0f : 2.0f;
|
||||
renderer.drawRect(
|
||||
Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_),
|
||||
renderer.drawRect(Rect(position.x - width_ / 2, position.y - height_ / 2,
|
||||
width_, height_),
|
||||
borderColor, borderWidth);
|
||||
}
|
||||
|
||||
|
|
@ -149,7 +150,8 @@ private:
|
|||
addChild(fpsText_);
|
||||
|
||||
// 创建退出提示文本
|
||||
float screenHeight = static_cast<float>(Application::instance().getConfig().height);
|
||||
float screenHeight =
|
||||
static_cast<float>(Application::instance().getConfig().height);
|
||||
exitHintText_ = Text::create("按 + 键退出", infoFont_);
|
||||
exitHintText_->setPosition(50.0f, screenHeight - 50.0f);
|
||||
exitHintText_->setTextColor(Color(0.8f, 0.8f, 0.8f, 1.0f));
|
||||
|
|
@ -238,8 +240,7 @@ private:
|
|||
// 程序入口
|
||||
// ============================================================================
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int main(int argc, char **argv) {
|
||||
// 初始化日志系统
|
||||
Logger::init();
|
||||
Logger::setLevel(LogLevel::Debug);
|
||||
|
|
|
|||
|
|
@ -123,8 +123,7 @@ void GameScene::onUpdate(float dt) {
|
|||
}
|
||||
|
||||
if (bird_->isLiving() && GAME_HEIGHT - bird_->pos().y <= 123.0f) {
|
||||
bird_->setPosition(
|
||||
extra2d::Vec2(bird_->pos().x, GAME_HEIGHT - 123.0f));
|
||||
bird_->setPosition(extra2d::Vec2(bird_->pos().x, GAME_HEIGHT - 123.0f));
|
||||
bird_->setStatus(Bird::Status::Still);
|
||||
onHit();
|
||||
}
|
||||
|
|
@ -159,7 +158,7 @@ bool GameScene::checkCollision() {
|
|||
if (!bird_ || !pipes_)
|
||||
return false;
|
||||
|
||||
extra2d::Rect birdBox = bird_->getBoundingBox();
|
||||
extra2d::Rect birdBox = bird_->boundingBox();
|
||||
|
||||
// 检查与每个水管的碰撞
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ void StartScene::onEnter() {
|
|||
playBtn_->setAnchor(0.5f, 0.5f);
|
||||
// PLAY 按钮在小鸟下方
|
||||
playBtn_->setPosition(screenWidth / 2.0f,
|
||||
screenHeight - playBtn_->getSize().height - 100.0f);
|
||||
screenHeight - playBtn_->size().height - 100.0f);
|
||||
playBtn_->setOnClick([this]() {
|
||||
ResLoader::playMusic(MusicType::Click);
|
||||
startGame();
|
||||
|
|
@ -95,7 +95,7 @@ void StartScene::onEnter() {
|
|||
shareBtn_->setAnchor(0.5f, 0.5f);
|
||||
// SHARE 按钮在 PLAY 按钮下方,靠近地面
|
||||
shareBtn_->setPosition(screenWidth / 2.0f,
|
||||
screenHeight - shareBtn_->getSize().height - 80.0f);
|
||||
screenHeight - shareBtn_->size().height - 80.0f);
|
||||
shareBtn_->setOnClick([this]() {
|
||||
ResLoader::playMusic(MusicType::Click);
|
||||
// 分享功能暂不实现
|
||||
|
|
|
|||
|
|
@ -7,9 +7,7 @@
|
|||
|
||||
namespace flappybird {
|
||||
|
||||
Bird::Bird() {
|
||||
setStatus(Status::Idle);
|
||||
}
|
||||
Bird::Bird() { setStatus(Status::Idle); }
|
||||
|
||||
Bird::~Bird() = default;
|
||||
|
||||
|
|
@ -28,7 +26,8 @@ void Bird::initAnimations() {
|
|||
// 加载动画帧序列: 0 -> 1 -> 2 -> 1
|
||||
int frameSequence[] = {0, 1, 2, 1};
|
||||
for (int frameIndex : frameSequence) {
|
||||
auto frameSprite = ResLoader::getKeyFrame(prefix + std::to_string(frameIndex));
|
||||
auto frameSprite =
|
||||
ResLoader::getKeyFrame(prefix + std::to_string(frameIndex));
|
||||
if (frameSprite) {
|
||||
frames_.push_back(frameSprite);
|
||||
} else {
|
||||
|
|
@ -41,25 +40,28 @@ void Bird::initAnimations() {
|
|||
sprite_ = extra2d::Sprite::create();
|
||||
setCurrentFrame(0);
|
||||
addChild(sprite_);
|
||||
E2D_LOG_INFO("小鸟动画创建成功: 颜色={}, 帧数={}", colorMode, frames_.size());
|
||||
E2D_LOG_INFO("小鸟动画创建成功: 颜色={}, 帧数={}", colorMode,
|
||||
frames_.size());
|
||||
} else {
|
||||
E2D_LOG_ERROR("小鸟动画创建失败: 没有找到任何动画帧");
|
||||
}
|
||||
}
|
||||
|
||||
void Bird::setCurrentFrame(int frameIndex) {
|
||||
if (frames_.empty() || !sprite_) return;
|
||||
if (frames_.empty() || !sprite_)
|
||||
return;
|
||||
|
||||
frameIndex = frameIndex % static_cast<int>(frames_.size());
|
||||
currentFrame_ = frameIndex;
|
||||
|
||||
auto& frame = frames_[frameIndex];
|
||||
auto &frame = frames_[frameIndex];
|
||||
sprite_->setTexture(frame->getTexture());
|
||||
sprite_->setTextureRect(frame->getRect());
|
||||
}
|
||||
|
||||
void Bird::updateFrameAnimation(float dt) {
|
||||
if (frames_.empty() || status_ == Status::Still) return;
|
||||
if (frames_.empty() || status_ == Status::Still)
|
||||
return;
|
||||
|
||||
frameTimer_ += dt;
|
||||
|
||||
|
|
@ -87,7 +89,7 @@ void Bird::onUpdate(float dt) {
|
|||
}
|
||||
}
|
||||
|
||||
void Bird::onRender(extra2d::RenderBackend& renderer) {
|
||||
void Bird::onRender(extra2d::RenderBackend &renderer) {
|
||||
// 精灵会自动渲染,这里只需要处理旋转和偏移
|
||||
if (sprite_) {
|
||||
sprite_->setRotation(rotation_);
|
||||
|
|
@ -105,7 +107,8 @@ void Bird::onRender(extra2d::RenderBackend& renderer) {
|
|||
}
|
||||
|
||||
void Bird::fall(float dt) {
|
||||
if (!living_) return;
|
||||
if (!living_)
|
||||
return;
|
||||
|
||||
// 更新垂直位置
|
||||
extra2d::Vec2 position = pos();
|
||||
|
|
@ -132,7 +135,8 @@ void Bird::fall(float dt) {
|
|||
}
|
||||
|
||||
void Bird::jump() {
|
||||
if (!living_) return;
|
||||
if (!living_)
|
||||
return;
|
||||
|
||||
// 给小鸟向上的速度
|
||||
speed_ = -jumpSpeed;
|
||||
|
|
@ -180,16 +184,12 @@ void Bird::setStatus(Status status) {
|
|||
}
|
||||
}
|
||||
|
||||
extra2d::Rect Bird::getBoundingBox() const {
|
||||
extra2d::Rect Bird::boundingBox() const {
|
||||
extra2d::Vec2 position = pos();
|
||||
// 小鸟碰撞框大小约为 24x24
|
||||
float halfSize = 12.0f;
|
||||
return extra2d::Rect(
|
||||
position.x - halfSize,
|
||||
position.y - halfSize,
|
||||
halfSize * 2.0f,
|
||||
halfSize * 2.0f
|
||||
);
|
||||
return extra2d::Rect(position.x - halfSize, position.y - halfSize,
|
||||
halfSize * 2.0f, halfSize * 2.0f);
|
||||
}
|
||||
|
||||
} // namespace flappybird
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public:
|
|||
* @brief 渲染小鸟
|
||||
* @param renderer 渲染后端
|
||||
*/
|
||||
void onRender(extra2d::RenderBackend& renderer) override;
|
||||
void onRender(extra2d::RenderBackend &renderer) override;
|
||||
|
||||
/**
|
||||
* @brief 进入场景时调用
|
||||
|
|
@ -90,7 +90,7 @@ public:
|
|||
* @brief 获取边界框(用于碰撞检测)
|
||||
* @return 边界框
|
||||
*/
|
||||
extra2d::Rect getBoundingBox() const override;
|
||||
extra2d::Rect boundingBox() const override;
|
||||
|
||||
private:
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
// ============================================================================
|
||||
|
||||
#include "Pipe.h"
|
||||
#include "ResLoader.h"
|
||||
#include "BaseScene.h"
|
||||
#include "ResLoader.h"
|
||||
|
||||
|
||||
namespace flappybird {
|
||||
|
||||
|
|
@ -31,12 +32,14 @@ void Pipe::onEnter() {
|
|||
// 与屏幕底部最小距离不小于地面上方 100 像素
|
||||
float minHeight = 100.0f;
|
||||
float maxHeight = screenHeight - landHeight - 100.0f - gapHeight_;
|
||||
float height = static_cast<float>(extra2d::randomInt(static_cast<int>(minHeight), static_cast<int>(maxHeight)));
|
||||
float height = static_cast<float>(extra2d::randomInt(
|
||||
static_cast<int>(minHeight), static_cast<int>(maxHeight)));
|
||||
|
||||
// 创建上水管
|
||||
auto topFrame = ResLoader::getKeyFrame("pipe_above");
|
||||
if (topFrame) {
|
||||
topPipe_ = extra2d::Sprite::create(topFrame->getTexture(), topFrame->getRect());
|
||||
topPipe_ =
|
||||
extra2d::Sprite::create(topFrame->getTexture(), topFrame->getRect());
|
||||
topPipe_->setAnchor(extra2d::Vec2(0.5f, 1.0f)); // 锚点设在底部中心
|
||||
topPipe_->setPosition(extra2d::Vec2(0.0f, height - gapHeight_ / 2.0f));
|
||||
addChild(topPipe_);
|
||||
|
|
@ -45,7 +48,8 @@ void Pipe::onEnter() {
|
|||
// 创建下水管
|
||||
auto bottomFrame = ResLoader::getKeyFrame("pipe_below");
|
||||
if (bottomFrame) {
|
||||
bottomPipe_ = extra2d::Sprite::create(bottomFrame->getTexture(), bottomFrame->getRect());
|
||||
bottomPipe_ = extra2d::Sprite::create(bottomFrame->getTexture(),
|
||||
bottomFrame->getRect());
|
||||
bottomPipe_->setAnchor(extra2d::Vec2(0.5f, 0.0f)); // 锚点设在顶部中心
|
||||
bottomPipe_->setPosition(extra2d::Vec2(0.0f, height + gapHeight_ / 2.0f));
|
||||
addChild(bottomPipe_);
|
||||
|
|
@ -55,7 +59,7 @@ void Pipe::onEnter() {
|
|||
|
||||
Pipe::~Pipe() = default;
|
||||
|
||||
extra2d::Rect Pipe::getBoundingBox() const {
|
||||
extra2d::Rect Pipe::boundingBox() const {
|
||||
// 返回整个水管的边界框(包含上下两根)
|
||||
extra2d::Vec2 position = pos();
|
||||
|
||||
|
|
@ -66,16 +70,12 @@ extra2d::Rect Pipe::getBoundingBox() const {
|
|||
// 使用游戏逻辑高度
|
||||
float screenHeight = GAME_HEIGHT;
|
||||
|
||||
return extra2d::Rect(
|
||||
position.x - halfWidth,
|
||||
0.0f,
|
||||
pipeWidth,
|
||||
screenHeight
|
||||
);
|
||||
return extra2d::Rect(position.x - halfWidth, 0.0f, pipeWidth, screenHeight);
|
||||
}
|
||||
|
||||
extra2d::Rect Pipe::getTopPipeBox() const {
|
||||
if (!topPipe_) return extra2d::Rect();
|
||||
if (!topPipe_)
|
||||
return extra2d::Rect();
|
||||
|
||||
extra2d::Vec2 position = pos();
|
||||
extra2d::Vec2 topPos = topPipe_->pos();
|
||||
|
|
@ -84,16 +84,14 @@ extra2d::Rect Pipe::getTopPipeBox() const {
|
|||
float pipeWidth = 52.0f;
|
||||
float pipeHeight = 320.0f;
|
||||
|
||||
return extra2d::Rect(
|
||||
position.x - pipeWidth / 2.0f,
|
||||
position.y + topPos.y - pipeHeight,
|
||||
pipeWidth,
|
||||
pipeHeight
|
||||
);
|
||||
return extra2d::Rect(position.x - pipeWidth / 2.0f,
|
||||
position.y + topPos.y - pipeHeight, pipeWidth,
|
||||
pipeHeight);
|
||||
}
|
||||
|
||||
extra2d::Rect Pipe::getBottomPipeBox() const {
|
||||
if (!bottomPipe_) return extra2d::Rect();
|
||||
if (!bottomPipe_)
|
||||
return extra2d::Rect();
|
||||
|
||||
extra2d::Vec2 position = pos();
|
||||
extra2d::Vec2 bottomPos = bottomPipe_->pos();
|
||||
|
|
@ -102,12 +100,8 @@ extra2d::Rect Pipe::getBottomPipeBox() const {
|
|||
float pipeWidth = 52.0f;
|
||||
float pipeHeight = 320.0f;
|
||||
|
||||
return extra2d::Rect(
|
||||
position.x - pipeWidth / 2.0f,
|
||||
position.y + bottomPos.y,
|
||||
pipeWidth,
|
||||
pipeHeight
|
||||
);
|
||||
return extra2d::Rect(position.x - pipeWidth / 2.0f, position.y + bottomPos.y,
|
||||
pipeWidth, pipeHeight);
|
||||
}
|
||||
|
||||
} // namespace flappybird
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public:
|
|||
* @brief 获取边界框(用于碰撞检测)
|
||||
* @return 边界框
|
||||
*/
|
||||
extra2d::Rect getBoundingBox() const override;
|
||||
extra2d::Rect boundingBox() const override;
|
||||
|
||||
/**
|
||||
* @brief 获取上水管边界框
|
||||
|
|
|
|||
|
|
@ -37,8 +37,8 @@ public:
|
|||
bool isColliding() const { return isColliding_; }
|
||||
int getId() const { return id_; }
|
||||
|
||||
// 必须实现 getBoundingBox() 才能参与空间索引碰撞检测
|
||||
Rect getBoundingBox() const override {
|
||||
// 必须实现 boundingBox() 才能参与空间索引碰撞检测
|
||||
Rect boundingBox() const override {
|
||||
Vec2 position = pos();
|
||||
return Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_);
|
||||
}
|
||||
|
|
@ -65,13 +65,15 @@ public:
|
|||
|
||||
// 碰撞时变红色
|
||||
Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.9f) : color_;
|
||||
renderer.fillRect(Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_),
|
||||
renderer.fillRect(
|
||||
Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_),
|
||||
fillColor);
|
||||
|
||||
// 绘制边框
|
||||
Color borderColor = isColliding_ ? Color(1.0f, 0.0f, 0.0f, 1.0f)
|
||||
: Color(0.3f, 0.3f, 0.3f, 0.5f);
|
||||
renderer.drawRect(Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_),
|
||||
renderer.drawRect(
|
||||
Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_),
|
||||
borderColor, 1.0f);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public:
|
|||
/// 获取遮罩尺寸
|
||||
int getWidth() const { return width_; }
|
||||
int getHeight() const { return height_; }
|
||||
Size getSize() const {
|
||||
Size size() const {
|
||||
return Size(static_cast<float>(width_), static_cast<float>(height_));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <graphics/texture.h>
|
||||
#include <graphics/alpha_mask.h>
|
||||
#include <graphics/texture.h>
|
||||
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
|
|
@ -15,17 +16,21 @@ namespace extra2d {
|
|||
// ============================================================================
|
||||
class GLTexture : public Texture {
|
||||
public:
|
||||
GLTexture(int width, int height, const uint8_t* pixels, int channels);
|
||||
GLTexture(const std::string& filepath);
|
||||
GLTexture(int width, int height, const uint8_t *pixels, int channels);
|
||||
GLTexture(const std::string &filepath);
|
||||
~GLTexture();
|
||||
|
||||
// Texture 接口实现
|
||||
int getWidth() const override { return width_; }
|
||||
int getHeight() const override { return height_; }
|
||||
Size getSize() const override { return Size(static_cast<float>(width_), static_cast<float>(height_)); }
|
||||
Size size() const override {
|
||||
return Size(static_cast<float>(width_), static_cast<float>(height_));
|
||||
}
|
||||
int getChannels() const override { return channels_; }
|
||||
PixelFormat getFormat() const override;
|
||||
void* getNativeHandle() const override { return reinterpret_cast<void*>(static_cast<uintptr_t>(textureID_)); }
|
||||
void *getNativeHandle() const override {
|
||||
return reinterpret_cast<void *>(static_cast<uintptr_t>(textureID_));
|
||||
}
|
||||
bool isValid() const override { return textureID_ != 0; }
|
||||
void setFilter(bool linear) override;
|
||||
void setWrap(bool repeat) override;
|
||||
|
|
@ -34,7 +39,7 @@ public:
|
|||
static Ptr<Texture> create(int width, int height, PixelFormat format);
|
||||
|
||||
// 加载压缩纹理(KTX/DDS 格式)
|
||||
bool loadCompressed(const std::string& filepath);
|
||||
bool loadCompressed(const std::string &filepath);
|
||||
|
||||
// OpenGL 特定
|
||||
GLuint getTextureID() const { return textureID_; }
|
||||
|
|
@ -45,8 +50,10 @@ public:
|
|||
size_t getDataSize() const { return dataSize_; }
|
||||
|
||||
// Alpha 遮罩
|
||||
bool hasAlphaMask() const { return alphaMask_ != nullptr && alphaMask_->isValid(); }
|
||||
const AlphaMask* getAlphaMask() const { return alphaMask_.get(); }
|
||||
bool hasAlphaMask() const {
|
||||
return alphaMask_ != nullptr && alphaMask_->isValid();
|
||||
}
|
||||
const AlphaMask *getAlphaMask() const { return alphaMask_.get(); }
|
||||
void generateAlphaMask(); // 从当前纹理数据生成遮罩
|
||||
|
||||
private:
|
||||
|
|
@ -61,12 +68,12 @@ private:
|
|||
std::vector<uint8_t> pixelData_;
|
||||
std::unique_ptr<AlphaMask> alphaMask_;
|
||||
|
||||
void createTexture(const uint8_t* pixels);
|
||||
void createTexture(const uint8_t *pixels);
|
||||
|
||||
// KTX 文件加载
|
||||
bool loadKTX(const std::string& filepath);
|
||||
bool loadKTX(const std::string &filepath);
|
||||
// DDS 文件加载
|
||||
bool loadDDS(const std::string& filepath);
|
||||
bool loadDDS(const std::string &filepath);
|
||||
};
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ public:
|
|||
|
||||
int getWidth() const { return width_; }
|
||||
int getHeight() const { return height_; }
|
||||
Vec2 getSize() const {
|
||||
Vec2 size() const {
|
||||
return Vec2(static_cast<float>(width_), static_cast<float>(height_));
|
||||
}
|
||||
PixelFormat getColorFormat() const { return colorFormat_; }
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <core/types.h>
|
||||
#include <core/math_types.h>
|
||||
#include <core/types.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -40,7 +41,7 @@ public:
|
|||
// 获取尺寸
|
||||
virtual int getWidth() const = 0;
|
||||
virtual int getHeight() const = 0;
|
||||
virtual Size getSize() const = 0;
|
||||
virtual Size size() const = 0;
|
||||
|
||||
// 获取通道数
|
||||
virtual int getChannels() const = 0;
|
||||
|
|
@ -49,7 +50,7 @@ public:
|
|||
virtual PixelFormat getFormat() const = 0;
|
||||
|
||||
// 获取原始句柄(用于底层渲染)
|
||||
virtual void* getNativeHandle() const = 0;
|
||||
virtual void *getNativeHandle() const = 0;
|
||||
|
||||
// 是否有效
|
||||
virtual bool isValid() const = 0;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <core/types.h>
|
||||
#include <core/string.h>
|
||||
#include <core/math_types.h>
|
||||
#include <core/string.h>
|
||||
#include <core/types.h>
|
||||
#include <functional>
|
||||
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
|
@ -27,7 +28,8 @@ struct WindowConfig {
|
|||
bool centerWindow = true;
|
||||
bool enableCursors = true;
|
||||
bool enableDpiScale = true;
|
||||
bool fullscreenDesktop = true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN
|
||||
bool fullscreenDesktop =
|
||||
true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
|
|
@ -55,7 +57,7 @@ public:
|
|||
~Window();
|
||||
|
||||
// 创建窗口
|
||||
bool create(const WindowConfig& config);
|
||||
bool create(const WindowConfig &config);
|
||||
void destroy();
|
||||
|
||||
// 窗口操作
|
||||
|
|
@ -65,7 +67,7 @@ public:
|
|||
void setShouldClose(bool close);
|
||||
|
||||
// 窗口属性
|
||||
void setTitle(const std::string& title);
|
||||
void setTitle(const std::string &title);
|
||||
void setSize(int width, int height);
|
||||
void setPosition(int x, int y);
|
||||
void setFullscreen(bool fullscreen);
|
||||
|
|
@ -75,7 +77,9 @@ public:
|
|||
// 获取窗口属性
|
||||
int getWidth() const { return width_; }
|
||||
int getHeight() const { return height_; }
|
||||
Size getSize() const { return Size(static_cast<float>(width_), static_cast<float>(height_)); }
|
||||
Size size() const {
|
||||
return Size(static_cast<float>(width_), static_cast<float>(height_));
|
||||
}
|
||||
Vec2 pos() const;
|
||||
bool isFullscreen() const { return fullscreen_; }
|
||||
bool isVSync() const { return vsync_; }
|
||||
|
|
@ -91,19 +95,19 @@ public:
|
|||
bool isMaximized() const;
|
||||
|
||||
// 获取 SDL2 窗口和 GL 上下文
|
||||
SDL_Window* getSDLWindow() const { return sdlWindow_; }
|
||||
SDL_Window *getSDLWindow() const { return sdlWindow_; }
|
||||
SDL_GLContext getGLContext() const { return glContext_; }
|
||||
|
||||
// 设置/获取用户数据
|
||||
void setUserData(void* data) { userData_ = data; }
|
||||
void* getUserData() const { return userData_; }
|
||||
void setUserData(void *data) { userData_ = data; }
|
||||
void *getUserData() const { return userData_; }
|
||||
|
||||
// 事件队列
|
||||
void setEventQueue(EventQueue* queue) { eventQueue_ = queue; }
|
||||
EventQueue* getEventQueue() const { return eventQueue_; }
|
||||
void setEventQueue(EventQueue *queue) { eventQueue_ = queue; }
|
||||
EventQueue *getEventQueue() const { return eventQueue_; }
|
||||
|
||||
// 获取输入管理器
|
||||
Input* getInput() const { return input_.get(); }
|
||||
Input *getInput() const { return input_.get(); }
|
||||
|
||||
// 光标操作 (PC 端有效,Switch 上为空操作)
|
||||
void setCursor(CursorShape shape);
|
||||
|
|
@ -115,16 +119,18 @@ public:
|
|||
using FocusCallback = std::function<void(bool focused)>;
|
||||
using CloseCallback = std::function<void()>;
|
||||
|
||||
void setResizeCallback(ResizeCallback callback) { resizeCallback_ = callback; }
|
||||
void setResizeCallback(ResizeCallback callback) {
|
||||
resizeCallback_ = callback;
|
||||
}
|
||||
void setFocusCallback(FocusCallback callback) { focusCallback_ = callback; }
|
||||
void setCloseCallback(CloseCallback callback) { closeCallback_ = callback; }
|
||||
|
||||
private:
|
||||
// SDL2 状态
|
||||
SDL_Window* sdlWindow_;
|
||||
SDL_Window *sdlWindow_;
|
||||
SDL_GLContext glContext_;
|
||||
SDL_Cursor* sdlCursors_[9]; // 光标缓存
|
||||
SDL_Cursor* currentCursor_;
|
||||
SDL_Cursor *sdlCursors_[9]; // 光标缓存
|
||||
SDL_Cursor *currentCursor_;
|
||||
|
||||
int width_;
|
||||
int height_;
|
||||
|
|
@ -135,15 +141,15 @@ private:
|
|||
float contentScaleX_;
|
||||
float contentScaleY_;
|
||||
bool enableDpiScale_;
|
||||
void* userData_;
|
||||
EventQueue* eventQueue_;
|
||||
void *userData_;
|
||||
EventQueue *eventQueue_;
|
||||
UniquePtr<Input> input_;
|
||||
|
||||
ResizeCallback resizeCallback_;
|
||||
FocusCallback focusCallback_;
|
||||
CloseCallback closeCallback_;
|
||||
|
||||
bool initSDL(const WindowConfig& config);
|
||||
bool initSDL(const WindowConfig &config);
|
||||
void deinitSDL();
|
||||
void initCursors();
|
||||
void deinitCursors();
|
||||
|
|
|
|||
|
|
@ -5,12 +5,13 @@
|
|||
#include <core/math_types.h>
|
||||
#include <core/types.h>
|
||||
#include <event/event_dispatcher.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <functional>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
// 前向声明
|
||||
|
|
@ -82,7 +83,7 @@ public:
|
|||
* @brief 设置颜色
|
||||
* @param color RGB颜色
|
||||
*/
|
||||
void setColor(const Color3B& color);
|
||||
void setColor(const Color3B &color);
|
||||
Color3B getColor() const { return color_; }
|
||||
|
||||
/**
|
||||
|
|
@ -148,7 +149,7 @@ public:
|
|||
// ------------------------------------------------------------------------
|
||||
// 边界框(用于空间索引)
|
||||
// ------------------------------------------------------------------------
|
||||
virtual Rect getBoundingBox() const;
|
||||
virtual Rect boundingBox() const;
|
||||
|
||||
// 是否需要参与空间索引(默认 true)
|
||||
void setSpatialIndexed(bool indexed) { spatialIndexed_ = indexed; }
|
||||
|
|
@ -170,12 +171,12 @@ public:
|
|||
* @brief 创建 Tween 动画
|
||||
* @return Tween 对象引用,支持链式调用
|
||||
*/
|
||||
Tween& tween();
|
||||
Tween &tween();
|
||||
|
||||
/**
|
||||
* @brief 创建具名 Tween 动画
|
||||
*/
|
||||
Tween& tween(const std::string &name);
|
||||
Tween &tween(const std::string &name);
|
||||
|
||||
/**
|
||||
* @brief 移动到目标位置
|
||||
|
|
@ -216,12 +217,14 @@ public:
|
|||
/**
|
||||
* @brief 淡入(透明度从当前到1)
|
||||
*/
|
||||
Tween &fadeIn(float duration, TweenEasing easing = static_cast<TweenEasing>(0));
|
||||
Tween &fadeIn(float duration,
|
||||
TweenEasing easing = static_cast<TweenEasing>(0));
|
||||
|
||||
/**
|
||||
* @brief 淡出(透明度从当前到0)
|
||||
*/
|
||||
Tween &fadeOut(float duration, TweenEasing easing = static_cast<TweenEasing>(0));
|
||||
Tween &fadeOut(float duration,
|
||||
TweenEasing easing = static_cast<TweenEasing>(0));
|
||||
|
||||
/**
|
||||
* @brief 透明度变化到目标值
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ public:
|
|||
void addPoint(const Vec2 &point);
|
||||
void clearPoints();
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDraw(RenderBackend &renderer) override;
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ public:
|
|||
static Ptr<Sprite> create(Ptr<Texture> texture);
|
||||
static Ptr<Sprite> create(Ptr<Texture> texture, const Rect &rect);
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDraw(RenderBackend &renderer) override;
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ public:
|
|||
*/
|
||||
void setStateTextColor(const Color &colorOff, const Color &colorOn);
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDrawWidget(RenderBackend &renderer) override;
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ public:
|
|||
// ------------------------------------------------------------------------
|
||||
void setOnStateChange(Function<void(bool)> callback);
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDrawWidget(RenderBackend &renderer) override;
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ public:
|
|||
Vec2 getTextSize() const;
|
||||
float getLineHeight() const;
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDrawWidget(RenderBackend &renderer) override;
|
||||
|
|
@ -138,7 +138,8 @@ private:
|
|||
mutable bool sizeDirty_ = true;
|
||||
|
||||
void updateCache() const;
|
||||
void drawText(RenderBackend &renderer, const Vec2 &position, const Color &color);
|
||||
void drawText(RenderBackend &renderer, const Vec2 &position,
|
||||
const Color &color);
|
||||
Vec2 calculateDrawPosition() const;
|
||||
std::vector<std::string> splitLines() const;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ public:
|
|||
void setStripeSpeed(float speed); // 条纹移动速度
|
||||
float getStripeSpeed() const { return stripeSpeed_; }
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onUpdate(float deltaTime) override;
|
||||
|
|
@ -210,8 +210,10 @@ private:
|
|||
|
||||
Color getCurrentFillColor() const;
|
||||
std::string formatText() const;
|
||||
void drawRoundedRect(RenderBackend &renderer, const Rect &rect, const Color &color, float radius);
|
||||
void fillRoundedRect(RenderBackend &renderer, const Rect &rect, const Color &color, float radius);
|
||||
void drawRoundedRect(RenderBackend &renderer, const Rect &rect,
|
||||
const Color &color, float radius);
|
||||
void fillRoundedRect(RenderBackend &renderer, const Rect &rect,
|
||||
const Color &color, float radius);
|
||||
void drawStripes(RenderBackend &renderer, const Rect &rect);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ public:
|
|||
// ------------------------------------------------------------------------
|
||||
void setOnStateChange(Function<void(bool)> callback);
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDrawWidget(RenderBackend &renderer) override;
|
||||
|
|
@ -112,12 +112,12 @@ public:
|
|||
void selectButton(RadioButton *button);
|
||||
RadioButton *getSelectedButton() const { return selectedButton_; }
|
||||
|
||||
void setOnSelectionChange(Function<void(RadioButton*)> callback);
|
||||
void setOnSelectionChange(Function<void(RadioButton *)> callback);
|
||||
|
||||
private:
|
||||
std::vector<RadioButton*> buttons_;
|
||||
std::vector<RadioButton *> buttons_;
|
||||
RadioButton *selectedButton_ = nullptr;
|
||||
Function<void(RadioButton*)> onSelectionChange_;
|
||||
Function<void(RadioButton *)> onSelectionChange_;
|
||||
};
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ public:
|
|||
void setOnDragStart(Function<void()> callback);
|
||||
void setOnDragEnd(Function<void()> callback);
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDrawWidget(RenderBackend &renderer) override;
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@
|
|||
|
||||
#include <core/color.h>
|
||||
#include <core/types.h>
|
||||
#include <graphics/font.h>
|
||||
#include <ui/widget.h>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <graphics/font.h>
|
||||
#include <string>
|
||||
#include <ui/widget.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -98,7 +99,7 @@ public:
|
|||
Vec2 getTextSize() const;
|
||||
float getLineHeight() const;
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
protected:
|
||||
void onDrawWidget(RenderBackend &renderer) override;
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ public:
|
|||
|
||||
void setSize(const Size &size);
|
||||
void setSize(float width, float height);
|
||||
Size getSize() const { return size_; }
|
||||
Size size() const { return size_; }
|
||||
|
||||
Rect getBoundingBox() const override;
|
||||
Rect boundingBox() const override;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 鼠标事件处理(子类可重写)
|
||||
|
|
@ -55,7 +55,7 @@ public:
|
|||
protected:
|
||||
// 供子类使用的辅助方法
|
||||
bool isPointInside(float x, float y) const {
|
||||
return getBoundingBox().containsPoint(Point(x, y));
|
||||
return boundingBox().containsPoint(Point(x, y));
|
||||
}
|
||||
|
||||
// 子类重写此方法以支持自定义渲染
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <animation/tween.h>
|
||||
#include <cmath>
|
||||
#include <graphics/render_command.h>
|
||||
#include <scene/node.h>
|
||||
#include <scene/scene.h>
|
||||
#include <utils/logger.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
Node::Node() = default;
|
||||
|
||||
Node::~Node() {
|
||||
removeAllChildren();
|
||||
}
|
||||
Node::~Node() { removeAllChildren(); }
|
||||
|
||||
void Node::addChild(Ptr<Node> child) {
|
||||
if (!child || child.get() == this) {
|
||||
|
|
@ -200,7 +199,7 @@ void Node::setOpacity(float opacity) {
|
|||
|
||||
void Node::setVisible(bool visible) { visible_ = visible; }
|
||||
|
||||
void Node::setColor(const Color3B& color) { color_ = color; }
|
||||
void Node::setColor(const Color3B &color) { color_ = color; }
|
||||
|
||||
void Node::setFlipX(bool flipX) { flipX_ = flipX; }
|
||||
|
||||
|
|
@ -260,7 +259,7 @@ glm::mat4 Node::getWorldTransform() const {
|
|||
if (worldTransformDirty_) {
|
||||
// 使用线程局部存储的固定数组,避免每帧内存分配
|
||||
// 限制最大深度为 256 层,足以覆盖绝大多数场景
|
||||
thread_local std::array<const Node*, 256> nodeChainCache;
|
||||
thread_local std::array<const Node *, 256> nodeChainCache;
|
||||
thread_local size_t chainCount = 0;
|
||||
|
||||
chainCount = 0;
|
||||
|
|
@ -384,7 +383,7 @@ void Node::onDetachFromScene() {
|
|||
}
|
||||
}
|
||||
|
||||
Rect Node::getBoundingBox() const {
|
||||
Rect Node::boundingBox() const {
|
||||
// 默认返回一个以位置为中心的点矩形
|
||||
return Rect(position_.x, position_.y, 0, 0);
|
||||
}
|
||||
|
|
@ -394,7 +393,7 @@ void Node::updateSpatialIndex() {
|
|||
return;
|
||||
}
|
||||
|
||||
Rect newBounds = getBoundingBox();
|
||||
Rect newBounds = boundingBox();
|
||||
if (newBounds != lastSpatialBounds_) {
|
||||
scene_->updateNodeInSpatialIndex(this, lastSpatialBounds_, newBounds);
|
||||
lastSpatialBounds_ = newBounds;
|
||||
|
|
@ -462,13 +461,13 @@ void Node::collectRenderCommands(std::vector<RenderCommand> &commands,
|
|||
}
|
||||
}
|
||||
|
||||
Tween& Node::tween() {
|
||||
Tween &Node::tween() {
|
||||
auto tw = std::make_shared<Tween>(this);
|
||||
tweens_.push_back(tw);
|
||||
return *tweens_.back();
|
||||
}
|
||||
|
||||
Tween& Node::tween(const std::string &name) {
|
||||
Tween &Node::tween(const std::string &name) {
|
||||
auto tw = std::make_shared<Tween>(this, name);
|
||||
tweens_.push_back(tw);
|
||||
return *tweens_.back();
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ Node *hitTestTopmost(const Ptr<Node> &node, const Vec2 &worldPos) {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
Rect bounds = node->getBoundingBox();
|
||||
Rect bounds = node->boundingBox();
|
||||
if (!bounds.empty() && bounds.containsPoint(worldPos)) {
|
||||
return node.get();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@
|
|||
#include <cmath>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <graphics/render_command.h>
|
||||
#include <scene/shape_node.h>
|
||||
#include <limits>
|
||||
#include <scene/shape_node.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -124,7 +125,7 @@ void ShapeNode::clearPoints() {
|
|||
updateSpatialIndex();
|
||||
}
|
||||
|
||||
Rect ShapeNode::getBoundingBox() const {
|
||||
Rect ShapeNode::boundingBox() const {
|
||||
if (points_.empty()) {
|
||||
return Rect();
|
||||
}
|
||||
|
|
@ -262,16 +263,16 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
case ShapeType::Point:
|
||||
if (!points_.empty()) {
|
||||
cmd.type = RenderCommandType::FilledCircle;
|
||||
cmd.data =
|
||||
CircleCommandData{points_[0] + offset, lineWidth_ * 0.5f, color_, 8, 0.0f, true};
|
||||
cmd.data = CircleCommandData{
|
||||
points_[0] + offset, lineWidth_ * 0.5f, color_, 8, 0.0f, true};
|
||||
}
|
||||
break;
|
||||
|
||||
case ShapeType::Line:
|
||||
if (points_.size() >= 2) {
|
||||
cmd.type = RenderCommandType::Line;
|
||||
cmd.data = LineCommandData{points_[0] + offset, points_[1] + offset, color_,
|
||||
lineWidth_};
|
||||
cmd.data = LineCommandData{points_[0] + offset, points_[1] + offset,
|
||||
color_, lineWidth_};
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -281,14 +282,14 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
cmd.type = RenderCommandType::FilledRect;
|
||||
Rect rect(points_[0].x, points_[0].y, points_[2].x - points_[0].x,
|
||||
points_[2].y - points_[0].y);
|
||||
cmd.data =
|
||||
RectCommandData{Rect(rect.origin + offset, rect.size), color_, 0.0f, true};
|
||||
cmd.data = RectCommandData{Rect(rect.origin + offset, rect.size),
|
||||
color_, 0.0f, true};
|
||||
} else {
|
||||
cmd.type = RenderCommandType::Rect;
|
||||
Rect rect(points_[0].x, points_[0].y, points_[2].x - points_[0].x,
|
||||
points_[2].y - points_[0].y);
|
||||
cmd.data =
|
||||
RectCommandData{Rect(rect.origin + offset, rect.size), color_, lineWidth_, false};
|
||||
cmd.data = RectCommandData{Rect(rect.origin + offset, rect.size),
|
||||
color_, lineWidth_, false};
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -298,12 +299,12 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
float radius = points_[1].x;
|
||||
if (filled_) {
|
||||
cmd.type = RenderCommandType::FilledCircle;
|
||||
cmd.data =
|
||||
CircleCommandData{points_[0] + offset, radius, color_, segments_, 0.0f, true};
|
||||
cmd.data = CircleCommandData{points_[0] + offset, radius, color_,
|
||||
segments_, 0.0f, true};
|
||||
} else {
|
||||
cmd.type = RenderCommandType::Circle;
|
||||
cmd.data = CircleCommandData{points_[0] + offset, radius, color_, segments_,
|
||||
lineWidth_, false};
|
||||
cmd.data = CircleCommandData{points_[0] + offset, radius, color_,
|
||||
segments_, lineWidth_, false};
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -336,7 +337,8 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
|
|||
cmd.data = PolygonCommandData{transformedPoints, color_, 0.0f, true};
|
||||
} else {
|
||||
cmd.type = RenderCommandType::Polygon;
|
||||
cmd.data = PolygonCommandData{transformedPoints, color_, lineWidth_, false};
|
||||
cmd.data =
|
||||
PolygonCommandData{transformedPoints, color_, lineWidth_, false};
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ Ptr<Sprite> Sprite::create(Ptr<Texture> texture, const Rect &rect) {
|
|||
return sprite;
|
||||
}
|
||||
|
||||
Rect Sprite::getBoundingBox() const {
|
||||
Rect Sprite::boundingBox() const {
|
||||
if (!texture_ || !texture_->isValid()) {
|
||||
return Rect();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ void SpatialManager::rebuild() {
|
|||
auto objects = oldIndex->query(bounds);
|
||||
for (Node *node : objects) {
|
||||
if (node) {
|
||||
auto nodeBounds = node->getBoundingBox();
|
||||
auto nodeBounds = node->boundingBox();
|
||||
index_->insert(node, nodeBounds);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <app/application.h>
|
||||
#include <cmath>
|
||||
#include <core/string.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <ui/button.h>
|
||||
|
|
@ -93,7 +93,7 @@ Ptr<Button> Button::create(const std::string &text, Ptr<FontAtlas> font) {
|
|||
*/
|
||||
void Button::setText(const std::string &text) {
|
||||
text_ = text;
|
||||
if (font_ && getSize().empty()) {
|
||||
if (font_ && size().empty()) {
|
||||
Vec2 textSize = font_->measureText(text_);
|
||||
setSize(textSize.x + padding_.x * 2.0f, textSize.y + padding_.y * 2.0f);
|
||||
}
|
||||
|
|
@ -105,7 +105,7 @@ void Button::setText(const std::string &text) {
|
|||
*/
|
||||
void Button::setFont(Ptr<FontAtlas> font) {
|
||||
font_ = font;
|
||||
if (font_ && getSize().empty() && !text_.empty()) {
|
||||
if (font_ && size().empty() && !text_.empty()) {
|
||||
Vec2 textSize = font_->measureText(text_);
|
||||
setSize(textSize.x + padding_.x * 2.0f, textSize.y + padding_.y * 2.0f);
|
||||
}
|
||||
|
|
@ -117,7 +117,7 @@ void Button::setFont(Ptr<FontAtlas> font) {
|
|||
*/
|
||||
void Button::setPadding(const Vec2 &padding) {
|
||||
padding_ = padding;
|
||||
if (font_ && getSize().empty() && !text_.empty()) {
|
||||
if (font_ && size().empty() && !text_.empty()) {
|
||||
Vec2 textSize = font_->measureText(text_);
|
||||
setSize(textSize.x + padding_.x * 2.0f, textSize.y + padding_.y * 2.0f);
|
||||
}
|
||||
|
|
@ -128,9 +128,7 @@ void Button::setPadding(const Vec2 &padding) {
|
|||
* @param x X方向内边距
|
||||
* @param y Y方向内边距
|
||||
*/
|
||||
void Button::setPadding(float x, float y) {
|
||||
setPadding(Vec2(x, y));
|
||||
}
|
||||
void Button::setPadding(float x, float y) { setPadding(Vec2(x, y)); }
|
||||
|
||||
/**
|
||||
* @brief 设置文本颜色
|
||||
|
|
@ -356,20 +354,20 @@ void Button::setCustomSize(float width, float height) {
|
|||
* @brief 获取边界框
|
||||
* @return 边界矩形
|
||||
*/
|
||||
Rect Button::getBoundingBox() const {
|
||||
Rect Button::boundingBox() const {
|
||||
auto position = convertToWorldSpace(extra2d::Vec2::Zero());
|
||||
auto anchorPt = anchor();
|
||||
auto scaleVal = scale();
|
||||
auto size = getSize();
|
||||
auto widgetSize = size();
|
||||
|
||||
if (size.empty()) {
|
||||
if (widgetSize.empty()) {
|
||||
return Rect();
|
||||
}
|
||||
|
||||
float w = size.width * scaleVal.x;
|
||||
float h = size.height * scaleVal.y;
|
||||
float x0 = position.x - size.width * anchorPt.x * scaleVal.x;
|
||||
float y0 = position.y - size.height * anchorPt.y * scaleVal.y;
|
||||
float w = widgetSize.width * scaleVal.x;
|
||||
float h = widgetSize.height * scaleVal.y;
|
||||
float x0 = position.x - widgetSize.width * anchorPt.x * scaleVal.x;
|
||||
float y0 = position.y - widgetSize.height * anchorPt.y * scaleVal.y;
|
||||
|
||||
return Rect(x0, y0, w, h);
|
||||
}
|
||||
|
|
@ -616,7 +614,7 @@ void Button::fillRoundedRect(RenderBackend &renderer, const Rect &rect,
|
|||
* @param renderer 渲染后端
|
||||
*/
|
||||
void Button::onDrawWidget(RenderBackend &renderer) {
|
||||
Rect rect = getBoundingBox();
|
||||
Rect rect = boundingBox();
|
||||
if (rect.empty()) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <ui/check_box.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <core/string.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <ui/check_box.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -16,9 +16,7 @@ CheckBox::CheckBox() {
|
|||
* @brief 创建复选框对象
|
||||
* @return 复选框对象指针
|
||||
*/
|
||||
Ptr<CheckBox> CheckBox::create() {
|
||||
return makePtr<CheckBox>();
|
||||
}
|
||||
Ptr<CheckBox> CheckBox::create() { return makePtr<CheckBox>(); }
|
||||
|
||||
/**
|
||||
* @brief 创建带标签的复选框对象
|
||||
|
|
@ -47,57 +45,43 @@ void CheckBox::setChecked(bool checked) {
|
|||
/**
|
||||
* @brief 切换选中状态
|
||||
*/
|
||||
void CheckBox::toggle() {
|
||||
setChecked(!checked_);
|
||||
}
|
||||
void CheckBox::toggle() { setChecked(!checked_); }
|
||||
|
||||
/**
|
||||
* @brief 设置标签文本
|
||||
* @param label 标签文本
|
||||
*/
|
||||
void CheckBox::setLabel(const std::string &label) {
|
||||
label_ = label;
|
||||
}
|
||||
void CheckBox::setLabel(const std::string &label) { label_ = label; }
|
||||
|
||||
/**
|
||||
* @brief 设置字体
|
||||
* @param font 字体图集指针
|
||||
*/
|
||||
void CheckBox::setFont(Ptr<FontAtlas> font) {
|
||||
font_ = font;
|
||||
}
|
||||
void CheckBox::setFont(Ptr<FontAtlas> font) { font_ = font; }
|
||||
|
||||
/**
|
||||
* @brief 设置文本颜色
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void CheckBox::setTextColor(const Color &color) {
|
||||
textColor_ = color;
|
||||
}
|
||||
void CheckBox::setTextColor(const Color &color) { textColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置复选框尺寸
|
||||
* @param size 复选框尺寸
|
||||
*/
|
||||
void CheckBox::setBoxSize(float size) {
|
||||
boxSize_ = size;
|
||||
}
|
||||
void CheckBox::setBoxSize(float size) { boxSize_ = size; }
|
||||
|
||||
/**
|
||||
* @brief 设置间距
|
||||
* @param spacing 间距值
|
||||
*/
|
||||
void CheckBox::setSpacing(float spacing) {
|
||||
spacing_ = spacing;
|
||||
}
|
||||
void CheckBox::setSpacing(float spacing) { spacing_ = spacing; }
|
||||
|
||||
/**
|
||||
* @brief 设置选中颜色
|
||||
* @param color 选中颜色
|
||||
*/
|
||||
void CheckBox::setCheckedColor(const Color &color) {
|
||||
checkedColor_ = color;
|
||||
}
|
||||
void CheckBox::setCheckedColor(const Color &color) { checkedColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置未选中颜色
|
||||
|
|
@ -127,7 +111,7 @@ void CheckBox::setOnStateChange(Function<void(bool)> callback) {
|
|||
* @brief 获取边界框
|
||||
* @return 边界矩形
|
||||
*/
|
||||
Rect CheckBox::getBoundingBox() const {
|
||||
Rect CheckBox::boundingBox() const {
|
||||
Vec2 position = pos();
|
||||
float width = boxSize_;
|
||||
|
||||
|
|
@ -146,7 +130,8 @@ Rect CheckBox::getBoundingBox() const {
|
|||
void CheckBox::onDrawWidget(RenderBackend &renderer) {
|
||||
Vec2 position = pos();
|
||||
|
||||
Rect boxRect(position.x, position.y + (getSize().height - boxSize_) * 0.5f, boxSize_, boxSize_);
|
||||
Rect boxRect(position.x, position.y + (size().height - boxSize_) * 0.5f,
|
||||
boxSize_, boxSize_);
|
||||
Color boxColor = checked_ ? checkedColor_ : uncheckedColor_;
|
||||
renderer.fillRect(boxRect, boxColor);
|
||||
renderer.drawRect(boxRect, Colors::White, 1.0f);
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
#include <ui/label.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <core/string.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <ui/label.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
/**
|
||||
* @brief 默认构造函数
|
||||
*/
|
||||
Label::Label() {
|
||||
setAnchor(0.0f, 0.0f);
|
||||
}
|
||||
Label::Label() { setAnchor(0.0f, 0.0f); }
|
||||
|
||||
/**
|
||||
* @brief 带文本的构造函数
|
||||
|
|
@ -24,9 +22,7 @@ Label::Label(const std::string &text) : text_(text) {
|
|||
* @brief 创建空标签对象
|
||||
* @return 标签对象指针
|
||||
*/
|
||||
Ptr<Label> Label::create() {
|
||||
return makePtr<Label>();
|
||||
}
|
||||
Ptr<Label> Label::create() { return makePtr<Label>(); }
|
||||
|
||||
/**
|
||||
* @brief 创建带文本的标签对象
|
||||
|
|
@ -73,9 +69,7 @@ void Label::setFont(Ptr<FontAtlas> font) {
|
|||
* @brief 设置文本颜色
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void Label::setTextColor(const Color &color) {
|
||||
textColor_ = color;
|
||||
}
|
||||
void Label::setTextColor(const Color &color) { textColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置字体大小
|
||||
|
|
@ -91,65 +85,49 @@ void Label::setFontSize(int size) {
|
|||
* @brief 设置水平对齐方式
|
||||
* @param align 对齐方式
|
||||
*/
|
||||
void Label::setHorizontalAlign(HorizontalAlign align) {
|
||||
hAlign_ = align;
|
||||
}
|
||||
void Label::setHorizontalAlign(HorizontalAlign align) { hAlign_ = align; }
|
||||
|
||||
/**
|
||||
* @brief 设置垂直对齐方式
|
||||
* @param align 垂直对齐方式
|
||||
*/
|
||||
void Label::setVerticalAlign(VerticalAlign align) {
|
||||
vAlign_ = align;
|
||||
}
|
||||
void Label::setVerticalAlign(VerticalAlign align) { vAlign_ = align; }
|
||||
|
||||
/**
|
||||
* @brief 设置阴影是否启用
|
||||
* @param enabled 是否启用
|
||||
*/
|
||||
void Label::setShadowEnabled(bool enabled) {
|
||||
shadowEnabled_ = enabled;
|
||||
}
|
||||
void Label::setShadowEnabled(bool enabled) { shadowEnabled_ = enabled; }
|
||||
|
||||
/**
|
||||
* @brief 设置阴影颜色
|
||||
* @param color 阴影颜色
|
||||
*/
|
||||
void Label::setShadowColor(const Color &color) {
|
||||
shadowColor_ = color;
|
||||
}
|
||||
void Label::setShadowColor(const Color &color) { shadowColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置阴影偏移
|
||||
* @param offset 偏移向量
|
||||
*/
|
||||
void Label::setShadowOffset(const Vec2 &offset) {
|
||||
shadowOffset_ = offset;
|
||||
}
|
||||
void Label::setShadowOffset(const Vec2 &offset) { shadowOffset_ = offset; }
|
||||
|
||||
/**
|
||||
* @brief 设置描边是否启用
|
||||
* @param enabled 是否启用
|
||||
*/
|
||||
void Label::setOutlineEnabled(bool enabled) {
|
||||
outlineEnabled_ = enabled;
|
||||
}
|
||||
void Label::setOutlineEnabled(bool enabled) { outlineEnabled_ = enabled; }
|
||||
|
||||
/**
|
||||
* @brief 设置描边颜色
|
||||
* @param color 描边颜色
|
||||
*/
|
||||
void Label::setOutlineColor(const Color &color) {
|
||||
outlineColor_ = color;
|
||||
}
|
||||
void Label::setOutlineColor(const Color &color) { outlineColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置描边宽度
|
||||
* @param width 描边宽度
|
||||
*/
|
||||
void Label::setOutlineWidth(float width) {
|
||||
outlineWidth_ = width;
|
||||
}
|
||||
void Label::setOutlineWidth(float width) { outlineWidth_ = width; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否多行模式
|
||||
|
|
@ -306,18 +284,18 @@ std::vector<std::string> Label::splitLines() const {
|
|||
*/
|
||||
Vec2 Label::calculateDrawPosition() const {
|
||||
Vec2 position = pos();
|
||||
Vec2 size = getTextSize();
|
||||
Size widgetSize = getSize();
|
||||
Vec2 textSize = getTextSize();
|
||||
Size widgetSize = size();
|
||||
|
||||
float refWidth = widgetSize.empty() ? size.x : widgetSize.width;
|
||||
float refHeight = widgetSize.empty() ? size.y : widgetSize.height;
|
||||
float refWidth = widgetSize.empty() ? textSize.x : widgetSize.width;
|
||||
float refHeight = widgetSize.empty() ? textSize.y : widgetSize.height;
|
||||
|
||||
switch (hAlign_) {
|
||||
case HorizontalAlign::Center:
|
||||
position.x += (refWidth - size.x) * 0.5f;
|
||||
position.x += (refWidth - textSize.x) * 0.5f;
|
||||
break;
|
||||
case HorizontalAlign::Right:
|
||||
position.x += refWidth - size.x;
|
||||
position.x += refWidth - textSize.x;
|
||||
break;
|
||||
case HorizontalAlign::Left:
|
||||
default:
|
||||
|
|
@ -326,10 +304,10 @@ Vec2 Label::calculateDrawPosition() const {
|
|||
|
||||
switch (vAlign_) {
|
||||
case VerticalAlign::Middle:
|
||||
position.y += (refHeight - size.y) * 0.5f;
|
||||
position.y += (refHeight - textSize.y) * 0.5f;
|
||||
break;
|
||||
case VerticalAlign::Bottom:
|
||||
position.y += refHeight - size.y;
|
||||
position.y += refHeight - textSize.y;
|
||||
break;
|
||||
case VerticalAlign::Top:
|
||||
default:
|
||||
|
|
@ -345,7 +323,8 @@ Vec2 Label::calculateDrawPosition() const {
|
|||
* @param position 绘制位置
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void Label::drawText(RenderBackend &renderer, const Vec2 &position, const Color &color) {
|
||||
void Label::drawText(RenderBackend &renderer, const Vec2 &position,
|
||||
const Color &color) {
|
||||
if (!font_ || text_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -368,7 +347,7 @@ void Label::drawText(RenderBackend &renderer, const Vec2 &position, const Color
|
|||
* @brief 获取边界框
|
||||
* @return 边界矩形
|
||||
*/
|
||||
Rect Label::getBoundingBox() const {
|
||||
Rect Label::boundingBox() const {
|
||||
if (!font_ || text_.empty()) {
|
||||
return Rect();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include <ui/progress_bar.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <core/string.h>
|
||||
#include <cmath>
|
||||
#include <core/string.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <ui/progress_bar.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -17,9 +17,7 @@ ProgressBar::ProgressBar() {
|
|||
* @brief 创建进度条对象
|
||||
* @return 进度条对象指针
|
||||
*/
|
||||
Ptr<ProgressBar> ProgressBar::create() {
|
||||
return makePtr<ProgressBar>();
|
||||
}
|
||||
Ptr<ProgressBar> ProgressBar::create() { return makePtr<ProgressBar>(); }
|
||||
|
||||
/**
|
||||
* @brief 创建带范围的进度条对象
|
||||
|
|
@ -67,7 +65,8 @@ void ProgressBar::setValue(float value) {
|
|||
* @return 百分比值(0.0-1.0)
|
||||
*/
|
||||
float ProgressBar::getPercent() const {
|
||||
if (max_ <= min_) return 0.0f;
|
||||
if (max_ <= min_)
|
||||
return 0.0f;
|
||||
return (displayValue_ - min_) / (max_ - min_);
|
||||
}
|
||||
|
||||
|
|
@ -75,25 +74,19 @@ float ProgressBar::getPercent() const {
|
|||
* @brief 设置方向
|
||||
* @param dir 方向枚举
|
||||
*/
|
||||
void ProgressBar::setDirection(Direction dir) {
|
||||
direction_ = dir;
|
||||
}
|
||||
void ProgressBar::setDirection(Direction dir) { direction_ = dir; }
|
||||
|
||||
/**
|
||||
* @brief 设置背景颜色
|
||||
* @param color 背景颜色
|
||||
*/
|
||||
void ProgressBar::setBackgroundColor(const Color &color) {
|
||||
bgColor_ = color;
|
||||
}
|
||||
void ProgressBar::setBackgroundColor(const Color &color) { bgColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置填充颜色
|
||||
* @param color 填充颜色
|
||||
*/
|
||||
void ProgressBar::setFillColor(const Color &color) {
|
||||
fillColor_ = color;
|
||||
}
|
||||
void ProgressBar::setFillColor(const Color &color) { fillColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用渐变填充
|
||||
|
|
@ -107,9 +100,7 @@ void ProgressBar::setGradientFillEnabled(bool enabled) {
|
|||
* @brief 设置渐变结束颜色
|
||||
* @param color 结束颜色
|
||||
*/
|
||||
void ProgressBar::setFillColorEnd(const Color &color) {
|
||||
fillColorEnd_ = color;
|
||||
}
|
||||
void ProgressBar::setFillColorEnd(const Color &color) { fillColorEnd_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用分段颜色
|
||||
|
|
@ -133,17 +124,13 @@ void ProgressBar::addColorSegment(float percentThreshold, const Color &color) {
|
|||
/**
|
||||
* @brief 清除所有颜色分段
|
||||
*/
|
||||
void ProgressBar::clearColorSegments() {
|
||||
colorSegments_.clear();
|
||||
}
|
||||
void ProgressBar::clearColorSegments() { colorSegments_.clear(); }
|
||||
|
||||
/**
|
||||
* @brief 设置圆角半径
|
||||
* @param radius 圆角半径
|
||||
*/
|
||||
void ProgressBar::setCornerRadius(float radius) {
|
||||
cornerRadius_ = radius;
|
||||
}
|
||||
void ProgressBar::setCornerRadius(float radius) { cornerRadius_ = radius; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用圆角
|
||||
|
|
@ -157,57 +144,43 @@ void ProgressBar::setRoundedCornersEnabled(bool enabled) {
|
|||
* @brief 设置是否启用边框
|
||||
* @param enabled 是否启用
|
||||
*/
|
||||
void ProgressBar::setBorderEnabled(bool enabled) {
|
||||
borderEnabled_ = enabled;
|
||||
}
|
||||
void ProgressBar::setBorderEnabled(bool enabled) { borderEnabled_ = enabled; }
|
||||
|
||||
/**
|
||||
* @brief 设置边框颜色
|
||||
* @param color 边框颜色
|
||||
*/
|
||||
void ProgressBar::setBorderColor(const Color &color) {
|
||||
borderColor_ = color;
|
||||
}
|
||||
void ProgressBar::setBorderColor(const Color &color) { borderColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置边框宽度
|
||||
* @param width 边框宽度
|
||||
*/
|
||||
void ProgressBar::setBorderWidth(float width) {
|
||||
borderWidth_ = width;
|
||||
}
|
||||
void ProgressBar::setBorderWidth(float width) { borderWidth_ = width; }
|
||||
|
||||
/**
|
||||
* @brief 设置内边距
|
||||
* @param padding 内边距值
|
||||
*/
|
||||
void ProgressBar::setPadding(float padding) {
|
||||
padding_ = padding;
|
||||
}
|
||||
void ProgressBar::setPadding(float padding) { padding_ = padding; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用文本显示
|
||||
* @param enabled 是否启用
|
||||
*/
|
||||
void ProgressBar::setTextEnabled(bool enabled) {
|
||||
textEnabled_ = enabled;
|
||||
}
|
||||
void ProgressBar::setTextEnabled(bool enabled) { textEnabled_ = enabled; }
|
||||
|
||||
/**
|
||||
* @brief 设置字体
|
||||
* @param font 字体图集指针
|
||||
*/
|
||||
void ProgressBar::setFont(Ptr<FontAtlas> font) {
|
||||
font_ = font;
|
||||
}
|
||||
void ProgressBar::setFont(Ptr<FontAtlas> font) { font_ = font; }
|
||||
|
||||
/**
|
||||
* @brief 设置文本颜色
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void ProgressBar::setTextColor(const Color &color) {
|
||||
textColor_ = color;
|
||||
}
|
||||
void ProgressBar::setTextColor(const Color &color) { textColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置文本格式
|
||||
|
|
@ -232,9 +205,7 @@ void ProgressBar::setAnimatedChangeEnabled(bool enabled) {
|
|||
* @brief 设置动画速度
|
||||
* @param speed 每秒变化量
|
||||
*/
|
||||
void ProgressBar::setAnimationSpeed(float speed) {
|
||||
animationSpeed_ = speed;
|
||||
}
|
||||
void ProgressBar::setAnimationSpeed(float speed) { animationSpeed_ = speed; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用延迟显示
|
||||
|
|
@ -248,9 +219,7 @@ void ProgressBar::setDelayedDisplayEnabled(bool enabled) {
|
|||
* @brief 设置延迟时间
|
||||
* @param seconds 延迟秒数
|
||||
*/
|
||||
void ProgressBar::setDelayTime(float seconds) {
|
||||
delayTime_ = seconds;
|
||||
}
|
||||
void ProgressBar::setDelayTime(float seconds) { delayTime_ = seconds; }
|
||||
|
||||
/**
|
||||
* @brief 设置延迟显示填充颜色
|
||||
|
|
@ -264,25 +233,19 @@ void ProgressBar::setDelayedFillColor(const Color &color) {
|
|||
* @brief 设置是否启用条纹效果
|
||||
* @param enabled 是否启用
|
||||
*/
|
||||
void ProgressBar::setStripedEnabled(bool enabled) {
|
||||
stripedEnabled_ = enabled;
|
||||
}
|
||||
void ProgressBar::setStripedEnabled(bool enabled) { stripedEnabled_ = enabled; }
|
||||
|
||||
/**
|
||||
* @brief 设置条纹颜色
|
||||
* @param color 条纹颜色
|
||||
*/
|
||||
void ProgressBar::setStripeColor(const Color &color) {
|
||||
stripeColor_ = color;
|
||||
}
|
||||
void ProgressBar::setStripeColor(const Color &color) { stripeColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置条纹移动速度
|
||||
* @param speed 移动速度
|
||||
*/
|
||||
void ProgressBar::setStripeSpeed(float speed) {
|
||||
stripeSpeed_ = speed;
|
||||
}
|
||||
void ProgressBar::setStripeSpeed(float speed) { stripeSpeed_ = speed; }
|
||||
|
||||
/**
|
||||
* @brief 获取当前填充颜色
|
||||
|
|
@ -330,8 +293,8 @@ std::string ProgressBar::formatText() const {
|
|||
* @brief 获取边界框
|
||||
* @return 边界矩形
|
||||
*/
|
||||
Rect ProgressBar::getBoundingBox() const {
|
||||
return Rect(pos().x, pos().y, getSize().width, getSize().height);
|
||||
Rect ProgressBar::boundingBox() const {
|
||||
return Rect(pos().x, pos().y, size().width, size().height);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -379,12 +342,12 @@ void ProgressBar::onUpdate(float deltaTime) {
|
|||
*/
|
||||
void ProgressBar::onDrawWidget(RenderBackend &renderer) {
|
||||
Vec2 position = pos();
|
||||
Size size = getSize();
|
||||
Size widgetSize = size();
|
||||
|
||||
float bgX = position.x + padding_;
|
||||
float bgY = position.y + padding_;
|
||||
float bgW = size.width - padding_ * 2;
|
||||
float bgH = size.height - padding_ * 2;
|
||||
float bgW = widgetSize.width - padding_ * 2;
|
||||
float bgH = widgetSize.height - padding_ * 2;
|
||||
Rect bgRect(bgX, bgY, bgW, bgH);
|
||||
|
||||
if (roundedCornersEnabled_) {
|
||||
|
|
@ -469,10 +432,8 @@ void ProgressBar::onDrawWidget(RenderBackend &renderer) {
|
|||
std::string text = formatText();
|
||||
Vec2 textSize = font_->measureText(text);
|
||||
|
||||
Vec2 textPos(
|
||||
position.x + (size.width - textSize.x) * 0.5f,
|
||||
position.y + (size.height - textSize.y) * 0.5f
|
||||
);
|
||||
Vec2 textPos(position.x + (widgetSize.width - textSize.x) * 0.5f,
|
||||
position.y + (widgetSize.height - textSize.y) * 0.5f);
|
||||
|
||||
renderer.drawText(*font_, text, textPos, textColor_);
|
||||
}
|
||||
|
|
@ -485,7 +446,8 @@ void ProgressBar::onDrawWidget(RenderBackend &renderer) {
|
|||
* @param color 颜色
|
||||
* @param radius 圆角半径
|
||||
*/
|
||||
void ProgressBar::drawRoundedRect(RenderBackend &renderer, const Rect &rect, const Color &color, float radius) {
|
||||
void ProgressBar::drawRoundedRect(RenderBackend &renderer, const Rect &rect,
|
||||
const Color &color, float radius) {
|
||||
renderer.drawRect(rect, color, borderWidth_);
|
||||
}
|
||||
|
||||
|
|
@ -496,7 +458,8 @@ void ProgressBar::drawRoundedRect(RenderBackend &renderer, const Rect &rect, con
|
|||
* @param color 颜色
|
||||
* @param radius 圆角半径
|
||||
*/
|
||||
void ProgressBar::fillRoundedRect(RenderBackend &renderer, const Rect &rect, const Color &color, float radius) {
|
||||
void ProgressBar::fillRoundedRect(RenderBackend &renderer, const Rect &rect,
|
||||
const Color &color, float radius) {
|
||||
renderer.fillRect(rect, color);
|
||||
}
|
||||
|
||||
|
|
@ -511,14 +474,17 @@ void ProgressBar::drawStripes(RenderBackend &renderer, const Rect &rect) {
|
|||
float rectRight = rect.origin.x + rect.size.width;
|
||||
float rectBottom = rect.origin.y + rect.size.height;
|
||||
|
||||
for (float x = rect.origin.x - spacing + stripeOffset_; x < rectRight; x += spacing) {
|
||||
for (float x = rect.origin.x - spacing + stripeOffset_; x < rectRight;
|
||||
x += spacing) {
|
||||
float x1 = x;
|
||||
float y1 = rect.origin.y;
|
||||
float x2 = x + stripeWidth;
|
||||
float y2 = rectBottom;
|
||||
|
||||
if (x1 < rect.origin.x) x1 = rect.origin.x;
|
||||
if (x2 > rectRight) x2 = rectRight;
|
||||
if (x1 < rect.origin.x)
|
||||
x1 = rect.origin.x;
|
||||
if (x2 > rectRight)
|
||||
x2 = rectRight;
|
||||
|
||||
if (x2 > x1) {
|
||||
for (int i = 0; i < static_cast<int>(rect.size.height); i += 4) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <ui/radio_button.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <core/string.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <ui/radio_button.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -16,9 +16,7 @@ RadioButton::RadioButton() {
|
|||
* @brief 创建单选按钮对象
|
||||
* @return 单选按钮对象指针
|
||||
*/
|
||||
Ptr<RadioButton> RadioButton::create() {
|
||||
return makePtr<RadioButton>();
|
||||
}
|
||||
Ptr<RadioButton> RadioButton::create() { return makePtr<RadioButton>(); }
|
||||
|
||||
/**
|
||||
* @brief 创建带标签的单选按钮对象
|
||||
|
|
@ -48,41 +46,31 @@ void RadioButton::setSelected(bool selected) {
|
|||
* @brief 设置标签文本
|
||||
* @param label 标签文本
|
||||
*/
|
||||
void RadioButton::setLabel(const std::string &label) {
|
||||
label_ = label;
|
||||
}
|
||||
void RadioButton::setLabel(const std::string &label) { label_ = label; }
|
||||
|
||||
/**
|
||||
* @brief 设置字体
|
||||
* @param font 字体图集指针
|
||||
*/
|
||||
void RadioButton::setFont(Ptr<FontAtlas> font) {
|
||||
font_ = font;
|
||||
}
|
||||
void RadioButton::setFont(Ptr<FontAtlas> font) { font_ = font; }
|
||||
|
||||
/**
|
||||
* @brief 设置文本颜色
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void RadioButton::setTextColor(const Color &color) {
|
||||
textColor_ = color;
|
||||
}
|
||||
void RadioButton::setTextColor(const Color &color) { textColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置圆形尺寸
|
||||
* @param size 圆形尺寸
|
||||
*/
|
||||
void RadioButton::setCircleSize(float size) {
|
||||
circleSize_ = size;
|
||||
}
|
||||
void RadioButton::setCircleSize(float size) { circleSize_ = size; }
|
||||
|
||||
/**
|
||||
* @brief 设置间距
|
||||
* @param spacing 间距值
|
||||
*/
|
||||
void RadioButton::setSpacing(float spacing) {
|
||||
spacing_ = spacing;
|
||||
}
|
||||
void RadioButton::setSpacing(float spacing) { spacing_ = spacing; }
|
||||
|
||||
/**
|
||||
* @brief 设置选中颜色
|
||||
|
|
@ -104,17 +92,13 @@ void RadioButton::setUnselectedColor(const Color &color) {
|
|||
* @brief 设置圆点颜色
|
||||
* @param color 圆点颜色
|
||||
*/
|
||||
void RadioButton::setDotColor(const Color &color) {
|
||||
dotColor_ = color;
|
||||
}
|
||||
void RadioButton::setDotColor(const Color &color) { dotColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置分组ID
|
||||
* @param groupId 分组ID
|
||||
*/
|
||||
void RadioButton::setGroupId(int groupId) {
|
||||
groupId_ = groupId;
|
||||
}
|
||||
void RadioButton::setGroupId(int groupId) { groupId_ = groupId; }
|
||||
|
||||
/**
|
||||
* @brief 设置状态改变回调
|
||||
|
|
@ -128,7 +112,7 @@ void RadioButton::setOnStateChange(Function<void(bool)> callback) {
|
|||
* @brief 获取边界框
|
||||
* @return 边界矩形
|
||||
*/
|
||||
Rect RadioButton::getBoundingBox() const {
|
||||
Rect RadioButton::boundingBox() const {
|
||||
Vec2 position = pos();
|
||||
float width = circleSize_;
|
||||
|
||||
|
|
@ -147,12 +131,13 @@ Rect RadioButton::getBoundingBox() const {
|
|||
void RadioButton::onDrawWidget(RenderBackend &renderer) {
|
||||
Vec2 position = pos();
|
||||
float centerX = position.x + circleSize_ * 0.5f;
|
||||
float centerY = position.y + getSize().height * 0.5f;
|
||||
float centerY = position.y + size().height * 0.5f;
|
||||
float radius = circleSize_ * 0.5f;
|
||||
|
||||
Color circleColor = selected_ ? selectedColor_ : unselectedColor_;
|
||||
renderer.drawCircle(Vec2(centerX, centerY), radius, circleColor, true);
|
||||
renderer.drawCircle(Vec2(centerX, centerY), radius, Colors::White, false, 1.0f);
|
||||
renderer.drawCircle(Vec2(centerX, centerY), radius, Colors::White, false,
|
||||
1.0f);
|
||||
|
||||
if (selected_) {
|
||||
float dotRadius = radius * 0.4f;
|
||||
|
|
@ -188,7 +173,7 @@ bool RadioButton::onMouseRelease(const MouseEvent &event) {
|
|||
pressed_ = false;
|
||||
Vec2 position = pos();
|
||||
float centerX = position.x + circleSize_ * 0.5f;
|
||||
float centerY = position.y + getSize().height * 0.5f;
|
||||
float centerY = position.y + size().height * 0.5f;
|
||||
float radius = circleSize_ * 0.5f;
|
||||
|
||||
float dx = event.x - centerX;
|
||||
|
|
@ -210,7 +195,8 @@ bool RadioButton::onMouseRelease(const MouseEvent &event) {
|
|||
* @param button 单选按钮指针
|
||||
*/
|
||||
void RadioButtonGroup::addButton(RadioButton *button) {
|
||||
if (button && std::find(buttons_.begin(), buttons_.end(), button) == buttons_.end()) {
|
||||
if (button &&
|
||||
std::find(buttons_.begin(), buttons_.end(), button) == buttons_.end()) {
|
||||
buttons_.push_back(button);
|
||||
button->setOnStateChange([this, button](bool selected) {
|
||||
if (selected) {
|
||||
|
|
@ -243,7 +229,8 @@ void RadioButtonGroup::removeButton(RadioButton *button) {
|
|||
* @param button 单选按钮指针
|
||||
*/
|
||||
void RadioButtonGroup::selectButton(RadioButton *button) {
|
||||
if (selectedButton_ == button) return;
|
||||
if (selectedButton_ == button)
|
||||
return;
|
||||
|
||||
if (selectedButton_) {
|
||||
selectedButton_->setSelected(false);
|
||||
|
|
@ -263,7 +250,8 @@ void RadioButtonGroup::selectButton(RadioButton *button) {
|
|||
* @brief 设置选择改变回调
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
void RadioButtonGroup::setOnSelectionChange(Function<void(RadioButton*)> callback) {
|
||||
void RadioButtonGroup::setOnSelectionChange(
|
||||
Function<void(RadioButton *)> callback) {
|
||||
onSelectionChange_ = callback;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include <ui/slider.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <core/string.h>
|
||||
#include <cmath>
|
||||
#include <core/string.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <ui/slider.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
|
|
@ -17,9 +17,7 @@ Slider::Slider() {
|
|||
* @brief 创建滑动条对象
|
||||
* @return 滑动条对象指针
|
||||
*/
|
||||
Ptr<Slider> Slider::create() {
|
||||
return makePtr<Slider>();
|
||||
}
|
||||
Ptr<Slider> Slider::create() { return makePtr<Slider>(); }
|
||||
|
||||
/**
|
||||
* @brief 创建带范围的滑动条对象
|
||||
|
|
@ -79,49 +77,37 @@ void Slider::setStep(float step) {
|
|||
* @brief 设置是否为垂直方向
|
||||
* @param vertical 是否垂直
|
||||
*/
|
||||
void Slider::setVertical(bool vertical) {
|
||||
vertical_ = vertical;
|
||||
}
|
||||
void Slider::setVertical(bool vertical) { vertical_ = vertical; }
|
||||
|
||||
/**
|
||||
* @brief 设置轨道尺寸
|
||||
* @param size 轨道尺寸
|
||||
*/
|
||||
void Slider::setTrackSize(float size) {
|
||||
trackSize_ = size;
|
||||
}
|
||||
void Slider::setTrackSize(float size) { trackSize_ = size; }
|
||||
|
||||
/**
|
||||
* @brief 设置滑块尺寸
|
||||
* @param size 滑块尺寸
|
||||
*/
|
||||
void Slider::setThumbSize(float size) {
|
||||
thumbSize_ = size;
|
||||
}
|
||||
void Slider::setThumbSize(float size) { thumbSize_ = size; }
|
||||
|
||||
/**
|
||||
* @brief 设置轨道颜色
|
||||
* @param color 轨道颜色
|
||||
*/
|
||||
void Slider::setTrackColor(const Color &color) {
|
||||
trackColor_ = color;
|
||||
}
|
||||
void Slider::setTrackColor(const Color &color) { trackColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置填充颜色
|
||||
* @param color 填充颜色
|
||||
*/
|
||||
void Slider::setFillColor(const Color &color) {
|
||||
fillColor_ = color;
|
||||
}
|
||||
void Slider::setFillColor(const Color &color) { fillColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置滑块颜色
|
||||
* @param color 滑块颜色
|
||||
*/
|
||||
void Slider::setThumbColor(const Color &color) {
|
||||
thumbColor_ = color;
|
||||
}
|
||||
void Slider::setThumbColor(const Color &color) { thumbColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置滑块悬停颜色
|
||||
|
|
@ -143,49 +129,37 @@ void Slider::setThumbPressedColor(const Color &color) {
|
|||
* @brief 设置是否显示滑块
|
||||
* @param show 是否显示
|
||||
*/
|
||||
void Slider::setShowThumb(bool show) {
|
||||
showThumb_ = show;
|
||||
}
|
||||
void Slider::setShowThumb(bool show) { showThumb_ = show; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否显示填充
|
||||
* @param show 是否显示
|
||||
*/
|
||||
void Slider::setShowFill(bool show) {
|
||||
showFill_ = show;
|
||||
}
|
||||
void Slider::setShowFill(bool show) { showFill_ = show; }
|
||||
|
||||
/**
|
||||
* @brief 设置是否启用文本显示
|
||||
* @param enabled 是否启用
|
||||
*/
|
||||
void Slider::setTextEnabled(bool enabled) {
|
||||
textEnabled_ = enabled;
|
||||
}
|
||||
void Slider::setTextEnabled(bool enabled) { textEnabled_ = enabled; }
|
||||
|
||||
/**
|
||||
* @brief 设置字体
|
||||
* @param font 字体图集指针
|
||||
*/
|
||||
void Slider::setFont(Ptr<FontAtlas> font) {
|
||||
font_ = font;
|
||||
}
|
||||
void Slider::setFont(Ptr<FontAtlas> font) { font_ = font; }
|
||||
|
||||
/**
|
||||
* @brief 设置文本颜色
|
||||
* @param color 文本颜色
|
||||
*/
|
||||
void Slider::setTextColor(const Color &color) {
|
||||
textColor_ = color;
|
||||
}
|
||||
void Slider::setTextColor(const Color &color) { textColor_ = color; }
|
||||
|
||||
/**
|
||||
* @brief 设置文本格式
|
||||
* @param format 格式字符串
|
||||
*/
|
||||
void Slider::setTextFormat(const std::string &format) {
|
||||
textFormat_ = format;
|
||||
}
|
||||
void Slider::setTextFormat(const std::string &format) { textFormat_ = format; }
|
||||
|
||||
/**
|
||||
* @brief 设置值改变回调
|
||||
|
|
@ -207,16 +181,14 @@ void Slider::setOnDragStart(Function<void()> callback) {
|
|||
* @brief 设置拖拽结束回调
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
void Slider::setOnDragEnd(Function<void()> callback) {
|
||||
onDragEnd_ = callback;
|
||||
}
|
||||
void Slider::setOnDragEnd(Function<void()> callback) { onDragEnd_ = callback; }
|
||||
|
||||
/**
|
||||
* @brief 获取边界框
|
||||
* @return 边界矩形
|
||||
*/
|
||||
Rect Slider::getBoundingBox() const {
|
||||
return Rect(pos().x, pos().y, getSize().width, getSize().height);
|
||||
Rect Slider::boundingBox() const {
|
||||
return Rect(pos().x, pos().y, size().width, size().height);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -226,14 +198,14 @@ Rect Slider::getBoundingBox() const {
|
|||
*/
|
||||
float Slider::valueToPosition(float value) const {
|
||||
Vec2 position = pos();
|
||||
Size size = getSize();
|
||||
Size sz = size();
|
||||
|
||||
float percent = (value - min_) / (max_ - min_);
|
||||
|
||||
if (vertical_) {
|
||||
return position.y + size.height - percent * size.height;
|
||||
return position.y + sz.height - percent * sz.height;
|
||||
} else {
|
||||
return position.x + percent * size.width;
|
||||
return position.x + percent * sz.width;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -244,13 +216,13 @@ float Slider::valueToPosition(float value) const {
|
|||
*/
|
||||
float Slider::positionToValue(float position) const {
|
||||
Vec2 widgetPos = pos();
|
||||
Size size = getSize();
|
||||
Size widgetSize = size();
|
||||
|
||||
float percent;
|
||||
if (vertical_) {
|
||||
percent = (widgetPos.y + size.height - position) / size.height;
|
||||
percent = (widgetPos.y + widgetSize.height - position) / widgetSize.height;
|
||||
} else {
|
||||
percent = (position - widgetPos.x) / size.width;
|
||||
percent = (position - widgetPos.x) / widgetSize.width;
|
||||
}
|
||||
|
||||
percent = std::clamp(percent, 0.0f, 1.0f);
|
||||
|
|
@ -262,25 +234,18 @@ float Slider::positionToValue(float position) const {
|
|||
* @return 滑块矩形
|
||||
*/
|
||||
Rect Slider::getThumbRect() const {
|
||||
Vec2 position = pos();
|
||||
Size size = getSize();
|
||||
Vec2 widgetPos = pos();
|
||||
Size widgetSize = size();
|
||||
|
||||
float thumbPos = valueToPosition(value_);
|
||||
|
||||
if (vertical_) {
|
||||
return Rect(
|
||||
position.x + (size.width - thumbSize_) * 0.5f,
|
||||
thumbPos - thumbSize_ * 0.5f,
|
||||
thumbSize_,
|
||||
thumbSize_
|
||||
);
|
||||
return Rect(widgetPos.x + (widgetSize.width - thumbSize_) * 0.5f,
|
||||
thumbPos - thumbSize_ * 0.5f, thumbSize_, thumbSize_);
|
||||
} else {
|
||||
return Rect(
|
||||
thumbPos - thumbSize_ * 0.5f,
|
||||
position.y + (size.height - thumbSize_) * 0.5f,
|
||||
thumbSize_,
|
||||
thumbSize_
|
||||
);
|
||||
return Rect(thumbPos - thumbSize_ * 0.5f,
|
||||
widgetPos.y + (widgetSize.height - thumbSize_) * 0.5f,
|
||||
thumbSize_, thumbSize_);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -289,23 +254,16 @@ Rect Slider::getThumbRect() const {
|
|||
* @return 轨道矩形
|
||||
*/
|
||||
Rect Slider::getTrackRect() const {
|
||||
Vec2 position = pos();
|
||||
Size size = getSize();
|
||||
Vec2 widgetPos = pos();
|
||||
Size widgetSize = size();
|
||||
|
||||
if (vertical_) {
|
||||
return Rect(
|
||||
position.x + (size.width - trackSize_) * 0.5f,
|
||||
position.y,
|
||||
trackSize_,
|
||||
size.height
|
||||
);
|
||||
return Rect(widgetPos.x + (widgetSize.width - trackSize_) * 0.5f,
|
||||
widgetPos.y, trackSize_, widgetSize.height);
|
||||
} else {
|
||||
return Rect(
|
||||
position.x,
|
||||
position.y + (size.height - trackSize_) * 0.5f,
|
||||
size.width,
|
||||
trackSize_
|
||||
);
|
||||
return Rect(widgetPos.x,
|
||||
widgetPos.y + (widgetSize.height - trackSize_) * 0.5f,
|
||||
widgetSize.width, trackSize_);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -326,7 +284,8 @@ std::string Slider::formatText() const {
|
|||
size_t endPos = result.find("}", pos);
|
||||
if (endPos != std::string::npos) {
|
||||
std::string format = result.substr(pos + 7, endPos - pos - 8);
|
||||
result.replace(pos, endPos - pos + 1, std::to_string(static_cast<int>(value_)));
|
||||
result.replace(pos, endPos - pos + 1,
|
||||
std::to_string(static_cast<int>(value_)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -387,13 +346,11 @@ void Slider::onDrawWidget(RenderBackend &renderer) {
|
|||
if (textEnabled_ && font_) {
|
||||
std::string text = formatText();
|
||||
Vec2 textSize = font_->measureText(text);
|
||||
Vec2 position = pos();
|
||||
Size size = getSize();
|
||||
Vec2 widgetPos = pos();
|
||||
Size widgetSize = size();
|
||||
|
||||
Vec2 textPos(
|
||||
position.x + size.width + 10.0f,
|
||||
position.y + (size.height - textSize.y) * 0.5f
|
||||
);
|
||||
Vec2 textPos(widgetPos.x + widgetSize.width + 10.0f,
|
||||
widgetPos.y + (widgetSize.height - textSize.y) * 0.5f);
|
||||
|
||||
renderer.drawText(*font_, text, textPos, textColor_);
|
||||
}
|
||||
|
|
@ -468,15 +425,11 @@ bool Slider::onMouseMove(const MouseEvent &event) {
|
|||
/**
|
||||
* @brief 鼠标进入事件处理
|
||||
*/
|
||||
void Slider::onMouseEnter() {
|
||||
hovered_ = true;
|
||||
}
|
||||
void Slider::onMouseEnter() { hovered_ = true; }
|
||||
|
||||
/**
|
||||
* @brief 鼠标离开事件处理
|
||||
*/
|
||||
void Slider::onMouseLeave() {
|
||||
hovered_ = false;
|
||||
}
|
||||
void Slider::onMouseLeave() { hovered_ = false; }
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
#include <core/string.h>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <core/string.h>
|
||||
#include <graphics/render_backend.h>
|
||||
#include <ui/text.h>
|
||||
|
||||
|
||||
namespace extra2d {
|
||||
|
||||
/**
|
||||
|
|
@ -114,7 +113,7 @@ void Text::updateCache() const {
|
|||
Vec2 Text::calculateDrawPosition() const {
|
||||
Vec2 position = pos();
|
||||
Vec2 textSize = getTextSize();
|
||||
Size widgetSize = getSize();
|
||||
Size widgetSize = size();
|
||||
Vec2 anchorPt = anchor();
|
||||
|
||||
float refWidth = widgetSize.empty() ? textSize.x : widgetSize.width;
|
||||
|
|
@ -216,7 +215,7 @@ Ptr<Text> Text::createFormat(Ptr<FontAtlas> font, const char *fmt, ...) {
|
|||
* @brief 获取边界框
|
||||
* @return 边界矩形
|
||||
*/
|
||||
Rect Text::getBoundingBox() const {
|
||||
Rect Text::boundingBox() const {
|
||||
if (!font_ || text_.empty()) {
|
||||
return Rect();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,7 @@
|
|||
|
||||
namespace extra2d {
|
||||
|
||||
Widget::Widget() {
|
||||
setSpatialIndexed(false);
|
||||
}
|
||||
Widget::Widget() { setSpatialIndexed(false); }
|
||||
|
||||
void Widget::setSize(const Size &size) {
|
||||
size_ = size;
|
||||
|
|
@ -16,7 +14,7 @@ void Widget::setSize(float width, float height) {
|
|||
setSize(Size(width, height));
|
||||
}
|
||||
|
||||
Rect Widget::getBoundingBox() const {
|
||||
Rect Widget::boundingBox() const {
|
||||
if (size_.empty()) {
|
||||
return Rect();
|
||||
}
|
||||
|
|
@ -40,8 +38,6 @@ Rect Widget::getBoundingBox() const {
|
|||
// ------------------------------------------------------------------------
|
||||
// 重写 onDraw
|
||||
// ------------------------------------------------------------------------
|
||||
void Widget::onDraw(RenderBackend &renderer) {
|
||||
onDrawWidget(renderer);
|
||||
}
|
||||
void Widget::onDraw(RenderBackend &renderer) { onDrawWidget(renderer); }
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
Loading…
Reference in New Issue