refactor(ui): 移除坐标空间系统并简化文本组件位置处理
移除 CoordinateSpace 枚举及相关功能,改为使用统一的位置处理方式 更新示例代码以使用新的位置设置方法
This commit is contained in:
parent
a6c1f66fff
commit
fa9ee0e2a7
|
|
@ -30,33 +30,29 @@ public:
|
|||
return;
|
||||
}
|
||||
|
||||
// 创建 "你好世界" 文本组件 - 使用屏幕空间(固定位置,不随相机移动)
|
||||
// 创建 "你好世界" 文本组件 - 使用节点位置
|
||||
auto text1 = Text::create("你好世界", font_);
|
||||
text1->setCoordinateSpace(CoordinateSpace::Screen);
|
||||
text1->setScreenPosition(640.0f, 360.0f); // 屏幕中心
|
||||
text1->setAnchor(0.5f, 0.5f); // 中心锚点,让文字中心对准位置
|
||||
text1->setPosition(640.0f, 360.0f); // 屏幕中心
|
||||
text1->setAnchor(0.5f, 0.5f); // 中心锚点,让文字中心对准位置
|
||||
text1->setTextColor(Color(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
addChild(text1);
|
||||
|
||||
// 创建提示文本组件 - 使用屏幕空间,固定在屏幕底部
|
||||
// 创建提示文本组件 - 固定在屏幕底部
|
||||
auto text2 = Text::create("退出按键(START 按钮)", font_);
|
||||
text2->setCoordinateSpace(CoordinateSpace::Screen);
|
||||
text2->setScreenPosition(640.0f, 650.0f); // 屏幕底部
|
||||
text2->setPosition(640.0f, 650.0f); // 屏幕底部
|
||||
text2->setAnchor(0.5f, 0.5f);
|
||||
text2->setTextColor(Color(1.0f, 1.0f, 0.0f, 1.0f));
|
||||
addChild(text2);
|
||||
|
||||
// 创建相机空间文本 - 跟随相机但保持相对偏移
|
||||
auto text3 = Text::create("相机空间文本", font_);
|
||||
text3->setCoordinateSpace(CoordinateSpace::Camera);
|
||||
text3->setCameraOffset(50.0f, 50.0f); // 相机左上角偏移(屏幕坐标系Y向下)
|
||||
text3->setAnchor(0.0f, 0.0f); // 左上角锚点,文字从指定位置开始显示
|
||||
// 创建带偏移的文本 - 使用相对位置
|
||||
auto text3 = Text::create("偏移文本", font_);
|
||||
text3->setPosition(50.0f, 50.0f); // 左上角偏移
|
||||
text3->setAnchor(0.0f, 0.0f); // 左上角锚点,文字从指定位置开始显示
|
||||
text3->setTextColor(Color(0.0f, 1.0f, 1.0f, 1.0f));
|
||||
addChild(text3);
|
||||
|
||||
// 创建世界空间文本 - 随相机移动(默认行为)
|
||||
auto text4 = Text::create("世界空间文本", font_);
|
||||
text4->setCoordinateSpace(CoordinateSpace::World);
|
||||
text4->setPosition(100.0f, 100.0f); // 世界坐标
|
||||
text4->setAnchor(0.0f, 0.0f); // 左上角锚点,文字从指定位置开始显示
|
||||
text4->setTextColor(Color(1.0f, 0.5f, 0.5f, 1.0f));
|
||||
|
|
|
|||
|
|
@ -17,15 +17,6 @@ struct MouseEvent {
|
|||
int mods; // 修饰键状态
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// 坐标空间枚举 - 定义 UI 组件的渲染坐标空间
|
||||
// ============================================================================
|
||||
enum class CoordinateSpace {
|
||||
Screen, // 屏幕空间 - 固定位置,不随相机移动
|
||||
World, // 世界空间 - 随相机移动(默认行为)
|
||||
Camera, // 相机空间 - 相对于相机位置的偏移
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Widget 基类 - UI 组件的基础
|
||||
// ============================================================================
|
||||
|
|
@ -40,26 +31,6 @@ public:
|
|||
|
||||
Rect getBoundingBox() const override;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 坐标空间设置
|
||||
// ------------------------------------------------------------------------
|
||||
void setCoordinateSpace(CoordinateSpace space);
|
||||
CoordinateSpace getCoordinateSpace() const { return coordinateSpace_; }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 屏幕空间位置设置(仅在 Screen 空间下有效)
|
||||
// ------------------------------------------------------------------------
|
||||
void setScreenPosition(const Vec2 &pos);
|
||||
void setScreenPosition(float x, float y);
|
||||
Vec2 getScreenPosition() const { return screenPosition_; }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 相机空间偏移设置(仅在 Camera 空间下有效)
|
||||
// ------------------------------------------------------------------------
|
||||
void setCameraOffset(const Vec2 &offset);
|
||||
void setCameraOffset(float x, float y);
|
||||
Vec2 getCameraOffset() const { return cameraOffset_; }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 鼠标事件处理(子类可重写)
|
||||
// ------------------------------------------------------------------------
|
||||
|
|
@ -87,13 +58,10 @@ protected:
|
|||
return getBoundingBox().containsPoint(Point(x, y));
|
||||
}
|
||||
|
||||
// 获取实际渲染位置(根据坐标空间计算)
|
||||
Vec2 getRenderPosition() const;
|
||||
|
||||
// 子类重写此方法以支持自定义渲染
|
||||
virtual void onDrawWidget(RenderBackend &renderer) {}
|
||||
|
||||
// 重写 Node 的 onDraw 以处理坐标空间
|
||||
// 重写 Node 的 onDraw
|
||||
void onDraw(RenderBackend &renderer) override;
|
||||
|
||||
private:
|
||||
|
|
@ -101,11 +69,6 @@ private:
|
|||
bool enabled_ = true;
|
||||
bool focused_ = false;
|
||||
bool hovered_ = false;
|
||||
|
||||
// 坐标空间相关
|
||||
CoordinateSpace coordinateSpace_ = CoordinateSpace::World;
|
||||
Vec2 screenPosition_ = Vec2::Zero(); // 屏幕空间位置
|
||||
Vec2 cameraOffset_ = Vec2::Zero(); // 相机空间偏移
|
||||
};
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
#include <cmath>
|
||||
#include <graphics/camera.h>
|
||||
#include <scene/scene.h>
|
||||
#include <ui/widget.h>
|
||||
|
||||
namespace extra2d {
|
||||
|
|
@ -23,7 +21,7 @@ Rect Widget::getBoundingBox() const {
|
|||
return Rect();
|
||||
}
|
||||
|
||||
auto pos = getRenderPosition();
|
||||
auto pos = convertToWorldSpace(extra2d::Vec2::Zero());
|
||||
auto anchor = getAnchor();
|
||||
auto scale = getScale();
|
||||
|
||||
|
|
@ -40,102 +38,10 @@ Rect Widget::getBoundingBox() const {
|
|||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 坐标空间设置
|
||||
// ------------------------------------------------------------------------
|
||||
void Widget::setCoordinateSpace(CoordinateSpace space) {
|
||||
coordinateSpace_ = space;
|
||||
}
|
||||
|
||||
void Widget::setScreenPosition(const Vec2 &pos) {
|
||||
screenPosition_ = pos;
|
||||
if (coordinateSpace_ == CoordinateSpace::Screen) {
|
||||
updateSpatialIndex();
|
||||
}
|
||||
}
|
||||
|
||||
void Widget::setScreenPosition(float x, float y) {
|
||||
setScreenPosition(Vec2(x, y));
|
||||
}
|
||||
|
||||
void Widget::setCameraOffset(const Vec2 &offset) {
|
||||
cameraOffset_ = offset;
|
||||
if (coordinateSpace_ == CoordinateSpace::Camera) {
|
||||
updateSpatialIndex();
|
||||
}
|
||||
}
|
||||
|
||||
void Widget::setCameraOffset(float x, float y) {
|
||||
setCameraOffset(Vec2(x, y));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 获取实际渲染位置(根据坐标空间计算)
|
||||
// ------------------------------------------------------------------------
|
||||
Vec2 Widget::getRenderPosition() const {
|
||||
switch (coordinateSpace_) {
|
||||
case CoordinateSpace::Screen:
|
||||
// 屏幕空间:直接使用屏幕位置
|
||||
return screenPosition_;
|
||||
|
||||
case CoordinateSpace::Camera: {
|
||||
// 相机空间:相机位置 + 偏移
|
||||
Scene *scene = getScene();
|
||||
if (scene) {
|
||||
Camera *camera = scene->getActiveCamera();
|
||||
if (camera) {
|
||||
return camera->getPosition() + cameraOffset_;
|
||||
}
|
||||
}
|
||||
// 如果没有场景或相机,使用偏移作为绝对位置
|
||||
return cameraOffset_;
|
||||
}
|
||||
|
||||
case CoordinateSpace::World:
|
||||
default:
|
||||
// 世界空间:使用节点的世界位置
|
||||
return convertToWorldSpace(extra2d::Vec2::Zero());
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 重写 onDraw 以处理坐标空间
|
||||
// 重写 onDraw
|
||||
// ------------------------------------------------------------------------
|
||||
void Widget::onDraw(RenderBackend &renderer) {
|
||||
// 根据坐标空间调整渲染
|
||||
if (coordinateSpace_ == CoordinateSpace::Screen) {
|
||||
// 屏幕空间:临时修改位置为屏幕位置,保持锚点不变
|
||||
Vec2 worldPos = getPosition();
|
||||
const_cast<Widget*>(this)->setPosition(screenPosition_);
|
||||
|
||||
// 调用子类的绘制
|
||||
onDrawWidget(renderer);
|
||||
|
||||
// 恢复原始位置
|
||||
const_cast<Widget*>(this)->setPosition(worldPos);
|
||||
} else if (coordinateSpace_ == CoordinateSpace::Camera) {
|
||||
// 相机空间:计算相对于相机的位置
|
||||
Scene *scene = getScene();
|
||||
if (scene) {
|
||||
Camera *camera = scene->getActiveCamera();
|
||||
if (camera) {
|
||||
Vec2 worldPos = getPosition();
|
||||
Vec2 cameraRelativePos = camera->getPosition() + cameraOffset_;
|
||||
const_cast<Widget*>(this)->setPosition(cameraRelativePos);
|
||||
|
||||
// 调用子类的绘制
|
||||
onDrawWidget(renderer);
|
||||
|
||||
// 恢复原始位置
|
||||
const_cast<Widget*>(this)->setPosition(worldPos);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 如果没有场景或相机,按世界空间处理
|
||||
onDrawWidget(renderer);
|
||||
} else {
|
||||
// 世界空间:正常渲染
|
||||
onDrawWidget(renderer);
|
||||
}
|
||||
onDrawWidget(renderer);
|
||||
}
|
||||
|
||||
} // namespace extra2d
|
||||
|
|
|
|||
Loading…
Reference in New Issue