Magic_Game/core/Node/Canvas.cpp

232 lines
4.0 KiB
C++
Raw Normal View History

2018-08-23 00:03:26 +08:00
#include "..\e2dnode.h"
2018-08-23 12:25:31 +08:00
e2d::Canvas::Canvas(float width, float height)
2018-08-23 00:03:26 +08:00
: _renderer(nullptr)
, _renderTarget(nullptr)
, _fillBrush(nullptr)
, _lineBrush(nullptr)
, _strokeStyle(nullptr)
, _strokeWidth(1.0f)
2018-08-23 12:25:31 +08:00
, _stroke(Stroke::Miter)
2018-08-23 00:03:26 +08:00
{
_renderer = Game::getInstance()->getRenderer();
_renderTarget = _renderer->getRenderTarget();
2018-08-23 00:03:26 +08:00
_renderTarget->AddRef();
ThrowIfFailed(
_renderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&_fillBrush
)
);
ThrowIfFailed(
_renderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&_lineBrush
)
);
2018-08-23 12:25:31 +08:00
this->setClipEnabled(true);
this->setWidth(width);
this->setHeight(height);
this->setStrokeStyle(_stroke);
2018-08-23 00:03:26 +08:00
}
e2d::Canvas::~Canvas()
{
SafeRelease(_lineBrush);
SafeRelease(_fillBrush);
SafeRelease(_renderTarget);
}
void e2d::Canvas::setLineColor(const Color & color)
2018-08-23 00:03:26 +08:00
{
_lineBrush->SetColor(D2D_COLOR_F(color));
}
void e2d::Canvas::setFillColor(const Color & color)
2018-08-23 00:03:26 +08:00
{
_fillBrush->SetColor(D2D_COLOR_F(color));
}
void e2d::Canvas::setStrokeWidth(float width)
2018-08-23 00:03:26 +08:00
{
_strokeWidth = std::max(width, 0.f);
}
void e2d::Canvas::setStrokeStyle(Stroke strokeStyle)
2018-08-23 00:03:26 +08:00
{
switch (strokeStyle)
{
case e2d::Stroke::Miter:
_strokeStyle = _renderer->getMiterStrokeStyle();
2018-08-23 00:03:26 +08:00
break;
case e2d::Stroke::Bevel:
_strokeStyle = _renderer->getBevelStrokeStyle();
2018-08-23 00:03:26 +08:00
break;
case e2d::Stroke::Round:
_strokeStyle = _renderer->getRoundStrokeStyle();
2018-08-23 00:03:26 +08:00
break;
}
}
2018-08-23 16:58:32 +08:00
e2d::Color e2d::Canvas::getLineColor() const
2018-08-23 12:25:31 +08:00
{
2018-08-23 16:58:32 +08:00
return _lineBrush->GetColor();
2018-08-23 12:25:31 +08:00
}
2018-08-23 16:58:32 +08:00
e2d::Color e2d::Canvas::getFillColor() const
2018-08-23 12:25:31 +08:00
{
2018-08-23 16:58:32 +08:00
return _fillBrush->GetColor();
2018-08-23 12:25:31 +08:00
}
float e2d::Canvas::getStrokeWidth() const
2018-08-23 12:25:31 +08:00
{
return _strokeWidth;
}
e2d::Stroke e2d::Canvas::getStrokeStyle() const
2018-08-23 12:25:31 +08:00
{
return _stroke;
}
void e2d::Canvas::drawLine(const Point & begin, const Point & end)
2018-08-23 00:03:26 +08:00
{
_renderTarget->DrawLine(
D2D1::Point2F(begin.x, begin.y),
D2D1::Point2F(end.x, end.y),
_lineBrush,
_strokeWidth,
_strokeStyle
);
}
void e2d::Canvas::drawCircle(const Point & center, float radius)
2018-08-23 12:25:31 +08:00
{
_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)
2018-08-23 12:25:31 +08:00
{
_renderTarget->DrawEllipse(
D2D1::Ellipse(
D2D1::Point2F(
center.x,
center.y
),
radiusX,
radiusY
),
_lineBrush,
_strokeWidth,
_strokeStyle
);
}
void e2d::Canvas::drawRect(const Rect & rect)
2018-08-23 00:03:26 +08:00
{
_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)
2018-08-23 00:03:26 +08:00
{
_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::fillCircle(const Point & center, float radius)
2018-08-23 12:25:31 +08:00
{
_renderTarget->FillEllipse(
D2D1::Ellipse(
D2D1::Point2F(
center.x,
center.y
),
radius,
radius
),
_fillBrush
);
}
void e2d::Canvas::fillEllipse(const Point & center, float radiusX, float radiusY)
2018-08-23 12:25:31 +08:00
{
_renderTarget->FillEllipse(
D2D1::Ellipse(
D2D1::Point2F(
center.x,
center.y
),
radiusX,
radiusY
),
_fillBrush
);
}
void e2d::Canvas::fillRect(const Rect & rect)
2018-08-23 00:03:26 +08:00
{
_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)
2018-08-23 00:03:26 +08:00
{
_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
);
}