修改Listener类,使其继承自Object

This commit is contained in:
Nomango 2018-05-22 21:21:04 +08:00
parent 6deb3964fb
commit a942eeea16
5 changed files with 102 additions and 23 deletions

View File

@ -220,11 +220,38 @@ double Input::getMouseDeltaZ()
return (double)s_MouseState.lZ; return (double)s_MouseState.lZ;
} }
e2d::Listener * e2d::Input::addListener(const Function& func, const String& name, bool paused)
void e2d::Input::addListener(const Function& func, const String& name, bool paused)
{ {
auto listener = new (std::nothrow) Listener(func, name, paused); auto listener = GC::create<Listener>(func, name, paused);
GC::retain(listener);
s_vListeners.push_back(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) 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()) if (s_vListeners.empty() || name.isEmpty())
return; return;
@ -285,7 +312,7 @@ void e2d::Input::startAllListeners()
} }
} }
void e2d::Input::clearAllListeners() void e2d::Input::removeAllListeners()
{ {
for (auto listener : s_vListeners) for (auto listener : s_vListeners)
{ {
@ -304,7 +331,7 @@ void e2d::Input::__updateListeners()
// 清除已停止的监听器 // 清除已停止的监听器
if (listener->_stopped) if (listener->_stopped)
{ {
delete listener; GC::release(listener);
s_vListeners.erase(s_vListeners.begin() + i); s_vListeners.erase(s_vListeners.begin() + i);
} }
else else
@ -320,7 +347,7 @@ void e2d::Input::__clearListeners()
{ {
for (auto listener : s_vListeners) for (auto listener : s_vListeners)
{ {
delete listener; GC::release(listener);
} }
s_vListeners.clear(); s_vListeners.clear();
} }

View File

@ -104,7 +104,7 @@ void e2d::Collision::__update(Node * active, Node * passive)
// 清除已停止的监听器 // 清除已停止的监听器
if (listener->_stopped) if (listener->_stopped)
{ {
delete listener; GC::release(listener);
s_vListeners.erase(s_vListeners.begin() + i); s_vListeners.erase(s_vListeners.begin() + i);
} }
else else
@ -119,10 +119,38 @@ void e2d::Collision::__update(Node * active, Node * passive)
s_pPassiveNode = nullptr; 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<Listener>(func, name, paused);
GC::retain(listener);
s_vListeners.push_back(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) 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()) if (s_vListeners.empty() || name.isEmpty())
return; return;
@ -183,7 +211,7 @@ void e2d::Collision::startAllListeners()
} }
} }
void e2d::Collision::clearAllListeners() void e2d::Collision::removeAllListeners()
{ {
for (auto listener : s_vListeners) for (auto listener : s_vListeners)
{ {
@ -195,7 +223,7 @@ void e2d::Collision::__clearListeners()
{ {
for (auto listener : s_vListeners) for (auto listener : s_vListeners)
{ {
delete listener; GC::release(listener);
} }
s_vListeners.clear(); s_vListeners.clear();
} }

View File

@ -183,6 +183,8 @@ private:
}; };
class Listener;
// 输入控制 // 输入控制
class Input class Input
{ {
@ -309,12 +311,22 @@ public:
static double getMouseDeltaZ(); static double getMouseDeltaZ();
// 添加输入监听 // 添加输入监听
static void addListener( static Listener * addListener(
const Function& func, /* 监听到用户输入时的执行函数 */ const Function& func, /* 监听到用户输入时的执行函数 */
const String& name = L"", /* 监听器名称 */ const String& name = L"", /* 监听器名称 */
bool paused = false /* 是否暂停 */ bool paused = false /* 是否暂停 */
); );
// 添加碰撞监听
static void addListener(
Listener * listener /* 监听器 */
);
// 移除监听器
static void removeListener(
Listener * listener /* 监听器 */
);
// 启动输入监听 // 启动输入监听
static void startListener( static void startListener(
const String& name const String& name
@ -325,8 +337,8 @@ public:
const String& name const String& name
); );
// 除输入监听 // 除输入监听
static void clearListener( static void removeListener(
const String& name const String& name
); );
@ -336,8 +348,8 @@ public:
// 停止所有监听器 // 停止所有监听器
static void stopAllListeners(); static void stopAllListeners();
// 除所有监听器 // 除所有监听器
static void clearAllListeners(); static void removeAllListeners();
private: private:
// 初始化 DirectInput 以及键盘鼠标设备 // 初始化 DirectInput 以及键盘鼠标设备

View File

@ -7,6 +7,7 @@ namespace e2d
class Listener;
class ColliderManager; class ColliderManager;
// 碰撞事件 // 碰撞事件
@ -66,12 +67,22 @@ public:
static bool isEnable(); static bool isEnable();
// 添加碰撞监听 // 添加碰撞监听
static void addListener( static Listener * addListener(
const Function& func, /* 监听到碰撞时的执行函数 */ const Function& func, /* 监听到碰撞时的执行函数 */
const String& name = L"", /* 监听器名称 */ const String& name = L"", /* 监听器名称 */
bool paused = false /* 是否暂停 */ bool paused = false /* 是否暂停 */
); );
// 添加碰撞监听
static void addListener(
Listener * listener /* 监听器 */
);
// 移除监听器
static void removeListener(
Listener * listener /* 监听器 */
);
// 启动碰撞监听 // 启动碰撞监听
static void startListener( static void startListener(
const String& name const String& name
@ -82,8 +93,8 @@ public:
const String& name const String& name
); );
// 除碰撞监听 // 除碰撞监听
static void clearListener( static void removeListener(
const String& name const String& name
); );
@ -93,8 +104,8 @@ public:
// 停止所有监听器 // 停止所有监听器
static void stopAllListeners(); static void stopAllListeners();
// 除所有监听器 // 除所有监听器
static void clearAllListeners(); static void removeAllListeners();
private: private:
// 更新监听器 // 更新监听器

View File

@ -322,6 +322,7 @@ class Collision;
// 监听器 // 监听器
class Listener class Listener
: public Object
{ {
friend Input; friend Input;
friend Collision; friend Collision;