2017-10-21 19:09:31 +08:00
|
|
|
|
#include "..\enodes.h"
|
2017-10-26 17:17:30 +08:00
|
|
|
|
#include "..\elisteners.h"
|
|
|
|
|
|
#include "..\emanagers.h"
|
2017-10-21 19:09:31 +08:00
|
|
|
|
#include "..\Win\winbase.h"
|
|
|
|
|
|
|
|
|
|
|
|
e2d::EButton::EButton()
|
2017-11-07 22:20:46 +08:00
|
|
|
|
: m_Callback((const BUTTON_CLICK_CALLBACK &)nullptr)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
, m_eStatus(STATUS::NORMAL)
|
2017-11-07 22:20:46 +08:00
|
|
|
|
, m_bEnable(true)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
, m_bIsSelected(false)
|
|
|
|
|
|
, m_pNormal(nullptr)
|
|
|
|
|
|
, m_pMouseover(nullptr)
|
|
|
|
|
|
, m_pSelected(nullptr)
|
|
|
|
|
|
, m_pDisabled(nullptr)
|
2017-11-07 22:20:46 +08:00
|
|
|
|
, m_pListener(nullptr)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-08 11:18:11 +08:00
|
|
|
|
m_pListener = new EListenerMouse(std::bind(&EButton::_updateStatus, this));
|
|
|
|
|
|
m_pListener->setAlwaysWorking(true);
|
|
|
|
|
|
EMsgManager::bindListener(m_pListener, this);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::EButton::EButton(ENode * normal, const BUTTON_CLICK_CALLBACK & callback)
|
|
|
|
|
|
: EButton()
|
|
|
|
|
|
{
|
|
|
|
|
|
this->setNormal(normal);
|
|
|
|
|
|
this->setCallback(callback);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::EButton::EButton(ENode * normal, ENode * selected, const BUTTON_CLICK_CALLBACK & callback)
|
|
|
|
|
|
: EButton()
|
|
|
|
|
|
{
|
|
|
|
|
|
this->setNormal(normal);
|
|
|
|
|
|
this->setSelected(selected);
|
|
|
|
|
|
this->setCallback(callback);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::EButton::EButton(ENode * normal, ENode * mouseover, ENode * selected, const BUTTON_CLICK_CALLBACK & callback)
|
|
|
|
|
|
: EButton()
|
|
|
|
|
|
{
|
|
|
|
|
|
this->setNormal(normal);
|
|
|
|
|
|
this->setMouseOver(mouseover);
|
|
|
|
|
|
this->setSelected(selected);
|
|
|
|
|
|
this->setCallback(callback);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::EButton::EButton(ENode * normal, ENode * mouseover, ENode * selected, ENode * disabled, const BUTTON_CLICK_CALLBACK & callback)
|
|
|
|
|
|
: EButton()
|
|
|
|
|
|
{
|
|
|
|
|
|
this->setNormal(normal);
|
|
|
|
|
|
this->setMouseOver(mouseover);
|
|
|
|
|
|
this->setSelected(selected);
|
|
|
|
|
|
this->setDisabled(disabled);
|
|
|
|
|
|
this->setCallback(callback);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
bool e2d::EButton::isEnable() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_bEnable;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-21 19:09:31 +08:00
|
|
|
|
void e2d::EButton::setNormal(ENode * normal)
|
|
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (normal != m_pNormal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2017-10-21 19:09:31 +08:00
|
|
|
|
if (m_pNormal)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->removeChild(m_pNormal);
|
|
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (normal)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->addChild(normal);
|
|
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
m_pNormal = normal;
|
2017-11-09 18:22:41 +08:00
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
_updateVisiable();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::EButton::setMouseOver(ENode * mouseover)
|
|
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (mouseover != m_pNormal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2017-10-21 19:09:31 +08:00
|
|
|
|
if (m_pMouseover)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->removeChild(m_pMouseover);
|
|
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (mouseover)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->addChild(mouseover);
|
|
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
m_pMouseover = mouseover;
|
2017-11-07 22:20:46 +08:00
|
|
|
|
_updateVisiable();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::EButton::setSelected(ENode * selected)
|
|
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (selected != m_pNormal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2017-10-21 19:09:31 +08:00
|
|
|
|
if (m_pSelected)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->removeChild(m_pSelected);
|
|
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (selected)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->addChild(selected);
|
|
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
m_pSelected = selected;
|
2017-11-07 22:20:46 +08:00
|
|
|
|
_updateVisiable();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::EButton::setDisabled(ENode * disabled)
|
|
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (disabled != m_pNormal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2017-10-21 19:09:31 +08:00
|
|
|
|
if (m_pDisabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->removeChild(m_pDisabled);
|
|
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (disabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->addChild(disabled);
|
|
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
m_pDisabled = disabled;
|
2017-11-07 22:20:46 +08:00
|
|
|
|
_updateVisiable();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
void e2d::EButton::setEnable(bool bEnable)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (m_bEnable != bEnable)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_bEnable = bEnable;
|
2017-11-09 18:22:41 +08:00
|
|
|
|
_updateVisiable();
|
2017-11-07 22:20:46 +08:00
|
|
|
|
_updateStatus();
|
|
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::EButton::setCallback(const BUTTON_CLICK_CALLBACK & callback)
|
|
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
WARN_IF(m_pNormal == nullptr, "EButton cannot work without something to show. Please set its normal displayed.");
|
|
|
|
|
|
|
2017-11-04 16:20:17 +08:00
|
|
|
|
m_Callback = callback;
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
void e2d::EButton::_setStatus(STATUS status)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (m_eStatus != status)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
m_eStatus = status;
|
|
|
|
|
|
_updateVisiable();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
|
2017-11-09 18:22:41 +08:00
|
|
|
|
void e2d::EButton::_updateTransform()
|
|
|
|
|
|
{
|
|
|
|
|
|
ENode::_updateTransform();
|
|
|
|
|
|
_updateStatus();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
void e2d::EButton::_updateVisiable()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_pNormal) m_pNormal->setVisiable(false);
|
|
|
|
|
|
if (m_pMouseover) m_pMouseover->setVisiable(false);
|
|
|
|
|
|
if (m_pSelected) m_pSelected->setVisiable(false);
|
|
|
|
|
|
if (m_pDisabled) m_pDisabled->setVisiable(false);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (m_bEnable)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (m_eStatus == STATUS::SELECTED && m_pSelected)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
m_pSelected->setVisiable(true);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
else if (m_eStatus == STATUS::MOUSEOVER && m_pMouseover)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
m_pMouseover->setVisiable(true);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_pNormal) m_pNormal->setVisiable(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_pDisabled)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
m_pDisabled->setVisiable(true);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
else
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (m_pNormal) m_pNormal->setVisiable(true);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
void e2d::EButton::_updateStatus()
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (m_bEnable && m_pNormal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
ENode * pMouseover = m_pMouseover ? m_pMouseover : m_pNormal;
|
|
|
|
|
|
ENode * pSelected = m_pSelected ? m_pSelected : m_pNormal;
|
2017-11-03 22:14:07 +08:00
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_UP)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̧<EFBFBD><CCA7>ʱ<EFBFBD><CAB1><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ڰ<EFBFBD>ť<EFBFBD>ڲ<EFBFBD>
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (m_bIsSelected &&
|
|
|
|
|
|
pSelected->isPointIn(EMouseMsg::getPos()))
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
_runCallback();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> m_bIsSelected Ϊ false
|
|
|
|
|
|
m_bIsSelected = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (pMouseover->isPointIn(EMouseMsg::getPos()))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD>λ<EFBFBD>ڰ<EFBFBD>ť<EFBFBD><C5A5>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> m_bIsSelected Ϊ true
|
|
|
|
|
|
m_bIsSelected = true;
|
|
|
|
|
|
_setStatus(STATUS::SELECTED);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
|
|
|
|
|
|
if (m_bIsSelected && EMouseMsg::isLButtonDown())
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
if (pSelected->isPointIn(EMouseMsg::getPos()))
|
|
|
|
|
|
{
|
|
|
|
|
|
_setStatus(STATUS::SELECTED);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
else if (m_pNormal->isPointIn(EMouseMsg::getPos()))
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
_setStatus(STATUS::MOUSEOVER);
|
|
|
|
|
|
return;
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
|
|
|
|
|
|
_setStatus(STATUS::NORMAL);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::EButton::_runCallback()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_Callback)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
m_Callback();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|