refactor: 统一边界框方法名称为 boundingBox

将 getBoundingBox 方法重命名为 boundingBox,以遵循更简洁的命名规范
同时更新相关文档和示例代码中的调用
This commit is contained in:
ChestnutYueyue 2026-02-26 00:55:13 +08:00
parent 377ec373b0
commit ea5ecd383f
39 changed files with 2031 additions and 2149 deletions

View File

@ -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); // 世界坐标

View File

@ -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()`** - 自动利用空间索引优化检测

View File

@ -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);

View File

@ -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) {

View File

@ -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);
// 分享功能暂不实现

View File

@ -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

View File

@ -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:
/**

View File

@ -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

View File

@ -34,7 +34,7 @@ public:
* @brief
* @return
*/
extra2d::Rect getBoundingBox() const override;
extra2d::Rect boundingBox() const override;
/**
* @brief

View File

@ -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);
}

View File

@ -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_));
}

View File

@ -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

View File

@ -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_; }

View File

@ -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;

View File

@ -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();

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
};

View File

@ -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);
};

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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));
}
// 子类重写此方法以支持自定义渲染

View File

@ -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();

View File

@ -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();
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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();
}

View File

@ -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) {

View File

@ -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;
}

View File

@ -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

View File

@ -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();
}

View File

@ -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