refactor: 统一方法命名风格,使用更简洁的getter命名

将get前缀的getter方法统一改为更简洁的属性风格命名,例如getPosition()改为pos(),getRotation()改为rot()等,提升代码一致性和可读性

- 修改Window、Sound、Tween、Sprite等类的方法命名
- 更新所有调用点以适应新的命名风格
- 保持原有功能不变,仅重构命名
This commit is contained in:
ChestnutYueyue 2026-02-26 00:38:31 +08:00
parent 0f520c8e37
commit 377ec373b0
36 changed files with 216 additions and 211 deletions

View File

@ -18,17 +18,17 @@ public:
Rect getBoundingBox() const override { Rect getBoundingBox() const override {
// 返回实际的矩形边界 // 返回实际的矩形边界
Vec2 pos = getPosition(); Vec2 position = pos();
return Rect(pos.x - width_ / 2, pos.y - height_ / 2, width_, height_); return Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_);
} }
void onRender(RenderBackend &renderer) override { void onRender(RenderBackend &renderer) override {
Vec2 pos = getPosition(); Vec2 position = pos();
// 绘制填充矩形 // 绘制填充矩形
Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.8f) : color_; Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.8f) : color_;
renderer.fillRect( renderer.fillRect(
Rect(pos.x - width_ / 2, pos.y - height_ / 2, width_, height_), Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_),
fillColor); fillColor);
// 绘制边框 // 绘制边框
@ -36,7 +36,7 @@ public:
: Color(1.0f, 1.0f, 1.0f, 0.5f); : Color(1.0f, 1.0f, 1.0f, 0.5f);
float borderWidth = isColliding_ ? 3.0f : 2.0f; float borderWidth = isColliding_ ? 3.0f : 2.0f;
renderer.drawRect( renderer.drawRect(
Rect(pos.x - width_ / 2, pos.y - height_ / 2, width_, height_), Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_),
borderColor, borderWidth); borderColor, borderWidth);
} }

View File

