From 7f402c75933c5004612e4ddd00142bef683bb292 Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Sat, 14 Oct 2017 11:40:47 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0ENode?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ConsoleDemo/ConsoleDemo.vcxproj | 2 +-
Demo/Demo.vcxproj | 6 +-
Demo/main.cpp | 13 +--
Easy2D/Base/EScene.cpp | 2 +-
Easy2D/Easy2D.vcxproj | 8 +-
Easy2D/Easy2D.vcxproj.filters | 12 +-
...EKeyListener.cpp => EKeyboardListener.cpp} | 104 +++++++++---------
Easy2D/Msg/Listener/EMouseClickListener.cpp | 3 +-
Easy2D/Node/ENode.cpp | 85 +++++++++++---
Easy2D/emacros.h | 4 +-
Easy2D/emsg.h | 16 +--
Easy2D/enodes.h | 67 ++++++-----
12 files changed, 196 insertions(+), 126 deletions(-)
rename Easy2D/Msg/Listener/{EKeyListener.cpp => EKeyboardListener.cpp} (85%)
diff --git a/ConsoleDemo/ConsoleDemo.vcxproj b/ConsoleDemo/ConsoleDemo.vcxproj
index 5d8465c9..8811716c 100644
--- a/ConsoleDemo/ConsoleDemo.vcxproj
+++ b/ConsoleDemo/ConsoleDemo.vcxproj
@@ -23,7 +23,7 @@
{70931955-FE2D-4A50-93C6-6955A730B0FE}
Win32Proj
ConsoleDemo
- 10.0.15063.0
+ 10.0.16299.0
diff --git a/Demo/Demo.vcxproj b/Demo/Demo.vcxproj
index 2440b382..a61d8f52 100644
--- a/Demo/Demo.vcxproj
+++ b/Demo/Demo.vcxproj
@@ -23,14 +23,14 @@
{9D85A92F-BCCE-4EF0-BAD3-601C0086661C}
Win32Proj
Demo
- 10.0.15063.0
+ 10.0.16299.0
Application
true
- v141
Unicode
+ v141
Application
@@ -42,7 +42,7 @@
Application
true
- v140
+ v141
Unicode
diff --git a/Demo/main.cpp b/Demo/main.cpp
index 8f792c0b..21517e10 100644
--- a/Demo/main.cpp
+++ b/Demo/main.cpp
@@ -19,14 +19,11 @@ int WINAPI WinMain(
node->setSize(30, 180);
scene->add(node);
- auto listener = new EMouseClickListener([=](EPoint) {
- if (EMouseMsg::getMsg() == EMouseMsg::MOUSE_MSG::MOVE)
- {
- node->setPos(EMouseMsg::getPos());
- }
+ auto mlistener = new EMouseClickListener([=](EPoint) {
+ node->setPos(EMouseMsg::getPos());
});
- auto listener = new EKeyPressListener([=] {
+ auto klistener = new EKeyPressListener([=] {
if (EKeyMsg::isCapitalLockOn())
{
if (EKeyMsg::getVal() == EKeyMsg::KEY::LEFT)
@@ -40,9 +37,9 @@ int WINAPI WinMain(
}
});
- listener->bindWithNode(node);
+ mlistener->bindWith(node);
- scene->bindListener(listener);
+ scene->bindListener(klistener);
app.enterScene(scene);
diff --git a/Easy2D/Base/EScene.cpp b/Easy2D/Base/EScene.cpp
index 6e47ca8b..50b69dd5 100644
--- a/Easy2D/Base/EScene.cpp
+++ b/Easy2D/Base/EScene.cpp
@@ -46,7 +46,7 @@ void e2d::EScene::add(ENode * child, int zOrder /* = 0 */)
// 忽略空指针
if (child == nullptr) return;
// 设置节点的父场景
- child->bindWithScene(this);
+ child->setParentScene(this);
// 设置 z 轴顺序
child->setZOrder(zOrder);
// 对象的引用计数加一
diff --git a/Easy2D/Easy2D.vcxproj b/Easy2D/Easy2D.vcxproj
index 0f4bdf91..8873f141 100644
--- a/Easy2D/Easy2D.vcxproj
+++ b/Easy2D/Easy2D.vcxproj
@@ -23,14 +23,14 @@
{FF7F943D-A89C-4E6C-97CF-84F7D8FF8EDF}
Win32Proj
Easy2D
- 10.0.15063.0
+ 10.0.16299.0
StaticLibrary
true
- v141
Unicode
+ v141
StaticLibrary
@@ -42,7 +42,7 @@
StaticLibrary
true
- v140
+ v141
Unicode
@@ -196,7 +196,7 @@
-
+
diff --git a/Easy2D/Easy2D.vcxproj.filters b/Easy2D/Easy2D.vcxproj.filters
index 5c6aded7..6b1a5adf 100644
--- a/Easy2D/Easy2D.vcxproj.filters
+++ b/Easy2D/Easy2D.vcxproj.filters
@@ -42,9 +42,6 @@
Msg
-
- Msg\Listener
-
Msg\Listener
@@ -57,12 +54,15 @@
Msg\Listener
-
- Msg\Listener
-
Tool
+
+ Msg\Listener
+
+
+ Msg\Listener
+
diff --git a/Easy2D/Msg/Listener/EKeyListener.cpp b/Easy2D/Msg/Listener/EKeyboardListener.cpp
similarity index 85%
rename from Easy2D/Msg/Listener/EKeyListener.cpp
rename to Easy2D/Msg/Listener/EKeyboardListener.cpp
index e9593725..8c467803 100644
--- a/Easy2D/Msg/Listener/EKeyListener.cpp
+++ b/Easy2D/Msg/Listener/EKeyboardListener.cpp
@@ -1,53 +1,53 @@
-#include "..\..\emsg.h"
-
-e2d::EKeyboardListener::EKeyboardListener()
- : EListener()
-{
-}
-
-e2d::EKeyboardListener::EKeyboardListener(EString name)
- : EListener(name)
-{
-}
-
-e2d::EKeyboardListener::EKeyboardListener(const KEY_LISTENER_CALLBACK & callback)
- : EListener()
-{
- m_callback = callback;
-}
-
-e2d::EKeyboardListener::EKeyboardListener(EString name, const KEY_LISTENER_CALLBACK & callback)
- : EListener(name)
-{
- m_callback = callback;
-}
-
-void e2d::EKeyboardListener::runCallback()
-{
- m_callback();
-}
-
-void e2d::EKeyboardListener::setCallback(const KEY_LISTENER_CALLBACK & callback)
-{
- m_callback = callback;
-}
-
-void e2d::EKeyboardListener::bindWithScene(EScene * pParentScene)
-{
- WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object.");
-
- if (pParentScene)
- {
- EMsgManager::bindListenerWith(this, pParentScene);
- }
-}
-
-void e2d::EKeyboardListener::bindWithNode(ENode * pParentNode)
-{
- WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object.");
-
- if (pParentNode != nullptr && m_pParentScene == nullptr)
- {
- EMsgManager::bindListenerWith(this, pParentNode);
- }
+#include "..\..\emsg.h"
+
+e2d::EKeyboardListener::EKeyboardListener()
+ : EListener()
+{
+}
+
+e2d::EKeyboardListener::EKeyboardListener(EString name)
+ : EListener(name)
+{
+}
+
+e2d::EKeyboardListener::EKeyboardListener(const KEY_LISTENER_CALLBACK & callback)
+ : EListener()
+{
+ m_callback = callback;
+}
+
+e2d::EKeyboardListener::EKeyboardListener(EString name, const KEY_LISTENER_CALLBACK & callback)
+ : EListener(name)
+{
+ m_callback = callback;
+}
+
+void e2d::EKeyboardListener::runCallback()
+{
+ m_callback();
+}
+
+void e2d::EKeyboardListener::setCallback(const KEY_LISTENER_CALLBACK & callback)
+{
+ m_callback = callback;
+}
+
+void e2d::EKeyboardListener::bindWith(EScene * pParentScene)
+{
+ WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object.");
+
+ if (pParentScene)
+ {
+ EMsgManager::bindListenerWith(this, pParentScene);
+ }
+}
+
+void e2d::EKeyboardListener::bindWith(ENode * pParentNode)
+{
+ WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object.");
+
+ if (pParentNode != nullptr && m_pParentScene == nullptr)
+ {
+ EMsgManager::bindListenerWith(this, pParentNode);
+ }
}
\ No newline at end of file
diff --git a/Easy2D/Msg/Listener/EMouseClickListener.cpp b/Easy2D/Msg/Listener/EMouseClickListener.cpp
index e324b338..951030a7 100644
--- a/Easy2D/Msg/Listener/EMouseClickListener.cpp
+++ b/Easy2D/Msg/Listener/EMouseClickListener.cpp
@@ -24,8 +24,7 @@ e2d::EMouseClickListener::EMouseClickListener(EString name, const MOUSE_CLICK_LI
void e2d::EMouseClickListener::runCallback()
{
- if (EMouseMsg::getMsg() == EMouseMsg::MOUSE_MSG::LBUTTON_DOWN ||
- EMouseMsg::getMsg() == EMouseMsg::MOUSE_MSG::LBUTTON_DBLCLK)
+ if (EMouseMsg::getMsg() == EMouseMsg::MOUSE_MSG::LBUTTON_UP)
{
m_callback(EMouseMsg::getPos());
}
diff --git a/Easy2D/Node/ENode.cpp b/Easy2D/Node/ENode.cpp
index a90ba6cc..3dd71628 100644
--- a/Easy2D/Node/ENode.cpp
+++ b/Easy2D/Node/ENode.cpp
@@ -4,30 +4,22 @@
e2d::ENode::ENode()
: m_nZOrder(0)
, m_bVisiable(true)
+ , m_pParent(nullptr)
+ , m_pParentScene(nullptr)
+ , m_nHashName(0)
{
}
-e2d::ENode::ENode(EPoint p)
+e2d::ENode::ENode(EString name)
: ENode()
{
- setPos(p);
-}
-
-e2d::ENode::ENode(int x, int y)
- : ENode()
-{
- setPos(x, y);
+ setName(name);
}
e2d::ENode::~ENode()
{
}
-bool e2d::ENode::_exec(bool active)
-{
- return false;
-}
-
void e2d::ENode::_onRender()
{
D2D1_RECT_F rectangle = D2D1::RectF(
@@ -173,6 +165,19 @@ void e2d::ENode::setParent(ENode * parent)
m_pParent = parent;
}
+void e2d::ENode::addChild(ENode * child)
+{
+ WARN_IF(child == nullptr, "NULL ENode pointer exception.");
+
+ if (child)
+ {
+ for (ENode * parent = this; parent != nullptr; parent = parent->getParent())
+ {
+ ASSERT(child != parent, "A ENode cannot be the child of his own children");
+ }
+ }
+}
+
e2d::ENode *& e2d::ENode::getParent()
{
return m_pParent;
@@ -183,16 +188,68 @@ e2d::EScene * &e2d::ENode::getParentScene()
return m_pParentScene;
}
-void e2d::ENode::bindWithScene(EScene * scene)
+std::vector& e2d::ENode::getChildren()
+{
+ return m_vChildren;
+}
+
+int e2d::ENode::getChildrenCount() const
+{
+ return m_vChildren.size();
+}
+
+e2d::ENode * e2d::ENode::getChild(EString name)
+{
+ WARN_IF(name.empty(), "Invalid ENode name.");
+
+ std::hash h;
+ size_t hash = h(name);
+
+ for (const auto& child : m_vChildren)
+ {
+ // 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度
+ if (child->m_nHashName == hash && child->m_sName == name)
+ return child;
+ }
+ return nullptr;
+}
+
+void e2d::ENode::setParentScene(EScene * scene)
{
m_pParentScene = scene;
}
+void e2d::ENode::removeFromParent(bool release)
+{
+}
+
+void e2d::ENode::removeChild(ENode * child, bool release)
+{
+}
+
+void e2d::ENode::removeChild(EString childName, bool release)
+{
+}
+
void e2d::ENode::setVisiable(bool value)
{
m_bVisiable = value;
}
+void e2d::ENode::setName(EString name)
+{
+ WARN_IF(name.empty(), "Invalid ENode name.");
+
+ if (!name.empty())
+ {
+ // 保存节点名
+ m_sName = name;
+ // 保存节点 Hash 名
+ std::hash h;
+ m_nHashName = h(name);
+ }
+}
+
bool e2d::ENode::isVisiable() const
{
return m_bVisiable;
diff --git a/Easy2D/emacros.h b/Easy2D/emacros.h
index 0b75b151..26073f69 100644
--- a/Easy2D/emacros.h
+++ b/Easy2D/emacros.h
@@ -20,12 +20,12 @@
// Windows Header Files:
#include
-
+#include
#ifndef ASSERT_IF
#if defined( DEBUG ) || defined( _DEBUG )
-#define ASSERT(b, m) do {if (!(b)) { fprintf(stderr, "Assert: " #m "\n"); abort(); }} while(0)
+#define ASSERT(b, m) do {if (!(b)) { fprintf(stderr, "Assert: " #m "\n"); assert(b); }} while(0)
#else
#define ASSERT(b, m) ((void)0)
#endif //DEBUG || _DEBUG
diff --git a/Easy2D/emsg.h b/Easy2D/emsg.h
index 603958d3..bc5554a8 100644
--- a/Easy2D/emsg.h
+++ b/Easy2D/emsg.h
@@ -183,12 +183,12 @@ public:
);
// 绑定监听器到场景
- virtual void bindWithScene(
+ virtual void bindWith(
EScene * pParentScene
) = 0;
// 绑定监听器到节点
- virtual void bindWithNode(
+ virtual void bindWith(
ENode * pParentNode
) = 0;
@@ -232,12 +232,12 @@ public:
// 绑定监听器到场景
virtual void bindWith(
EScene * pParentScene
- );
+ ) override;
// 绑定监听器到节点
virtual void bindWith(
ENode * pParentNode
- );
+ ) override;
protected:
MOUSE_LISTENER_CALLBACK m_callback;
@@ -341,14 +341,14 @@ public:
);
// 绑定监听器到场景
- virtual void bindWithScene(
+ virtual void bindWith(
EScene * pParentScene
- );
+ ) override;
// 绑定监听器到节点
- virtual void bindWithNode(
+ virtual void bindWith(
ENode * pParentNode
- );
+ ) override;
protected:
KEY_LISTENER_CALLBACK m_callback;
diff --git a/Easy2D/enodes.h b/Easy2D/enodes.h
index ba3a8d86..cfc778b8 100644
--- a/Easy2D/enodes.h
+++ b/Easy2D/enodes.h
@@ -4,9 +4,6 @@
namespace e2d
{
-class EScene;
-class EObject;
-
class ENode :
public EObject
{
@@ -16,12 +13,7 @@ public:
ENode();
ENode(
- EPoint p
- );
-
- ENode(
- int x,
- int y
+ EString name
);
virtual ~ENode();
@@ -48,22 +40,34 @@ public:
virtual UINT32 getHeight() const;
// 获取节点大小
- virtual e2d::ESize getSize() const;
+ virtual ESize getSize() const;
// 获取节点所在的矩形
- virtual e2d::ERect getRect() const;
+ virtual ERect getRect() const;
// 获取父节点
- virtual e2d::ENode* &getParent();
+ virtual ENode * &getParent();
// 获取节点所在场景
- EScene * &getParentScene();
+ virtual EScene * &getParentScene();
+
+ // 获取所有子节点
+ virtual std::vector &getChildren();
+
+ // 获取子节点数量
+ virtual int getChildrenCount() const;
+
+ // 根据名字获取子节点
+ virtual ENode * getChild(EString name);
// 设置节点是否显示
virtual void setVisiable(
bool value
);
+ // 设置节点名称
+ virtual void setName(EString name);
+
// 设置节点横坐标
virtual void setX(
int x
@@ -114,7 +118,7 @@ public:
// 设置节点大小
virtual void setSize(
- e2d::ESize size
+ ESize size
);
// 设置节点所在的矩形
@@ -133,7 +137,7 @@ public:
// 设置节点所在的矩形
virtual void setRect(
- e2d::ERect rect
+ ERect rect
);
// 设置节点绘图顺序(0为最先绘制,显示在最底层)
@@ -141,28 +145,41 @@ public:
int z
);
+ // 设置节点所在场景
+ virtual void setParentScene(
+ EScene * scene
+ );
+
// 设置父节点
virtual void setParent(
ENode* parent
);
- // 设置节点所在场景
- void bindWithScene(
- EScene * scene
- );
+ // 添加子节点
+ virtual void addChild(ENode * child);
+
+ // 从父节点移除
+ virtual void removeFromParent(bool release = false);
+
+ // 移除子节点
+ virtual void removeChild(ENode * child, bool release = false);
+
+ // 移除子节点
+ virtual void removeChild(EString childName, bool release = false);
protected:
+ // 渲染节点
+ virtual void _onRender();
+
+protected:
+ EString m_sName;
+ size_t m_nHashName;
int m_nZOrder;
bool m_bVisiable;
ERect m_Rect;
EScene * m_pParentScene;
ENode * m_pParent;
-
-protected:
-
- virtual bool _exec(bool active);
-
- virtual void _onRender();
+ std::vector m_vChildren;
};
}
\ No newline at end of file