From 486ca4f848890dcac43f4786038ae2ebd6f14081 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sun, 1 Apr 2018 00:04:33 +0800 Subject: [PATCH] update Shape --- core/Action/ActionSequence.cpp | 8 +-- core/Action/Animation.cpp | 9 +++ core/Common/Scene.cpp | 8 +++ core/Manager/CollisionManager.cpp | 24 +++++--- core/Node/Node.cpp | 12 ++-- core/Shape/Shape.cpp | 22 +++---- core/eactions.h | 22 +++---- core/ecommon.h | 99 ++++++------------------------- core/emanagers.h | 21 ++++--- core/enodes.h | 11 ++-- core/eshape.h | 77 +++++++++++++++++++++++- core/etools.h | 1 + 12 files changed, 174 insertions(+), 140 deletions(-) diff --git a/core/Action/ActionSequence.cpp b/core/Action/ActionSequence.cpp index 84ee4a97..265fde84 100644 --- a/core/Action/ActionSequence.cpp +++ b/core/Action/ActionSequence.cpp @@ -1,12 +1,12 @@ #include "..\eactions.h" -e2d::ActionSequence::ActionSequence() : - m_nActionIndex(0) +e2d::ActionSequence::ActionSequence() + : m_nActionIndex(0) { } -e2d::ActionSequence::ActionSequence(const std::initializer_list& vActions) : - m_nActionIndex(0) +e2d::ActionSequence::ActionSequence(const std::initializer_list& vActions) + : m_nActionIndex(0) { this->add(vActions); } diff --git a/core/Action/Animation.cpp b/core/Action/Animation.cpp index 7529bc3f..612cf9dc 100644 --- a/core/Action/Animation.cpp +++ b/core/Action/Animation.cpp @@ -13,6 +13,15 @@ e2d::Animation::Animation(double interval) } e2d::Animation::Animation(const std::initializer_list& vImages) + : m_nFrameIndex(0) + , m_fInterval(1) +{ + this->add(vImages); +} + +e2d::Animation::Animation(double interval, const std::initializer_list& vImages) + : m_nFrameIndex(0) + , m_fInterval(interval) { this->add(vImages); } diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp index c9aa7a83..59b544ae 100644 --- a/core/Common/Scene.cpp +++ b/core/Common/Scene.cpp @@ -53,6 +53,14 @@ void e2d::Scene::add(Node * child, int order /* = 0 */) m_pRoot->addChild(child, order); } +void e2d::Scene::add(const std::initializer_list& vNodes, int order) +{ + for (const auto &node : vNodes) + { + this->add(node, order); + } +} + bool e2d::Scene::remove(Node * child) { return m_pRoot->removeChild(child); diff --git a/core/Manager/CollisionManager.cpp b/core/Manager/CollisionManager.cpp index 3c77c152..580deef2 100644 --- a/core/Manager/CollisionManager.cpp +++ b/core/Manager/CollisionManager.cpp @@ -4,7 +4,7 @@ #include "..\etools.h" // 形状集合 -static std::vector s_vShapes; +static std::vector s_vShapes; // 监听器容器 static std::vector s_vListeners; // 碰撞触发状态 @@ -40,7 +40,7 @@ void e2d::CollisionManager::__update() } } -void e2d::CollisionManager::__updateShape(e2d::Shape * pActiveShape) +void e2d::CollisionManager::__updateShape(e2d::ShapeBase * pActiveShape) { // 判断碰撞触发是否打开 if (!s_bCollisionEnable) @@ -206,17 +206,25 @@ std::vector e2d::CollisionManager::getAll() return s_vListeners; } -e2d::Node * e2d::CollisionManager::getNode1() +e2d::Node* e2d::CollisionManager::isCausedBy(Node * pNode) { - return s_pActiveNode; + if (s_pActiveNode == pNode) + return s_pPassiveNode; + if (s_pPassiveNode == pNode) + return s_pActiveNode; + return nullptr; } -e2d::Node * e2d::CollisionManager::getNode2() +e2d::Node* e2d::CollisionManager::isCausedBy(String name) { - return s_pPassiveNode; + if (s_pActiveNode->getName() == name) + return s_pActiveNode; + if (s_pPassiveNode->getName() == name) + return s_pPassiveNode; + return nullptr; } -void e2d::CollisionManager::__addShape(Shape * pShape) +void e2d::CollisionManager::__addShape(ShapeBase * pShape) { if (pShape) { @@ -230,7 +238,7 @@ void e2d::CollisionManager::__addShape(Shape * pShape) } } -void e2d::CollisionManager::__removeShape(Shape * pShape) +void e2d::CollisionManager::__removeShape(ShapeBase * pShape) { if (pShape) { diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 91f8e80f..2d5a72ef 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -378,7 +378,7 @@ double e2d::Node::getOpacity() const return m_fRealOpacity; } -e2d::Shape * e2d::Node::getShape() const +e2d::ShapeBase * e2d::Node::getShape() const { return m_pShape; } @@ -547,25 +547,25 @@ void e2d::Node::setSize(Size size) this->setSize(size.width, size.height); } -void e2d::Node::setShape(Shape::TYPE type) +void e2d::Node::setShape(Shape type) { switch (type) { - case Shape::TYPE::RECTANGLE: + case Shape::RECTANGLE: { auto rect = new ShapeRectangle(this); this->setShape(rect); break; } - case Shape::TYPE::CIRCLE: + case Shape::CIRCLE: { auto rect = new ShapeCircle(this); this->setShape(rect); break; } - case Shape::TYPE::ELLIPSE: + case Shape::ELLIPSE: { auto rect = new ShapeEllipse(this); this->setShape(rect); @@ -577,7 +577,7 @@ void e2d::Node::setShape(Shape::TYPE type) } } -void e2d::Node::setShape(Shape * pShape) +void e2d::Node::setShape(ShapeBase * pShape) { // 删除旧的形状 CollisionManager::__removeShape(m_pShape); diff --git a/core/Shape/Shape.cpp b/core/Shape/Shape.cpp index 3016a8a8..1b733cc5 100644 --- a/core/Shape/Shape.cpp +++ b/core/Shape/Shape.cpp @@ -2,7 +2,7 @@ #include "..\emanagers.h" #include "..\enodes.h" -e2d::Shape::Shape() +e2d::ShapeBase::ShapeBase() : m_bIsVisiable(true) , m_nColor(Color::RED) , m_fOpacity(1) @@ -13,42 +13,42 @@ e2d::Shape::Shape() { } -e2d::Shape::~Shape() +e2d::ShapeBase::~ShapeBase() { SafeReleaseInterface(&m_pTransformedShape); } -e2d::Node * e2d::Shape::getParentNode() const +e2d::Node * e2d::ShapeBase::getParentNode() const { return m_pParentNode; } -void e2d::Shape::setEnable(bool bEnable) +void e2d::ShapeBase::setEnable(bool bEnable) { m_bEnable = bEnable; } -void e2d::Shape::setVisiable(bool bVisiable) +void e2d::ShapeBase::setVisiable(bool bVisiable) { m_bIsVisiable = bVisiable; } -void e2d::Shape::setColor(UINT32 color) +void e2d::ShapeBase::setColor(UINT32 color) { m_nColor = color; } -void e2d::Shape::setOpacity(double opacity) +void e2d::ShapeBase::setOpacity(double opacity) { m_fOpacity = min(max(static_cast(opacity), 0), 1); } -void e2d::Shape::setAutoResize(bool bEnable) +void e2d::ShapeBase::setAutoResize(bool bEnable) { m_bAutoResize = bEnable; } -void e2d::Shape::_render() +void e2d::ShapeBase::_render() { if (m_pTransformedShape && m_bEnable) { @@ -65,7 +65,7 @@ void e2d::Shape::_render() } } -e2d::Relation e2d::Shape::getRelationWith(Shape * pShape) const +e2d::Relation e2d::ShapeBase::getRelationWith(ShapeBase * pShape) const { if (m_pTransformedShape && pShape->m_pTransformedShape) { @@ -85,7 +85,7 @@ e2d::Relation e2d::Shape::getRelationWith(Shape * pShape) const return Relation::UNKNOWN; } -void e2d::Shape::_transform() +void e2d::ShapeBase::_transform() { if (m_pParentNode && m_bEnable) { diff --git a/core/eactions.h b/core/eactions.h index 44eec7a0..7dbbb7c1 100644 --- a/core/eactions.h +++ b/core/eactions.h @@ -517,7 +517,13 @@ public: // 创建帧动画 Animation( - const std::initializer_list& vImages + const std::initializer_list& vImages /* 关键帧数组 */ + ); + + // 创建特定帧间隔的帧动画 + Animation( + double interval, /* 帧间隔(秒) */ + const std::initializer_list& vImages /* 关键帧数组 */ ); virtual ~Animation(); @@ -763,12 +769,7 @@ namespace e2d inline e2d::ActionSequence * e2d::action::Sequence(const std::initializer_list& vActions) { - auto action = new (std::nothrow) ActionSequence(); - if (action) - { - action->add(vActions); - } - return action; + return new (std::nothrow) ActionSequence(vActions); } inline e2d::ActionDelay * e2d::action::Delay(double duration) @@ -783,12 +784,7 @@ namespace e2d inline e2d::Animation * e2d::action::Animate(double interval, const std::initializer_list& vFrames) { - auto animation = new (std::nothrow) Animation(interval); - if (animation) - { - animation->add(vFrames); - } - return animation; + return new (std::nothrow) Animation(interval, vFrames); } inline e2d::ActionFunc * e2d::action::Func(Function func) diff --git a/core/ecommon.h b/core/ecommon.h index c1b419de..a68ffd67 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -372,6 +372,15 @@ enum class Relation : int }; +// 形状类别 +enum class Shape : int +{ + RECTANGLE, /* 矩形 */ + CIRCLE, /* 圆形 */ + ELLIPSE /* 椭圆形 */ +}; + + // 文本样式 struct Font { @@ -545,8 +554,14 @@ public: // 添加节点到场景 void add( - Node * child, - int zOrder = 0 + Node * child, /* 要添加的节点 */ + int zOrder = 0 /* 渲染顺序 */ + ); + + // 添加节点到场景 + virtual void add( + const std::initializer_list& vNodes, /* 节点数组 */ + int order = 0 /* 渲染顺序 */ ); // 删除子节点 @@ -578,86 +593,6 @@ protected: }; -class CollisionManager; - -// 形状 -class Shape : - public Object -{ - friend CollisionManager; - friend Node; - -public: - // 形状类别 - enum class TYPE : int - { - RECTANGLE, /* 矩形 */ - CIRCLE, /* 圆形 */ - ELLIPSE /* 椭圆形 */ - }; - -public: - Shape(); - - virtual ~Shape(); - - // 判断两形状的交集关系 - virtual Relation getRelationWith( - Shape * pShape - ) const; - - // 获取父节点 - Node * getParentNode() const; - - // 启用或关闭该形状 - 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_nColor; - float m_fOpacity; - Node * m_pParentNode; - ID2D1TransformedGeometry * m_pTransformedShape; -}; - - // String 类模板函数定义 template inline e2d::String e2d::String::toString(T value) diff --git a/core/emanagers.h b/core/emanagers.h index 7965a7a3..fcce3c4d 100644 --- a/core/emanagers.h +++ b/core/emanagers.h @@ -12,6 +12,7 @@ class Node; class Timer; class Action; class Music; +class ShapeBase; class Transition; class InputListener; class CollisionListener; @@ -354,7 +355,7 @@ private: class CollisionManager { friend Node; - friend Shape; + friend ShapeBase; friend CollisionListener; public: @@ -401,11 +402,15 @@ public: // 获取全部监听器 static std::vector getAll(); - // 获取发生碰撞的节点 1 - static Node* getNode1(); + // 判断碰撞是否由该节点引发(如果是,返回与其相撞的节点指针,否则返回空) + static Node* isCausedBy( + Node * pNode + ); - // 获取发生碰撞的节点 2 - static Node* getNode2(); + // 判断发生碰撞的节点名称是否相同(若相同返回其指针,否则返回空) + static Node* isCausedBy( + String name + ); private: // 添加碰撞监听 @@ -418,17 +423,17 @@ private: // 更新形状 static void __updateShape( - Shape * pActiveShape + ShapeBase * pActiveShape ); // 添加形状 static void __addShape( - Shape * pShape + ShapeBase * pShape ); // 删除已绑定的形状 static void __removeShape( - Shape * pShape + ShapeBase * pShape ); }; diff --git a/core/enodes.h b/core/enodes.h index 835ee864..ff657a95 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -7,13 +7,14 @@ namespace e2d class Action; class Transition; +class ShapeBase; class CollisionManager; class Node : public Object { friend Scene; - friend Shape; + friend ShapeBase; friend Transition; friend CollisionManager; @@ -116,7 +117,7 @@ public: virtual double getOpacity() const; // 获取节点形状 - virtual Shape * getShape() const; + virtual ShapeBase * getShape() const; // 获取父节点 virtual Node * getParent() const; @@ -312,12 +313,12 @@ public: // 设置节点形状 virtual void setShape( - Shape::TYPE type + Shape type ); // 设置节点形状 virtual void setShape( - Shape * pShape + ShapeBase * pShape ); // 添加可碰撞节点的名称 @@ -455,7 +456,7 @@ protected: bool m_bDisplayedInScene; bool m_bSortChildrenNeeded; bool m_bTransformNeeded; - Shape * m_pShape; + ShapeBase * m_pShape; Scene * m_pParentScene; Node * m_pParent; D2D1::Matrix3x2F m_MatriInitial; diff --git a/core/eshape.h b/core/eshape.h index 9f9e4bd2..2ff4e290 100644 --- a/core/eshape.h +++ b/core/eshape.h @@ -6,9 +6,80 @@ namespace e2d { +class CollisionManager; + +// 形状 +class ShapeBase : + public Object +{ + friend CollisionManager; + friend Node; + +public: + ShapeBase(); + + virtual ~ShapeBase(); + + // 判断两形状的交集关系 + virtual Relation getRelationWith( + ShapeBase * pShape + ) const; + + // 获取父节点 + Node * getParentNode() const; + + // 启用或关闭该形状 + 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_nColor; + float m_fOpacity; + Node * m_pParentNode; + ID2D1TransformedGeometry * m_pTransformedShape; +}; + + // 矩形 class ShapeRectangle : - public Shape + public ShapeBase { public: // 创建一个默认矩形 @@ -51,7 +122,7 @@ protected: // 圆形 class ShapeCircle : - public Shape + public ShapeBase { public: // 创建一个默认圆形 @@ -90,7 +161,7 @@ protected: // 椭圆形 class ShapeEllipse : - public Shape + public ShapeBase { public: // 创建一个默认椭圆 diff --git a/core/etools.h b/core/etools.h index 5334623e..64ca1dd1 100644 --- a/core/etools.h +++ b/core/etools.h @@ -8,6 +8,7 @@ namespace e2d class TimerManager; class MusicManager; class InputManager; +class CollisionManager; // 随机数产生器 class Random