@ -95,8 +95,8 @@ void BaseScene::renderContent(extra2d::RenderBackend &renderer) {
// 检查当前场景是否作为 TransitionScene 的子场景被渲染 // 检查当前场景是否作为 TransitionScene 的子场景被渲染
bool isChildOfTransition = false; bool isChildOfTransition = false;
if (auto parent = getParent()) { if (auto parentNode = parent()) {
if (dynamic_cast<extra2d::TransitionScene *>(parent.get())) { if (dynamic_cast<extra2d::TransitionScene *>(parentNode.get())) {
isChildOfTransition = true; isChildOfTransition = true;
} }
} }

View File

@ -107,8 +107,8 @@ void GameScene::onUpdate(float dt) {
if (pipes_) { if (pipes_) {
Pipe *firstPipe = pipes_->getPipe(0); Pipe *firstPipe = pipes_->getPipe(0);
if (firstPipe && !firstPipe->scored) { if (firstPipe && !firstPipe->scored) {
float birdX = bird_->getPosition().x; float birdX = bird_->pos().x;
float pipeX = firstPipe->getPosition().x; float pipeX = firstPipe->pos().x;
if (pipeX <= birdX) { if (pipeX <= birdX) {
score_++; score_++;
scoreNumber_->setNumber(score_); scoreNumber_->setNumber(score_);
@ -122,9 +122,9 @@ void GameScene::onUpdate(float dt) {
onHit(); onHit();
} }
if (bird_->isLiving() && GAME_HEIGHT - bird_->getPosition().y <= 123.0f) { if (bird_->isLiving() && GAME_HEIGHT - bird_->pos().y <= 123.0f) {
bird_->setPosition( bird_->setPosition(
extra2d::Vec2(bird_->getPosition().x, GAME_HEIGHT - 123.0f)); extra2d::Vec2(bird_->pos().x, GAME_HEIGHT - 123.0f));
bird_->setStatus(Bird::Status::Still); bird_->setStatus(Bird::Status::Still);
onHit(); onHit();
} }

View File

@ -108,17 +108,17 @@ void Bird::fall(float dt) {
if (!living_) return; if (!living_) return;
// 更新垂直位置 // 更新垂直位置
extra2d::Vec2 pos = getPosition(); extra2d::Vec2 position = pos();
pos.y += speed_ * dt; position.y += speed_ * dt;
setPosition(pos); setPosition(position);
// 应用重力 // 应用重力
speed_ += gravity * dt; speed_ += gravity * dt;
// 限制顶部边界 // 限制顶部边界
if (pos.y < 0) { if (position.y < 0) {
pos.y = 0; position.y = 0;
setPosition(pos); setPosition(position);
speed_ = 0; speed_ = 0;
} }
@ -181,12 +181,12 @@ void Bird::setStatus(Status status) {
} }
extra2d::Rect Bird::getBoundingBox() const { extra2d::Rect Bird::getBoundingBox() const {
extra2d::Vec2 pos = getPosition(); extra2d::Vec2 position = pos();
// 小鸟碰撞框大小约为 24x24 // 小鸟碰撞框大小约为 24x24
float halfSize = 12.0f; float halfSize = 12.0f;
return extra2d::Rect( return extra2d::Rect(
pos.x - halfSize, position.x - halfSize,
pos.y - halfSize, position.y - halfSize,
halfSize * 2.0f, halfSize * 2.0f,
halfSize * 2.0f halfSize * 2.0f
); );

View File

@ -47,8 +47,8 @@ void Ground::onUpdate(float dt) {
float groundWidth = ground1_->getTextureRect().size.width; float groundWidth = ground1_->getTextureRect().size.width;
// 移动两块地面 // 移动两块地面
extra2d::Vec2 pos1 = ground1_->getPosition(); extra2d::Vec2 pos1 = ground1_->pos();
extra2d::Vec2 pos2 = ground2_->getPosition(); extra2d::Vec2 pos2 = ground2_->pos();
pos1.x -= speed * dt; pos1.x -= speed * dt;
pos2.x -= speed * dt; pos2.x -= speed * dt;

View File

@ -57,7 +57,7 @@ Pipe::~Pipe() = default;
extra2d::Rect Pipe::getBoundingBox() const { extra2d::Rect Pipe::getBoundingBox() const {
// 返回整个水管的边界框(包含上下两根) // 返回整个水管的边界框(包含上下两根)
extra2d::Vec2 pos = getPosition(); extra2d::Vec2 position = pos();
// 水管宽度约为 52 // 水管宽度约为 52
float pipeWidth = 52.0f; float pipeWidth = 52.0f;
@ -67,7 +67,7 @@ extra2d::Rect Pipe::getBoundingBox() const {
float screenHeight = GAME_HEIGHT; float screenHeight = GAME_HEIGHT;
return extra2d::Rect( return extra2d::Rect(
pos.x - halfWidth, position.x - halfWidth,
0.0f, 0.0f,
pipeWidth, pipeWidth,
screenHeight screenHeight
@ -77,16 +77,16 @@ extra2d::Rect Pipe::getBoundingBox() const {
extra2d::Rect Pipe::getTopPipeBox() const { extra2d::Rect Pipe::getTopPipeBox() const {
if (!topPipe_) return extra2d::Rect(); if (!topPipe_) return extra2d::Rect();
extra2d::Vec2 pos = getPosition(); extra2d::Vec2 position = pos();
extra2d::Vec2 topPos = topPipe_->getPosition(); extra2d::Vec2 topPos = topPipe_->pos();
// 上水管尺寸 // 上水管尺寸
float pipeWidth = 52.0f; float pipeWidth = 52.0f;
float pipeHeight = 320.0f; float pipeHeight = 320.0f;
return extra2d::Rect( return extra2d::Rect(
pos.x - pipeWidth / 2.0f, position.x - pipeWidth / 2.0f,
pos.y + topPos.y - pipeHeight, position.y + topPos.y - pipeHeight,
pipeWidth, pipeWidth,
pipeHeight pipeHeight
); );
@ -95,16 +95,16 @@ extra2d::Rect Pipe::getTopPipeBox() const {
extra2d::Rect Pipe::getBottomPipeBox() const { extra2d::Rect Pipe::getBottomPipeBox() const {
if (!bottomPipe_) return extra2d::Rect(); if (!bottomPipe_) return extra2d::Rect();
extra2d::Vec2 pos = getPosition(); extra2d::Vec2 position = pos();
extra2d::Vec2 bottomPos = bottomPipe_->getPosition(); extra2d::Vec2 bottomPos = bottomPipe_->pos();
// 下水管尺寸 // 下水管尺寸
float pipeWidth = 52.0f; float pipeWidth = 52.0f;
float pipeHeight = 320.0f; float pipeHeight = 320.0f;
return extra2d::Rect( return extra2d::Rect(
pos.x - pipeWidth / 2.0f, position.x - pipeWidth / 2.0f,
pos.y + bottomPos.y, position.y + bottomPos.y,
pipeWidth, pipeWidth,
pipeHeight pipeHeight
); );

View File

@ -40,20 +40,20 @@ void Pipes::onUpdate(float dt) {
// 移动所有水管 // 移动所有水管
for (int i = 0; i < pipeCount_; ++i) { for (int i = 0; i < pipeCount_; ++i) {
if (pipes_[i]) { if (pipes_[i]) {
extra2d::Vec2 pos = pipes_[i]->getPosition(); extra2d::Vec2 pos = pipes_[i]->pos();
pos.x -= pipeSpeed * dt; pos.x -= pipeSpeed * dt;
pipes_[i]->setPosition(pos); pipes_[i]->setPosition(pos);
} }
} }
// 检查最前面的水管是否移出屏幕 // 检查最前面的水管是否移出屏幕
if (pipes_[0] && pipes_[0]->getPosition().x <= -30.0f) { if (pipes_[0] && pipes_[0]->pos().x <= -30.0f) {
// 移除第一个水管(通过名称查找并移除) // 移除第一个水管(通过名称查找并移除)
// 由于 removeChild 需要 Ptr<Node>,我们使用 removeChildByName 或直接操作 // 由于 removeChild 需要 Ptr<Node>,我们使用 removeChildByName 或直接操作
// 这里我们直接移除第一个子节点(假设它是水管) // 这里我们直接移除第一个子节点(假设它是水管)
auto children = getChildren(); auto childNodes = children();
if (!children.empty()) { if (!childNodes.empty()) {
removeChild(children[0]); removeChild(childNodes[0]);
} }
// 将后面的水管前移 // 将后面的水管前移
@ -83,7 +83,7 @@ void Pipes::addPipe() {
)); ));
} else { } else {
// 其他水管在前一个水管后方 // 其他水管在前一个水管后方
float prevX = pipes_[pipeCount_ - 1]->getPosition().x; float prevX = pipes_[pipeCount_ - 1]->pos().x;
pipe->setPosition(extra2d::Vec2(prevX + pipeSpacing, 0.0f)); pipe->setPosition(extra2d::Vec2(prevX + pipeSpacing, 0.0f));
} }

View File

@ -106,8 +106,8 @@ void BaseScene::renderContent(extra2d::RenderBackend &renderer) {
// 检查当前场景是否作为 TransitionScene 的子场景被渲染 // 检查当前场景是否作为 TransitionScene 的子场景被渲染
bool isChildOfTransition = false; bool isChildOfTransition = false;
if (auto parent = getParent()) { if (auto parentNode = parent()) {
if (dynamic_cast<extra2d::TransitionScene *>(parent.get())) { if (dynamic_cast<extra2d::TransitionScene *>(parentNode.get())) {
isChildOfTransition = true; isChildOfTransition = true;
} }
} }

View File

@ -30,7 +30,7 @@ void StartScene::onEnter() {
auto &app = extra2d::Application::instance(); auto &app = extra2d::Application::instance();
auto &resources = app.resources(); auto &resources = app.resources();
if (getChildren().empty()) { if (children().empty()) {
// 使用游戏逻辑分辨率 // 使用游戏逻辑分辨率
float screenW = GAME_WIDTH; float screenW = GAME_WIDTH;
float screenH = GAME_HEIGHT; float screenH = GAME_HEIGHT;

View File

@ -27,7 +27,7 @@ void SuccessScene::onEnter() {
auto& app = extra2d::Application::instance(); auto& app = extra2d::Application::instance();
auto& resources = app.resources(); auto& resources = app.resources();
if (getChildren().empty()) { if (children().empty()) {
// 使用游戏逻辑分辨率 // 使用游戏逻辑分辨率
float screenW = GAME_WIDTH; float screenW = GAME_WIDTH;
float screenH = GAME_HEIGHT; float screenH = GAME_HEIGHT;

View File

@ -34,7 +34,12 @@ int main(int argc, char **argv)
} }
// 初始化存储系统 // 初始化存储系统
// 在 Windows 上使用当前工作目录,在 Switch 上使用 sdmc:/
#ifdef __SWITCH__
pushbox::initStorage("sdmc:/"); pushbox::initStorage("sdmc:/");
#else
pushbox::initStorage(".");
#endif
pushbox::g_CurrentLevel = pushbox::loadCurrentLevel(1); pushbox::g_CurrentLevel = pushbox::loadCurrentLevel(1);
if (pushbox::g_CurrentLevel > MAX_LEVEL) { if (pushbox::g_CurrentLevel > MAX_LEVEL) {
pushbox::g_CurrentLevel = 1; pushbox::g_CurrentLevel = 1;

View File

@ -39,39 +39,39 @@ public:
// 必须实现 getBoundingBox() 才能参与空间索引碰撞检测 // 必须实现 getBoundingBox() 才能参与空间索引碰撞检测
Rect getBoundingBox() const override { Rect getBoundingBox() const override {
Vec2 pos = getPosition(); Vec2 position = pos();
return Rect(pos.x - size_ / 2, pos.y - size_ / 2, size_, size_); return Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_);
} }
void update(float dt, float screenWidth, float screenHeight) { void update(float dt, float screenWidth, float screenHeight) {
Vec2 pos = getPosition(); Vec2 position = pos();
pos = pos + velocity_ * dt; position = position + velocity_ * dt;
// 边界反弹 // 边界反弹
if (pos.x < size_ / 2 || pos.x > screenWidth - size_ / 2) { if (position.x < size_ / 2 || position.x > screenWidth - size_ / 2) {
velocity_.x = -velocity_.x; velocity_.x = -velocity_.x;
pos.x = std::clamp(pos.x, size_ / 2, screenWidth - size_ / 2); position.x = std::clamp(position.x, size_ / 2, screenWidth - size_ / 2);
} }
if (pos.y < size_ / 2 || pos.y > screenHeight - size_ / 2) { if (position.y < size_ / 2 || position.y > screenHeight - size_ / 2) {
velocity_.y = -velocity_.y; velocity_.y = -velocity_.y;
pos.y = std::clamp(pos.y, size_ / 2, screenHeight - size_ / 2); position.y = std::clamp(position.y, size_ / 2, screenHeight - size_ / 2);
} }
setPosition(pos); setPosition(position);
} }
void onRender(RenderBackend &renderer) override { void onRender(RenderBackend &renderer) override {
Vec2 pos = getPosition(); Vec2 position = pos();
// 碰撞时变红色 // 碰撞时变红色
Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.9f) : color_; Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.9f) : color_;
renderer.fillRect(Rect(pos.x - size_ / 2, pos.y - size_ / 2, size_, size_), renderer.fillRect(Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_),
fillColor); fillColor);
// 绘制边框 // 绘制边框
Color borderColor = isColliding_ ? Color(1.0f, 0.0f, 0.0f, 1.0f) Color borderColor = isColliding_ ? Color(1.0f, 0.0f, 0.0f, 1.0f)
: Color(0.3f, 0.3f, 0.3f, 0.5f); : Color(0.3f, 0.3f, 0.3f, 0.5f);
renderer.drawRect(Rect(pos.x - size_ / 2, pos.y - size_ / 2, size_, size_), renderer.drawRect(Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_),
borderColor, 1.0f); borderColor, 1.0f);
} }
@ -125,7 +125,7 @@ public:
auto startTime = std::chrono::high_resolution_clock::now(); auto startTime = std::chrono::high_resolution_clock::now();
// 更新所有物理节点位置 // 更新所有物理节点位置
for (const auto &child : getChildren()) { for (const auto &child : children()) {
if (auto node = dynamic_cast<PhysicsNode *>(child.get())) { if (auto node = dynamic_cast<PhysicsNode *>(child.get())) {
node->update(dt, screenWidth_, screenHeight_); node->update(dt, screenWidth_, screenHeight_);
} }
@ -330,7 +330,7 @@ private:
*/ */
size_t getPhysicsNodeCount() const { size_t getPhysicsNodeCount() const {
size_t count = 0; size_t count = 0;
for (const auto &child : getChildren()) { for (const auto &child : children()) {
if (dynamic_cast<PhysicsNode *>(child.get())) { if (dynamic_cast<PhysicsNode *>(child.get())) {
++count; ++count;
} }
@ -343,7 +343,7 @@ private:
*/ */
std::vector<PhysicsNode *> getPhysicsNodes() const { std::vector<PhysicsNode *> getPhysicsNodes() const {
std::vector<PhysicsNode *> nodes; std::vector<PhysicsNode *> nodes;
for (const auto &child : getChildren()) { for (const auto &child : children()) {
if (auto node = dynamic_cast<PhysicsNode *>(child.get())) { if (auto node = dynamic_cast<PhysicsNode *>(child.get())) {
nodes.push_back(node); nodes.push_back(node);
} }
@ -378,7 +378,7 @@ private:
// 从后往前移除指定数量的节点 // 从后往前移除指定数量的节点
for (size_t i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
// 找到最后一个物理节点对应的子节点并移除 // 找到最后一个物理节点对应的子节点并移除
for (auto it = getChildren().rbegin(); it != getChildren().rend(); ++it) { for (auto it = children().rbegin(); it != children().rend(); ++it) {
if (dynamic_cast<PhysicsNode *>(it->get())) { if (dynamic_cast<PhysicsNode *>(it->get())) {
removeChild(*it); removeChild(*it);
break; break;
@ -413,7 +413,7 @@ private:
*/ */
void performCollisionDetection() { void performCollisionDetection() {
// 清除之前的碰撞状态 // 清除之前的碰撞状态
for (const auto &child : getChildren()) { for (const auto &child : children()) {
if (auto node = dynamic_cast<PhysicsNode *>(child.get())) { if (auto node = dynamic_cast<PhysicsNode *>(child.get())) {
node->setColliding(false); node->setColliding(false);
} }

View File

@ -56,7 +56,7 @@ public:
// ------ 名称(用于缓存索引)------ // ------ 名称(用于缓存索引)------
void setName(const std::string &name) { name_ = name; } void setName(const std::string &name) { name_ = name; }
const std::string &getName() const { return name_; } const std::string &name() const { return name_; }
// ------ 有效性检查 ------ // ------ 有效性检查 ------
bool isValid() const { return texture_ != nullptr; } bool isValid() const { return texture_ != nullptr; }

View File

@ -99,7 +99,7 @@ public:
float getTotalDuration() const; float getTotalDuration() const;
void setName(const std::string &name) { name_ = name; } void setName(const std::string &name) { name_ = name; }
const std::string &getName() const { return name_; } const std::string &name() const { return name_; }
private: private:
Node *target_ = nullptr; Node *target_ = nullptr;

View File

@ -38,7 +38,7 @@ public:
void setCursor(float seconds); void setCursor(float seconds);
const std::string& getFilePath() const { return filePath_; } const std::string& getFilePath() const { return filePath_; }
const std::string& getName() const { return name_; } const std::string& name() const { return name_; }
private: private:
friend class AudioEngine; friend class AudioEngine;

View File

@ -22,10 +22,10 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setPosition(const Vec2 &position); void setPosition(const Vec2 &position);
void setPosition(float x, float y); void setPosition(float x, float y);
Vec2 getPosition() const { return position_; } Vec2 pos() const { return position_; }
void setRotation(float degrees); void setRotation(float degrees);
float getRotation() const { return rotation_; } float rot() const { return rotation_; }
void setZoom(float zoom); void setZoom(float zoom);
float getZoom() const { return zoom_; } float getZoom() const { return zoom_; }

View File

@ -76,7 +76,7 @@ public:
int getWidth() const { return width_; } int getWidth() const { return width_; }
int getHeight() const { return height_; } int getHeight() const { return height_; }
Size getSize() const { return Size(static_cast<float>(width_), static_cast<float>(height_)); } Size getSize() const { return Size(static_cast<float>(width_), static_cast<float>(height_)); }
Vec2 getPosition() const; Vec2 pos() const;
bool isFullscreen() const { return fullscreen_; } bool isFullscreen() const { return fullscreen_; }
bool isVSync() const { return vsync_; } bool isVSync() const { return vsync_; }

View File

@ -44,39 +44,39 @@ public:
void removeFromParent(); void removeFromParent();
void removeAllChildren(); void removeAllChildren();
Ptr<Node> getParent() const { return parent_.lock(); } Ptr<Node> parent() const { return parent_.lock(); }
const std::vector<Ptr<Node>> &getChildren() const { return children_; } const std::vector<Ptr<Node>> &children() const { return children_; }
Ptr<Node> getChildByName(const std::string &name) const; Ptr<Node> childByName(const std::string &name) const;
Ptr<Node> getChildByTag(int tag) const; Ptr<Node> childByTag(int tag) const;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// 变换属性 // 变换属性
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setPosition(const Vec2 &pos); void setPosition(const Vec2 &pos);
void setPosition(float x, float y); void setPosition(float x, float y);
Vec2 getPosition() const { return position_; } Vec2 pos() const { return position_; }
void setRotation(float degrees); void setRotation(float degrees);
float getRotation() const { return rotation_; } float rot() const { return rotation_; }
void setScale(const Vec2 &scale); void setScale(const Vec2 &scale);
void setScale(float scale); void setScale(float scale);
void setScale(float x, float y); void setScale(float x, float y);
Vec2 getScale() const { return scale_; } Vec2 scale() const { return scale_; }
void setAnchor(const Vec2 &anchor); void setAnchor(const Vec2 &anchor);
void setAnchor(float x, float y); void setAnchor(float x, float y);
Vec2 getAnchor() const { return anchor_; } Vec2 anchor() const { return anchor_; }
void setSkew(const Vec2 &skew); void setSkew(const Vec2 &skew);
void setSkew(float x, float y); void setSkew(float x, float y);
Vec2 getSkew() const { return skew_; } Vec2 skew() const { return skew_; }
void setOpacity(float opacity); void setOpacity(float opacity);
float getOpacity() const { return opacity_; } float opacity() const { return opacity_; }
void setVisible(bool visible); void setVisible(bool visible);
bool isVisible() const { return visible_; } bool visible() const { return visible_; }
/** /**
* @brief * @brief
@ -89,16 +89,16 @@ public:
* @brief X轴翻转 * @brief X轴翻转
*/ */
void setFlipX(bool flipX); void setFlipX(bool flipX);
bool isFlipX() const { return flipX_; } bool flipX() const { return flipX_; }
/** /**
* @brief Y轴翻转 * @brief Y轴翻转
*/ */
void setFlipY(bool flipY); void setFlipY(bool flipY);
bool isFlipY() const { return flipY_; } bool flipY() const { return flipY_; }
void setZOrder(int zOrder); void setZOrder(int zOrder);
int getZOrder() const { return zOrder_; } int zOrder() const { return zOrder_; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// 世界变换 // 世界变换
@ -130,10 +130,10 @@ public:
// 名称和标签 // 名称和标签
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setName(const std::string &name) { name_ = name; } void setName(const std::string &name) { name_ = name; }
const std::string &getName() const { return name_; } const std::string &name() const { return name_; }
void setTag(int tag) { tag_ = tag; } void setTag(int tag) { tag_ = tag; }
int getTag() const { return tag_; } int tag() const { return tag_; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// 生命周期回调 // 生命周期回调

View File

@ -29,8 +29,8 @@ public:
// 翻转 // 翻转
void setFlipX(bool flip); void setFlipX(bool flip);
void setFlipY(bool flip); void setFlipY(bool flip);
bool isFlipX() const { return flipX_; } bool flipX() const { return flipX_; }
bool isFlipY() const { return flipY_; } bool flipY() const { return flipY_; }
// 静态创建方法 // 静态创建方法
static Ptr<Sprite> create(); static Ptr<Sprite> create();

View File

@ -50,10 +50,10 @@ public:
started_ = true; started_ = true;
if (target_) { if (target_) {
startPos_ = target_->getPosition(); startPos_ = target_->pos();
startScale_ = target_->getScale(); startScale_ = target_->scale();
startRotation_ = target_->getRotation(); startRotation_ = target_->rot();
startOpacity_ = target_->getOpacity(); startOpacity_ = target_->opacity();
} }
} }

View File

@ -301,7 +301,7 @@ void Window::setResizable(bool resizable) {
} }
} }
Vec2 Window::getPosition() const { Vec2 Window::pos() const {
if (sdlWindow_) { if (sdlWindow_) {
int x, y; int x, y;
SDL_GetWindowPosition(sdlWindow_, &x, &y); SDL_GetWindowPosition(sdlWindow_, &x, &y);

View File

@ -25,11 +25,11 @@ void Node::addChild(Ptr<Node> child) {
childrenOrderDirty_ = true; childrenOrderDirty_ = true;
// 更新索引 // 更新索引
if (!child->getName().empty()) { if (!child->name().empty()) {
nameIndex_[child->getName()] = child; nameIndex_[child->name()] = child;
} }
if (child->getTag() != -1) { if (child->tag() != -1) {
tagIndex_[child->getTag()] = child; tagIndex_[child->tag()] = child;
} }
if (running_) { if (running_) {
@ -57,11 +57,11 @@ void Node::addChildren(std::vector<Ptr<Node>> &&children) {
children_.push_back(child); children_.push_back(child);
// 更新索引 // 更新索引
if (!child->getName().empty()) { if (!child->name().empty()) {
nameIndex_[child->getName()] = child; nameIndex_[child->name()] = child;
} }
if (child->getTag() != -1) { if (child->tag() != -1) {
tagIndex_[child->getTag()] = child; tagIndex_[child->tag()] = child;
} }
if (running_) { if (running_) {
@ -91,11 +91,11 @@ void Node::removeChild(Ptr<Node> child) {
(*it)->onExit(); (*it)->onExit();
} }
// 从索引中移除 // 从索引中移除
if (!(*it)->getName().empty()) { if (!(*it)->name().empty()) {
nameIndex_.erase((*it)->getName()); nameIndex_.erase((*it)->name());
} }
if ((*it)->getTag() != -1) { if ((*it)->tag() != -1) {
tagIndex_.erase((*it)->getTag()); tagIndex_.erase((*it)->tag());
} }
(*it)->parent_.reset(); (*it)->parent_.reset();
children_.erase(it); children_.erase(it);
@ -103,7 +103,7 @@ void Node::removeChild(Ptr<Node> child) {
} }
void Node::removeChildByName(const std::string &name) { void Node::removeChildByName(const std::string &name) {
auto child = getChildByName(name); auto child = childByName(name);
if (child) { if (child) {
removeChild(child); removeChild(child);
} }
@ -138,7 +138,7 @@ void Node::removeAllChildren() {
tagIndex_.clear(); tagIndex_.clear();
} }
Ptr<Node> Node::getChildByName(const std::string &name) const { Ptr<Node> Node::childByName(const std::string &name) const {
// 使用哈希索引O(1) 查找 // 使用哈希索引O(1) 查找
auto it = nameIndex_.find(name); auto it = nameIndex_.find(name);
if (it != nameIndex_.end()) { if (it != nameIndex_.end()) {
@ -147,7 +147,7 @@ Ptr<Node> Node::getChildByName(const std::string &name) const {
return nullptr; return nullptr;
} }
Ptr<Node> Node::getChildByTag(int tag) const { Ptr<Node> Node::childByTag(int tag) const {
// 使用哈希索引O(1) 查找 // 使用哈希索引O(1) 查找
auto it = tagIndex_.find(tag); auto it = tagIndex_.find(tag);
if (it != tagIndex_.end()) { if (it != tagIndex_.end()) {
@ -424,10 +424,10 @@ void Node::sortChildren() {
// 插入排序 // 插入排序
for (size_t i = 1; i < n; ++i) { for (size_t i = 1; i < n; ++i) {
auto key = children_[i]; auto key = children_[i];
int keyZOrder = key->getZOrder(); int keyZOrder = key->zOrder();
int j = static_cast<int>(i) - 1; int j = static_cast<int>(i) - 1;
while (j >= 0 && children_[j]->getZOrder() > keyZOrder) { while (j >= 0 && children_[j]->zOrder() > keyZOrder) {
children_[j + 1] = children_[j]; children_[j + 1] = children_[j];
--j; --j;
} }
@ -437,7 +437,7 @@ void Node::sortChildren() {
// 大数组使用标准排序 // 大数组使用标准排序
std::sort(children_.begin(), children_.end(), std::sort(children_.begin(), children_.end(),
[](const Ptr<Node> &a, const Ptr<Node> &b) { [](const Ptr<Node> &a, const Ptr<Node> &b) {
return a->getZOrder() < b->getZOrder(); return a->zOrder() < b->zOrder();
}); });
} }

View File

@ -23,7 +23,7 @@ void Scene::setViewportSize(const Size &size) {
} }
void Scene::renderScene(RenderBackend &renderer) { void Scene::renderScene(RenderBackend &renderer) {
if (!isVisible()) if (!visible())
return; return;
// Begin frame with background color // Begin frame with background color
@ -33,7 +33,7 @@ void Scene::renderScene(RenderBackend &renderer) {
} }
void Scene::renderContent(RenderBackend &renderer) { void Scene::renderContent(RenderBackend &renderer) {
if (!isVisible()) if (!visible())
return; return;
// 在渲染前批量更新所有节点的世界变换 // 在渲染前批量更新所有节点的世界变换
@ -119,7 +119,7 @@ std::vector<std::pair<Node *, Node *>> Scene::queryCollisions() const {
void Scene::collectRenderCommands(std::vector<RenderCommand> &commands, void Scene::collectRenderCommands(std::vector<RenderCommand> &commands,
int parentZOrder) { int parentZOrder) {
if (!isVisible()) if (!visible())
return; return;
// 从场景的子节点开始收集渲染命令 // 从场景的子节点开始收集渲染命令

View File

@ -17,14 +17,14 @@ namespace extra2d {
namespace { namespace {
Node *hitTestTopmost(const Ptr<Node> &node, const Vec2 &worldPos) { Node *hitTestTopmost(const Ptr<Node> &node, const Vec2 &worldPos) {
if (!node || !node->isVisible()) { if (!node || !node->visible()) {
return nullptr; return nullptr;
} }
std::vector<Ptr<Node>> children = node->getChildren(); std::vector<Ptr<Node>> children = node->children();
std::stable_sort(children.begin(), children.end(), std::stable_sort(children.begin(), children.end(),
[](const Ptr<Node> &a, const Ptr<Node> &b) { [](const Ptr<Node> &a, const Ptr<Node> &b) {
return a->getZOrder() < b->getZOrder(); return a->zOrder() < b->zOrder();
}); });
for (auto it = children.rbegin(); it != children.rend(); ++it) { for (auto it = children.rbegin(); it != children.rend(); ++it) {
@ -365,7 +365,7 @@ void SceneManager::popToScene(const std::string &name) {
while (!sceneStack_.empty()) { while (!sceneStack_.empty()) {
auto scene = sceneStack_.top(); auto scene = sceneStack_.top();
if (scene->getName() == name) { if (scene->name() == name) {
target = scene; target = scene;
break; break;
} }
@ -449,7 +449,7 @@ Ptr<Scene> SceneManager::getSceneByName(const std::string &name) const {
auto tempStack = sceneStack_; auto tempStack = sceneStack_;
while (!tempStack.empty()) { while (!tempStack.empty()) {
auto scene = tempStack.top(); auto scene = tempStack.top();
if (scene->getName() == name) { if (scene->name() == name) {
return scene; return scene;
} }
tempStack.pop(); tempStack.pop();

View File

@ -129,7 +129,7 @@ Rect ShapeNode::getBoundingBox() const {
return Rect(); return Rect();
} }
Vec2 offset = getPosition(); Vec2 offset = pos();
if (shapeType_ == ShapeType::Circle && points_.size() >= 2) { if (shapeType_ == ShapeType::Circle && points_.size() >= 2) {
float radius = std::abs(points_[1].x); float radius = std::abs(points_[1].x);
@ -171,7 +171,7 @@ void ShapeNode::onDraw(RenderBackend &renderer) {
return; return;
} }
Vec2 offset = getPosition(); Vec2 offset = pos();
switch (shapeType_) { switch (shapeType_) {
case ShapeType::Point: case ShapeType::Point:
@ -254,7 +254,7 @@ void ShapeNode::generateRenderCommand(std::vector<RenderCommand> &commands,
return; return;
} }
Vec2 offset = getPosition(); Vec2 offset = pos();
RenderCommand cmd; RenderCommand cmd;
cmd.layer = zOrder; cmd.layer = zOrder;

View File

@ -51,14 +51,14 @@ Rect Sprite::getBoundingBox() const {
float width = textureRect_.width(); float width = textureRect_.width();
float height = textureRect_.height(); float height = textureRect_.height();
auto pos = getPosition(); auto position = pos();
auto anchor = getAnchor(); auto anchorPt = anchor();
auto scale = getScale(); auto scaleVal = scale();
float w = width * scale.x; float w = width * scaleVal.x;
float h = height * scale.y; float h = height * scaleVal.y;
float x0 = pos.x - width * anchor.x * scale.x; float x0 = position.x - width * anchorPt.x * scaleVal.x;
float y0 = pos.y - height * anchor.y * scale.y; float y0 = position.y - height * anchorPt.y * scaleVal.y;
float x1 = x0 + w; float x1 = x0 + w;
float y1 = y0 + h; float y1 = y0 + h;
@ -89,7 +89,7 @@ void Sprite::onDraw(RenderBackend &renderer) {
float worldScaleY = float worldScaleY =
glm::length(glm::vec2(worldTransform[1][0], worldTransform[1][1])); glm::length(glm::vec2(worldTransform[1][0], worldTransform[1][1]));
auto anchor = getAnchor(); auto anchorPt = anchor();
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸 // 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY); Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY);
@ -109,7 +109,7 @@ void Sprite::onDraw(RenderBackend &renderer) {
float worldRotation = std::atan2(worldTransform[0][1], worldTransform[0][0]); float worldRotation = std::atan2(worldTransform[0][1], worldTransform[0][0]);
renderer.drawSprite(*texture_, destRect, srcRect, color_, worldRotation, renderer.drawSprite(*texture_, destRect, srcRect, color_, worldRotation,
anchor); anchorPt);
} }
void Sprite::generateRenderCommand(std::vector<RenderCommand> &commands, void Sprite::generateRenderCommand(std::vector<RenderCommand> &commands,
@ -135,7 +135,7 @@ void Sprite::generateRenderCommand(std::vector<RenderCommand> &commands,
float worldScaleY = float worldScaleY =
glm::length(glm::vec2(worldTransform[1][0], worldTransform[1][1])); glm::length(glm::vec2(worldTransform[1][0], worldTransform[1][1]));
auto anchor = getAnchor(); auto anchorPt = anchor();
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸 // 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY); Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY);
@ -159,7 +159,7 @@ void Sprite::generateRenderCommand(std::vector<RenderCommand> &commands,
cmd.type = RenderCommandType::Sprite; cmd.type = RenderCommandType::Sprite;
cmd.layer = zOrder; cmd.layer = zOrder;
cmd.data = SpriteCommandData{texture_.get(), destRect, srcRect, color_, cmd.data = SpriteCommandData{texture_.get(), destRect, srcRect, color_,
worldRotation, anchor, 0}; worldRotation, anchorPt, 0};
commands.push_back(std::move(cmd)); commands.push_back(std::move(cmd));
} }

View File

@ -36,7 +36,7 @@ void TransitionFlipScene::renderContent(RenderBackend &renderer) {
float currentAngle = angle; float currentAngle = angle;
Camera *camera = outScene_->getActiveCamera(); Camera *camera = outScene_->getActiveCamera();
float originalRotation = camera ? camera->getRotation() : 0.0f; float originalRotation = camera ? camera->rot() : 0.0f;
if (axis_ == Axis::Horizontal) { if (axis_ == Axis::Horizontal) {
// 水平轴翻转 - 模拟绕X轴旋转 // 水平轴翻转 - 模拟绕X轴旋转
@ -62,7 +62,7 @@ void TransitionFlipScene::renderContent(RenderBackend &renderer) {
float currentAngle = angle - PI_F; float currentAngle = angle - PI_F;
Camera *camera = inScene_->getActiveCamera(); Camera *camera = inScene_->getActiveCamera();
float originalRotation = camera ? camera->getRotation() : 0.0f; float originalRotation = camera ? camera->rot() : 0.0f;
if (axis_ == Axis::Horizontal) { if (axis_ == Axis::Horizontal) {
if (camera) { if (camera) {

View File

@ -65,7 +65,7 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) {
} }
Camera *camera = outScene_->getActiveCamera(); Camera *camera = outScene_->getActiveCamera();
Vec2 originalPos = camera ? camera->getPosition() : Vec2::Zero(); Vec2 originalPos = camera ? camera->pos() : Vec2::Zero();
if (camera) { if (camera) {
camera->setPosition(originalPos.x + offsetX, originalPos.y + offsetY); camera->setPosition(originalPos.x + offsetX, originalPos.y + offsetY);
@ -99,7 +99,7 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) {
} }
Camera *camera = inScene_->getActiveCamera(); Camera *camera = inScene_->getActiveCamera();
Vec2 originalPos = camera ? camera->getPosition() : Vec2::Zero(); Vec2 originalPos = camera ? camera->pos() : Vec2::Zero();
if (camera) { if (camera) {
camera->setPosition(originalPos.x + offsetX, originalPos.y + offsetY); camera->setPosition(originalPos.x + offsetX, originalPos.y + offsetY);

View File

@ -357,19 +357,19 @@ void Button::setCustomSize(float width, float height) {
* @return * @return
*/ */
Rect Button::getBoundingBox() const { Rect Button::getBoundingBox() const {
auto pos = convertToWorldSpace(extra2d::Vec2::Zero()); auto position = convertToWorldSpace(extra2d::Vec2::Zero());
auto anchor = getAnchor(); auto anchorPt = anchor();
auto scale = getScale(); auto scaleVal = scale();
auto size = getSize(); auto size = getSize();
if (size.empty()) { if (size.empty()) {
return Rect(); return Rect();
} }
float w = size.width * scale.x; float w = size.width * scaleVal.x;
float h = size.height * scale.y; float h = size.height * scaleVal.y;
float x0 = pos.x - size.width * anchor.x * scale.x; float x0 = position.x - size.width * anchorPt.x * scaleVal.x;
float y0 = pos.y - size.height * anchor.y * scale.y; float y0 = position.y - size.height * anchorPt.y * scaleVal.y;
return Rect(x0, y0, w, h); return Rect(x0, y0, w, h);
} }

View File

@ -128,7 +128,7 @@ void CheckBox::setOnStateChange(Function<void(bool)> callback) {
* @return * @return
*/ */
Rect CheckBox::getBoundingBox() const { Rect CheckBox::getBoundingBox() const {
Vec2 pos = getPosition(); Vec2 position = pos();
float width = boxSize_; float width = boxSize_;
if (!label_.empty() && font_) { if (!label_.empty() && font_) {
@ -136,7 +136,7 @@ Rect CheckBox::getBoundingBox() const {
width += spacing_ + textSize.x; width += spacing_ + textSize.x;
} }
return Rect(pos.x, pos.y, width, boxSize_); return Rect(position.x, position.y, width, boxSize_);
} }
/** /**
@ -144,9 +144,9 @@ Rect CheckBox::getBoundingBox() const {
* @param renderer * @param renderer
*/ */
void CheckBox::onDrawWidget(RenderBackend &renderer) { void CheckBox::onDrawWidget(RenderBackend &renderer) {
Vec2 pos = getPosition(); Vec2 position = pos();
Rect boxRect(pos.x, pos.y + (getSize().height - boxSize_) * 0.5f, boxSize_, boxSize_); Rect boxRect(position.x, position.y + (getSize().height - boxSize_) * 0.5f, boxSize_, boxSize_);
Color boxColor = checked_ ? checkedColor_ : uncheckedColor_; Color boxColor = checked_ ? checkedColor_ : uncheckedColor_;
renderer.fillRect(boxRect, boxColor); renderer.fillRect(boxRect, boxColor);
renderer.drawRect(boxRect, Colors::White, 1.0f); renderer.drawRect(boxRect, Colors::White, 1.0f);
@ -165,7 +165,7 @@ void CheckBox::onDrawWidget(RenderBackend &renderer) {
} }
if (!label_.empty() && font_) { if (!label_.empty() && font_) {
Vec2 textPos(pos.x + boxSize_ + spacing_, pos.y); Vec2 textPos(position.x + boxSize_ + spacing_, position.y);
renderer.drawText(*font_, label_, textPos, textColor_); renderer.drawText(*font_, label_, textPos, textColor_);
} }
} }
@ -191,8 +191,8 @@ bool CheckBox::onMousePress(const MouseEvent &event) {
bool CheckBox::onMouseRelease(const MouseEvent &event) { bool CheckBox::onMouseRelease(const MouseEvent &event) {
if (event.button == MouseButton::Left && pressed_) { if (event.button == MouseButton::Left && pressed_) {
pressed_ = false; pressed_ = false;
Vec2 pos = getPosition(); Vec2 position = pos();
Rect boxRect(pos.x, pos.y, boxSize_, boxSize_); Rect boxRect(position.x, position.y, boxSize_, boxSize_);
if (boxRect.containsPoint(Point(event.x, event.y))) { if (boxRect.containsPoint(Point(event.x, event.y))) {
toggle(); toggle();
} }

View File

@ -305,7 +305,7 @@ std::vector<std::string> Label::splitLines() const {
* @return * @return
*/ */
Vec2 Label::calculateDrawPosition() const { Vec2 Label::calculateDrawPosition() const {
Vec2 pos = getPosition(); Vec2 position = pos();
Vec2 size = getTextSize(); Vec2 size = getTextSize();
Size widgetSize = getSize(); Size widgetSize = getSize();
@ -314,10 +314,10 @@ Vec2 Label::calculateDrawPosition() const {
switch (hAlign_) { switch (hAlign_) {
case HorizontalAlign::Center: case HorizontalAlign::Center:
pos.x += (refWidth - size.x) * 0.5f; position.x += (refWidth - size.x) * 0.5f;
break; break;
case HorizontalAlign::Right: case HorizontalAlign::Right:
pos.x += refWidth - size.x; position.x += refWidth - size.x;
break; break;
case HorizontalAlign::Left: case HorizontalAlign::Left:
default: default:
@ -326,17 +326,17 @@ Vec2 Label::calculateDrawPosition() const {
switch (vAlign_) { switch (vAlign_) {
case VerticalAlign::Middle: case VerticalAlign::Middle:
pos.y += (refHeight - size.y) * 0.5f; position.y += (refHeight - size.y) * 0.5f;
break; break;
case VerticalAlign::Bottom: case VerticalAlign::Bottom:
pos.y += refHeight - size.y; position.y += refHeight - size.y;
break; break;
case VerticalAlign::Top: case VerticalAlign::Top:
default: default:
break; break;
} }
return pos; return position;
} }
/** /**
@ -353,11 +353,11 @@ void Label::drawText(RenderBackend &renderer, const Vec2 &position, const Color
if (multiLine_) { if (multiLine_) {
auto lines = splitLines(); auto lines = splitLines();
float lineHeight = getLineHeight(); float lineHeight = getLineHeight();
Vec2 pos = position; Vec2 drawPos = position;
for (const auto &line : lines) { for (const auto &line : lines) {
renderer.drawText(*font_, line, pos, color); renderer.drawText(*font_, line, drawPos, color);
pos.y += lineHeight; drawPos.y += lineHeight;
} }
} else { } else {
renderer.drawText(*font_, text_, position, color); renderer.drawText(*font_, text_, position, color);
@ -379,8 +379,8 @@ Rect Label::getBoundingBox() const {
return Rect(); return Rect();
} }
Vec2 pos = calculateDrawPosition(); Vec2 drawPos = calculateDrawPosition();
return Rect(pos.x, pos.y, size.x, size.y); return Rect(drawPos.x, drawPos.y, size.x, size.y);
} }
/** /**

View File

@ -331,7 +331,7 @@ std::string ProgressBar::formatText() const {
* @return * @return
*/ */
Rect ProgressBar::getBoundingBox() const { Rect ProgressBar::getBoundingBox() const {
return Rect(getPosition().x, getPosition().y, getSize().width, getSize().height); return Rect(pos().x, pos().y, getSize().width, getSize().height);
} }
/** /**
@ -378,11 +378,11 @@ void ProgressBar::onUpdate(float deltaTime) {
* @param renderer * @param renderer
*/ */
void ProgressBar::onDrawWidget(RenderBackend &renderer) { void ProgressBar::onDrawWidget(RenderBackend &renderer) {
Vec2 pos = getPosition(); Vec2 position = pos();
Size size = getSize(); Size size = getSize();
float bgX = pos.x + padding_; float bgX = position.x + padding_;
float bgY = pos.y + padding_; float bgY = position.y + padding_;
float bgW = size.width - padding_ * 2; float bgW = size.width - padding_ * 2;
float bgH = size.height - padding_ * 2; float bgH = size.height - padding_ * 2;
Rect bgRect(bgX, bgY, bgW, bgH); Rect bgRect(bgX, bgY, bgW, bgH);
@ -470,8 +470,8 @@ void ProgressBar::onDrawWidget(RenderBackend &renderer) {
Vec2 textSize = font_->measureText(text); Vec2 textSize = font_->measureText(text);
Vec2 textPos( Vec2 textPos(
pos.x + (size.width - textSize.x) * 0.5f, position.x + (size.width - textSize.x) * 0.5f,
pos.y + (size.height - textSize.y) * 0.5f position.y + (size.height - textSize.y) * 0.5f
); );
renderer.drawText(*font_, text, textPos, textColor_); renderer.drawText(*font_, text, textPos, textColor_);

View File

@ -129,7 +129,7 @@ void RadioButton::setOnStateChange(Function<void(bool)> callback) {
* @return * @return
*/ */
Rect RadioButton::getBoundingBox() const { Rect RadioButton::getBoundingBox() const {
Vec2 pos = getPosition(); Vec2 position = pos();
float width = circleSize_; float width = circleSize_;
if (!label_.empty() && font_) { if (!label_.empty() && font_) {
@ -137,7 +137,7 @@ Rect RadioButton::getBoundingBox() const {
width += spacing_ + textSize.x; width += spacing_ + textSize.x;
} }
return Rect(pos.x, pos.y, width, circleSize_); return Rect(position.x, position.y, width, circleSize_);
} }
/** /**
@ -145,9 +145,9 @@ Rect RadioButton::getBoundingBox() const {
* @param renderer * @param renderer
*/ */
void RadioButton::onDrawWidget(RenderBackend &renderer) { void RadioButton::onDrawWidget(RenderBackend &renderer) {
Vec2 pos = getPosition(); Vec2 position = pos();
float centerX = pos.x + circleSize_ * 0.5f; float centerX = position.x + circleSize_ * 0.5f;
float centerY = pos.y + getSize().height * 0.5f; float centerY = position.y + getSize().height * 0.5f;
float radius = circleSize_ * 0.5f; float radius = circleSize_ * 0.5f;
Color circleColor = selected_ ? selectedColor_ : unselectedColor_; Color circleColor = selected_ ? selectedColor_ : unselectedColor_;
@ -160,7 +160,7 @@ void RadioButton::onDrawWidget(RenderBackend &renderer) {
} }
if (!label_.empty() && font_) { if (!label_.empty() && font_) {
Vec2 textPos(pos.x + circleSize_ + spacing_, pos.y); Vec2 textPos(position.x + circleSize_ + spacing_, position.y);
renderer.drawText(*font_, label_, textPos, textColor_); renderer.drawText(*font_, label_, textPos, textColor_);
} }
} }
@ -186,9 +186,9 @@ bool RadioButton::onMousePress(const MouseEvent &event) {
bool RadioButton::onMouseRelease(const MouseEvent &event) { bool RadioButton::onMouseRelease(const MouseEvent &event) {
if (event.button == MouseButton::Left && pressed_) { if (event.button == MouseButton::Left && pressed_) {
pressed_ = false; pressed_ = false;
Vec2 pos = getPosition(); Vec2 position = pos();
float centerX = pos.x + circleSize_ * 0.5f; float centerX = position.x + circleSize_ * 0.5f;
float centerY = pos.y + getSize().height * 0.5f; float centerY = position.y + getSize().height * 0.5f;
float radius = circleSize_ * 0.5f; float radius = circleSize_ * 0.5f;
float dx = event.x - centerX; float dx = event.x - centerX;

View File

@ -216,7 +216,7 @@ void Slider::setOnDragEnd(Function<void()> callback) {
* @return * @return
*/ */
Rect Slider::getBoundingBox() const { Rect Slider::getBoundingBox() const {
return Rect(getPosition().x, getPosition().y, getSize().width, getSize().height); return Rect(pos().x, pos().y, getSize().width, getSize().height);
} }
/** /**
@ -225,32 +225,32 @@ Rect Slider::getBoundingBox() const {
* @return * @return
*/ */
float Slider::valueToPosition(float value) const { float Slider::valueToPosition(float value) const {
Vec2 pos = getPosition(); Vec2 position = pos();
Size size = getSize(); Size size = getSize();
float percent = (value - min_) / (max_ - min_); float percent = (value - min_) / (max_ - min_);
if (vertical_) { if (vertical_) {
return pos.y + size.height - percent * size.height; return position.y + size.height - percent * size.height;
} else { } else {
return pos.x + percent * size.width; return position.x + percent * size.width;
} }
} }
/** /**
* @brief * @brief
* @param pos * @param position
* @return * @return
*/ */
float Slider::positionToValue(float pos) const { float Slider::positionToValue(float position) const {
Vec2 widgetPos = getPosition(); Vec2 widgetPos = pos();
Size size = getSize(); Size size = getSize();
float percent; float percent;
if (vertical_) { if (vertical_) {
percent = (widgetPos.y + size.height - pos) / size.height; percent = (widgetPos.y + size.height - position) / size.height;
} else { } else {
percent = (pos - widgetPos.x) / size.width; percent = (position - widgetPos.x) / size.width;
} }
percent = std::clamp(percent, 0.0f, 1.0f); percent = std::clamp(percent, 0.0f, 1.0f);
@ -262,14 +262,14 @@ float Slider::positionToValue(float pos) const {
* @return * @return
*/ */
Rect Slider::getThumbRect() const { Rect Slider::getThumbRect() const {
Vec2 pos = getPosition(); Vec2 position = pos();
Size size = getSize(); Size size = getSize();
float thumbPos = valueToPosition(value_); float thumbPos = valueToPosition(value_);
if (vertical_) { if (vertical_) {
return Rect( return Rect(
pos.x + (size.width - thumbSize_) * 0.5f, position.x + (size.width - thumbSize_) * 0.5f,
thumbPos - thumbSize_ * 0.5f, thumbPos - thumbSize_ * 0.5f,
thumbSize_, thumbSize_,
thumbSize_ thumbSize_
@ -277,7 +277,7 @@ Rect Slider::getThumbRect() const {
} else { } else {
return Rect( return Rect(
thumbPos - thumbSize_ * 0.5f, thumbPos - thumbSize_ * 0.5f,
pos.y + (size.height - thumbSize_) * 0.5f, position.y + (size.height - thumbSize_) * 0.5f,
thumbSize_, thumbSize_,
thumbSize_ thumbSize_
); );
@ -289,20 +289,20 @@ Rect Slider::getThumbRect() const {
* @return * @return
*/ */
Rect Slider::getTrackRect() const { Rect Slider::getTrackRect() const {
Vec2 pos = getPosition(); Vec2 position = pos();
Size size = getSize(); Size size = getSize();
if (vertical_) { if (vertical_) {
return Rect( return Rect(
pos.x + (size.width - trackSize_) * 0.5f, position.x + (size.width - trackSize_) * 0.5f,
pos.y, position.y,
trackSize_, trackSize_,
size.height size.height
); );
} else { } else {
return Rect( return Rect(
pos.x, position.x,
pos.y + (size.height - trackSize_) * 0.5f, position.y + (size.height - trackSize_) * 0.5f,
size.width, size.width,
trackSize_ trackSize_
); );
@ -387,12 +387,12 @@ void Slider::onDrawWidget(RenderBackend &renderer) {
if (textEnabled_ && font_) { if (textEnabled_ && font_) {
std::string text = formatText(); std::string text = formatText();
Vec2 textSize = font_->measureText(text); Vec2 textSize = font_->measureText(text);
Vec2 pos = getPosition(); Vec2 position = pos();
Size size = getSize(); Size size = getSize();
Vec2 textPos( Vec2 textPos(
pos.x + size.width + 10.0f, position.x + size.width + 10.0f,
pos.y + (size.height - textSize.y) * 0.5f position.y + (size.height - textSize.y) * 0.5f
); );
renderer.drawText(*font_, text, textPos, textColor_); renderer.drawText(*font_, text, textPos, textColor_);

View File

@ -112,24 +112,24 @@ void Text::updateCache() const {
* @return * @return
*/ */
Vec2 Text::calculateDrawPosition() const { Vec2 Text::calculateDrawPosition() const {
Vec2 pos = getPosition(); Vec2 position = pos();
Vec2 textSize = getTextSize(); Vec2 textSize = getTextSize();
Size widgetSize = getSize(); Size widgetSize = getSize();
Vec2 anchor = getAnchor(); Vec2 anchorPt = anchor();
float refWidth = widgetSize.empty() ? textSize.x : widgetSize.width; float refWidth = widgetSize.empty() ? textSize.x : widgetSize.width;
float refHeight = widgetSize.empty() ? textSize.y : widgetSize.height; float refHeight = widgetSize.empty() ? textSize.y : widgetSize.height;
pos.x -= textSize.x * anchor.x; position.x -= textSize.x * anchorPt.x;
pos.y -= textSize.y * anchor.y; position.y -= textSize.y * anchorPt.y;
if (!widgetSize.empty()) { if (!widgetSize.empty()) {
switch (alignment_) { switch (alignment_) {
case Alignment::Center: case Alignment::Center:
pos.x += (refWidth - textSize.x) * 0.5f; position.x += (refWidth - textSize.x) * 0.5f;
break; break;
case Alignment::Right: case Alignment::Right:
pos.x += refWidth - textSize.x; position.x += refWidth - textSize.x;
break; break;
case Alignment::Left: case Alignment::Left:
default: default:
@ -140,10 +140,10 @@ Vec2 Text::calculateDrawPosition() const {
if (!widgetSize.empty()) { if (!widgetSize.empty()) {
switch (verticalAlignment_) { switch (verticalAlignment_) {
case VerticalAlignment::Middle: case VerticalAlignment::Middle:
pos.y += (refHeight - textSize.y) * 0.5f; position.y += (refHeight - textSize.y) * 0.5f;
break; break;
case VerticalAlignment::Bottom: case VerticalAlignment::Bottom:
pos.y += refHeight - textSize.y; position.y += refHeight - textSize.y;
break; break;
case VerticalAlignment::Top: case VerticalAlignment::Top:
default: default:
@ -151,7 +151,7 @@ Vec2 Text::calculateDrawPosition() const {
} }
} }
return pos; return position;
} }
/** /**

View File

@ -21,14 +21,14 @@ Rect Widget::getBoundingBox() const {
return Rect(); return Rect();
} }
auto pos = convertToWorldSpace(extra2d::Vec2::Zero()); auto position = convertToWorldSpace(extra2d::Vec2::Zero());
auto anchor = getAnchor(); auto anchorPt = anchor();
auto scale = getScale(); auto scaleVal = scale();
float w = size_.width * scale.x; float w = size_.width * scaleVal.x;
float h = size_.height * scale.y; float h = size_.height * scaleVal.y;
float x0 = pos.x - size_.width * anchor.x * scale.x; float x0 = position.x - size_.width * anchorPt.x * scaleVal.x;
float y0 = pos.y - size_.height * anchor.y * scale.y; float y0 = position.y - size_.height * anchorPt.y * scaleVal.y;
float x1 = x0 + w; float x1 = x0 + w;
float y1 = y0 + h; float y1 = y0 + h;