重做输入监听器和碰撞监听器

This commit is contained in:
Nomango 2018-05-22 00:36:03 +08:00
parent 432a0150b9
commit afb8780a31
12 changed files with 252 additions and 263 deletions

View File

@ -200,9 +200,9 @@ void e2d::Game::destroy()
// 删除所有场景
SceneManager::__uninit();
// 删除输入监听器
InputManager::__uninit();
Input::__clearListeners();
// 删除碰撞监听器
Collision::__uninit();
Collision::__clearListeners();
// 删除动作
ActionManager::__uninit();
// 回收音乐播放器资源

View File

@ -18,6 +18,8 @@ static DIMOUSESTATE s_MouseState; //
static DIMOUSESTATE s_MouseRecordState; // 報炎佚連屈雫産喝
static POINT s_MousePosition; // 報炎了崔贋刈潤更悶
static std::vector<e2d::Listener*> 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();
}

View File

@ -3,6 +3,10 @@
typedef std::pair<UINT, UINT> HashPair;
// 监听器容器
static std::vector<e2d::Listener*> s_vListeners;
// 碰撞触发状态
static bool s_bCollisionEnable = false;
static e2d::Node * s_pActiveNode = nullptr;
static e2d::Node * s_pPassiveNode = nullptr;
static std::set<HashPair> 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<Listener*> 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)
{

33
core/Common/Listener.cpp Normal file
View File

@ -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;
}

View File

@ -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<Listener*> 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();
}

View File

@ -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();
};

View File

@ -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();
};

View File

@ -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;
};
}

View File

@ -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;

View File

@ -4,8 +4,6 @@
namespace e2d
{
class InputManager;
class ColliderManager;
// 随机数产生器
class Random

View File

@ -228,6 +228,7 @@
<ClCompile Include="..\..\core\Collider\Collision.cpp" />
<ClCompile Include="..\..\core\Common\Color.cpp" />
<ClCompile Include="..\..\core\Common\Function.cpp" />
<ClCompile Include="..\..\core\Common\Listener.cpp" />
<ClCompile Include="..\..\core\Common\Object.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" />
<ClCompile Include="..\..\core\Common\Scene.cpp" />
@ -236,7 +237,6 @@
<ClCompile Include="..\..\core\Common\Image.cpp" />
<ClCompile Include="..\..\core\Custom\CustomTextRenderer.cpp" />
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
<ClCompile Include="..\..\core\Manager\InputManager.cpp" />
<ClCompile Include="..\..\core\Manager\ColliderManager.cpp" />
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />

View File

@ -114,9 +114,6 @@
<ClCompile Include="..\..\core\Tool\Path.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\InputManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Collider\Collider.cpp">
<Filter>Collider</Filter>
</ClCompile>
@ -225,6 +222,9 @@
<ClCompile Include="..\..\core\Base\GC.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Listener.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" />