diff --git a/Demo/main.cpp b/Demo/main.cpp index bc0016a2..8f792c0b 100644 --- a/Demo/main.cpp +++ b/Demo/main.cpp @@ -19,15 +19,12 @@ int WINAPI WinMain( node->setSize(30, 180); scene->add(node); - /*auto listener = new EMouseListener([=] { - if (!EMouseMsg::isLButtonDown()) + auto listener = new EMouseClickListener([=](EPoint) { + if (EMouseMsg::getMsg() == EMouseMsg::MOUSE_MSG::MOVE) { - if (EMouseMsg::getMsg() == EMouseMsg::MOVE) - { - node->setPos(EMouseMsg::getPos()); - } + node->setPos(EMouseMsg::getPos()); } - });*/ + }); auto listener = new EKeyPressListener([=] { if (EKeyMsg::isCapitalLockOn()) @@ -41,12 +38,11 @@ int WINAPI WinMain( node->move(3, 0); } } - EApp::setWindowSize(500, 500); }); listener->bindWithNode(node); - EMsgManager::bindListenerWithScene(listener, scene); + scene->bindListener(listener); app.enterScene(scene); diff --git a/Easy2D/Base/EScene.cpp b/Easy2D/Base/EScene.cpp index 9d174c10..6e47ca8b 100644 --- a/Easy2D/Base/EScene.cpp +++ b/Easy2D/Base/EScene.cpp @@ -115,10 +115,10 @@ void e2d::EScene::clearAllChildren() void e2d::EScene::bindListener(EMouseListener * listener) { - EMsgManager::bindListenerWithScene(listener, this); + EMsgManager::bindListenerWith(listener, this); } void e2d::EScene::bindListener(EKeyboardListener * listener) { - EMsgManager::bindListenerWithScene(listener, this); + EMsgManager::bindListenerWith(listener, this); } diff --git a/Easy2D/Msg/EMsgManager.cpp b/Easy2D/Msg/EMsgManager.cpp index 33514906..3d3644a4 100644 --- a/Easy2D/Msg/EMsgManager.cpp +++ b/Easy2D/Msg/EMsgManager.cpp @@ -10,7 +10,7 @@ e2d::EKeyMsg keyMsg; // 鼠标消息监听器 std::vector s_vMouseListeners; // 按键消息监听器 -std::vector s_vKeyListeners; +std::vector s_vKeyboardListeners; DWORD e2d::EMouseMsg::getX() @@ -141,7 +141,7 @@ void e2d::EMsgManager::KeyboardProc(UINT message, WPARAM wParam, LPARAM lParam) keyMsg.m_wParam = wParam; keyMsg.m_lParam = lParam; // 执行按键消息监听函数 - for (auto klistener : s_vKeyListeners) + for (auto klistener : s_vKeyboardListeners) { if (klistener->isRunning()) { @@ -150,7 +150,7 @@ void e2d::EMsgManager::KeyboardProc(UINT message, WPARAM wParam, LPARAM lParam) } } -void e2d::EMsgManager::bindListenerWithScene(e2d::EMouseListener * listener, EScene * pParentScene) +void e2d::EMsgManager::bindListenerWith(e2d::EMouseListener * listener, EScene * pParentScene) { WARN_IF(listener == nullptr, "EMouseListener NULL pointer exception!"); WARN_IF(pParentScene == nullptr, "Bind EMouseListener with a NULL EScene pointer!"); @@ -159,12 +159,12 @@ void e2d::EMsgManager::bindListenerWithScene(e2d::EMouseListener * listener, ESc { listener->start(); listener->retain(); - listener->bindWithScene(pParentScene); + listener->m_pParentScene = pParentScene; s_vMouseListeners.push_back(listener); } } -void e2d::EMsgManager::bindListenerWithScene(e2d::EKeyboardListener * listener, EScene * pParentScene) +void e2d::EMsgManager::bindListenerWith(EKeyboardListener * listener, EScene * pParentScene) { WARN_IF(listener == nullptr, "EKeyboardListener NULL pointer exception!"); WARN_IF(pParentScene == nullptr, "Bind EKeyboardListener with a NULL EScene pointer!"); @@ -173,8 +173,36 @@ void e2d::EMsgManager::bindListenerWithScene(e2d::EKeyboardListener * listener, { listener->start(); listener->retain(); - listener->bindWithScene(pParentScene); - s_vKeyListeners.push_back(listener); + listener->m_pParentScene = pParentScene; + s_vKeyboardListeners.push_back(listener); + } +} + +void e2d::EMsgManager::bindListenerWith(EMouseListener * listener, ENode * pParentNode) +{ + WARN_IF(listener == nullptr, "EMouseListener NULL pointer exception!"); + WARN_IF(pParentNode == nullptr, "Bind EMouseListener with a NULL ENode pointer!"); + + if (listener && pParentNode) + { + listener->start(); + listener->retain(); + listener->m_pParentNode = pParentNode; + s_vMouseListeners.push_back(listener); + } +} + +void e2d::EMsgManager::bindListenerWith(EKeyboardListener * listener, ENode * pParentNode) +{ + WARN_IF(listener == nullptr, "EKeyboardListener NULL pointer exception!"); + WARN_IF(pParentNode == nullptr, "Bind EKeyboardListener with a NULL ENode pointer!"); + + if (listener && pParentNode) + { + listener->start(); + listener->retain(); + listener->m_pParentNode = pParentNode; + s_vKeyboardListeners.push_back(listener); } } @@ -189,7 +217,7 @@ void e2d::EMsgManager::startListener(EString name) } } // 启动按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getName() == name) { @@ -209,7 +237,7 @@ void e2d::EMsgManager::stopListener(EString name) } } // 停止按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getName() == name) { @@ -237,13 +265,13 @@ void e2d::EMsgManager::delListener(EString name) } // 删除按键消息监听器 std::vector::iterator kIter; - for (kIter = s_vKeyListeners.begin(); kIter != s_vKeyListeners.end();) + for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();) { if ((*kIter)->getName() == name) { (*kIter)->autoRelease(); (*kIter)->release(); - kIter = s_vKeyListeners.erase(kIter); + kIter = s_vKeyboardListeners.erase(kIter); } else { @@ -286,7 +314,7 @@ void e2d::EMsgManager::clearAllMouseListeners() void e2d::EMsgManager::startAllKeyboardListener() { - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (!l->isWaiting()) { @@ -297,7 +325,7 @@ void e2d::EMsgManager::startAllKeyboardListener() void e2d::EMsgManager::stopAllKeyboardListener() { - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (!l->isWaiting()) { @@ -308,12 +336,12 @@ void e2d::EMsgManager::stopAllKeyboardListener() void e2d::EMsgManager::clearAllKeyboardListeners() { - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { l->autoRelease(); l->release(); } - s_vKeyListeners.clear(); + s_vKeyboardListeners.clear(); } void e2d::EMsgManager::startAllMouseListenersBindWithScene(EScene * pParentScene) @@ -343,7 +371,7 @@ void e2d::EMsgManager::stopAllMouseListenersBindWithScene(EScene * pParentScene) void e2d::EMsgManager::startAllKeyboardListenersBindWithScene(EScene * pParentScene) { // 启动按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getParentScene() == pParentScene) { @@ -355,7 +383,7 @@ void e2d::EMsgManager::startAllKeyboardListenersBindWithScene(EScene * pParentSc void e2d::EMsgManager::stopAllKeyboardListenersBindWithScene(EScene * pParentScene) { // 停止按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getParentScene() == pParentScene) { @@ -375,7 +403,7 @@ void e2d::EMsgManager::waitAllListenersBindWithScene(EScene * scene) } } // 挂起按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getParentScene() == scene) { @@ -395,7 +423,7 @@ void e2d::EMsgManager::notifyAllListenersBindWithScene(EScene * scene) } } // 重启按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getParentScene() == scene) { @@ -421,13 +449,13 @@ void e2d::EMsgManager::clearAllListenersBindWithScene(EScene * scene) } } std::vector::iterator kIter; - for (kIter = s_vKeyListeners.begin(); kIter != s_vKeyListeners.end();) + for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();) { if ((*kIter)->getParentScene() == scene) { (*kIter)->autoRelease(); (*kIter)->release(); - kIter = s_vKeyListeners.erase(kIter); + kIter = s_vKeyboardListeners.erase(kIter); } else { @@ -447,7 +475,7 @@ void e2d::EMsgManager::waitAllListenersBindWithNode(ENode * pParentNode) } } // 挂起按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getParentNode() == pParentNode) { @@ -467,7 +495,7 @@ void e2d::EMsgManager::notifyAllListenersBindWithNode(ENode * pParentNode) } } // 重启按键消息监听器 - for (auto l : s_vKeyListeners) + for (auto l : s_vKeyboardListeners) { if (l->getParentNode() == pParentNode) { @@ -493,13 +521,13 @@ void e2d::EMsgManager::clearAllListenersBindWithNode(ENode * pParentNode) } } std::vector::iterator kIter; - for (kIter = s_vKeyListeners.begin(); kIter != s_vKeyListeners.end();) + for (kIter = s_vKeyboardListeners.begin(); kIter != s_vKeyboardListeners.end();) { if ((*kIter)->getParentNode() == pParentNode) { (*kIter)->autoRelease(); (*kIter)->release(); - kIter = s_vKeyListeners.erase(kIter); + kIter = s_vKeyboardListeners.erase(kIter); } else { diff --git a/Easy2D/Msg/Listener/EKeyListener.cpp b/Easy2D/Msg/Listener/EKeyListener.cpp index 64092693..e9593725 100644 --- a/Easy2D/Msg/Listener/EKeyListener.cpp +++ b/Easy2D/Msg/Listener/EKeyListener.cpp @@ -31,3 +31,23 @@ void e2d::EKeyboardListener::setCallback(const KEY_LISTENER_CALLBACK & callback) { m_callback = callback; } + +void e2d::EKeyboardListener::bindWithScene(EScene * pParentScene) +{ + WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object."); + + if (pParentScene) + { + EMsgManager::bindListenerWith(this, pParentScene); + } +} + +void e2d::EKeyboardListener::bindWithNode(ENode * pParentNode) +{ + WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object."); + + if (pParentNode != nullptr && m_pParentScene == nullptr) + { + EMsgManager::bindListenerWith(this, pParentNode); + } +} \ No newline at end of file diff --git a/Easy2D/Msg/Listener/EListener.cpp b/Easy2D/Msg/Listener/EListener.cpp index 43704a15..56284616 100644 --- a/Easy2D/Msg/Listener/EListener.cpp +++ b/Easy2D/Msg/Listener/EListener.cpp @@ -45,11 +45,6 @@ void e2d::EListener::notify() m_bWaiting = false; } -e2d::EListener * e2d::EListener::clone() -{ - return nullptr; -} - e2d::EString e2d::EListener::getName() const { return m_sName; @@ -69,23 +64,3 @@ void e2d::EListener::setName(EString name) { m_sName = name; } - -void e2d::EListener::bindWithScene(EScene * scene) -{ - WARN_IF(m_pParentNode != nullptr, "EListener bind with Scene Failed! Please use its clone."); - - if (scene != nullptr && m_pParentNode == nullptr) - { - m_pParentScene = scene; - } -} - -void e2d::EListener::bindWithNode(ENode * pParentNode) -{ - WARN_IF(m_pParentScene != nullptr, "EListener bind with Node Failed! Please use its clone."); - - if (pParentNode != nullptr && m_pParentScene == nullptr) - { - m_pParentNode = pParentNode; - } -} diff --git a/Easy2D/Msg/Listener/EMouseListener.cpp b/Easy2D/Msg/Listener/EMouseListener.cpp index f44910f6..0087de44 100644 --- a/Easy2D/Msg/Listener/EMouseListener.cpp +++ b/Easy2D/Msg/Listener/EMouseListener.cpp @@ -31,3 +31,23 @@ void e2d::EMouseListener::setCallback(const MOUSE_LISTENER_CALLBACK & callback) { m_callback = callback; } + +void e2d::EMouseListener::bindWith(EScene * pParentScene) +{ + WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object."); + + if (pParentScene) + { + EMsgManager::bindListenerWith(this, pParentScene); + } +} + +void e2d::EMouseListener::bindWith(ENode * pParentNode) +{ + WARN_IF(m_pParentScene != nullptr || m_pParentNode != nullptr, "EListener cannot bind with two object."); + + if (pParentNode != nullptr && m_pParentScene == nullptr) + { + EMsgManager::bindListenerWith(this, pParentNode); + } +} diff --git a/Easy2D/emsg.h b/Easy2D/emsg.h index 0d37b960..603958d3 100644 --- a/Easy2D/emsg.h +++ b/Easy2D/emsg.h @@ -14,7 +14,7 @@ class EMouseMsg public: // 鼠标消息集合 - enum class MOUSE_MSG + enum MOUSE_MSG { MOVE = 0x0200, // 鼠标移动 LBUTTON_DOWN, // 鼠标左键按下 @@ -73,7 +73,7 @@ class EKeyMsg public: // 按键消息类型集合 - enum class KEYBOARD_MSG + enum KEYBOARD_MSG { KEY_DOWN = 0x0100, // 按下 KEY_UP // 抬起 @@ -141,6 +141,8 @@ protected: class EListener : public EObject { + friend EMsgManager; + public: EListener(); @@ -166,9 +168,6 @@ public: // 唤醒 void notify(); - // 克隆一个相同的监听器 - virtual EListener * clone(); - // 获取监听器名称 EString getName() const; @@ -184,14 +183,14 @@ public: ); // 绑定监听器到场景 - void bindWithScene( + virtual void bindWithScene( EScene * pParentScene - ); + ) = 0; // 绑定监听器到节点 - void bindWithNode( + virtual void bindWithNode( ENode * pParentNode - ); + ) = 0; protected: EString m_sName; @@ -230,6 +229,16 @@ public: const MOUSE_LISTENER_CALLBACK &callback ); + // 绑定监听器到场景 + virtual void bindWith( + EScene * pParentScene + ); + + // 绑定监听器到节点 + virtual void bindWith( + ENode * pParentNode + ); + protected: MOUSE_LISTENER_CALLBACK m_callback; }; @@ -331,6 +340,16 @@ public: const KEY_LISTENER_CALLBACK &callback ); + // 绑定监听器到场景 + virtual void bindWithScene( + EScene * pParentScene + ); + + // 绑定监听器到节点 + virtual void bindWithNode( + ENode * pParentNode + ); + protected: KEY_LISTENER_CALLBACK m_callback; }; @@ -368,17 +387,29 @@ class EMsgManager public: // 绑定鼠标消息监听器到场景 - static void bindListenerWithScene( + static void bindListenerWith( EMouseListener * listener, EScene * pParentScene ); - // 绑定按键消息监听器到场景 - static void bindListenerWithScene( + // 绑定鼠标消息监听器到场景 + static void bindListenerWith( EKeyboardListener * listener, EScene * pParentScene ); + // 绑定按键消息监听器到节点 + static void bindListenerWith( + EMouseListener * listener, + ENode * pParentNode + ); + + // 绑定按键消息监听器到节点 + static void bindListenerWith( + EKeyboardListener * listener, + ENode * pParentNode + ); + // 启动具有相同名称的监听器 static void startListener( EString name