diff --git a/examples/collision_demo/main.cpp b/examples/collision_demo/main.cpp index eaa6d9d..9ce6bba 100644 --- a/examples/collision_demo/main.cpp +++ b/examples/collision_demo/main.cpp @@ -18,17 +18,17 @@ public: Rect getBoundingBox() const override { // 返回实际的矩形边界 - Vec2 pos = getPosition(); - return Rect(pos.x - width_ / 2, pos.y - height_ / 2, width_, height_); + Vec2 position = pos(); + return Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_); } void onRender(RenderBackend &renderer) override { - Vec2 pos = getPosition(); + Vec2 position = pos(); // 绘制填充矩形 Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.8f) : color_; 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); // 绘制边框 @@ -36,7 +36,7 @@ public: : Color(1.0f, 1.0f, 1.0f, 0.5f); float borderWidth = isColliding_ ? 3.0f : 2.0f; 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); } diff --git a/examples/flappy_bird/BaseScene.cpp b/examples/flappy_bird/BaseScene.cpp index 75de238..aab1af3 100644 --- a/examples/flappy_bird/BaseScene.cpp +++ b/examples/flappy_bird/BaseScene.cpp @@ -95,8 +95,8 @@ void BaseScene::renderContent(extra2d::RenderBackend &renderer) { // 检查当前场景是否作为 TransitionScene 的子场景被渲染 bool isChildOfTransition = false; - if (auto parent = getParent()) { - if (dynamic_cast(parent.get())) { + if (auto parentNode = parent()) { + if (dynamic_cast(parentNode.get())) { isChildOfTransition = true; } } diff --git a/examples/flappy_bird/GameScene.cpp b/examples/flappy_bird/GameScene.cpp index 0130f9c..b04d5df 100644 --- a/examples/flappy_bird/GameScene.cpp +++ b/examples/flappy_bird/GameScene.cpp @@ -107,8 +107,8 @@ void GameScene::onUpdate(float dt) { if (pipes_) { Pipe *firstPipe = pipes_->getPipe(0); if (firstPipe && !firstPipe->scored) { - float birdX = bird_->getPosition().x; - float pipeX = firstPipe->getPosition().x; + float birdX = bird_->pos().x; + float pipeX = firstPipe->pos().x; if (pipeX <= birdX) { score_++; scoreNumber_->setNumber(score_); @@ -122,9 +122,9 @@ void GameScene::onUpdate(float dt) { onHit(); } - if (bird_->isLiving() && GAME_HEIGHT - bird_->getPosition().y <= 123.0f) { + if (bird_->isLiving() && GAME_HEIGHT - bird_->pos().y <= 123.0f) { 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); onHit(); } diff --git a/examples/flappy_bird/bird.cpp b/examples/flappy_bird/bird.cpp index 920d619..e690736 100644 --- a/examples/flappy_bird/bird.cpp +++ b/examples/flappy_bird/bird.cpp @@ -108,17 +108,17 @@ void Bird::fall(float dt) { if (!living_) return; // 更新垂直位置 - extra2d::Vec2 pos = getPosition(); - pos.y += speed_ * dt; - setPosition(pos); + extra2d::Vec2 position = pos(); + position.y += speed_ * dt; + setPosition(position); // 应用重力 speed_ += gravity * dt; // 限制顶部边界 - if (pos.y < 0) { - pos.y = 0; - setPosition(pos); + if (position.y < 0) { + position.y = 0; + setPosition(position); speed_ = 0; } @@ -181,12 +181,12 @@ void Bird::setStatus(Status status) { } extra2d::Rect Bird::getBoundingBox() const { - extra2d::Vec2 pos = getPosition(); + extra2d::Vec2 position = pos(); // 小鸟碰撞框大小约为 24x24 float halfSize = 12.0f; return extra2d::Rect( - pos.x - halfSize, - pos.y - halfSize, + position.x - halfSize, + position.y - halfSize, halfSize * 2.0f, halfSize * 2.0f ); diff --git a/examples/flappy_bird/ground.cpp b/examples/flappy_bird/ground.cpp index 1592b68..caa6f8b 100644 --- a/examples/flappy_bird/ground.cpp +++ b/examples/flappy_bird/ground.cpp @@ -47,8 +47,8 @@ void Ground::onUpdate(float dt) { float groundWidth = ground1_->getTextureRect().size.width; // 移动两块地面 - extra2d::Vec2 pos1 = ground1_->getPosition(); - extra2d::Vec2 pos2 = ground2_->getPosition(); + extra2d::Vec2 pos1 = ground1_->pos(); + extra2d::Vec2 pos2 = ground2_->pos(); pos1.x -= speed * dt; pos2.x -= speed * dt; diff --git a/examples/flappy_bird/pipe.cpp b/examples/flappy_bird/pipe.cpp index 1f7432a..a68d7e5 100644 --- a/examples/flappy_bird/pipe.cpp +++ b/examples/flappy_bird/pipe.cpp @@ -57,7 +57,7 @@ Pipe::~Pipe() = default; extra2d::Rect Pipe::getBoundingBox() const { // 返回整个水管的边界框(包含上下两根) - extra2d::Vec2 pos = getPosition(); + extra2d::Vec2 position = pos(); // 水管宽度约为 52 float pipeWidth = 52.0f; @@ -67,7 +67,7 @@ extra2d::Rect Pipe::getBoundingBox() const { float screenHeight = GAME_HEIGHT; return extra2d::Rect( - pos.x - halfWidth, + position.x - halfWidth, 0.0f, pipeWidth, screenHeight @@ -77,16 +77,16 @@ extra2d::Rect Pipe::getBoundingBox() const { extra2d::Rect Pipe::getTopPipeBox() const { if (!topPipe_) return extra2d::Rect(); - extra2d::Vec2 pos = getPosition(); - extra2d::Vec2 topPos = topPipe_->getPosition(); + extra2d::Vec2 position = pos(); + extra2d::Vec2 topPos = topPipe_->pos(); // 上水管尺寸 float pipeWidth = 52.0f; float pipeHeight = 320.0f; return extra2d::Rect( - pos.x - pipeWidth / 2.0f, - pos.y + topPos.y - pipeHeight, + position.x - pipeWidth / 2.0f, + position.y + topPos.y - pipeHeight, pipeWidth, pipeHeight ); @@ -95,16 +95,16 @@ extra2d::Rect Pipe::getTopPipeBox() const { extra2d::Rect Pipe::getBottomPipeBox() const { if (!bottomPipe_) return extra2d::Rect(); - extra2d::Vec2 pos = getPosition(); - extra2d::Vec2 bottomPos = bottomPipe_->getPosition(); + extra2d::Vec2 position = pos(); + extra2d::Vec2 bottomPos = bottomPipe_->pos(); // 下水管尺寸 float pipeWidth = 52.0f; float pipeHeight = 320.0f; return extra2d::Rect( - pos.x - pipeWidth / 2.0f, - pos.y + bottomPos.y, + position.x - pipeWidth / 2.0f, + position.y + bottomPos.y, pipeWidth, pipeHeight ); diff --git a/examples/flappy_bird/pipes.cpp b/examples/flappy_bird/pipes.cpp index 7326175..f8f6aa8 100644 --- a/examples/flappy_bird/pipes.cpp +++ b/examples/flappy_bird/pipes.cpp @@ -40,20 +40,20 @@ void Pipes::onUpdate(float dt) { // 移动所有水管 for (int i = 0; i < pipeCount_; ++i) { if (pipes_[i]) { - extra2d::Vec2 pos = pipes_[i]->getPosition(); + extra2d::Vec2 pos = pipes_[i]->pos(); pos.x -= pipeSpeed * dt; pipes_[i]->setPosition(pos); } } // 检查最前面的水管是否移出屏幕 - if (pipes_[0] && pipes_[0]->getPosition().x <= -30.0f) { + if (pipes_[0] && pipes_[0]->pos().x <= -30.0f) { // 移除第一个水管(通过名称查找并移除) // 由于 removeChild 需要 Ptr,我们使用 removeChildByName 或直接操作 // 这里我们直接移除第一个子节点(假设它是水管) - auto children = getChildren(); - if (!children.empty()) { - removeChild(children[0]); + auto childNodes = children(); + if (!childNodes.empty()) { + removeChild(childNodes[0]); } // 将后面的水管前移 @@ -83,7 +83,7 @@ void Pipes::addPipe() { )); } else { // 其他水管在前一个水管后方 - float prevX = pipes_[pipeCount_ - 1]->getPosition().x; + float prevX = pipes_[pipeCount_ - 1]->pos().x; pipe->setPosition(extra2d::Vec2(prevX + pipeSpacing, 0.0f)); } diff --git a/examples/push_box/BaseScene.cpp b/examples/push_box/BaseScene.cpp index c44163f..6a3a8e4 100644 --- a/examples/push_box/BaseScene.cpp +++ b/examples/push_box/BaseScene.cpp @@ -106,8 +106,8 @@ void BaseScene::renderContent(extra2d::RenderBackend &renderer) { // 检查当前场景是否作为 TransitionScene 的子场景被渲染 bool isChildOfTransition = false; - if (auto parent = getParent()) { - if (dynamic_cast(parent.get())) { + if (auto parentNode = parent()) { + if (dynamic_cast(parentNode.get())) { isChildOfTransition = true; } } diff --git a/examples/push_box/StartScene.cpp b/examples/push_box/StartScene.cpp index 2bf0273..36cf601 100644 --- a/examples/push_box/StartScene.cpp +++ b/examples/push_box/StartScene.cpp @@ -30,7 +30,7 @@ void StartScene::onEnter() { auto &app = extra2d::Application::instance(); auto &resources = app.resources(); - if (getChildren().empty()) { + if (children().empty()) { // 使用游戏逻辑分辨率 float screenW = GAME_WIDTH; float screenH = GAME_HEIGHT; diff --git a/examples/push_box/SuccessScene.cpp b/examples/push_box/SuccessScene.cpp index dd5b2f0..828821d 100644 --- a/examples/push_box/SuccessScene.cpp +++ b/examples/push_box/SuccessScene.cpp @@ -27,7 +27,7 @@ void SuccessScene::onEnter() { auto& app = extra2d::Application::instance(); auto& resources = app.resources(); - if (getChildren().empty()) { + if (children().empty()) { // 使用游戏逻辑分辨率 float screenW = GAME_WIDTH; float screenH = GAME_HEIGHT; diff --git a/examples/push_box/main.cpp b/examples/push_box/main.cpp index 9fa64ac..fbeb7a7 100644 --- a/examples/push_box/main.cpp +++ b/examples/push_box/main.cpp @@ -34,7 +34,12 @@ int main(int argc, char **argv) } // 初始化存储系统 + // 在 Windows 上使用当前工作目录,在 Switch 上使用 sdmc:/ +#ifdef __SWITCH__ pushbox::initStorage("sdmc:/"); +#else + pushbox::initStorage("."); +#endif pushbox::g_CurrentLevel = pushbox::loadCurrentLevel(1); if (pushbox::g_CurrentLevel > MAX_LEVEL) { pushbox::g_CurrentLevel = 1; diff --git a/examples/spatial_index_demo/main.cpp b/examples/spatial_index_demo/main.cpp index 09609b0..5b67af8 100644 --- a/examples/spatial_index_demo/main.cpp +++ b/examples/spatial_index_demo/main.cpp @@ -39,39 +39,39 @@ public: // 必须实现 getBoundingBox() 才能参与空间索引碰撞检测 Rect getBoundingBox() const override { - Vec2 pos = getPosition(); - return Rect(pos.x - size_ / 2, pos.y - size_ / 2, size_, size_); + Vec2 position = pos(); + return Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_); } void update(float dt, float screenWidth, float screenHeight) { - Vec2 pos = getPosition(); - pos = pos + velocity_ * dt; + Vec2 position = pos(); + 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; - 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; - 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 { - Vec2 pos = getPosition(); + Vec2 position = pos(); // 碰撞时变红色 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); // 绘制边框 Color borderColor = isColliding_ ? Color(1.0f, 0.0f, 0.0f, 1.0f) : 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); } @@ -125,7 +125,7 @@ public: auto startTime = std::chrono::high_resolution_clock::now(); // 更新所有物理节点位置 - for (const auto &child : getChildren()) { + for (const auto &child : children()) { if (auto node = dynamic_cast(child.get())) { node->update(dt, screenWidth_, screenHeight_); } @@ -330,7 +330,7 @@ private: */ size_t getPhysicsNodeCount() const { size_t count = 0; - for (const auto &child : getChildren()) { + for (const auto &child : children()) { if (dynamic_cast(child.get())) { ++count; } @@ -343,7 +343,7 @@ private: */ std::vector getPhysicsNodes() const { std::vector nodes; - for (const auto &child : getChildren()) { + for (const auto &child : children()) { if (auto node = dynamic_cast(child.get())) { nodes.push_back(node); } @@ -378,7 +378,7 @@ private: // 从后往前移除指定数量的节点 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(it->get())) { removeChild(*it); break; @@ -413,7 +413,7 @@ private: */ void performCollisionDetection() { // 清除之前的碰撞状态 - for (const auto &child : getChildren()) { + for (const auto &child : children()) { if (auto node = dynamic_cast(child.get())) { node->setColliding(false); } diff --git a/include/animation/sprite_frame.h b/include/animation/sprite_frame.h index c76b9ef..c122156 100644 --- a/include/animation/sprite_frame.h +++ b/include/animation/sprite_frame.h @@ -56,7 +56,7 @@ public: // ------ 名称(用于缓存索引)------ 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; } diff --git a/include/animation/tween.h b/include/animation/tween.h index 4859703..6f042f1 100644 --- a/include/animation/tween.h +++ b/include/animation/tween.h @@ -99,7 +99,7 @@ public: float getTotalDuration() const; void setName(const std::string &name) { name_ = name; } - const std::string &getName() const { return name_; } + const std::string &name() const { return name_; } private: Node *target_ = nullptr; diff --git a/include/audio/sound.h b/include/audio/sound.h index 3d83f14..7493047 100644 --- a/include/audio/sound.h +++ b/include/audio/sound.h @@ -38,7 +38,7 @@ public: void setCursor(float seconds); const std::string& getFilePath() const { return filePath_; } - const std::string& getName() const { return name_; } + const std::string& name() const { return name_; } private: friend class AudioEngine; diff --git a/include/graphics/camera.h b/include/graphics/camera.h index 1743146..eaf859f 100644 --- a/include/graphics/camera.h +++ b/include/graphics/camera.h @@ -22,10 +22,10 @@ public: // ------------------------------------------------------------------------ void setPosition(const Vec2 &position); void setPosition(float x, float y); - Vec2 getPosition() const { return position_; } + Vec2 pos() const { return position_; } void setRotation(float degrees); - float getRotation() const { return rotation_; } + float rot() const { return rotation_; } void setZoom(float zoom); float getZoom() const { return zoom_; } diff --git a/include/platform/window.h b/include/platform/window.h index a1f4cac..1fb3efd 100644 --- a/include/platform/window.h +++ b/include/platform/window.h @@ -76,7 +76,7 @@ public: int getWidth() const { return width_; } int getHeight() const { return height_; } Size getSize() const { return Size(static_cast(width_), static_cast(height_)); } - Vec2 getPosition() const; + Vec2 pos() const; bool isFullscreen() const { return fullscreen_; } bool isVSync() const { return vsync_; } diff --git a/include/scene/node.h b/include/scene/node.h index e4d8304..bacaebe 100644 --- a/include/scene/node.h +++ b/include/scene/node.h @@ -44,39 +44,39 @@ public: void removeFromParent(); void removeAllChildren(); - Ptr getParent() const { return parent_.lock(); } - const std::vector> &getChildren() const { return children_; } - Ptr getChildByName(const std::string &name) const; - Ptr getChildByTag(int tag) const; + Ptr parent() const { return parent_.lock(); } + const std::vector> &children() const { return children_; } + Ptr childByName(const std::string &name) const; + Ptr childByTag(int tag) const; // ------------------------------------------------------------------------ // 变换属性 // ------------------------------------------------------------------------ void setPosition(const Vec2 &pos); void setPosition(float x, float y); - Vec2 getPosition() const { return position_; } + Vec2 pos() const { return position_; } void setRotation(float degrees); - float getRotation() const { return rotation_; } + float rot() const { return rotation_; } void setScale(const Vec2 &scale); void setScale(float scale); void setScale(float x, float y); - Vec2 getScale() const { return scale_; } + Vec2 scale() const { return scale_; } void setAnchor(const Vec2 &anchor); void setAnchor(float x, float y); - Vec2 getAnchor() const { return anchor_; } + Vec2 anchor() const { return anchor_; } void setSkew(const Vec2 &skew); void setSkew(float x, float y); - Vec2 getSkew() const { return skew_; } + Vec2 skew() const { return skew_; } void setOpacity(float opacity); - float getOpacity() const { return opacity_; } + float opacity() const { return opacity_; } void setVisible(bool visible); - bool isVisible() const { return visible_; } + bool visible() const { return visible_; } /** * @brief 设置颜色 @@ -89,16 +89,16 @@ public: * @brief 设置X轴翻转 */ void setFlipX(bool flipX); - bool isFlipX() const { return flipX_; } + bool flipX() const { return flipX_; } /** * @brief 设置Y轴翻转 */ void setFlipY(bool flipY); - bool isFlipY() const { return flipY_; } + bool flipY() const { return flipY_; } 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; } - const std::string &getName() const { return name_; } + const std::string &name() const { return name_; } void setTag(int tag) { tag_ = tag; } - int getTag() const { return tag_; } + int tag() const { return tag_; } // ------------------------------------------------------------------------ // 生命周期回调 diff --git a/include/scene/sprite.h b/include/scene/sprite.h index d706f7e..dc212db 100644 --- a/include/scene/sprite.h +++ b/include/scene/sprite.h @@ -29,8 +29,8 @@ public: // 翻转 void setFlipX(bool flip); void setFlipY(bool flip); - bool isFlipX() const { return flipX_; } - bool isFlipY() const { return flipY_; } + bool flipX() const { return flipX_; } + bool flipY() const { return flipY_; } // 静态创建方法 static Ptr create(); diff --git a/src/animation/tween.cpp b/src/animation/tween.cpp index 2e56e51..85a6150 100644 --- a/src/animation/tween.cpp +++ b/src/animation/tween.cpp @@ -50,10 +50,10 @@ public: started_ = true; if (target_) { - startPos_ = target_->getPosition(); - startScale_ = target_->getScale(); - startRotation_ = target_->getRotation(); - startOpacity_ = target_->getOpacity(); + startPos_ = target_->pos(); + startScale_ = target_->scale(); + startRotation_ = target_->rot(); + startOpacity_ = target_->opacity(); } } diff --git a/src/platform/window.cpp b/src/platform/window.cpp index 96d6b78..23f8e0d 100644 --- a/src/platform/window.cpp +++ b/src/platform/window.cpp @@ -301,7 +301,7 @@ void Window::setResizable(bool resizable) { } } -Vec2 Window::getPosition() const { +Vec2 Window::pos() const { if (sdlWindow_) { int x, y; SDL_GetWindowPosition(sdlWindow_, &x, &y); diff --git a/src/scene/node.cpp b/src/scene/node.cpp index 4d344e2..8cf96f9 100644 --- a/src/scene/node.cpp +++ b/src/scene/node.cpp @@ -25,11 +25,11 @@ void Node::addChild(Ptr child) { childrenOrderDirty_ = true; // 更新索引 - if (!child->getName().empty()) { - nameIndex_[child->getName()] = child; + if (!child->name().empty()) { + nameIndex_[child->name()] = child; } - if (child->getTag() != -1) { - tagIndex_[child->getTag()] = child; + if (child->tag() != -1) { + tagIndex_[child->tag()] = child; } if (running_) { @@ -57,11 +57,11 @@ void Node::addChildren(std::vector> &&children) { children_.push_back(child); // 更新索引 - if (!child->getName().empty()) { - nameIndex_[child->getName()] = child; + if (!child->name().empty()) { + nameIndex_[child->name()] = child; } - if (child->getTag() != -1) { - tagIndex_[child->getTag()] = child; + if (child->tag() != -1) { + tagIndex_[child->tag()] = child; } if (running_) { @@ -91,11 +91,11 @@ void Node::removeChild(Ptr child) { (*it)->onExit(); } // 从索引中移除 - if (!(*it)->getName().empty()) { - nameIndex_.erase((*it)->getName()); + if (!(*it)->name().empty()) { + nameIndex_.erase((*it)->name()); } - if ((*it)->getTag() != -1) { - tagIndex_.erase((*it)->getTag()); + if ((*it)->tag() != -1) { + tagIndex_.erase((*it)->tag()); } (*it)->parent_.reset(); children_.erase(it); @@ -103,7 +103,7 @@ void Node::removeChild(Ptr child) { } void Node::removeChildByName(const std::string &name) { - auto child = getChildByName(name); + auto child = childByName(name); if (child) { removeChild(child); } @@ -138,7 +138,7 @@ void Node::removeAllChildren() { tagIndex_.clear(); } -Ptr Node::getChildByName(const std::string &name) const { +Ptr Node::childByName(const std::string &name) const { // 使用哈希索引,O(1) 查找 auto it = nameIndex_.find(name); if (it != nameIndex_.end()) { @@ -147,7 +147,7 @@ Ptr Node::getChildByName(const std::string &name) const { return nullptr; } -Ptr Node::getChildByTag(int tag) const { +Ptr Node::childByTag(int tag) const { // 使用哈希索引,O(1) 查找 auto it = tagIndex_.find(tag); if (it != tagIndex_.end()) { @@ -424,10 +424,10 @@ void Node::sortChildren() { // 插入排序 for (size_t i = 1; i < n; ++i) { auto key = children_[i]; - int keyZOrder = key->getZOrder(); + int keyZOrder = key->zOrder(); int j = static_cast(i) - 1; - while (j >= 0 && children_[j]->getZOrder() > keyZOrder) { + while (j >= 0 && children_[j]->zOrder() > keyZOrder) { children_[j + 1] = children_[j]; --j; } @@ -437,7 +437,7 @@ void Node::sortChildren() { // 大数组使用标准排序 std::sort(children_.begin(), children_.end(), [](const Ptr &a, const Ptr &b) { - return a->getZOrder() < b->getZOrder(); + return a->zOrder() < b->zOrder(); }); } diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index 5bd58f1..08ae908 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -23,7 +23,7 @@ void Scene::setViewportSize(const Size &size) { } void Scene::renderScene(RenderBackend &renderer) { - if (!isVisible()) + if (!visible()) return; // Begin frame with background color @@ -33,7 +33,7 @@ void Scene::renderScene(RenderBackend &renderer) { } void Scene::renderContent(RenderBackend &renderer) { - if (!isVisible()) + if (!visible()) return; // 在渲染前批量更新所有节点的世界变换 @@ -119,7 +119,7 @@ std::vector> Scene::queryCollisions() const { void Scene::collectRenderCommands(std::vector &commands, int parentZOrder) { - if (!isVisible()) + if (!visible()) return; // 从场景的子节点开始收集渲染命令 diff --git a/src/scene/scene_manager.cpp b/src/scene/scene_manager.cpp index bf768cc..4b338c6 100644 --- a/src/scene/scene_manager.cpp +++ b/src/scene/scene_manager.cpp @@ -17,14 +17,14 @@ namespace extra2d { namespace { Node *hitTestTopmost(const Ptr &node, const Vec2 &worldPos) { - if (!node || !node->isVisible()) { + if (!node || !node->visible()) { return nullptr; } - std::vector> children = node->getChildren(); + std::vector> children = node->children(); std::stable_sort(children.begin(), children.end(), [](const Ptr &a, const Ptr &b) { - return a->getZOrder() < b->getZOrder(); + return a->zOrder() < b->zOrder(); }); for (auto it = children.rbegin(); it != children.rend(); ++it) { @@ -365,7 +365,7 @@ void SceneManager::popToScene(const std::string &name) { while (!sceneStack_.empty()) { auto scene = sceneStack_.top(); - if (scene->getName() == name) { + if (scene->name() == name) { target = scene; break; } @@ -449,7 +449,7 @@ Ptr SceneManager::getSceneByName(const std::string &name) const { auto tempStack = sceneStack_; while (!tempStack.empty()) { auto scene = tempStack.top(); - if (scene->getName() == name) { + if (scene->name() == name) { return scene; } tempStack.pop(); diff --git a/src/scene/shape_node.cpp b/src/scene/shape_node.cpp index 80a1268..09bde4a 100644 --- a/src/scene/shape_node.cpp +++ b/src/scene/shape_node.cpp @@ -129,7 +129,7 @@ Rect ShapeNode::getBoundingBox() const { return Rect(); } - Vec2 offset = getPosition(); + Vec2 offset = pos(); if (shapeType_ == ShapeType::Circle && points_.size() >= 2) { float radius = std::abs(points_[1].x); @@ -171,7 +171,7 @@ void ShapeNode::onDraw(RenderBackend &renderer) { return; } - Vec2 offset = getPosition(); + Vec2 offset = pos(); switch (shapeType_) { case ShapeType::Point: @@ -254,7 +254,7 @@ void ShapeNode::generateRenderCommand(std::vector &commands, return; } - Vec2 offset = getPosition(); + Vec2 offset = pos(); RenderCommand cmd; cmd.layer = zOrder; diff --git a/src/scene/sprite.cpp b/src/scene/sprite.cpp index e2890f3..1baf85b 100644 --- a/src/scene/sprite.cpp +++ b/src/scene/sprite.cpp @@ -51,14 +51,14 @@ Rect Sprite::getBoundingBox() const { float width = textureRect_.width(); float height = textureRect_.height(); - auto pos = getPosition(); - auto anchor = getAnchor(); - auto scale = getScale(); + auto position = pos(); + auto anchorPt = anchor(); + auto scaleVal = scale(); - float w = width * scale.x; - float h = height * scale.y; - float x0 = pos.x - width * anchor.x * scale.x; - float y0 = pos.y - height * anchor.y * scale.y; + float w = width * scaleVal.x; + float h = height * scaleVal.y; + float x0 = position.x - width * anchorPt.x * scaleVal.x; + float y0 = position.y - height * anchorPt.y * scaleVal.y; float x1 = x0 + w; float y1 = y0 + h; @@ -89,7 +89,7 @@ void Sprite::onDraw(RenderBackend &renderer) { float worldScaleY = glm::length(glm::vec2(worldTransform[1][0], worldTransform[1][1])); - auto anchor = getAnchor(); + auto anchorPt = anchor(); // 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸 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]); renderer.drawSprite(*texture_, destRect, srcRect, color_, worldRotation, - anchor); + anchorPt); } void Sprite::generateRenderCommand(std::vector &commands, @@ -135,7 +135,7 @@ void Sprite::generateRenderCommand(std::vector &commands, float worldScaleY = glm::length(glm::vec2(worldTransform[1][0], worldTransform[1][1])); - auto anchor = getAnchor(); + auto anchorPt = anchor(); // 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸 Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY); @@ -159,7 +159,7 @@ void Sprite::generateRenderCommand(std::vector &commands, cmd.type = RenderCommandType::Sprite; cmd.layer = zOrder; cmd.data = SpriteCommandData{texture_.get(), destRect, srcRect, color_, - worldRotation, anchor, 0}; + worldRotation, anchorPt, 0}; commands.push_back(std::move(cmd)); } diff --git a/src/scene/transition_flip_scene.cpp b/src/scene/transition_flip_scene.cpp index c600265..4b28f93 100644 --- a/src/scene/transition_flip_scene.cpp +++ b/src/scene/transition_flip_scene.cpp @@ -36,7 +36,7 @@ void TransitionFlipScene::renderContent(RenderBackend &renderer) { float currentAngle = angle; Camera *camera = outScene_->getActiveCamera(); - float originalRotation = camera ? camera->getRotation() : 0.0f; + float originalRotation = camera ? camera->rot() : 0.0f; if (axis_ == Axis::Horizontal) { // 水平轴翻转 - 模拟绕X轴旋转 @@ -62,7 +62,7 @@ void TransitionFlipScene::renderContent(RenderBackend &renderer) { float currentAngle = angle - PI_F; Camera *camera = inScene_->getActiveCamera(); - float originalRotation = camera ? camera->getRotation() : 0.0f; + float originalRotation = camera ? camera->rot() : 0.0f; if (axis_ == Axis::Horizontal) { if (camera) { diff --git a/src/scene/transition_slide_scene.cpp b/src/scene/transition_slide_scene.cpp index e5f9de8..1f59582 100644 --- a/src/scene/transition_slide_scene.cpp +++ b/src/scene/transition_slide_scene.cpp @@ -65,7 +65,7 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) { } Camera *camera = outScene_->getActiveCamera(); - Vec2 originalPos = camera ? camera->getPosition() : Vec2::Zero(); + Vec2 originalPos = camera ? camera->pos() : Vec2::Zero(); if (camera) { camera->setPosition(originalPos.x + offsetX, originalPos.y + offsetY); @@ -99,7 +99,7 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) { } Camera *camera = inScene_->getActiveCamera(); - Vec2 originalPos = camera ? camera->getPosition() : Vec2::Zero(); + Vec2 originalPos = camera ? camera->pos() : Vec2::Zero(); if (camera) { camera->setPosition(originalPos.x + offsetX, originalPos.y + offsetY); diff --git a/src/ui/button.cpp b/src/ui/button.cpp index 7cf2e4a..f6a107a 100644 --- a/src/ui/button.cpp +++ b/src/ui/button.cpp @@ -357,19 +357,19 @@ void Button::setCustomSize(float width, float height) { * @return 边界矩形 */ Rect Button::getBoundingBox() const { - auto pos = convertToWorldSpace(extra2d::Vec2::Zero()); - auto anchor = getAnchor(); - auto scale = getScale(); + auto position = convertToWorldSpace(extra2d::Vec2::Zero()); + auto anchorPt = anchor(); + auto scaleVal = scale(); auto size = getSize(); if (size.empty()) { return Rect(); } - float w = size.width * scale.x; - float h = size.height * scale.y; - float x0 = pos.x - size.width * anchor.x * scale.x; - float y0 = pos.y - size.height * anchor.y * scale.y; + 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; return Rect(x0, y0, w, h); } diff --git a/src/ui/check_box.cpp b/src/ui/check_box.cpp index be4e401..b60c166 100644 --- a/src/ui/check_box.cpp +++ b/src/ui/check_box.cpp @@ -128,7 +128,7 @@ void CheckBox::setOnStateChange(Function callback) { * @return 边界矩形 */ Rect CheckBox::getBoundingBox() const { - Vec2 pos = getPosition(); + Vec2 position = pos(); float width = boxSize_; if (!label_.empty() && font_) { @@ -136,7 +136,7 @@ Rect CheckBox::getBoundingBox() const { 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 渲染后端 */ 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_; renderer.fillRect(boxRect, boxColor); renderer.drawRect(boxRect, Colors::White, 1.0f); @@ -165,7 +165,7 @@ void CheckBox::onDrawWidget(RenderBackend &renderer) { } 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_); } } @@ -191,8 +191,8 @@ bool CheckBox::onMousePress(const MouseEvent &event) { bool CheckBox::onMouseRelease(const MouseEvent &event) { if (event.button == MouseButton::Left && pressed_) { pressed_ = false; - Vec2 pos = getPosition(); - Rect boxRect(pos.x, pos.y, boxSize_, boxSize_); + Vec2 position = pos(); + Rect boxRect(position.x, position.y, boxSize_, boxSize_); if (boxRect.containsPoint(Point(event.x, event.y))) { toggle(); } diff --git a/src/ui/label.cpp b/src/ui/label.cpp index 00e82e1..74ad521 100644 --- a/src/ui/label.cpp +++ b/src/ui/label.cpp @@ -305,7 +305,7 @@ std::vector Label::splitLines() const { * @return 绘制位置坐标 */ Vec2 Label::calculateDrawPosition() const { - Vec2 pos = getPosition(); + Vec2 position = pos(); Vec2 size = getTextSize(); Size widgetSize = getSize(); @@ -314,10 +314,10 @@ Vec2 Label::calculateDrawPosition() const { switch (hAlign_) { case HorizontalAlign::Center: - pos.x += (refWidth - size.x) * 0.5f; + position.x += (refWidth - size.x) * 0.5f; break; case HorizontalAlign::Right: - pos.x += refWidth - size.x; + position.x += refWidth - size.x; break; case HorizontalAlign::Left: default: @@ -326,17 +326,17 @@ Vec2 Label::calculateDrawPosition() const { switch (vAlign_) { case VerticalAlign::Middle: - pos.y += (refHeight - size.y) * 0.5f; + position.y += (refHeight - size.y) * 0.5f; break; case VerticalAlign::Bottom: - pos.y += refHeight - size.y; + position.y += refHeight - size.y; break; case VerticalAlign::Top: default: break; } - return pos; + return position; } /** @@ -353,11 +353,11 @@ void Label::drawText(RenderBackend &renderer, const Vec2 &position, const Color if (multiLine_) { auto lines = splitLines(); float lineHeight = getLineHeight(); - Vec2 pos = position; - + Vec2 drawPos = position; + for (const auto &line : lines) { - renderer.drawText(*font_, line, pos, color); - pos.y += lineHeight; + renderer.drawText(*font_, line, drawPos, color); + drawPos.y += lineHeight; } } else { renderer.drawText(*font_, text_, position, color); @@ -379,8 +379,8 @@ Rect Label::getBoundingBox() const { return Rect(); } - Vec2 pos = calculateDrawPosition(); - return Rect(pos.x, pos.y, size.x, size.y); + Vec2 drawPos = calculateDrawPosition(); + return Rect(drawPos.x, drawPos.y, size.x, size.y); } /** diff --git a/src/ui/progress_bar.cpp b/src/ui/progress_bar.cpp index d7a87b4..96b031e 100644 --- a/src/ui/progress_bar.cpp +++ b/src/ui/progress_bar.cpp @@ -331,7 +331,7 @@ std::string ProgressBar::formatText() const { * @return 边界矩形 */ 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 渲染后端 */ void ProgressBar::onDrawWidget(RenderBackend &renderer) { - Vec2 pos = getPosition(); + Vec2 position = pos(); Size size = getSize(); - float bgX = pos.x + padding_; - float bgY = pos.y + padding_; + float bgX = position.x + padding_; + float bgY = position.y + padding_; float bgW = size.width - padding_ * 2; float bgH = size.height - padding_ * 2; Rect bgRect(bgX, bgY, bgW, bgH); @@ -470,8 +470,8 @@ void ProgressBar::onDrawWidget(RenderBackend &renderer) { Vec2 textSize = font_->measureText(text); Vec2 textPos( - pos.x + (size.width - textSize.x) * 0.5f, - pos.y + (size.height - textSize.y) * 0.5f + position.x + (size.width - textSize.x) * 0.5f, + position.y + (size.height - textSize.y) * 0.5f ); renderer.drawText(*font_, text, textPos, textColor_); diff --git a/src/ui/radio_button.cpp b/src/ui/radio_button.cpp index 33cf53e..23c5f83 100644 --- a/src/ui/radio_button.cpp +++ b/src/ui/radio_button.cpp @@ -129,7 +129,7 @@ void RadioButton::setOnStateChange(Function callback) { * @return 边界矩形 */ Rect RadioButton::getBoundingBox() const { - Vec2 pos = getPosition(); + Vec2 position = pos(); float width = circleSize_; if (!label_.empty() && font_) { @@ -137,7 +137,7 @@ Rect RadioButton::getBoundingBox() const { 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 渲染后端 */ void RadioButton::onDrawWidget(RenderBackend &renderer) { - Vec2 pos = getPosition(); - float centerX = pos.x + circleSize_ * 0.5f; - float centerY = pos.y + getSize().height * 0.5f; + Vec2 position = pos(); + float centerX = position.x + circleSize_ * 0.5f; + float centerY = position.y + getSize().height * 0.5f; float radius = circleSize_ * 0.5f; Color circleColor = selected_ ? selectedColor_ : unselectedColor_; @@ -160,7 +160,7 @@ void RadioButton::onDrawWidget(RenderBackend &renderer) { } 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_); } } @@ -186,9 +186,9 @@ bool RadioButton::onMousePress(const MouseEvent &event) { bool RadioButton::onMouseRelease(const MouseEvent &event) { if (event.button == MouseButton::Left && pressed_) { pressed_ = false; - Vec2 pos = getPosition(); - float centerX = pos.x + circleSize_ * 0.5f; - float centerY = pos.y + getSize().height * 0.5f; + Vec2 position = pos(); + float centerX = position.x + circleSize_ * 0.5f; + float centerY = position.y + getSize().height * 0.5f; float radius = circleSize_ * 0.5f; float dx = event.x - centerX; diff --git a/src/ui/slider.cpp b/src/ui/slider.cpp index 5d081b5..a7833b2 100644 --- a/src/ui/slider.cpp +++ b/src/ui/slider.cpp @@ -216,7 +216,7 @@ void Slider::setOnDragEnd(Function callback) { * @return 边界矩形 */ 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,34 +225,34 @@ Rect Slider::getBoundingBox() const { * @return 位置坐标 */ float Slider::valueToPosition(float value) const { - Vec2 pos = getPosition(); + Vec2 position = pos(); Size size = getSize(); float percent = (value - min_) / (max_ - min_); if (vertical_) { - return pos.y + size.height - percent * size.height; + return position.y + size.height - percent * size.height; } else { - return pos.x + percent * size.width; + return position.x + percent * size.width; } } /** * @brief 将位置转换为值 - * @param pos 位置坐标 + * @param position 位置坐标 * @return 数值 */ -float Slider::positionToValue(float pos) const { - Vec2 widgetPos = getPosition(); +float Slider::positionToValue(float position) const { + Vec2 widgetPos = pos(); Size size = getSize(); - + float percent; if (vertical_) { - percent = (widgetPos.y + size.height - pos) / size.height; + percent = (widgetPos.y + size.height - position) / size.height; } else { - percent = (pos - widgetPos.x) / size.width; + percent = (position - widgetPos.x) / size.width; } - + percent = std::clamp(percent, 0.0f, 1.0f); return min_ + percent * (max_ - min_); } @@ -262,14 +262,14 @@ float Slider::positionToValue(float pos) const { * @return 滑块矩形 */ Rect Slider::getThumbRect() const { - Vec2 pos = getPosition(); + Vec2 position = pos(); Size size = getSize(); float thumbPos = valueToPosition(value_); if (vertical_) { return Rect( - pos.x + (size.width - thumbSize_) * 0.5f, + position.x + (size.width - thumbSize_) * 0.5f, thumbPos - thumbSize_ * 0.5f, thumbSize_, thumbSize_ @@ -277,7 +277,7 @@ Rect Slider::getThumbRect() const { } else { return Rect( thumbPos - thumbSize_ * 0.5f, - pos.y + (size.height - thumbSize_) * 0.5f, + position.y + (size.height - thumbSize_) * 0.5f, thumbSize_, thumbSize_ ); @@ -289,20 +289,20 @@ Rect Slider::getThumbRect() const { * @return 轨道矩形 */ Rect Slider::getTrackRect() const { - Vec2 pos = getPosition(); + Vec2 position = pos(); Size size = getSize(); if (vertical_) { return Rect( - pos.x + (size.width - trackSize_) * 0.5f, - pos.y, + position.x + (size.width - trackSize_) * 0.5f, + position.y, trackSize_, size.height ); } else { return Rect( - pos.x, - pos.y + (size.height - trackSize_) * 0.5f, + position.x, + position.y + (size.height - trackSize_) * 0.5f, size.width, trackSize_ ); @@ -387,12 +387,12 @@ void Slider::onDrawWidget(RenderBackend &renderer) { if (textEnabled_ && font_) { std::string text = formatText(); Vec2 textSize = font_->measureText(text); - Vec2 pos = getPosition(); + Vec2 position = pos(); Size size = getSize(); Vec2 textPos( - pos.x + size.width + 10.0f, - pos.y + (size.height - textSize.y) * 0.5f + position.x + size.width + 10.0f, + position.y + (size.height - textSize.y) * 0.5f ); renderer.drawText(*font_, text, textPos, textColor_); diff --git a/src/ui/text.cpp b/src/ui/text.cpp index ebbc0c7..99b4d38 100644 --- a/src/ui/text.cpp +++ b/src/ui/text.cpp @@ -112,24 +112,24 @@ void Text::updateCache() const { * @return 绘制位置坐标 */ Vec2 Text::calculateDrawPosition() const { - Vec2 pos = getPosition(); + Vec2 position = pos(); Vec2 textSize = getTextSize(); Size widgetSize = getSize(); - Vec2 anchor = getAnchor(); + Vec2 anchorPt = anchor(); float refWidth = widgetSize.empty() ? textSize.x : widgetSize.width; float refHeight = widgetSize.empty() ? textSize.y : widgetSize.height; - pos.x -= textSize.x * anchor.x; - pos.y -= textSize.y * anchor.y; + position.x -= textSize.x * anchorPt.x; + position.y -= textSize.y * anchorPt.y; if (!widgetSize.empty()) { switch (alignment_) { case Alignment::Center: - pos.x += (refWidth - textSize.x) * 0.5f; + position.x += (refWidth - textSize.x) * 0.5f; break; case Alignment::Right: - pos.x += refWidth - textSize.x; + position.x += refWidth - textSize.x; break; case Alignment::Left: default: @@ -140,10 +140,10 @@ Vec2 Text::calculateDrawPosition() const { if (!widgetSize.empty()) { switch (verticalAlignment_) { case VerticalAlignment::Middle: - pos.y += (refHeight - textSize.y) * 0.5f; + position.y += (refHeight - textSize.y) * 0.5f; break; case VerticalAlignment::Bottom: - pos.y += refHeight - textSize.y; + position.y += refHeight - textSize.y; break; case VerticalAlignment::Top: default: @@ -151,7 +151,7 @@ Vec2 Text::calculateDrawPosition() const { } } - return pos; + return position; } /** diff --git a/src/ui/widget.cpp b/src/ui/widget.cpp index 5dcd185..2a37cbd 100644 --- a/src/ui/widget.cpp +++ b/src/ui/widget.cpp @@ -21,14 +21,14 @@ Rect Widget::getBoundingBox() const { return Rect(); } - auto pos = convertToWorldSpace(extra2d::Vec2::Zero()); - auto anchor = getAnchor(); - auto scale = getScale(); + auto position = convertToWorldSpace(extra2d::Vec2::Zero()); + auto anchorPt = anchor(); + auto scaleVal = scale(); - float w = size_.width * scale.x; - float h = size_.height * scale.y; - float x0 = pos.x - size_.width * anchor.x * scale.x; - float y0 = pos.y - size_.height * anchor.y * scale.y; + 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 x1 = x0 + w; float y1 = y0 + h;