From 92711b411ae0f54c0d7ccd74b1fb969bdee784a3 Mon Sep 17 00:00:00 2001 From: Haibo Date: Tue, 20 Nov 2018 14:30:39 +0800 Subject: [PATCH] add: LineGeometry & RoundedRectGeometry --- core/base/Geometry.cpp | 99 ++++++++++++++++++++++++++++++++++++++ core/base/Geometry.h | 84 +++++++++++++++++++++++++++++++- core/base/GeometryNode.cpp | 6 +-- core/base/base.hpp | 2 + core/base/d2dres.hpp | 1 + core/base/render.cpp | 20 ++++++++ core/base/render.h | 7 +++ 7 files changed, 214 insertions(+), 5 deletions(-) diff --git a/core/base/Geometry.cpp b/core/base/Geometry.cpp index dfc5940e..2f52d850 100644 --- a/core/base/Geometry.cpp +++ b/core/base/Geometry.cpp @@ -112,6 +112,60 @@ namespace easy2d } + //------------------------------------------------------- + // LineGeometry + //------------------------------------------------------- + + LineGeometry::LineGeometry() + { + } + + LineGeometry::LineGeometry(Point const & begin, Point const & end) + { + SetLine(begin, end); + } + + LineGeometry::~LineGeometry() + { + } + + void LineGeometry::SetLine(Point const & begin, Point const & end) + { + cpPathGeometry path_geo; + cpGeometrySink path_sink; + + HRESULT hr = devices::Graphics::Instance()->CreatePathGeometry(path_geo); + + if (SUCCEEDED(hr)) + { + hr = path_geo->Open(&path_sink); + } + + if (SUCCEEDED(hr)) + { + path_sink->BeginFigure(begin, D2D1_FIGURE_BEGIN_FILLED); + path_sink->AddLine(end); + path_sink->EndFigure(D2D1_FIGURE_END_OPEN); + hr = path_sink->Close(); + } + + if (SUCCEEDED(hr)) + { + geo_ = path_geo; + } + } + + void LineGeometry::SetBegin(Point const & begin) + { + SetLine(begin, end_); + } + + void LineGeometry::SetEnd(Point const & end) + { + SetLine(begin_, end); + } + + //------------------------------------------------------- // RectangleGeometry //------------------------------------------------------- @@ -150,6 +204,7 @@ namespace easy2d //------------------------------------------------------- CircleGeometry::CircleGeometry() + : radius_(0.f) { } @@ -189,6 +244,8 @@ namespace easy2d //------------------------------------------------------- EllipseGeometry::EllipseGeometry() + : radius_x_(0.f) + , radius_y_(0.f) { } @@ -314,4 +371,46 @@ namespace easy2d current_geometry_ = nullptr; } + + //------------------------------------------------------- + // RoundedRectGeometry + //------------------------------------------------------- + + RoundedRectGeometry::RoundedRectGeometry() + : radius_x_(0.f) + , radius_y_(0.f) + { + } + + RoundedRectGeometry::RoundedRectGeometry(Rect const & rect, float radius_x, float radius_y) + { + SetRoundedRect(rect, radius_x, radius_y); + } + + RoundedRectGeometry::~RoundedRectGeometry() + { + } + + void RoundedRectGeometry::SetRadius(float radius_x, float radius_y) + { + SetRoundedRect(rect_, radius_x, radius_y); + } + + void RoundedRectGeometry::SetRect(Rect const & rect) + { + SetRoundedRect(rect, radius_x_, radius_y_); + } + + void RoundedRectGeometry::SetRoundedRect(Rect const & rect, float radius_x, float radius_y) + { + cpRoundedRectangleGeometry geo; + if (SUCCEEDED(devices::Graphics::Instance()->CreateRoundedRectangleGeometry(geo, rect, radius_x, radius_y))) + { + geo_ = geo; + rect_ = rect; + radius_x_ = radius_x; + radius_y_ = radius_y; + } + } + } \ No newline at end of file diff --git a/core/base/Geometry.h b/core/base/Geometry.h index 4ed38a20..fc277fbb 100644 --- a/core/base/Geometry.h +++ b/core/base/Geometry.h @@ -39,6 +39,8 @@ namespace easy2d class Geometry : public Unit { + friend class GeometryNode; + public: Geometry(); @@ -64,13 +66,48 @@ namespace easy2d Point* tangent ); - cpGeometry const& GetD2DGeometry() const { return geo_; } - protected: cpGeometry geo_; }; + // 直线 + class LineGeometry + : public Geometry + { + public: + LineGeometry(); + + LineGeometry( + Point const& begin, + Point const& end + ); + + virtual ~LineGeometry(); + + Point const& GetBegin() const { return begin_; } + + Point const& GetEnd() const { return end_; } + + void SetLine( + Point const& begin, + Point const& end + ); + + void SetBegin( + Point const& begin + ); + + void SetEnd( + Point const& end + ); + + protected: + Point begin_; + Point end_; + }; + + // 几何矩形 class RectangleGeometry : public Geometry @@ -221,4 +258,47 @@ namespace easy2d cpPathGeometry current_geometry_; cpGeometrySink current_sink_; }; + + + // 几何圆角矩形 + class RoundedRectGeometry + : public Geometry + { + public: + RoundedRectGeometry(); + + RoundedRectGeometry( + Rect const& rect, + float radius_x, + float radius_y + ); + + virtual ~RoundedRectGeometry(); + + float GetRadiusX() const { return radius_x_; } + + float GetRadiusY() const { return radius_y_; } + + void SetRadius( + float radius_x, + float radius_y + ); + + Rect const& GetRect() const { return rect_; } + + void SetRect( + Rect const& rect + ); + + void SetRoundedRect( + Rect const& rect, + float radius_x, + float radius_y + ); + + protected: + Rect rect_; + float radius_x_; + float radius_y_; + }; } diff --git a/core/base/GeometryNode.cpp b/core/base/GeometryNode.cpp index d55f4af6..0d9ba623 100644 --- a/core/base/GeometryNode.cpp +++ b/core/base/GeometryNode.cpp @@ -68,18 +68,18 @@ namespace easy2d void GeometryNode::OnDraw() { - if (geometry_) + if (geometry_ && geometry_->geo_) { auto graphics = devices::Graphics::Instance(); graphics->SetTransform(geometry_->GetTransformMatrix() * GetTransformMatrix()); graphics->FillGeometry( - geometry_->GetD2DGeometry(), + geometry_->geo_, fill_color_ ); graphics->DrawGeometry( - geometry_->GetD2DGeometry(), + geometry_->geo_, stroke_color_, stroke_width_, outline_join_ diff --git a/core/base/base.hpp b/core/base/base.hpp index 1fa21264..0c47e511 100644 --- a/core/base/base.hpp +++ b/core/base/base.hpp @@ -45,7 +45,9 @@ namespace easy2d E2D_DECLARE_SMART_PTR(Task); E2D_DECLARE_SMART_PTR(Geometry); + E2D_DECLARE_SMART_PTR(LineGeometry); E2D_DECLARE_SMART_PTR(RectangleGeometry); + E2D_DECLARE_SMART_PTR(RoundedRectGeometry); E2D_DECLARE_SMART_PTR(CircleGeometry); E2D_DECLARE_SMART_PTR(EllipseGeometry); E2D_DECLARE_SMART_PTR(PathGeometry); diff --git a/core/base/d2dres.hpp b/core/base/d2dres.hpp index f4462d8c..13fc9788 100644 --- a/core/base/d2dres.hpp +++ b/core/base/d2dres.hpp @@ -43,6 +43,7 @@ namespace easy2d E2D_DECLARE_D2D_SMART_PTR(ID2D1Geometry, cpGeometry); E2D_DECLARE_D2D_SMART_PTR(ID2D1RectangleGeometry, cpRectangleGeometry); + E2D_DECLARE_D2D_SMART_PTR(ID2D1RoundedRectangleGeometry, cpRoundedRectangleGeometry); E2D_DECLARE_D2D_SMART_PTR(ID2D1EllipseGeometry, cpEllipseGeometry); E2D_DECLARE_D2D_SMART_PTR(ID2D1GeometryGroup, cpGeometryGroup); E2D_DECLARE_D2D_SMART_PTR(ID2D1PathGeometry, cpPathGeometry); diff --git a/core/base/render.cpp b/core/base/render.cpp index 5ac1c41c..8c528851 100644 --- a/core/base/render.cpp +++ b/core/base/render.cpp @@ -184,6 +184,26 @@ namespace easy2d return hr; } + HRESULT GraphicsDevice::CreateRoundedRectangleGeometry(cpRoundedRectangleGeometry & geo, Rect const & rect, float radius_x, float radius_y) const + { + if (!d2d.factory) + return E_UNEXPECTED; + + cpRoundedRectangleGeometry rounded_rect; + HRESULT hr = d2d.factory->CreateRoundedRectangleGeometry( + D2D1::RoundedRect( + rect, + radius_x, + radius_y + ), + &rounded_rect + ); + + if (SUCCEEDED(hr)) + geo = rounded_rect; + return hr; + } + HRESULT GraphicsDevice::CreateEllipseGeometry(cpEllipseGeometry & geo, Point const & center, float radius_x, float radius_y) const { if (!d2d.factory) diff --git a/core/base/render.h b/core/base/render.h index d8d83eba..0eaeadc9 100644 --- a/core/base/render.h +++ b/core/base/render.h @@ -73,6 +73,13 @@ namespace easy2d Rect const& rect ) const; + HRESULT CreateRoundedRectangleGeometry( + cpRoundedRectangleGeometry& geo, + Rect const& rect, + float radius_x, + float radius_y + ) const; + HRESULT CreateEllipseGeometry( cpEllipseGeometry& geo, Point const& center,