Extra2D/Extra2D/include/extra2d/scene/components/shape_renderer.h

285 lines
7.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <extra2d/core/color.h>
#include <extra2d/core/math_types.h>
#include <extra2d/render/core/render_command.h>
#include <extra2d/render/rhi/rhi_types.h>
#include <extra2d/scene/component.h>
#include <vector>
namespace extra2d {
/**
* @brief 形状渲染组件
*
* 负责渲染基本形状:线条、矩形、圆形、三角形、多边形等
* 支持填充和轮廓两种渲染模式
*/
class ShapeRenderer : public Component {
E2D_COMPONENT_TYPE_DECL(ShapeRenderer)
public:
/**
* @brief 默认构造函数
*/
ShapeRenderer();
/**
* @brief 析构函数
*/
~ShapeRenderer() override;
// -------------------------------------------------------------------------
// 颜色和混合
// -------------------------------------------------------------------------
/**
* @brief 设置颜色
* @param color 颜色值
*/
void setColor(const Color& color);
/**
* @brief 获取颜色
* @return 颜色值
*/
Color getColor() const { return color_; }
/**
* @brief 设置混合状态
* @param blend 混合状态
*/
void setBlendState(const rhi::BlendState& blend);
/**
* @brief 获取混合状态
* @return 混合状态
*/
rhi::BlendState getBlendState() const { return blend_; }
// -------------------------------------------------------------------------
// 线条宽度
// -------------------------------------------------------------------------
/**
* @brief 设置线条宽度
* @param width 线条宽度
*/
void setLineWidth(float width);
/**
* @brief 获取线条宽度
* @return 线条宽度
*/
float getLineWidth() const { return lineWidth_; }
// -------------------------------------------------------------------------
// 渲染层级
// -------------------------------------------------------------------------
/**
* @brief 设置渲染层级
* @param layer 层级值
*/
void setLayer(int layer);
/**
* @brief 获取渲染层级
* @return 层级值
*/
int getLayer() const { return layer_; }
// -------------------------------------------------------------------------
// 形状绘制方法
// -------------------------------------------------------------------------
/**
* @brief 绘制线条
* @param start 起点坐标
* @param end 终点坐标
*/
void drawLine(const Vec2& start, const Vec2& end);
/**
* @brief 绘制多条线段
* @param points 点列表
*/
void drawLines(const std::vector<Vec2>& points);
/**
* @brief 绘制折线
* @param points 点列表
*/
void drawPolyline(const std::vector<Vec2>& points);
/**
* @brief 绘制矩形(轮廓)
* @param rect 矩形区域
*/
void drawRect(const Rect& rect);
/**
* @brief 绘制填充矩形
* @param rect 矩形区域
*/
void drawFilledRect(const Rect& rect);
/**
* @brief 绘制圆形(轮廓)
* @param center 圆心坐标
* @param radius 半径
* @param segments 分段数0表示使用默认值
*/
void drawCircle(const Vec2& center, float radius, int segments = 0);
/**
* @brief 绘制填充圆形
* @param center 圆心坐标
* @param radius 半径
* @param segments 分段数0表示使用默认值
*/
void drawFilledCircle(const Vec2& center, float radius, int segments = 0);
/**
* @brief 绘制椭圆(轮廓)
* @param center 圆心坐标
* @param radiusX X轴半径
* @param radiusY Y轴半径
* @param segments 分段数0表示使用默认值
*/
void drawEllipse(const Vec2& center, float radiusX, float radiusY, int segments = 0);
/**
* @brief 绘制填充椭圆
* @param center 圆心坐标
* @param radiusX X轴半径
* @param radiusY Y轴半径
* @param segments 分段数0表示使用默认值
*/
void drawFilledEllipse(const Vec2& center, float radiusX, float radiusY, int segments = 0);
/**
* @brief 绘制三角形(轮廓)
* @param p1 第一个顶点
* @param p2 第二个顶点
* @param p3 第三个顶点
*/
void drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3);
/**
* @brief 绘制填充三角形
* @param p1 第一个顶点
* @param p2 第二个顶点
* @param p3 第三个顶点
*/
void drawFilledTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3);
/**
* @brief 绘制多边形(轮廓)
* @param points 顶点列表
*/
void drawPolygon(const std::vector<Vec2>& points);
/**
* @brief 绘制填充多边形
* @param points 顶点列表
*/
void drawFilledPolygon(const std::vector<Vec2>& points);
/**
* @brief 绘制圆弧
* @param center 圆心坐标
* @param radius 半径
* @param startAngle 起始角度(度)
* @param endAngle 结束角度(度)
* @param segments 分段数0表示使用默认值
*/
void drawArc(const Vec2& center, float radius, float startAngle, float endAngle, int segments = 0);
// -------------------------------------------------------------------------
// 形状管理
// -------------------------------------------------------------------------
/**
* @brief 清除所有形状
*/
void clearShapes();
/**
* @brief 检查是否有形状
* @return 如果有形状返回true
*/
bool hasShapes() const { return !shapes_.empty(); }
/**
* @brief 获取形状数量
* @return 形状数量
*/
size_t getShapeCount() const { return shapes_.size(); }
// -------------------------------------------------------------------------
// 组件生命周期
// -------------------------------------------------------------------------
/**
* @brief 初始化组件
*/
void init() override;
/**
* @brief 销毁组件
*/
void destroy() override;
/**
* @brief 收集渲染命令
* @param queue 渲染队列
*/
void collectRenderCommands(RenderQueue& queue) override;
// -------------------------------------------------------------------------
// 边界框
// -------------------------------------------------------------------------
/**
* @brief 获取边界矩形
* @return 边界矩形
*/
Rect getBounds() const;
private:
/**
* @brief 内部形状数据结构
*/
struct ShapeData {
ShapeType type;
std::vector<Vec2> points;
Color color;
float lineWidth;
float radius;
int segments;
rhi::BlendState blend;
bool filled;
ShapeData()
: type(ShapeType::Rect), points(), color(Colors::White),
lineWidth(1.0f), radius(0.0f), segments(32),
blend(rhi::BlendState::alphaBlend()), filled(false) {}
};
/**
* @brief 添加形状
* @param shape 形状数据
*/
void addShape(const ShapeData& shape);
std::vector<ShapeData> shapes_;
Color color_;
rhi::BlendState blend_;
float lineWidth_;
int layer_;
int defaultSegments_;
};
} // namespace extra2d