diff --git a/Easy2D/Easy2D.vcxproj b/Easy2D/Easy2D.vcxproj index 842141d0..5820be2a 100644 --- a/Easy2D/Easy2D.vcxproj +++ b/Easy2D/Easy2D.vcxproj @@ -214,6 +214,7 @@ + diff --git a/Easy2D/Easy2D.vcxproj.filters b/Easy2D/Easy2D.vcxproj.filters index 9dc4e2be..b5590415 100644 --- a/Easy2D/Easy2D.vcxproj.filters +++ b/Easy2D/Easy2D.vcxproj.filters @@ -213,6 +213,9 @@ Common + + Common + diff --git a/Easy2D/Node/EButton.cpp b/Easy2D/Node/EButton.cpp index c605bcdb..572fe0c6 100644 --- a/Easy2D/Node/EButton.cpp +++ b/Easy2D/Node/EButton.cpp @@ -14,6 +14,9 @@ e2d::EButton::EButton() , m_pDisabled(nullptr) , m_pListener(nullptr) { + m_pListener = new EListenerMouse(std::bind(&EButton::_updateStatus, this)); + m_pListener->setAlwaysWorking(true); + EMsgManager::bindListener(m_pListener, this); } e2d::EButton::EButton(ENode * normal, const BUTTON_CLICK_CALLBACK & callback) @@ -145,13 +148,6 @@ void e2d::EButton::setCallback(const BUTTON_CLICK_CALLBACK & callback) WARN_IF(m_pNormal == nullptr, "EButton cannot work without something to show. Please set its normal displayed."); m_Callback = callback; - - if (m_pListener == nullptr) - { - m_pListener = new EListenerMouse(std::bind(&EButton::_updateStatus, this)); - m_pListener->setAlwaysWorking(true); - EMsgManager::bindListener(m_pListener, this); - } } void e2d::EButton::_setStatus(STATUS status) diff --git a/Easy2D/Node/EMenu.cpp b/Easy2D/Node/EMenu.cpp index bcdab0d8..ff44409b 100644 --- a/Easy2D/Node/EMenu.cpp +++ b/Easy2D/Node/EMenu.cpp @@ -1,4 +1,5 @@ #include "..\enodes.h" +#include "..\elisteners.h" e2d::EMenu::EMenu() : m_bEnable(true) @@ -30,18 +31,40 @@ size_t e2d::EMenu::getButtonCount() const void e2d::EMenu::setEnable(bool enable) { - m_bEnable = enable; - - for (auto &buttons : m_vButtons) + if (m_bEnable != enable) { - buttons->setEnable(enable); + m_bEnable = enable; + + for (auto &button : m_vButtons) + { + if (enable) + { + button->m_pListener->start(); + } + else + { + button->m_pListener->stop(); + } + } } } void e2d::EMenu::addButton(EButton * button) { - this->addChild(button); - m_vButtons.push_back(button); + if (button) + { + this->addChild(button); + m_vButtons.push_back(button); + + if (m_bEnable) + { + button->m_pListener->start(); + } + else + { + button->m_pListener->stop(); + } + } } bool e2d::EMenu::removeButton(EButton * button) @@ -60,6 +83,8 @@ bool e2d::EMenu::removeButton(EButton * button) { if (m_vButtons[i] == button) { + // 移除按钮前,将它的监听器启用 + button->m_pListener->start(); m_vButtons.erase(m_vButtons.begin() + i); return true; } diff --git a/Easy2D/enodes.h b/Easy2D/enodes.h index 23418aa3..49bdada1 100644 --- a/Easy2D/enodes.h +++ b/Easy2D/enodes.h @@ -10,6 +10,7 @@ class EAction; class EButton; class EButtonToggle; class EGeometry; +class EMenu; class ENode : public EObject @@ -570,6 +571,8 @@ protected: class EButton : public ENode { + friend EMenu; + public: // 创建一个空按钮 EButton();