Extra2D/Extra2D/include/extra2d/scene/shape_node.h

112 lines
4.4 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/scene/node.h>
#include <vector>
namespace extra2d {
// ============================================================================
// 形状类型
// ============================================================================
enum class ShapeType { Point, Line, Rect, Circle, Triangle, Polygon };
// ============================================================================
// 形状节点 - 用于绘制几何形状
// 注意ShapeNode 继承自 Node使用 SharedPtr 因为需要 enable_shared_from_this
// ============================================================================
class ShapeNode : public Node {
public:
ShapeNode();
~ShapeNode() override = default;
// ------------------------------------------------------------------------
// 静态创建方法
// ------------------------------------------------------------------------
static SharedPtr<ShapeNode> create();
// 点
static SharedPtr<ShapeNode> createPoint(const Vec2 &pos, const Color &color);
// 线
static SharedPtr<ShapeNode> createLine(const Vec2 &start, const Vec2 &end,
const Color &color,
float width = 1.0f);
// 矩形
static SharedPtr<ShapeNode> createRect(const Rect &rect, const Color &color,
float width = 1.0f);
static SharedPtr<ShapeNode> createFilledRect(const Rect &rect,
const Color &color);
// 圆形
static SharedPtr<ShapeNode> createCircle(const Vec2 &center, float radius,
const Color &color,
int segments = 32,
float width = 1.0f);
static SharedPtr<ShapeNode> createFilledCircle(const Vec2 &center,
float radius,
const Color &color,
int segments = 32);
// 三角形
static SharedPtr<ShapeNode> createTriangle(const Vec2 &p1, const Vec2 &p2,
const Vec2 &p3, const Color &color,
float width = 1.0f);
static SharedPtr<ShapeNode> createFilledTriangle(const Vec2 &p1,
const Vec2 &p2,
const Vec2 &p3,
const Color &color);
// 多边形
static SharedPtr<ShapeNode> createPolygon(const std::vector<Vec2> &points,
const Color &color,
float width = 1.0f);
static SharedPtr<ShapeNode>
createFilledPolygon(const std::vector<Vec2> &points, const Color &color);
// ------------------------------------------------------------------------
// 属性设置
// ------------------------------------------------------------------------
void setShapeType(ShapeType type) { shapeType_ = type; }
ShapeType getShapeType() const { return shapeType_; }
void setColor(const Color &color) { color_ = color; }
Color getColor() const { return color_; }
void setFilled(bool filled) { filled_ = filled; }
bool isFilled() const { return filled_; }
void setLineWidth(float width) { lineWidth_ = width; }
float getLineWidth() const { return lineWidth_; }
void setSegments(int segments) { segments_ = segments; }
int getSegments() const { return segments_; }
// ------------------------------------------------------------------------
// 点设置
// ------------------------------------------------------------------------
void setPoints(const std::vector<Vec2> &points);
const std::vector<Vec2> &getPoints() const { return points_; }
void addPoint(const Vec2 &point);
void clearPoints();
Rect getBounds() const override;
protected:
void onDraw(RenderBackend &renderer) override;
void generateRenderCommand(std::vector<RenderCommand> &commands,
int zOrder) override;
private:
ShapeType shapeType_ = ShapeType::Rect;
Color color_ = Colors::White;
bool filled_ = false;
float lineWidth_ = 1.0f;
int segments_ = 32;
std::vector<Vec2> points_;
};
} // namespace extra2d