From 6deb3964fbf61aa2949d129449fe15b83faf5a89 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 22 May 2018 15:54:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=99=A8=E4=BB=8ECommon?= =?UTF-8?q?=E7=BB=84=E7=A7=BB=E5=8A=A8=E5=88=B0Tool=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Input.cpp | 90 +++++++++++++------------- core/Collider/Collision.cpp | 91 ++++++++++++++------------- core/{Common => Tool}/Listener.cpp | 26 +++++++- core/e2dbase.h | 22 +++---- core/e2dcollider.h | 22 +++---- core/e2dcommon.h | 38 ----------- core/e2dtool.h | 51 +++++++++++++++ project/vs2017/Easy2D.vcxproj | 2 +- project/vs2017/Easy2D.vcxproj.filters | 6 +- 9 files changed, 193 insertions(+), 155 deletions(-) rename core/{Common => Tool}/Listener.cpp (56%) diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index 7abd8838..0d38578c 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -227,35 +227,35 @@ void e2d::Input::addListener(const Function& func, const String& name, bool paus s_vListeners.push_back(listener); } -void e2d::Input::pauseListener(const String& name) -{ - if (s_vListeners.empty() || name.isEmpty()) - return; - - for (auto listener : s_vListeners) - { - if (listener->_name == name) - { - listener->_running = false; - } - } -} - -void e2d::Input::resumeListener(const String& name) -{ - if (s_vListeners.empty() || name.isEmpty()) - return; - - for (auto listener : s_vListeners) - { - if (listener->_name == name) - { - listener->_running = true; - } - } -} - void e2d::Input::stopListener(const String& name) +{ + if (s_vListeners.empty() || name.isEmpty()) + return; + + for (auto listener : s_vListeners) + { + if (listener->_name == name) + { + listener->stop(); + } + } +} + +void e2d::Input::startListener(const String& name) +{ + if (s_vListeners.empty() || name.isEmpty()) + return; + + for (auto listener : s_vListeners) + { + if (listener->_name == name) + { + listener->start(); + } + } +} + +void e2d::Input::clearListener(const String& name) { if (s_vListeners.empty() || name.isEmpty()) return; @@ -269,23 +269,23 @@ void e2d::Input::stopListener(const String& name) } } -void e2d::Input::pauseAllListeners() -{ - for (auto listener : s_vListeners) - { - listener->_running = false; - } -} - -void e2d::Input::resumeAllListeners() -{ - for (auto listener : s_vListeners) - { - listener->_running = true; - } -} - void e2d::Input::stopAllListeners() +{ + for (auto listener : s_vListeners) + { + listener->stop(); + } +} + +void e2d::Input::startAllListeners() +{ + for (auto listener : s_vListeners) + { + listener->start(); + } +} + +void e2d::Input::clearAllListeners() { for (auto listener : s_vListeners) { @@ -310,7 +310,7 @@ void e2d::Input::__updateListeners() else { // 更新监听器 - listener->update(); + listener->_update(); ++i; } } diff --git a/core/Collider/Collision.cpp b/core/Collider/Collision.cpp index 0b0a042e..add6f66c 100644 --- a/core/Collider/Collision.cpp +++ b/core/Collider/Collision.cpp @@ -1,5 +1,6 @@ #include "..\e2dcollider.h" #include "..\e2dnode.h" +#include "..\e2dtool.h" typedef std::pair HashPair; @@ -109,7 +110,7 @@ void e2d::Collision::__update(Node * active, Node * passive) else { // 更新监听器 - listener->update(); + listener->_update(); ++i; } } @@ -124,35 +125,35 @@ void e2d::Collision::addListener(const Function& func, const String& name, bool s_vListeners.push_back(listener); } -void e2d::Collision::pauseListener(const String& name) -{ - if (s_vListeners.empty() || name.isEmpty()) - return; - - for (auto listener : s_vListeners) - { - if (listener->_name == name) - { - listener->_running = false; - } - } -} - -void e2d::Collision::resumeListener(const String& name) -{ - if (s_vListeners.empty() || name.isEmpty()) - return; - - for (auto listener : s_vListeners) - { - if (listener->_name == name) - { - listener->_running = true; - } - } -} - void e2d::Collision::stopListener(const String& name) +{ + if (s_vListeners.empty() || name.isEmpty()) + return; + + for (auto listener : s_vListeners) + { + if (listener->_name == name) + { + listener->stop(); + } + } +} + +void e2d::Collision::startListener(const String& name) +{ + if (s_vListeners.empty() || name.isEmpty()) + return; + + for (auto listener : s_vListeners) + { + if (listener->_name == name) + { + listener->start(); + } + } +} + +void e2d::Collision::clearListener(const String& name) { if (s_vListeners.empty() || name.isEmpty()) return; @@ -166,23 +167,23 @@ void e2d::Collision::stopListener(const String& name) } } -void e2d::Collision::pauseAllListeners() -{ - for (auto listener : s_vListeners) - { - listener->_running = false; - } -} - -void e2d::Collision::resumeAllListeners() -{ - for (auto listener : s_vListeners) - { - listener->_running = true; - } -} - void e2d::Collision::stopAllListeners() +{ + for (auto listener : s_vListeners) + { + listener->stop(); + } +} + +void e2d::Collision::startAllListeners() +{ + for (auto listener : s_vListeners) + { + listener->start(); + } +} + +void e2d::Collision::clearAllListeners() { for (auto listener : s_vListeners) { diff --git a/core/Common/Listener.cpp b/core/Tool/Listener.cpp similarity index 56% rename from core/Common/Listener.cpp rename to core/Tool/Listener.cpp index 98625e95..fc1bfb99 100644 --- a/core/Common/Listener.cpp +++ b/core/Tool/Listener.cpp @@ -1,6 +1,15 @@ #include "..\e2dcommon.h" +#include "..\e2dtool.h" +e2d::Listener::Listener() + : _name() + , _callback() + , _running(true) + , _stopped(false) +{ +} + e2d::Listener::Listener(const Function & func, const String & name, bool paused) : _name(name) , _callback(func) @@ -9,7 +18,7 @@ e2d::Listener::Listener(const Function & func, const String & name, bool paused) { } -void e2d::Listener::update() +void e2d::Listener::_update() { if (_callback) { @@ -31,3 +40,18 @@ void e2d::Listener::setName(const String & name) { _name = name; } + +void e2d::Listener::setFunc(const Function & func) +{ + _callback = func; +} + +void e2d::Listener::start() +{ + _running = true; +} + +void e2d::Listener::stop() +{ + _running = false; +} diff --git a/core/e2dbase.h b/core/e2dbase.h index 7d41e214..cbe5b570 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -315,13 +315,8 @@ public: bool paused = false /* 是否暂停 */ ); - // 暂停输入监听 - static void pauseListener( - const String& name - ); - - // 暂停输入监听 - static void resumeListener( + // 启动输入监听 + static void startListener( const String& name ); @@ -330,15 +325,20 @@ public: const String& name ); - // 暂停所有监听器 - static void pauseAllListeners(); + // 清除输入监听 + static void clearListener( + const String& name + ); - // 继续所有监听器 - static void resumeAllListeners(); + // 启动所有监听器 + static void startAllListeners(); // 停止所有监听器 static void stopAllListeners(); + // 清除所有监听器 + static void clearAllListeners(); + private: // 初始化 DirectInput 以及键盘鼠标设备 static bool __init(); diff --git a/core/e2dcollider.h b/core/e2dcollider.h index 9e2c3323..a78f2fac 100644 --- a/core/e2dcollider.h +++ b/core/e2dcollider.h @@ -72,13 +72,8 @@ public: bool paused = false /* 是否暂停 */ ); - // 暂停碰撞监听 - static void pauseListener( - const String& name - ); - - // 暂停碰撞监听 - static void resumeListener( + // 启动碰撞监听 + static void startListener( const String& name ); @@ -87,15 +82,20 @@ public: const String& name ); - // 暂停所有监听器 - static void pauseAllListeners(); + // 清除碰撞监听 + static void clearListener( + const String& name + ); - // 继续所有监听器 - static void resumeAllListeners(); + // 启动所有监听器 + static void startAllListeners(); // 停止所有监听器 static void stopAllListeners(); + // 清除所有监听器 + static void clearAllListeners(); + private: // 更新监听器 static void __update( diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 93d2a4a5..46ddf8e9 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -576,42 +576,4 @@ protected: }; -class Input; -class Collision; - -// 监听器 -class Listener -{ - friend Input; - friend Collision; - -public: - Listener( - const Function& func, - const String& name, - bool paused - ); - - // 更新监听器状态 - virtual void update(); - - // 获取监听器运行状态 - bool isRunning() const; - - // 获取名称 - String getName() const; - - // 设置名称 - void setName( - const String& name - ); - -protected: - bool _running; - bool _stopped; - String _name; - Function _callback; -}; - - } \ No newline at end of file diff --git a/core/e2dtool.h b/core/e2dtool.h index e6eb8e6d..c59adc48 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -318,6 +318,57 @@ private: }; +class Collision; + +// 监听器 +class Listener +{ + friend Input; + friend Collision; + +public: + Listener(); + + Listener( + const Function& func, + const String& name, + bool paused + ); + + // 启动监听 + void start(); + + // 停止监听 + void stop(); + + // 获取监听器运行状态 + bool isRunning() const; + + // 获取名称 + String getName() const; + + // 设置名称 + void setName( + const String& name + ); + + // 设置监听回调函数 + void setFunc( + const Function& func + ); + +protected: + // 更新监听器状态 + virtual void _update(); + +protected: + bool _running; + bool _stopped; + String _name; + Function _callback; +}; + + // 数据管理工具 class Data { diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 42904b75..be85718d 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -228,7 +228,6 @@ - @@ -252,6 +251,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index ffa19d4d..184a2b93 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -219,9 +219,6 @@ Base - - Common - Custom @@ -231,6 +228,9 @@ Transition + + Tool +