增加Canvas画布节点
This commit is contained in:
parent
00a2b9ebac
commit
e53a7df27a
|
|
@ -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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -15,7 +15,7 @@ e2d::Text::Style::Style()
|
||||||
, hasOutline(true)
|
, hasOutline(true)
|
||||||
, outlineColor(Color(Color::Black, 0.5))
|
, outlineColor(Color(Color::Black, 0.5))
|
||||||
, outlineWidth(1.f)
|
, outlineWidth(1.f)
|
||||||
, outlineJoin(LineJoin::Round)
|
, outlineStroke(Stroke::Round)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
e2d::Text::Style::Style(
|
e2d::Text::Style::Style(
|
||||||
|
|
@ -29,7 +29,7 @@ e2d::Text::Style::Style(
|
||||||
bool hasOutline,
|
bool hasOutline,
|
||||||
Color outlineColor,
|
Color outlineColor,
|
||||||
float outlineWidth,
|
float outlineWidth,
|
||||||
LineJoin outlineJoin
|
Stroke outlineStroke
|
||||||
)
|
)
|
||||||
: color(color)
|
: color(color)
|
||||||
, alignment(alignment)
|
, alignment(alignment)
|
||||||
|
|
@ -41,7 +41,7 @@ e2d::Text::Style::Style(
|
||||||
, hasOutline(hasOutline)
|
, hasOutline(hasOutline)
|
||||||
, outlineColor(outlineColor)
|
, outlineColor(outlineColor)
|
||||||
, outlineWidth(outlineWidth)
|
, outlineWidth(outlineWidth)
|
||||||
, outlineJoin(outlineJoin)
|
, outlineStroke(outlineStroke)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -119,9 +119,9 @@ float e2d::Text::getOutlineWidth() const
|
||||||
return _style.outlineWidth;
|
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
|
int e2d::Text::getLineCount() const
|
||||||
|
|
@ -282,9 +282,9 @@ void e2d::Text::setOutlineWidth(float outlineWidth)
|
||||||
_style.outlineWidth = 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
|
void e2d::Text::draw(Renderer * renderer) const
|
||||||
|
|
@ -302,7 +302,7 @@ void e2d::Text::draw(Renderer * renderer) const
|
||||||
_style.hasOutline,
|
_style.hasOutline,
|
||||||
(D2D1_COLOR_F)_style.outlineColor,
|
(D2D1_COLOR_F)_style.outlineColor,
|
||||||
_style.outlineWidth,
|
_style.outlineWidth,
|
||||||
D2D1_LINE_JOIN(_style.outlineJoin)
|
D2D1_LINE_JOIN(_style.outlineStroke)
|
||||||
);
|
);
|
||||||
_textLayout->Draw(nullptr, pTextRenderer, 0, 0);
|
_textLayout->Draw(nullptr, pTextRenderer, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ enum class Direction : int
|
||||||
|
|
||||||
|
|
||||||
// ÏßÌõÏཻÑùʽ
|
// ÏßÌõÏཻÑùʽ
|
||||||
enum class LineJoin : int
|
enum class Stroke : int
|
||||||
{
|
{
|
||||||
Miter = 0, /* бÇÐ */
|
Miter = 0, /* бÇÐ */
|
||||||
Bevel = 1, /* б½Ç */
|
Bevel = 1, /* б½Ç */
|
||||||
|
|
|
||||||
104
core/e2dnode.h
104
core/e2dnode.h
|
|
@ -446,18 +446,18 @@ protected:
|
||||||
E2D_DISABLE_COPY(Node);
|
E2D_DISABLE_COPY(Node);
|
||||||
|
|
||||||
// 设置节点所在场景
|
// 设置节点所在场景
|
||||||
virtual void _setParentScene(
|
void _setParentScene(
|
||||||
Scene * scene
|
Scene * scene
|
||||||
);
|
);
|
||||||
|
|
||||||
// 子节点排序
|
// 子节点排序
|
||||||
virtual void _sortChildren();
|
void _sortChildren();
|
||||||
|
|
||||||
// 更新转换矩阵
|
// 更新转换矩阵
|
||||||
void _updateTransform();
|
void _updateTransform();
|
||||||
|
|
||||||
// 更新节点透明度
|
// 更新节点透明度
|
||||||
virtual void _updateOpacity();
|
void _updateOpacity();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
String _name;
|
String _name;
|
||||||
|
|
@ -632,7 +632,7 @@ public:
|
||||||
bool hasOutline; // 显示描边
|
bool hasOutline; // 显示描边
|
||||||
Color outlineColor; // 描边颜色
|
Color outlineColor; // 描边颜色
|
||||||
float outlineWidth; // 描边线宽
|
float outlineWidth; // 描边线宽
|
||||||
LineJoin outlineJoin; // Ãè±ßÏßÏཻÑùʽ
|
Stroke outlineStroke; // 描边线相交样式
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Style();
|
Style();
|
||||||
|
|
@ -648,7 +648,7 @@ public:
|
||||||
bool hasOutline = true,
|
bool hasOutline = true,
|
||||||
Color outlineColor = Color(Color::Black, 0.5),
|
Color outlineColor = Color(Color::Black, 0.5),
|
||||||
float outlineWidth = 1.f,
|
float outlineWidth = 1.f,
|
||||||
LineJoin outlineJoin = LineJoin::Round
|
Stroke outlineStroke = Stroke::Round
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -691,7 +691,7 @@ public:
|
||||||
float getOutlineWidth() const;
|
float getOutlineWidth() const;
|
||||||
|
|
||||||
// 获取描边线相交样式
|
// 获取描边线相交样式
|
||||||
LineJoin getOutlineJoin() const;
|
Stroke getOutlineStroke() const;
|
||||||
|
|
||||||
// 获取文本显示行数
|
// 获取文本显示行数
|
||||||
int getLineCount() const;
|
int getLineCount() const;
|
||||||
|
|
@ -794,8 +794,8 @@ public:
|
||||||
);
|
);
|
||||||
|
|
||||||
// 设置描边线相交样式
|
// 设置描边线相交样式
|
||||||
void setOutlineJoin(
|
void setOutlineStroke(
|
||||||
LineJoin outlineJoin
|
Stroke outlineStroke
|
||||||
);
|
);
|
||||||
|
|
||||||
// 渲染文字
|
// 渲染文字
|
||||||
|
|
@ -1095,4 +1095,92 @@ protected:
|
||||||
std::vector<Button*> _buttons;
|
std::vector<Button*> _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;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -79,6 +79,7 @@
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
||||||
|
<ClCompile Include="..\..\core\Node\Canvas.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Node.cpp" />
|
<ClCompile Include="..\..\core\Node\Node.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -235,5 +235,8 @@
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp">
|
<ClCompile Include="..\..\core\Node\Scene.cpp">
|
||||||
<Filter>Node</Filter>
|
<Filter>Node</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\core\Node\Canvas.cpp">
|
||||||
|
<Filter>Node</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -223,6 +223,7 @@
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
||||||
|
<ClCompile Include="..\..\core\Node\Canvas.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Node.cpp" />
|
<ClCompile Include="..\..\core\Node\Node.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -235,5 +235,8 @@
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp">
|
<ClCompile Include="..\..\core\Node\Scene.cpp">
|
||||||
<Filter>Node</Filter>
|
<Filter>Node</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\core\Node\Canvas.cpp">
|
||||||
|
<Filter>Node</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -243,6 +243,7 @@
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
||||||
|
<ClCompile Include="..\..\core\Node\Canvas.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\ToggleButton.cpp" />
|
<ClCompile Include="..\..\core\Node\ToggleButton.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,9 @@
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp">
|
<ClCompile Include="..\..\core\Node\Scene.cpp">
|
||||||
<Filter>Node</Filter>
|
<Filter>Node</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\core\Node\Canvas.cpp">
|
||||||
|
<Filter>Node</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\core\easy2d.h" />
|
<ClInclude Include="..\..\core\easy2d.h" />
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue