update Shape

Now you can directly set the shape of nodes by Shape::TYPE. The subclasses of Shape are prefixed with 'Shape'.
This commit is contained in:
Nomango 2018-03-31 11:59:51 +08:00
parent 4b677e727e
commit e78f8795e7
9 changed files with 217 additions and 167 deletions

View File

@ -39,7 +39,7 @@ e2d::Node::Node()
{
if (s_fDefaultShapeEnabled)
{
auto rect = new Rect(this);
auto rect = new ShapeRectangle(this);
this->setShape(rect);
}
}
@ -542,6 +542,36 @@ void e2d::Node::setSize(Size size)
this->setSize(size.width, size.height);
}
void e2d::Node::setShape(Shape::TYPE type)
{
switch (type)
{
case Shape::TYPE::RECTANGLE:
{
auto rect = new ShapeRectangle(this);
this->setShape(rect);
break;
}
case Shape::TYPE::CIRCLE:
{
auto rect = new ShapeCircle(this);
this->setShape(rect);
break;
}
case Shape::TYPE::ELLIPSE:
{
auto rect = new ShapeEllipse(this);
this->setShape(rect);
break;
}
default:
break;
}
}
void e2d::Node::setShape(Shape * pShape)
{
// 删除旧的形状

View File

@ -1,22 +1,22 @@
#include "..\eshape.h"
#include "..\enodes.h"
e2d::Circle::Circle()
e2d::ShapeCircle::ShapeCircle()
: m_pD2dCircle(nullptr)
{
}
e2d::Circle::Circle(Point center, double radius)
e2d::ShapeCircle::ShapeCircle(Point center, double radius)
: m_pD2dCircle(nullptr)
{
this->_setCircle(center, radius);
this->setCircle(center, radius);
}
e2d::Circle::Circle(Node * node)
e2d::ShapeCircle::ShapeCircle(Node * node)
: m_pD2dCircle(nullptr)
{
double minSide = min(node->getRealWidth(), node->getRealHeight());
this->_setCircle(
this->setCircle(
Point(
node->getRealWidth() / 2,
node->getRealHeight() / 2
@ -25,12 +25,12 @@ e2d::Circle::Circle(Node * node)
);
}
e2d::Circle::~Circle()
e2d::ShapeCircle::~ShapeCircle()
{
SafeReleaseInterface(&m_pD2dCircle);
}
void e2d::Circle::_setCircle(Point center, double radius)
void e2d::ShapeCircle::setCircle(Point center, double radius)
{
SafeReleaseInterface(&m_pD2dCircle);
@ -45,12 +45,12 @@ void e2d::Circle::_setCircle(Point center, double radius)
);
}
void e2d::Circle::_resize()
void e2d::ShapeCircle::_resize()
{
if (m_pParentNode && m_bEnable)
{
double minSide = min(m_pParentNode->getRealWidth(), m_pParentNode->getRealHeight());
this->_setCircle(
this->setCircle(
Point(
m_pParentNode->getRealWidth() / 2,
m_pParentNode->getRealHeight() / 2
@ -60,7 +60,7 @@ void e2d::Circle::_resize()
}
}
ID2D1EllipseGeometry * e2d::Circle::getD2dGeometry() const
ID2D1EllipseGeometry * e2d::ShapeCircle::getD2dGeometry() const
{
return m_pD2dCircle;
}

View File

@ -1,21 +1,21 @@
#include "..\eshape.h"
#include "..\enodes.h"
e2d::Ellipse::Ellipse()
e2d::ShapeEllipse::ShapeEllipse()
: m_pD2dEllipse(nullptr)
{
}
e2d::Ellipse::Ellipse(Point center, double radiusX, double radiusY)
e2d::ShapeEllipse::ShapeEllipse(Point center, double radiusX, double radiusY)
: m_pD2dEllipse(nullptr)
{
this->_setEllipse(center, radiusX, radiusY);
this->setEllipse(center, radiusX, radiusY);
}
e2d::Ellipse::Ellipse(Node * node)
e2d::ShapeEllipse::ShapeEllipse(Node * node)
: m_pD2dEllipse(nullptr)
{
this->_setEllipse(
this->setEllipse(
Point(
node->getWidth() / 2,
node->getHeight() / 2
@ -25,12 +25,12 @@ e2d::Ellipse::Ellipse(Node * node)
);
}
e2d::Ellipse::~Ellipse()
e2d::ShapeEllipse::~ShapeEllipse()
{
SafeReleaseInterface(&m_pD2dEllipse);
}
void e2d::Ellipse::_setEllipse(Point center, double radiusX, double radiusY)
void e2d::ShapeEllipse::setEllipse(Point center, double radiusX, double radiusY)
{
SafeReleaseInterface(&m_pD2dEllipse);
@ -45,11 +45,11 @@ void e2d::Ellipse::_setEllipse(Point center, double radiusX, double radiusY)
);
}
void e2d::Ellipse::_resize()
void e2d::ShapeEllipse::_resize()
{
if (m_pParentNode && m_bEnable)
{
this->_setEllipse(
this->setEllipse(
Point(
m_pParentNode->getWidth() / 2,
m_pParentNode->getHeight() / 2
@ -60,7 +60,7 @@ void e2d::Ellipse::_resize()
}
}
ID2D1EllipseGeometry * e2d::Ellipse::getD2dGeometry() const
ID2D1EllipseGeometry * e2d::ShapeEllipse::getD2dGeometry() const
{
return m_pD2dEllipse;
}

View File

@ -1,21 +1,21 @@
#include "..\eshape.h"
#include "..\enodes.h"
e2d::Rect::Rect()
e2d::ShapeRectangle::ShapeRectangle()
: m_pD2dRectangle(nullptr)
{
}
e2d::Rect::Rect(double x, double y, double width, double height)
e2d::ShapeRectangle::ShapeRectangle(double x, double y, double width, double height)
: m_pD2dRectangle(nullptr)
{
this->_setRect(x, y, x + width, y + height);
this->setRect(x, y, x + width, y + height);
}
e2d::Rect::Rect(Node * node)
e2d::ShapeRectangle::ShapeRectangle(Node * node)
: m_pD2dRectangle(nullptr)
{
this->_setRect(
this->setRect(
0,
0,
node->getRealWidth(),
@ -23,12 +23,12 @@ e2d::Rect::Rect(Node * node)
);
}
e2d::Rect::~Rect()
e2d::ShapeRectangle::~ShapeRectangle()
{
SafeReleaseInterface(&m_pD2dRectangle);
}
void e2d::Rect::_setRect(double left, double top, double right, double bottom)
void e2d::ShapeRectangle::setRect(double left, double top, double right, double bottom)
{
SafeReleaseInterface(&m_pD2dRectangle);
@ -42,11 +42,11 @@ void e2d::Rect::_setRect(double left, double top, double right, double bottom)
);
}
void e2d::Rect::_resize()
void e2d::ShapeRectangle::_resize()
{
if (m_pParentNode && m_bEnable)
{
this->_setRect(
this->setRect(
0,
0,
m_pParentNode->getRealWidth(),
@ -55,7 +55,7 @@ void e2d::Rect::_resize()
}
}
ID2D1RectangleGeometry * e2d::Rect::getD2dGeometry() const
ID2D1RectangleGeometry * e2d::ShapeRectangle::getD2dGeometry() const
{
return m_pD2dRectangle;
}

View File

@ -648,6 +648,104 @@ protected:
};
class ShapeManager;
// 形状
class Shape :
public Object
{
friend ShapeManager;
friend Node;
public:
// 形状类别
enum TYPE
{
RECTANGLE, /* 矩形 */
CIRCLE, /* 圆形 */
ELLIPSE /* 椭圆形 */
};
public:
Shape();
virtual ~Shape();
// 判断两形状的交集关系
virtual int getRelationWith(
Shape * pShape
) const;
// 获取父节点
Node * getParentNode() const;
// 获取类别掩码
UINT32 getCategoryBitmask() const;
// 获取冲突掩码
UINT32 getCollisionBitmask() const;
// 设置类别掩码
void setCategoryBitmask(
UINT32 mask
);
// 设置冲突掩码
void setCollisionBitmask(
UINT32 mask
);
// 启用或关闭该形状
virtual void setEnable(
bool bEnable
);
// 设置形状的可见性
void setVisiable(
bool bVisiable
);
// 设置绘制颜色
void setColor(
UINT32 color
);
// 设置绘制透明度
void setOpacity(
double opacity
);
// 设置大小跟随
void setAutoResize(
bool bEnable
);
// 获取 ID2D1Geometry 对象
virtual ID2D1Geometry * getD2dGeometry() const = 0;
protected:
// 转换形状
virtual void _transform();
// 重设大小
virtual void _resize() = 0;
// 渲染形状
virtual void _render();
protected:
bool m_bEnable;
bool m_bIsVisiable;
bool m_bAutoResize;
UINT32 m_nCategoryBitmask;
UINT32 m_nCollisionBitmask;
UINT32 m_nColor;
float m_fOpacity;
Node * m_pParentNode;
ID2D1TransformedGeometry * m_pTransformedShape;
};
// String 类模板函数定义
template<typename T>
inline e2d::String e2d::String::toString(T value)

View File

@ -6,7 +6,6 @@ namespace e2d
class Action;
class Shape;
class Transition;
class Node :
@ -307,6 +306,11 @@ public:
Size size
);
// 设置节点形状
virtual void setShape(
Shape::TYPE type
);
// 设置节点形状
virtual void setShape(
Shape * pShape
@ -426,7 +430,7 @@ protected:
bool m_bDisplayedInScene;
bool m_bSortChildrenNeeded;
bool m_bTransformNeeded;
Shape * m_pShape;
Shape * m_pShape;
Scene * m_pParentScene;
Node * m_pParent;
D2D1::Matrix3x2F m_MatriInitial;
@ -704,6 +708,7 @@ public:
virtual void onFixedUpdate() override;
protected:
// 按钮状态枚举
enum BTN_STATE { NORMAL, MOUSEOVER, SELECTED };
// 设置按钮状态

View File

@ -5,105 +5,17 @@
namespace e2d
{
class ShapeManager;
class Node;
class Shape :
public Object
{
friend ShapeManager;
friend Node;
public:
Shape();
virtual ~Shape();
// 判断两形状的交集关系
virtual int getRelationWith(
Shape * pShape
) const;
// 获取父节点
Node * getParentNode() const;
// 获取类别掩码
UINT32 getCategoryBitmask() const;
// 获取冲突掩码
UINT32 getCollisionBitmask() const;
// 设置类别掩码
void setCategoryBitmask(
UINT32 mask
);
// 设置冲突掩码
void setCollisionBitmask(
UINT32 mask
);
// 启用或关闭该形状
virtual void setEnable(
bool bEnable
);
// 设置形状的可见性
void setVisiable(
bool bVisiable
);
// 设置绘制颜色
void setColor(
UINT32 color
);
// 设置绘制透明度
void setOpacity(
double opacity
);
// 设置大小跟随
void setAutoResize(
bool bEnable
);
// 获取 ID2D1Geometry 对象
virtual ID2D1Geometry * getD2dGeometry() const = 0;
protected:
// 转换形状
virtual void _transform();
// 重设大小
virtual void _resize() = 0;
// 渲染形状
virtual void _render();
protected:
bool m_bEnable;
bool m_bIsVisiable;
bool m_bAutoResize;
UINT32 m_nCategoryBitmask;
UINT32 m_nCollisionBitmask;
UINT32 m_nColor;
float m_fOpacity;
Node * m_pParentNode;
ID2D1TransformedGeometry * m_pTransformedShape;
};
class Rect :
// 矩形
class ShapeRectangle :
public Shape
{
public:
// 创建一个矩形
Rect();
// 创建一个默认矩形
ShapeRectangle();
// 根据左上角坐标和宽高创建矩形
Rect(
ShapeRectangle(
double x,
double y,
double width,
@ -111,23 +23,24 @@ public:
);
// 创建一个和节点位置大小相同的矩形
Rect(
ShapeRectangle(
Node * node
);
virtual ~Rect();
virtual ~ShapeRectangle();
// 获取 ID2D1Geometry 对象
virtual ID2D1RectangleGeometry * getD2dGeometry() const override;
protected:
void _setRect(
// 修改矩形大小
void setRect(
double left,
double top,
double right,
double bottom
);
// 获取 ID2D1Geometry 对象
virtual ID2D1RectangleGeometry * getD2dGeometry() const override;
protected:
// 重设大小
virtual void _resize();
@ -136,35 +49,37 @@ protected:
};
class Circle :
// 圆形
class ShapeCircle :
public Shape
{
public:
// 创建一个空的圆形
Circle();
// 创建一个默认圆形
ShapeCircle();
// 根据圆心和半径创建圆形
Circle(
ShapeCircle(
Point center,
double radius
);
// 创建一个和节点位置大小相同的圆形
Circle(
ShapeCircle(
Node * node
);
virtual ~Circle();
virtual ~ShapeCircle();
// 修改圆形大小
void setCircle(
Point center,
double radius
);
// 获取 ID2D1Geometry 对象
virtual ID2D1EllipseGeometry * getD2dGeometry() const override;
protected:
void _setCircle(
Point center,
double radius
);
// 重设大小
virtual void _resize();
@ -173,37 +88,39 @@ protected:
};
class Ellipse :
// 椭圆形
class ShapeEllipse :
public Shape
{
public:
// 创建一个空的椭圆
Ellipse();
// 创建一个默认椭圆
ShapeEllipse();
// 根据圆心和半径创建椭圆
Ellipse(
ShapeEllipse(
Point center,
double radiusX,
double radiusY
);
// 创建一个和节点位置大小相同的椭圆
Ellipse(
ShapeEllipse(
Node * node
);
virtual ~Ellipse();
virtual ~ShapeEllipse();
// 获取 ID2D1Geometry 对象
virtual ID2D1EllipseGeometry * getD2dGeometry() const override;
protected:
void _setEllipse(
// 修改椭圆大小
void setEllipse(
Point center,
double radiusX,
double radiusY
);
// 获取 ID2D1Geometry 对象
virtual ID2D1EllipseGeometry * getD2dGeometry() const override;
protected:
// 重设大小
virtual void _resize();

View File

@ -231,9 +231,9 @@
<ClCompile Include="..\..\core\Node\Node.cpp" />
<ClCompile Include="..\..\core\Node\Sprite.cpp" />
<ClCompile Include="..\..\core\Node\Text.cpp" />
<ClCompile Include="..\..\core\Shape\Circle.cpp" />
<ClCompile Include="..\..\core\Shape\Ellipse.cpp" />
<ClCompile Include="..\..\core\Shape\Rect.cpp" />
<ClCompile Include="..\..\core\Shape\ShapeCircle.cpp" />
<ClCompile Include="..\..\core\Shape\ShapeEllipse.cpp" />
<ClCompile Include="..\..\core\Shape\ShapeRectangle.cpp" />
<ClCompile Include="..\..\core\Shape\Shape.cpp" />
<ClCompile Include="..\..\core\Tool\Data.cpp" />
<ClCompile Include="..\..\core\Tool\Path.cpp" />

View File

@ -150,18 +150,9 @@
<ClCompile Include="..\..\core\Manager\ShapeManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\Circle.cpp">
<Filter>Shape</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\Ellipse.cpp">
<Filter>Shape</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\Shape.cpp">
<Filter>Shape</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\Rect.cpp">
<Filter>Shape</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Point.cpp">
<Filter>Common</Filter>
</ClCompile>
@ -189,6 +180,15 @@
<ClCompile Include="..\..\core\Tool\Path.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\ShapeRectangle.cpp">
<Filter>Shape</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\ShapeEllipse.cpp">
<Filter>Shape</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\ShapeCircle.cpp">
<Filter>Shape</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\etools.h" />