diff --git a/src/kiwano/2d/ShapeActor.cpp b/src/kiwano/2d/ShapeActor.cpp index 5bd96330..a4a4b423 100644 --- a/src/kiwano/2d/ShapeActor.cpp +++ b/src/kiwano/2d/ShapeActor.cpp @@ -32,7 +32,7 @@ namespace kiwano { } - ShapeActor::ShapeActor(Geometry geometry) + ShapeActor::ShapeActor(Geometry const& geometry) : ShapeActor() { SetGeometry(geometry); @@ -80,7 +80,7 @@ namespace kiwano stroke_style_ = stroke_style; } - void ShapeActor::SetGeometry(Geometry geometry) + void ShapeActor::SetGeometry(Geometry const& geometry) { geo_ = geometry; if (geo_) @@ -134,13 +134,7 @@ namespace kiwano void LineActor::SetPoint(Point const& point) { - Geometry geo = Geometry::CreateLine(Point{}, point); - - if (geo) - { - point_ = point; - SetGeometry(geo); - } + SetGeometry(Geometry::CreateLine(Point{}, point)); } @@ -163,13 +157,7 @@ namespace kiwano void RectActor::SetRectSize(Size const& size) { - Geometry geo = Geometry::CreateRect(Rect{ Point{}, size }); - - if (geo) - { - rect_size_ = size; - SetGeometry(geo); - } + SetGeometry(Geometry::CreateRect(Rect{ Point{}, size })); } @@ -202,13 +190,7 @@ namespace kiwano void RoundRectActor::SetRoundedRect(Size const& size, Vec2 const& radius) { - Geometry geo = Geometry::CreateRoundedRect(Rect{ Point{}, size }, radius); - - if (geo) - { - rect_size_ = size; - SetGeometry(geo); - } + SetGeometry(Geometry::CreateRoundedRect(Rect{ Point{}, size }, radius)); } @@ -232,12 +214,7 @@ namespace kiwano void CircleActor::SetRadius(Float32 radius) { - Geometry geo = Geometry::CreateCircle(Point{}, radius); - - if (geo) - { - SetGeometry(geo); - } + SetGeometry(Geometry::CreateCircle(Point{ radius, radius }, radius)); } @@ -260,11 +237,38 @@ namespace kiwano void EllipseActor::SetRadius(Vec2 const& radius) { - Geometry geo = Geometry::CreateEllipse(Point{}, radius); + SetGeometry(Geometry::CreateEllipse(radius, radius)); + } - if (geo) + + //------------------------------------------------------- + // PolygonActor + //------------------------------------------------------- + + PolygonActor::PolygonActor() + { + } + + PolygonActor::PolygonActor(Vector const& points) + { + SetVertices(points); + } + + PolygonActor::~PolygonActor() + { + } + + void PolygonActor::SetVertices(Vector const& points) + { + if (points.size() > 1) { - SetGeometry(geo); + SetGeometry( + GeometrySink() + .BeginPath(points[0]) + .AddLines(&points[1], points.size() - 1) + .EndPath(true) + .GetGeometry() + ); } } diff --git a/src/kiwano/2d/ShapeActor.h b/src/kiwano/2d/ShapeActor.h index 0ead7c30..d0eec865 100644 --- a/src/kiwano/2d/ShapeActor.h +++ b/src/kiwano/2d/ShapeActor.h @@ -33,7 +33,7 @@ namespace kiwano ShapeActor(); ShapeActor( - Geometry geometry + Geometry const& geometry ); virtual ~ShapeActor(); @@ -75,7 +75,7 @@ namespace kiwano void SetStrokeStyle(StrokeStyle stroke_style); // 设置形状 - void SetGeometry(Geometry geometry); + void SetGeometry(Geometry const& geometry); void OnRender(RenderTarget* rt) override; @@ -218,6 +218,25 @@ namespace kiwano }; + // 多边形角色 + class KGE_API PolygonActor + : public ShapeActor + { + public: + PolygonActor(); + + PolygonActor( + Vector const& points + ); + + virtual ~PolygonActor(); + + void SetVertices( + Vector const& points + ); + }; + + // 路径角色 class KGE_API PathActor : public ShapeActor diff --git a/src/kiwano/2d/action/ActionWalk.h b/src/kiwano/2d/action/ActionWalk.h index 9d05c893..eeaae114 100644 --- a/src/kiwano/2d/action/ActionWalk.h +++ b/src/kiwano/2d/action/ActionWalk.h @@ -90,10 +90,10 @@ namespace kiwano void ClearPath(); // 获取路线 - inline Geometry GetPath() const { return path_; } + inline Geometry const& GetPath() const { return path_; } // 设置路径 - inline void SetPath(Geometry path) { path_ = path; } + inline void SetPath(Geometry const& path) { path_ = path; } protected: void Init(ActorPtr target) override; diff --git a/src/kiwano/2d/include-forwards.h b/src/kiwano/2d/include-forwards.h index bbe73ae7..047488e0 100644 --- a/src/kiwano/2d/include-forwards.h +++ b/src/kiwano/2d/include-forwards.h @@ -45,6 +45,7 @@ namespace kiwano KGE_DECLARE_SMART_PTR(RoundRectActor); KGE_DECLARE_SMART_PTR(CircleActor); KGE_DECLARE_SMART_PTR(EllipseActor); + KGE_DECLARE_SMART_PTR(PolygonActor); KGE_DECLARE_SMART_PTR(PathActor); KGE_DECLARE_SMART_PTR(Action); diff --git a/src/kiwano/renderer/Geometry.cpp b/src/kiwano/renderer/Geometry.cpp index 5858ba1d..eba0f2fb 100644 --- a/src/kiwano/renderer/Geometry.cpp +++ b/src/kiwano/renderer/Geometry.cpp @@ -246,6 +246,14 @@ namespace kiwano return (*this); } + GeometrySink& kiwano::GeometrySink::AddLines(const Point* points, UInt32 count) + { + if (!sink_) BeginPath(); + + sink_->AddLines(reinterpret_cast(points), count); + return (*this); + } + GeometrySink& GeometrySink::AddBezier(Point const& point1, Point const& point2, Point const& point3) { if (!sink_) BeginPath(); diff --git a/src/kiwano/renderer/Geometry.h b/src/kiwano/renderer/Geometry.h index 86b2f290..d10bd68e 100644 --- a/src/kiwano/renderer/Geometry.h +++ b/src/kiwano/renderer/Geometry.h @@ -151,6 +151,12 @@ namespace kiwano Vector const& points ); + // 添加多条线段 + GeometrySink& AddLines( + const Point* points, + UInt32 count + ); + // 添加一条三次方贝塞尔曲线 GeometrySink& AddBezier( Point const& point1, /* 贝塞尔曲线的第一个控制点 */ @@ -162,7 +168,7 @@ namespace kiwano GeometrySink& AddArc( Point const& point, /* 终点 */ Size const& radius, /* 椭圆半径 */ - Float32 rotation, /* 椭圆旋转角度 */ + Float32 rotation, /* 椭圆旋转角度 */ bool clockwise = true, /* 顺时针 or 逆时针 */ bool is_small = true /* 是否取小于 180° 的弧 */ );