From afb8780a31711ad0c834e1ec940fd1ff4a7f44df Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 22 May 2018 00:36:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=81=9A=E8=BE=93=E5=85=A5=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=99=A8=E5=92=8C=E7=A2=B0=E6=92=9E=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Game.cpp | 4 +- core/Base/Input.cpp | 109 +++++++++++++++++++- core/Collider/Collision.cpp | 77 ++++---------- core/Common/Listener.cpp | 33 ++++++ core/Manager/InputManager.cpp | 142 -------------------------- core/e2dbase.h | 37 +++++++ core/e2dcollider.h | 16 +-- core/e2dcommon.h | 38 +++++++ core/e2dmanager.h | 49 +-------- core/e2dtool.h | 2 - project/vs2017/Easy2D.vcxproj | 2 +- project/vs2017/Easy2D.vcxproj.filters | 6 +- 12 files changed, 252 insertions(+), 263 deletions(-) create mode 100644 core/Common/Listener.cpp delete mode 100644 core/Manager/InputManager.cpp diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index f2ba83b8..aa9c6f9b 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -200,9 +200,9 @@ void e2d::Game::destroy() // 删除所有场景 SceneManager::__uninit(); // 删除输入监听器 - InputManager::__uninit(); + Input::__clearListeners(); // 删除碰撞监听器 - Collision::__uninit(); + Collision::__clearListeners(); // 删除动作 ActionManager::__uninit(); // 回收音乐播放器资源 diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index 99bb875c..5817bf96 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -18,6 +18,8 @@ static DIMOUSESTATE s_MouseState; // static DIMOUSESTATE s_MouseRecordState; // 鼠标信息二级缓冲 static POINT s_MousePosition; // 鼠标位置存储结构体 +static std::vector s_vListeners; // 监听器容器 + bool Input::__init() { @@ -99,7 +101,7 @@ void Input::__uninit() void e2d::Input::__update() { Input::__updateDeviceState(); - InputManager::__update(); + Input::__updateListeners(); } void Input::__updateDeviceState() @@ -216,4 +218,109 @@ double Input::getMouseDeltaY() double Input::getMouseDeltaZ() { return (double)s_MouseState.lZ; +} + + +void e2d::Input::addListener(const Function& func, const String& name, bool paused) +{ + auto listener = new (std::nothrow) Listener(func, name, paused); + 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->_stopped = true; + } + } +} + +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->_stopped = true; + } +} + +void e2d::Input::__updateListeners() +{ + if (s_vListeners.empty() || Game::isPaused()) + return; + + for (size_t i = 0; i < s_vListeners.size(); ++i) + { + auto listener = s_vListeners[i]; + // 清除已停止的监听器 + if (listener->_stopped) + { + delete listener; + s_vListeners.erase(s_vListeners.begin() + i); + } + else + { + // 更新监听器 + listener->update(); + ++i; + } + } +} + +void e2d::Input::__clearListeners() +{ + for (auto listener : s_vListeners) + { + delete listener; + } + s_vListeners.clear(); } \ No newline at end of file diff --git a/core/Collider/Collision.cpp b/core/Collider/Collision.cpp index fe168410..0b0a042e 100644 --- a/core/Collider/Collision.cpp +++ b/core/Collider/Collision.cpp @@ -3,6 +3,10 @@ typedef std::pair HashPair; +// 监听器容器 +static std::vector s_vListeners; +// 碰撞触发状态 +static bool s_bCollisionEnable = false; static e2d::Node * s_pActiveNode = nullptr; static e2d::Node * s_pPassiveNode = nullptr; static std::set s_sCollisionList; @@ -75,45 +79,6 @@ e2d::Node* e2d::Collision::isCausedBy(const String& name) } -// 监听器 -class Listener -{ -public: - Listener( - const e2d::Function& func, - const e2d::String& name, - bool paused - ) - : name(name) - , callback(func) - , running(!paused) - , stopped(false) - { - } - - // 更新监听器状态 - virtual void update() - { - if (callback) - { - callback(); - } - } - -public: - bool running; - bool stopped; - e2d::String name; - e2d::Function callback; -}; - - -// 监听器容器 -static std::vector s_vListeners; -// 碰撞触发状态 -static bool s_bCollisionEnable = false; - - void e2d::Collision::setEnable(bool enable) { s_bCollisionEnable = enable; @@ -136,7 +101,7 @@ void e2d::Collision::__update(Node * active, Node * passive) { auto listener = s_vListeners[i]; // 清除已停止的监听器 - if (listener->stopped) + if (listener->_stopped) { delete listener; s_vListeners.erase(s_vListeners.begin() + i); @@ -159,73 +124,73 @@ void e2d::Collision::addListener(const Function& func, const String& name, bool s_vListeners.push_back(listener); } -void e2d::Collision::pause(const String& name) +void e2d::Collision::pauseListener(const String& name) { if (s_vListeners.empty() || name.isEmpty()) return; for (auto listener : s_vListeners) { - if (listener->name == name) + if (listener->_name == name) { - listener->running = false; + listener->_running = false; } } } -void e2d::Collision::resume(const String& name) +void e2d::Collision::resumeListener(const String& name) { if (s_vListeners.empty() || name.isEmpty()) return; for (auto listener : s_vListeners) { - if (listener->name == name) + if (listener->_name == name) { - listener->running = true; + listener->_running = true; } } } -void e2d::Collision::stop(const String& name) +void e2d::Collision::stopListener(const String& name) { if (s_vListeners.empty() || name.isEmpty()) return; for (auto listener : s_vListeners) { - if (listener->name == name) + if (listener->_name == name) { - listener->stopped = true; + listener->_stopped = true; } } } -void e2d::Collision::pauseAll() +void e2d::Collision::pauseAllListeners() { for (auto listener : s_vListeners) { - listener->running = false; + listener->_running = false; } } -void e2d::Collision::resumeAll() +void e2d::Collision::resumeAllListeners() { for (auto listener : s_vListeners) { - listener->running = true; + listener->_running = true; } } -void e2d::Collision::stopAll() +void e2d::Collision::stopAllListeners() { for (auto listener : s_vListeners) { - listener->stopped = true; + listener->_stopped = true; } } -void e2d::Collision::__uninit() +void e2d::Collision::__clearListeners() { for (auto listener : s_vListeners) { diff --git a/core/Common/Listener.cpp b/core/Common/Listener.cpp new file mode 100644 index 00000000..98625e95 --- /dev/null +++ b/core/Common/Listener.cpp @@ -0,0 +1,33 @@ +#include "..\e2dcommon.h" + + +e2d::Listener::Listener(const Function & func, const String & name, bool paused) + : _name(name) + , _callback(func) + , _running(!paused) + , _stopped(false) +{ +} + +void e2d::Listener::update() +{ + if (_callback) + { + _callback(); + } +} + +bool e2d::Listener::isRunning() const +{ + return _running; +} + +e2d::String e2d::Listener::getName() const +{ + return _name; +} + +void e2d::Listener::setName(const String & name) +{ + _name = name; +} diff --git a/core/Manager/InputManager.cpp b/core/Manager/InputManager.cpp deleted file mode 100644 index 304453b2..00000000 --- a/core/Manager/InputManager.cpp +++ /dev/null @@ -1,142 +0,0 @@ -#include "..\e2dmanager.h" -#include "..\e2dtool.h" - -// 监听器 -class Listener -{ -public: - Listener( - const e2d::Function& func, - const e2d::String& name, - bool paused - ) - : name(name) - , callback(func) - , running(!paused) - , stopped(false) - { - } - - // 更新监听器状态 - virtual void update() - { - if (callback) - { - callback(); - } - } - -public: - bool running; - bool stopped; - e2d::String name; - e2d::Function callback; -}; - -// 监听器容器 -static std::vector s_vListeners; - - -void e2d::InputManager::add(const Function& func, const String& name, bool paused) -{ - auto listener = new (std::nothrow) Listener(func, name, paused); - s_vListeners.push_back(listener); -} - -void e2d::InputManager::pause(const String& name) -{ - if (s_vListeners.empty() || name.isEmpty()) - return; - - for (auto listener : s_vListeners) - { - if (listener->name == name) - { - listener->running = false; - } - } -} - -void e2d::InputManager::resume(const String& name) -{ - if (s_vListeners.empty() || name.isEmpty()) - return; - - for (auto listener : s_vListeners) - { - if (listener->name == name) - { - listener->running = true; - } - } -} - -void e2d::InputManager::stop(const String& name) -{ - if (s_vListeners.empty() || name.isEmpty()) - return; - - for (auto listener : s_vListeners) - { - if (listener->name == name) - { - listener->stopped = true; - } - } -} - -void e2d::InputManager::pauseAll() -{ - for (auto listener : s_vListeners) - { - listener->running = false; - } -} - -void e2d::InputManager::resumeAll() -{ - for (auto listener : s_vListeners) - { - listener->running = true; - } -} - -void e2d::InputManager::stopAll() -{ - for (auto listener : s_vListeners) - { - listener->stopped = true; - } -} - -void e2d::InputManager::__update() -{ - if (s_vListeners.empty() || Game::isPaused()) - return; - - for (size_t i = 0; i < s_vListeners.size(); ++i) - { - auto listener = s_vListeners[i]; - // 清除已停止的监听器 - if (listener->stopped) - { - delete listener; - s_vListeners.erase(s_vListeners.begin() + i); - } - else - { - // 更新监听器 - listener->update(); - ++i; - } - } -} - -void e2d::InputManager::__uninit() -{ - for (auto listener : s_vListeners) - { - delete listener; - } - s_vListeners.clear(); -} \ No newline at end of file diff --git a/core/e2dbase.h b/core/e2dbase.h index b0be3d05..8d73852e 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -307,6 +307,37 @@ public: // 获得鼠标Z轴(鼠标滚轮)坐标增量 static double getMouseDeltaZ(); + // 添加输入监听 + static void addListener( + const Function& func, /* 监听到用户输入时的执行函数 */ + const String& name = L"", /* 监听器名称 */ + bool paused = false /* 是否暂停 */ + ); + + // 暂停输入监听 + static void pauseListener( + const String& name + ); + + // 暂停输入监听 + static void resumeListener( + const String& name + ); + + // 停止输入监听 + static void stopListener( + const String& name + ); + + // 暂停所有监听器 + static void pauseAllListeners(); + + // 继续所有监听器 + static void resumeAllListeners(); + + // 停止所有监听器 + static void stopAllListeners(); + private: // 初始化 DirectInput 以及键盘鼠标设备 static bool __init(); @@ -317,8 +348,14 @@ private: // 刷新设备状态 static void __updateDeviceState(); + // 更新监听器 + static void __updateListeners(); + // 卸载 DirectInput static void __uninit(); + + // 清空监听器 + static void __clearListeners(); }; diff --git a/core/e2dcollider.h b/core/e2dcollider.h index 22c2d50a..9e2c3323 100644 --- a/core/e2dcollider.h +++ b/core/e2dcollider.h @@ -73,28 +73,28 @@ public: ); // 暂停碰撞监听 - static void pause( + static void pauseListener( const String& name ); // 暂停碰撞监听 - static void resume( + static void resumeListener( const String& name ); // 停止碰撞监听 - static void stop( + static void stopListener( const String& name ); // 暂停所有监听器 - static void pauseAll(); + static void pauseAllListeners(); // 继续所有监听器 - static void resumeAll(); + static void resumeAllListeners(); // 停止所有监听器 - static void stopAll(); + static void stopAllListeners(); private: // 更新监听器 @@ -103,8 +103,8 @@ private: Node * passive ); - // 回收资源 - static void __uninit(); + // 清空监听器 + static void __clearListeners(); }; diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 8bfdd012..7cfb4982 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -578,4 +578,42 @@ 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/e2dmanager.h b/core/e2dmanager.h index e92fe1b9..6fa889e7 100644 --- a/core/e2dmanager.h +++ b/core/e2dmanager.h @@ -151,54 +151,7 @@ private: }; -// 键盘和鼠标消息管理器 -class InputManager -{ - friend Game; - friend Input; - -public: - // 添加输入监听 - static void add( - const Function& func, /* 监听到用户输入时的执行函数 */ - const String& name = L"", /* 监听器名称 */ - bool paused = false /* 是否暂停 */ - ); - - // 暂停输入监听 - static void pause( - const String& name - ); - - // 暂停输入监听 - static void resume( - const String& name - ); - - // 停止输入监听 - static void stop( - const String& name - ); - - // 暂停所有监听器 - static void pauseAll(); - - // 继续所有监听器 - static void resumeAll(); - - // 停止所有监听器 - static void stopAll(); - -private: - // 更新监听器 - static void __update(); - - // 回收资源 - static void __uninit(); -}; - - -// 碰撞管理器 +// 碰撞体管理器 class ColliderManager { friend Node; diff --git a/core/e2dtool.h b/core/e2dtool.h index f88ad85c..2bd03146 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -4,8 +4,6 @@ namespace e2d { -class InputManager; -class ColliderManager; // 随机数产生器 class Random diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index c3b57bb1..2e1c2a1a 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -228,6 +228,7 @@ + @@ -236,7 +237,6 @@ - diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index b7f1bf85..ee9b4821 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -114,9 +114,6 @@ Tool - - Manager - Collider @@ -225,6 +222,9 @@ Base + + Common +