From a7b29bbdf9621272ef366ae1f77274e02cc45cd4 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 8 Nov 2017 11:18:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86EMenu=E7=A6=81?= =?UTF-8?q?=E7=94=A8=E5=90=8E=EF=BC=8CEButton=E4=BB=8D=E7=84=B6=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=87=AA=E8=A1=8C=E5=90=AF=E7=94=A8=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Easy2D/Easy2D.vcxproj | 1 + Easy2D/Easy2D.vcxproj.filters | 3 +++ Easy2D/Node/EButton.cpp | 10 +++------- Easy2D/Node/EMenu.cpp | 37 +++++++++++++++++++++++++++++------ Easy2D/enodes.h | 3 +++ 5 files changed, 41 insertions(+), 13 deletions(-) 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();