From 8323a245ee3d245da185d0f9dc8fd4610572e15e Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 23 Aug 2018 12:25:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Canvas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Common/Color.cpp | 8 ++++ core/Node/Canvas.cpp | 98 +++++++++++++++++++++++++++++++++++++++---- core/e2dcommon.h | 4 ++ core/e2dnode.h | 63 ++++++++++++++++++++-------- 4 files changed, 147 insertions(+), 26 deletions(-) diff --git a/core/Common/Color.cpp b/core/Common/Color.cpp index d857ec7c..d5690073 100644 --- a/core/Common/Color.cpp +++ b/core/Common/Color.cpp @@ -42,6 +42,14 @@ e2d::Color::Color(UINT rgb, float alpha) _init(rgb, alpha); } +e2d::Color::Color(const D2D1_COLOR_F& color) + : r(color.r) + , g(color.g) + , b(color.b) + , a(color.a) +{ +} + e2d::Color::operator D2D1_COLOR_F() const { return std::move(D2D1::ColorF(r, g, b, a)); diff --git a/core/Node/Canvas.cpp b/core/Node/Canvas.cpp index 4d381560..574133a8 100644 --- a/core/Node/Canvas.cpp +++ b/core/Node/Canvas.cpp @@ -1,12 +1,13 @@ #include "..\e2dnode.h" -e2d::Canvas::Canvas() +e2d::Canvas::Canvas(float width, float height) : _renderer(nullptr) , _renderTarget(nullptr) , _fillBrush(nullptr) , _lineBrush(nullptr) , _strokeStyle(nullptr) , _strokeWidth(1.0f) + , _stroke(Stroke::Miter) { _renderer = Game::getInstance()->getRenderer(); _renderTarget = _renderer->getRenderTarget(); @@ -25,10 +26,11 @@ e2d::Canvas::Canvas() &_lineBrush ) ); - - _strokeStyle = _renderer->getMiterStrokeStyle(); - + this->setClipEnabled(true); + this->setWidth(width); + this->setHeight(height); + this->setStrokeStyle(_stroke); } e2d::Canvas::~Canvas() @@ -69,6 +71,26 @@ void e2d::Canvas::setStrokeStyle(Stroke strokeStyle) } } +const e2d::Color & e2d::Canvas::getLineColor() const +{ + return Color(_lineBrush->GetColor()); +} + +const e2d::Color & e2d::Canvas::getFillColor() const +{ + return Color(_fillBrush->GetColor()); +} + +float e2d::Canvas::getStrokeWidth() const +{ + return _strokeWidth; +} + +e2d::Stroke e2d::Canvas::getStrokeStyle() const +{ + return _stroke; +} + void e2d::Canvas::drawLine(const Point & begin, const Point & end) { _renderTarget->DrawLine( @@ -80,6 +102,40 @@ void e2d::Canvas::drawLine(const Point & begin, const Point & end) ); } +void e2d::Canvas::drawCircle(const Point & center, float radius) +{ + _renderTarget->DrawEllipse( + D2D1::Ellipse( + D2D1::Point2F( + center.x, + center.y + ), + radius, + radius + ), + _lineBrush, + _strokeWidth, + _strokeStyle + ); +} + +void e2d::Canvas::drawEllipse(const Point & center, float radiusX, float radiusY) +{ + _renderTarget->DrawEllipse( + D2D1::Ellipse( + D2D1::Point2F( + center.x, + center.y + ), + radiusX, + radiusY + ), + _lineBrush, + _strokeWidth, + _strokeStyle + ); +} + void e2d::Canvas::drawRect(const Rect & rect) { _renderTarget->DrawRectangle( @@ -114,6 +170,36 @@ void e2d::Canvas::drawRoundedRect(const Rect & rect, float radiusX, float radius ); } +void e2d::Canvas::fillCircle(const Point & center, float radius) +{ + _renderTarget->FillEllipse( + D2D1::Ellipse( + D2D1::Point2F( + center.x, + center.y + ), + radius, + radius + ), + _fillBrush + ); +} + +void e2d::Canvas::fillEllipse(const Point & center, float radiusX, float radiusY) +{ + _renderTarget->FillEllipse( + D2D1::Ellipse( + D2D1::Point2F( + center.x, + center.y + ), + radiusX, + radiusY + ), + _fillBrush + ); +} + void e2d::Canvas::fillRect(const Rect & rect) { _renderTarget->FillRectangle( @@ -143,7 +229,3 @@ void e2d::Canvas::fillRoundedRect(const Rect & rect, float radiusX, float radius _fillBrush ); } - -void e2d::Canvas::visit(Game * game) -{ -} diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 97158da1..249846b4 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -310,6 +310,10 @@ public: float alpha ); + Color( + const D2D1_COLOR_F& color + ); + E2D_OP_EXPLICIT operator D2D1_COLOR_F() const; public: diff --git a/core/e2dnode.h b/core/e2dnode.h index 73b9e591..67227ff3 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -1098,10 +1098,14 @@ protected: // 画布 class Canvas : - public Node + public Node, + public Drawable { public: - Canvas(); + Canvas( + float width, + float height + ); virtual ~Canvas(); @@ -1125,12 +1129,37 @@ public: Stroke strokeStyle ); + // 获取线条颜色 + const Color& getLineColor() const; + + // 获取填充颜色 + const Color& getFillColor() const; + + // 获取线条宽度 + float getStrokeWidth() const; + + // 获取线条相交样式 + Stroke getStrokeStyle() const; + // 画直线 void drawLine( const Point& begin, const Point& end ); + // 画圆形边框 + void drawCircle( + const Point& center, + float radius + ); + + // 画椭圆形边框 + void drawEllipse( + const Point& center, + float radiusX, + float radiusY + ); + // 画矩形边框 void drawRect( const Rect& rect @@ -1143,6 +1172,19 @@ public: float radiusY ); + // 填充圆形 + void fillCircle( + const Point& center, + float radius + ); + + // 填充椭圆形 + void fillEllipse( + const Point& center, + float radiusX, + float radiusY + ); + // 填充矩形 void fillRect( const Rect& rect @@ -1155,27 +1197,12 @@ public: float radiusY ); - // 开启路径 - void beginPath(); - - // 添加点 - void addPoint( - const Point& p - ); - - // 结束路径 - void endPath(); - - // 遍历节点 - virtual void visit( - Game * game - ) override; - protected: E2D_DISABLE_COPY(Canvas); protected: float _strokeWidth; + Stroke _stroke; Renderer * _renderer; ID2D1RenderTarget * _renderTarget; ID2D1SolidColorBrush * _fillBrush;