完善Canvas

This commit is contained in:
Nomango 2018-08-23 12:25:31 +08:00
parent e53a7df27a
commit 8323a245ee
4 changed files with 147 additions and 26 deletions

View File

@ -42,6 +42,14 @@ e2d::Color::Color(UINT rgb, float alpha)
_init(rgb, 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 e2d::Color::operator D2D1_COLOR_F() const
{ {
return std::move(D2D1::ColorF(r, g, b, a)); return std::move(D2D1::ColorF(r, g, b, a));

View File

@ -1,12 +1,13 @@
#include "..\e2dnode.h" #include "..\e2dnode.h"
e2d::Canvas::Canvas() e2d::Canvas::Canvas(float width, float height)
: _renderer(nullptr) : _renderer(nullptr)
, _renderTarget(nullptr) , _renderTarget(nullptr)
, _fillBrush(nullptr) , _fillBrush(nullptr)
, _lineBrush(nullptr) , _lineBrush(nullptr)
, _strokeStyle(nullptr) , _strokeStyle(nullptr)
, _strokeWidth(1.0f) , _strokeWidth(1.0f)
, _stroke(Stroke::Miter)
{ {
_renderer = Game::getInstance()->getRenderer(); _renderer = Game::getInstance()->getRenderer();
_renderTarget = _renderer->getRenderTarget(); _renderTarget = _renderer->getRenderTarget();
@ -26,9 +27,10 @@ e2d::Canvas::Canvas()
) )
); );
_strokeStyle = _renderer->getMiterStrokeStyle();
this->setClipEnabled(true); this->setClipEnabled(true);
this->setWidth(width);
this->setHeight(height);
this->setStrokeStyle(_stroke);
} }
e2d::Canvas::~Canvas() 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) void e2d::Canvas::drawLine(const Point & begin, const Point & end)
{ {
_renderTarget->DrawLine( _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) void e2d::Canvas::drawRect(const Rect & rect)
{ {
_renderTarget->DrawRectangle( _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) void e2d::Canvas::fillRect(const Rect & rect)
{ {
_renderTarget->FillRectangle( _renderTarget->FillRectangle(
@ -143,7 +229,3 @@ void e2d::Canvas::fillRoundedRect(const Rect & rect, float radiusX, float radius
_fillBrush _fillBrush
); );
} }
void e2d::Canvas::visit(Game * game)
{
}

View File

@ -310,6 +310,10 @@ public:
float alpha float alpha
); );
Color(
const D2D1_COLOR_F& color
);
E2D_OP_EXPLICIT operator D2D1_COLOR_F() const; E2D_OP_EXPLICIT operator D2D1_COLOR_F() const;
public: public:

View File

@ -1098,10 +1098,14 @@ protected:
// »­²¼ // »­²¼
class Canvas : class Canvas :
public Node public Node,
public Drawable
{ {
public: public:
Canvas(); Canvas(
float width,
float height
);
virtual ~Canvas(); virtual ~Canvas();
@ -1125,12 +1129,37 @@ public:
Stroke strokeStyle Stroke strokeStyle
); );
// »ñÈ¡ÏßÌõÑÕÉ«
const Color& getLineColor() const;
// »ñÈ¡Ìî³äÑÕÉ«
const Color& getFillColor() const;
// »ñÈ¡ÏßÌõ¿í¶È
float getStrokeWidth() const;
// »ñÈ¡ÏßÌõÏཻÑùʽ
Stroke getStrokeStyle() const;
// »­Ö±Ïß // »­Ö±Ïß
void drawLine( void drawLine(
const Point& begin, const Point& begin,
const Point& end const Point& end
); );
// »­Ô²Ðα߿ò
void drawCircle(
const Point& center,
float radius
);
// »­ÍÖÔ²Ðα߿ò
void drawEllipse(
const Point& center,
float radiusX,
float radiusY
);
// »­¾ØÐα߿ò // »­¾ØÐα߿ò
void drawRect( void drawRect(
const Rect& rect const Rect& rect
@ -1143,6 +1172,19 @@ public:
float radiusY float radiusY
); );
// Ìî³äÔ²ÐÎ
void fillCircle(
const Point& center,
float radius
);
// Ìî³äÍÖÔ²ÐÎ
void fillEllipse(
const Point& center,
float radiusX,
float radiusY
);
// Ìî³ä¾ØÐÎ // Ìî³ä¾ØÐÎ
void fillRect( void fillRect(
const Rect& rect const Rect& rect
@ -1155,27 +1197,12 @@ public:
float radiusY float radiusY
); );
// 开启路径
void beginPath();
// 添加点
void addPoint(
const Point& p
);
// 结束路径
void endPath();
// 遍历节点
virtual void visit(
Game * game
) override;
protected: protected:
E2D_DISABLE_COPY(Canvas); E2D_DISABLE_COPY(Canvas);
protected: protected:
float _strokeWidth; float _strokeWidth;
Stroke _stroke;
Renderer * _renderer; Renderer * _renderer;
ID2D1RenderTarget * _renderTarget; ID2D1RenderTarget * _renderTarget;
ID2D1SolidColorBrush * _fillBrush; ID2D1SolidColorBrush * _fillBrush;