diff --git a/core/Node/Canvas.cpp b/core/Node/Canvas.cpp new file mode 100644 index 00000000..4d381560 --- /dev/null +++ b/core/Node/Canvas.cpp @@ -0,0 +1,149 @@ +#include "..\e2dnode.h" + +e2d::Canvas::Canvas() + : _renderer(nullptr) + , _renderTarget(nullptr) + , _fillBrush(nullptr) + , _lineBrush(nullptr) + , _strokeStyle(nullptr) + , _strokeWidth(1.0f) +{ + _renderer = Game::getInstance()->getRenderer(); + _renderTarget = _renderer->getRenderTarget(); + _renderTarget->AddRef(); + + ThrowIfFailed( + _renderTarget->CreateSolidColorBrush( + D2D1::ColorF(D2D1::ColorF::White), + &_fillBrush + ) + ); + + ThrowIfFailed( + _renderTarget->CreateSolidColorBrush( + D2D1::ColorF(D2D1::ColorF::White), + &_lineBrush + ) + ); + + _strokeStyle = _renderer->getMiterStrokeStyle(); + + this->setClipEnabled(true); +} + +e2d::Canvas::~Canvas() +{ + SafeRelease(_lineBrush); + SafeRelease(_fillBrush); + SafeRelease(_renderTarget); +} + +void e2d::Canvas::setLineColor(const Color & color) +{ + _lineBrush->SetColor(D2D_COLOR_F(color)); +} + +void e2d::Canvas::setFillColor(const Color & color) +{ + _fillBrush->SetColor(D2D_COLOR_F(color)); +} + +void e2d::Canvas::setStrokeWidth(float width) +{ + _strokeWidth = std::max(width, 0.f); +} + +void e2d::Canvas::setStrokeStyle(Stroke strokeStyle) +{ + switch (strokeStyle) + { + case e2d::Stroke::Miter: + _strokeStyle = _renderer->getMiterStrokeStyle(); + break; + case e2d::Stroke::Bevel: + _strokeStyle = _renderer->getBevelStrokeStyle(); + break; + case e2d::Stroke::Round: + _strokeStyle = _renderer->getRoundStrokeStyle(); + break; + } +} + +void e2d::Canvas::drawLine(const Point & begin, const Point & end) +{ + _renderTarget->DrawLine( + D2D1::Point2F(begin.x, begin.y), + D2D1::Point2F(end.x, end.y), + _lineBrush, + _strokeWidth, + _strokeStyle + ); +} + +void e2d::Canvas::drawRect(const Rect & rect) +{ + _renderTarget->DrawRectangle( + D2D1::RectF( + rect.origin.x, + rect.origin.y, + rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height + ), + _lineBrush, + _strokeWidth, + _strokeStyle + ); +} + +void e2d::Canvas::drawRoundedRect(const Rect & rect, float radiusX, float radiusY) +{ + _renderTarget->DrawRoundedRectangle( + D2D1::RoundedRect( + D2D1::RectF( + rect.origin.x, + rect.origin.y, + rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height + ), + radiusX, + radiusY + ), + _lineBrush, + _strokeWidth, + _strokeStyle + ); +} + +void e2d::Canvas::fillRect(const Rect & rect) +{ + _renderTarget->FillRectangle( + D2D1::RectF( + rect.origin.x, + rect.origin.y, + rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height + ), + _fillBrush + ); +} + +void e2d::Canvas::fillRoundedRect(const Rect & rect, float radiusX, float radiusY) +{ + _renderTarget->FillRoundedRectangle( + D2D1::RoundedRect( + D2D1::RectF( + rect.origin.x, + rect.origin.y, + rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height + ), + radiusX, + radiusY + ), + _fillBrush + ); +} + +void e2d::Canvas::visit(Game * game) +{ +} diff --git a/core/Node/Text.cpp b/core/Node/Text.cpp index 5cb74ff7..2f646cc2 100644 --- a/core/Node/Text.cpp +++ b/core/Node/Text.cpp @@ -15,7 +15,7 @@ e2d::Text::Style::Style() , hasOutline(true) , outlineColor(Color(Color::Black, 0.5)) , outlineWidth(1.f) - , outlineJoin(LineJoin::Round) + , outlineStroke(Stroke::Round) {} e2d::Text::Style::Style( @@ -29,7 +29,7 @@ e2d::Text::Style::Style( bool hasOutline, Color outlineColor, float outlineWidth, - LineJoin outlineJoin + Stroke outlineStroke ) : color(color) , alignment(alignment) @@ -41,7 +41,7 @@ e2d::Text::Style::Style( , hasOutline(hasOutline) , outlineColor(outlineColor) , outlineWidth(outlineWidth) - , outlineJoin(outlineJoin) + , outlineStroke(outlineStroke) {} @@ -119,9 +119,9 @@ float e2d::Text::getOutlineWidth() const return _style.outlineWidth; } -e2d::LineJoin e2d::Text::getOutlineJoin() const +e2d::Stroke e2d::Text::getOutlineStroke() const { - return _style.outlineJoin; + return _style.outlineStroke; } int e2d::Text::getLineCount() const @@ -282,9 +282,9 @@ void e2d::Text::setOutlineWidth(float outlineWidth) _style.outlineWidth = outlineWidth; } -void e2d::Text::setOutlineJoin(LineJoin outlineJoin) +void e2d::Text::setOutlineStroke(Stroke outlineStroke) { - _style.outlineJoin = outlineJoin; + _style.outlineStroke = outlineStroke; } void e2d::Text::draw(Renderer * renderer) const @@ -302,7 +302,7 @@ void e2d::Text::draw(Renderer * renderer) const _style.hasOutline, (D2D1_COLOR_F)_style.outlineColor, _style.outlineWidth, - D2D1_LINE_JOIN(_style.outlineJoin) + D2D1_LINE_JOIN(_style.outlineStroke) ); _textLayout->Draw(nullptr, pTextRenderer, 0, 0); } diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 1e5cd5a8..97158da1 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -16,7 +16,7 @@ enum class Direction : int // 线条相交样式 -enum class LineJoin : int +enum class Stroke : int { Miter = 0, /* 斜切 */ Bevel = 1, /* 斜角 */ diff --git a/core/e2dnode.h b/core/e2dnode.h index 07ea41ec..73b9e591 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -446,18 +446,18 @@ protected: E2D_DISABLE_COPY(Node); // 设置节点所在场景 - virtual void _setParentScene( + void _setParentScene( Scene * scene ); // 子节点排序 - virtual void _sortChildren(); + void _sortChildren(); // 更新转换矩阵 void _updateTransform(); // 更新节点透明度 - virtual void _updateOpacity(); + void _updateOpacity(); protected: String _name; @@ -632,7 +632,7 @@ public: bool hasOutline; // 显示描边 Color outlineColor; // 描边颜色 float outlineWidth; // 描边线宽 - LineJoin outlineJoin; // 描边线相交样式 + Stroke outlineStroke; // 描边线相交样式 public: Style(); @@ -648,7 +648,7 @@ public: bool hasOutline = true, Color outlineColor = Color(Color::Black, 0.5), float outlineWidth = 1.f, - LineJoin outlineJoin = LineJoin::Round + Stroke outlineStroke = Stroke::Round ); }; @@ -691,7 +691,7 @@ public: float getOutlineWidth() const; // 获取描边线相交样式 - LineJoin getOutlineJoin() const; + Stroke getOutlineStroke() const; // 获取文本显示行数 int getLineCount() const; @@ -794,8 +794,8 @@ public: ); // 设置描边线相交样式 - void setOutlineJoin( - LineJoin outlineJoin + void setOutlineStroke( + Stroke outlineStroke ); // 渲染文字 @@ -1095,4 +1095,92 @@ protected: std::vector _buttons; }; + +// 画布 +class Canvas : + public Node +{ +public: + Canvas(); + + virtual ~Canvas(); + + // 设置线条颜色 + void setLineColor( + const Color& color + ); + + // 设置填充颜色 + void setFillColor( + const Color& color + ); + + // 设置线条宽度 + void setStrokeWidth( + float width + ); + + // 设置线条相交样式 + void setStrokeStyle( + Stroke strokeStyle + ); + + // 画直线 + void drawLine( + const Point& begin, + const Point& end + ); + + // 画矩形边框 + void drawRect( + const Rect& rect + ); + + // 画圆角矩形边框 + void drawRoundedRect( + const Rect& rect, + float radiusX, + float radiusY + ); + + // 填充矩形 + void fillRect( + const Rect& rect + ); + + // 填充圆角矩形 + void fillRoundedRect( + const Rect& rect, + float radiusX, + 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; + Renderer * _renderer; + ID2D1RenderTarget * _renderTarget; + ID2D1SolidColorBrush * _fillBrush; + ID2D1SolidColorBrush * _lineBrush; + ID2D1StrokeStyle * _strokeStyle; +}; + } \ No newline at end of file diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index dfa03e46..7424e7bc 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -79,6 +79,7 @@ + diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index c8bd501b..ced3f30e 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -235,5 +235,8 @@ Node + + Node + \ No newline at end of file diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 5398932d..d9b81f0b 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -223,6 +223,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 66eaa534..03a44d54 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -235,5 +235,8 @@ Node + + Node + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 4085a90d..e29b7f68 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -243,6 +243,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 250d3c49..fe4a33d1 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -228,6 +228,9 @@ Node + + Node +