diff --git a/Extra2D/include/extra2d/extra2d.h b/Extra2D/include/extra2d/extra2d.h index d9284f5..32210b1 100644 --- a/Extra2D/include/extra2d/extra2d.h +++ b/Extra2D/include/extra2d/extra2d.h @@ -36,16 +36,6 @@ #include #include -// UI -#include -#include -#include -#include -#include -#include -#include -#include - // Event #include #include @@ -65,12 +55,6 @@ #include #include -// Spatial -#include -#include -#include -#include - // Application #include diff --git a/Extra2D/include/extra2d/scene/node.h b/Extra2D/include/extra2d/scene/node.h index 6d448bd..71317d4 100644 --- a/Extra2D/include/extra2d/scene/node.h +++ b/Extra2D/include/extra2d/scene/node.h @@ -143,17 +143,10 @@ public: virtual void onDetachFromScene(); // ------------------------------------------------------------------------ - // 边界框(用于空间索引) + // 边界框 // ------------------------------------------------------------------------ virtual Rect getBoundingBox() const; - // 是否需要参与空间索引(默认 true) - void setSpatialIndexed(bool indexed) { spatialIndexed_ = indexed; } - bool isSpatialIndexed() const { return spatialIndexed_; } - - // 更新空间索引(手动调用,通常在边界框变化后) - void updateSpatialIndex(); - // ------------------------------------------------------------------------ // 事件系统 // ------------------------------------------------------------------------ @@ -218,10 +211,7 @@ private: Vec2 anchor_ = Vec2(0.5f, 0.5f); // 8 bytes Vec2 skew_ = Vec2::Zero(); // 8 bytes - // 8. 边界框(用于空间索引) - Rect lastSpatialBounds_; // 16 bytes - - // 9. 浮点属性 + // 8. 浮点属性 float rotation_ = 0.0f; // 4 bytes float opacity_ = 1.0f; // 4 bytes @@ -236,17 +226,15 @@ private: bool flipX_ = false; // 1 byte bool flipY_ = false; // 1 byte - // 11. 场景指针 + // 13. 场景指针 Scene *scene_ = nullptr; // 8 bytes - // 12. 布尔标志(打包在一起) + // 14. 布尔标志(打包在一起) mutable bool transformDirty_ = true; // 1 byte mutable bool worldTransformDirty_ = true; // 1 byte bool childrenOrderDirty_ = false; // 1 byte bool visible_ = true; // 1 byte bool running_ = false; // 1 byte - bool spatialIndexed_ = true; // 1 byte - // 填充 2 bytes 到 8 字节对齐 }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/scene/scene.h b/Extra2D/include/extra2d/scene/scene.h index 9d0a3d6..89b8b65 100644 --- a/Extra2D/include/extra2d/scene/scene.h +++ b/Extra2D/include/extra2d/scene/scene.h @@ -3,7 +3,6 @@ #include #include #include -#include #include namespace extra2d { @@ -61,28 +60,6 @@ public: void collectRenderCommands(std::vector &commands, int parentZOrder = 0) override; - // ------------------------------------------------------------------------ - // 空间索引系统 - // ------------------------------------------------------------------------ - SpatialManager &getSpatialManager() { return spatialManager_; } - const SpatialManager &getSpatialManager() const { return spatialManager_; } - - // 启用/禁用空间索引 - void setSpatialIndexingEnabled(bool enabled) { - spatialIndexingEnabled_ = enabled; - } - bool isSpatialIndexingEnabled() const { return spatialIndexingEnabled_; } - - // 节点空间索引管理(内部使用) - void updateNodeInSpatialIndex(Node *node, const Rect &oldBounds, - const Rect &newBounds); - void removeNodeFromSpatialIndex(Node *node); - - // 碰撞检测查询 - std::vector queryNodesInArea(const Rect &area) const; - std::vector queryNodesAtPoint(const Vec2 &point) const; - std::vector> queryCollisions() const; - // ------------------------------------------------------------------------ // 静态创建方法 // ------------------------------------------------------------------------ @@ -107,10 +84,6 @@ private: Ptr defaultCamera_; bool paused_ = false; - - // 空间索引系统 - SpatialManager spatialManager_; - bool spatialIndexingEnabled_ = true; }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/spatial/quadtree.h b/Extra2D/include/extra2d/spatial/quadtree.h deleted file mode 100644 index d48337e..0000000 --- a/Extra2D/include/extra2d/spatial/quadtree.h +++ /dev/null @@ -1,70 +0,0 @@ -#pragma once - -#include -#include - -namespace extra2d { - -class QuadTree : public ISpatialIndex { -public: - static constexpr int MAX_OBJECTS = 10; - static constexpr int MAX_LEVELS = 5; - - struct QuadTreeNode { - Rect bounds; - int level; - std::vector> objects; - std::array, 4> children; - - QuadTreeNode(const Rect &bounds, int level); - bool contains(const Rect &rect) const; - bool intersects(const Rect &rect) const; - }; - - explicit QuadTree(const Rect &worldBounds); - ~QuadTree() override = default; - - void insert(Node *node, const Rect &bounds) override; - void remove(Node *node) override; - void update(Node *node, const Rect &newBounds) override; - - std::vector query(const Rect &area) const override; - std::vector query(const Vec2 &point) const override; - std::vector> queryCollisions() const override; - - void clear() override; - size_t size() const override; - bool empty() const override; - - void rebuild() override; - -private: - void split(QuadTreeNode *node); - void insertIntoNode(QuadTreeNode *node, Node *object, const Rect &bounds); - void queryNode(const QuadTreeNode *node, const Rect &area, - std::vector &results) const; - void queryNode(const QuadTreeNode *node, const Vec2 &point, - std::vector &results) const; - void - collectCollisions(const QuadTreeNode *node, - std::vector> &collisions) const; - bool removeFromNode(QuadTreeNode *node, Node *object); - - /** - * @brief 使用扫描线算法检测节点内对象的碰撞 - * @param objects 对象列表 - * @param collisions 输出碰撞对 - */ - void detectCollisionsInNode( - const std::vector> &objects, - std::vector> &collisions) const; - - std::unique_ptr root_; - Rect worldBounds_; - size_t objectCount_ = 0; - - // 碰撞检测用的临时缓冲区,避免重复分配 - mutable std::vector> collisionBuffer_; -}; - -} // namespace extra2d diff --git a/Extra2D/include/extra2d/spatial/spatial_hash.h b/Extra2D/include/extra2d/spatial/spatial_hash.h deleted file mode 100644 index 57d42bf..0000000 --- a/Extra2D/include/extra2d/spatial/spatial_hash.h +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace extra2d { - -/** - * @brief 空间哈希实现 - 优化内存布局版本 - * 使用连续内存存储单元格内容,减少内存碎片 - */ -class SpatialHash : public ISpatialIndex { -public: - using CellKey = std::pair; - - struct CellKeyHash { - size_t operator()(const CellKey &key) const { - return std::hash()(key.first) ^ - (std::hash()(key.second) << 1); - } - }; - - explicit SpatialHash(float cellSize = 64.0f); - ~SpatialHash() override = default; - - void insert(Node *node, const Rect &bounds) override; - void remove(Node *node) override; - void update(Node *node, const Rect &newBounds) override; - - std::vector query(const Rect &area) const override; - std::vector query(const Vec2 &point) const override; - std::vector> queryCollisions() const override; - - void clear() override; - size_t size() const override; - bool empty() const override; - - void rebuild() override; - - void setCellSize(float cellSize); - float getCellSize() const { return cellSize_; } - -private: - /** - * @brief 单元格数据 - 使用vector代替unordered_set减少内存开销 - */ - struct Cell { - std::vector objects; - - void insert(Node *node); - void remove(Node *node); - bool contains(Node *node) const; - void clear() { objects.clear(); } - size_t size() const { return objects.size(); } - bool empty() const { return objects.empty(); } - }; - - CellKey getCellKey(float x, float y) const; - void getCellsForRect(const Rect &rect, std::vector &cells) const; - void insertIntoCells(Node *node, const Rect &bounds); - void removeFromCells(Node *node, const Rect &bounds); - - float cellSize_; - // 使用vector存储对象列表代替unordered_set,内存更紧凑 - std::unordered_map grid_; - std::unordered_map objectBounds_; - size_t objectCount_ = 0; - - // 查询用的临时缓冲区,避免重复分配 - mutable std::vector queryBuffer_; - mutable std::vector> collisionBuffer_; -}; - -} // namespace extra2d diff --git a/Extra2D/include/extra2d/spatial/spatial_index.h b/Extra2D/include/extra2d/spatial/spatial_index.h deleted file mode 100644 index a8e33dd..0000000 --- a/Extra2D/include/extra2d/spatial/spatial_index.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace extra2d { - -class Node; - -enum class SpatialStrategy { Auto, QuadTree, SpatialHash }; - -struct SpatialQueryResult { - Node *node; - Rect bounds; -}; - -class ISpatialIndex { -public: - virtual ~ISpatialIndex() = default; - - virtual void insert(Node *node, const Rect &bounds) = 0; - virtual void remove(Node *node) = 0; - virtual void update(Node *node, const Rect &newBounds) = 0; - - virtual std::vector query(const Rect &area) const = 0; - virtual std::vector query(const Vec2 &point) const = 0; - virtual std::vector> queryCollisions() const = 0; - - virtual void clear() = 0; - virtual size_t size() const = 0; - virtual bool empty() const = 0; - - virtual void rebuild() = 0; -}; - -using SpatialIndexPtr = std::unique_ptr; - -} // namespace extra2d diff --git a/Extra2D/include/extra2d/spatial/spatial_manager.h b/Extra2D/include/extra2d/spatial/spatial_manager.h deleted file mode 100644 index ad6ddad..0000000 --- a/Extra2D/include/extra2d/spatial/spatial_manager.h +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace extra2d { - -class SpatialManager { -public: - using QueryCallback = std::function; - - SpatialManager(); - explicit SpatialManager(const Rect &worldBounds); - ~SpatialManager() = default; - - void setStrategy(SpatialStrategy strategy); - void setAutoThresholds(size_t quadTreeThreshold, size_t hashThreshold); - - void setWorldBounds(const Rect &bounds); - Rect getWorldBounds() const { return worldBounds_; } - - void insert(Node *node, const Rect &bounds); - void remove(Node *node); - void update(Node *node, const Rect &newBounds); - - std::vector query(const Rect &area) const; - std::vector query(const Vec2 &point) const; - std::vector> queryCollisions() const; - - void query(const Rect &area, const QueryCallback &callback) const; - void query(const Vec2 &point, const QueryCallback &callback) const; - - void clear(); - size_t size() const; - bool empty() const; - - void rebuild(); - void optimize(); - - SpatialStrategy getCurrentStrategy() const; - const char *getStrategyName() const; - - static std::unique_ptr createIndex(SpatialStrategy strategy, - const Rect &bounds); - -private: - void selectOptimalStrategy(); - - SpatialStrategy currentStrategy_ = SpatialStrategy::Auto; - SpatialStrategy activeStrategy_ = SpatialStrategy::QuadTree; - std::unique_ptr index_; - Rect worldBounds_; - - size_t quadTreeThreshold_ = 1000; - size_t hashThreshold_ = 5000; - - mutable size_t queryCount_ = 0; - mutable size_t totalQueryTime_ = 0; -}; - -} // namespace extra2d diff --git a/Extra2D/include/extra2d/ui/button.h b/Extra2D/include/extra2d/ui/button.h deleted file mode 100644 index db81183..0000000 --- a/Extra2D/include/extra2d/ui/button.h +++ /dev/null @@ -1,261 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace extra2d { - -// 图片缩放模式 -enum class ImageScaleMode { - Original, // 使用原图大小 - Stretch, // 拉伸填充 - ScaleFit, // 等比缩放,保持完整显示 - ScaleFill // 等比缩放,填充整个区域(可能裁剪) -}; - -// ============================================================================ -// 基础按钮类 -// ============================================================================ -class Button : public Widget { -public: - Button(); - explicit Button(const std::string &text); - ~Button() override = default; - - // ------------------------------------------------------------------------ - // 静态创建方法 - // ------------------------------------------------------------------------ - static Ptr