diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index 0d38578c..c5a843fb 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -220,11 +220,38 @@ double Input::getMouseDeltaZ() return (double)s_MouseState.lZ; } - -void e2d::Input::addListener(const Function& func, const String& name, bool paused) +e2d::Listener * e2d::Input::addListener(const Function& func, const String& name, bool paused) { - auto listener = new (std::nothrow) Listener(func, name, paused); + auto listener = GC::create(func, name, paused); + GC::retain(listener); s_vListeners.push_back(listener); + return listener; +} + +void e2d::Input::addListener(Listener * listener) +{ + if (listener) + { + auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); + if (iter == s_vListeners.end()) + { + GC::retain(listener); + s_vListeners.push_back(listener); + } + } +} + +void e2d::Input::removeListener(Listener * listener) +{ + if (listener) + { + auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); + if (iter != s_vListeners.end()) + { + GC::release(listener); + s_vListeners.erase(iter); + } + } } void e2d::Input::stopListener(const String& name) @@ -255,7 +282,7 @@ void e2d::Input::startListener(const String& name) } } -void e2d::Input::clearListener(const String& name) +void e2d::Input::removeListener(const String& name) { if (s_vListeners.empty() || name.isEmpty()) return; @@ -285,7 +312,7 @@ void e2d::Input::startAllListeners() } } -void e2d::Input::clearAllListeners() +void e2d::Input::removeAllListeners() { for (auto listener : s_vListeners) { @@ -304,7 +331,7 @@ void e2d::Input::__updateListeners() // 清除已停止的监听器 if (listener->_stopped) { - delete listener; + GC::release(listener); s_vListeners.erase(s_vListeners.begin() + i); } else @@ -320,7 +347,7 @@ void e2d::Input::__clearListeners() { for (auto listener : s_vListeners) { - delete listener; + GC::release(listener); } s_vListeners.clear(); } \ No newline at end of file diff --git a/core/Collider/Collision.cpp b/core/Collider/Collision.cpp index add6f66c..46179c83 100644 --- a/core/Collider/Collision.cpp +++ b/core/Collider/Collision.cpp @@ -104,7 +104,7 @@ void e2d::Collision::__update(Node * active, Node * passive) // 清除已停止的监听器 if (listener->_stopped) { - delete listener; + GC::release(listener); s_vListeners.erase(s_vListeners.begin() + i); } else @@ -119,10 +119,38 @@ void e2d::Collision::__update(Node * active, Node * passive) s_pPassiveNode = nullptr; } -void e2d::Collision::addListener(const Function& func, const String& name, bool paused) +e2d::Listener * e2d::Collision::addListener(const Function& func, const String& name, bool paused) { - auto listener = new (std::nothrow) Listener(func, name, paused); + auto listener = GC::create(func, name, paused); + GC::retain(listener); s_vListeners.push_back(listener); + return listener; +} + +void e2d::Collision::addListener(Listener * listener) +{ + if (listener) + { + auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); + if (iter == s_vListeners.end()) + { + GC::retain(listener); + s_vListeners.push_back(listener); + } + } +} + +void e2d::Collision::removeListener(Listener * listener) +{ + if (listener) + { + auto iter = std::find(s_vListeners.begin(), s_vListeners.end(), listener); + if (iter != s_vListeners.end()) + { + GC::release(listener); + s_vListeners.erase(iter); + } + } } void e2d::Collision::stopListener(const String& name) @@ -153,7 +181,7 @@ void e2d::Collision::startListener(const String& name) } } -void e2d::Collision::clearListener(const String& name) +void e2d::Collision::removeListener(const String& name) { if (s_vListeners.empty() || name.isEmpty()) return; @@ -183,7 +211,7 @@ void e2d::Collision::startAllListeners() } } -void e2d::Collision::clearAllListeners() +void e2d::Collision::removeAllListeners() { for (auto listener : s_vListeners) { @@ -195,7 +223,7 @@ void e2d::Collision::__clearListeners() { for (auto listener : s_vListeners) { - delete listener; + GC::release(listener); } s_vListeners.clear(); } \ No newline at end of file diff --git a/core/e2dbase.h b/core/e2dbase.h index cbe5b570..cd1ea851 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -183,6 +183,8 @@ private: }; +class Listener; + // 输入控制 class Input { @@ -309,12 +311,22 @@ public: static double getMouseDeltaZ(); // 添加输入监听 - static void addListener( + static Listener * addListener( const Function& func, /* 监听到用户输入时的执行函数 */ const String& name = L"", /* 监听器名称 */ bool paused = false /* 是否暂停 */ ); + // 添加碰撞监听 + static void addListener( + Listener * listener /* 监听器 */ + ); + + // 移除监听器 + static void removeListener( + Listener * listener /* 监听器 */ + ); + // 启动输入监听 static void startListener( const String& name @@ -325,8 +337,8 @@ public: const String& name ); - // 清除输入监听 - static void clearListener( + // 移除输入监听 + static void removeListener( const String& name ); @@ -336,8 +348,8 @@ public: // 停止所有监听器 static void stopAllListeners(); - // 清除所有监听器 - static void clearAllListeners(); + // 移除所有监听器 + static void removeAllListeners(); private: // 初始化 DirectInput 以及键盘鼠标设备 diff --git a/core/e2dcollider.h b/core/e2dcollider.h index a78f2fac..800de6fa 100644 --- a/core/e2dcollider.h +++ b/core/e2dcollider.h @@ -7,6 +7,7 @@ namespace e2d +class Listener; class ColliderManager; // 碰撞事件 @@ -66,12 +67,22 @@ public: static bool isEnable(); // 添加碰撞监听 - static void addListener( + static Listener * addListener( const Function& func, /* 监听到碰撞时的执行函数 */ const String& name = L"", /* 监听器名称 */ bool paused = false /* 是否暂停 */ ); + // 添加碰撞监听 + static void addListener( + Listener * listener /* 监听器 */ + ); + + // 移除监听器 + static void removeListener( + Listener * listener /* 监听器 */ + ); + // 启动碰撞监听 static void startListener( const String& name @@ -82,8 +93,8 @@ public: const String& name ); - // 清除碰撞监听 - static void clearListener( + // 移除碰撞监听 + static void removeListener( const String& name ); @@ -93,8 +104,8 @@ public: // 停止所有监听器 static void stopAllListeners(); - // 清除所有监听器 - static void clearAllListeners(); + // 移除所有监听器 + static void removeAllListeners(); private: // 更新监听器 diff --git a/core/e2dtool.h b/core/e2dtool.h index c59adc48..ed5e6560 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -322,6 +322,7 @@ class Collision; // 监听器 class Listener + : public Object { friend Input; friend Collision;