Magic_Game/core/Node/Button.cpp

278 lines
5.0 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dnode.h"
#include "..\e2dmanager.h"
2017-10-21 19:09:31 +08:00
#define SAFE_SET(pointer, func, ...) if (pointer) { pointer->##func(__VA_ARGS__); }
e2d::Button::Button()
2018-03-08 15:03:12 +08:00
: m_Callback(nullptr)
, m_eBtnState(Button::NORMAL)
, 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)
{
}
e2d::Button::Button(Node * normal, Function func)
2018-03-08 15:03:12 +08:00
: m_Callback(nullptr)
, m_eBtnState(Button::NORMAL)
2017-12-15 21:51:07 +08:00
, m_bEnable(true)
, m_bIsSelected(false)
, m_pNormal(nullptr)
, m_pMouseover(nullptr)
, m_pSelected(nullptr)
, m_pDisabled(nullptr)
2017-10-21 19:09:31 +08:00
{
this->setNormal(normal);
this->setClickFunc(func);
2017-10-21 19:09:31 +08:00
}
e2d::Button::Button(Node * normal, Node * selected, Function func)
2018-03-08 15:03:12 +08:00
: m_Callback(nullptr)
, m_eBtnState(Button::NORMAL)
2017-12-15 21:51:07 +08:00
, m_bEnable(true)
, m_bIsSelected(false)
, m_pNormal(nullptr)
, m_pMouseover(nullptr)
, m_pSelected(nullptr)
, m_pDisabled(nullptr)
2017-10-21 19:09:31 +08:00
{
this->setNormal(normal);
this->setSelected(selected);
this->setClickFunc(func);
2017-10-21 19:09:31 +08:00
}
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Function func)
2018-03-08 15:03:12 +08:00
: m_Callback(nullptr)
, m_eBtnState(Button::NORMAL)
2017-12-15 21:51:07 +08:00
, m_bEnable(true)
, m_bIsSelected(false)
, m_pNormal(nullptr)
, m_pMouseover(nullptr)
, m_pSelected(nullptr)
, m_pDisabled(nullptr)
2017-10-21 19:09:31 +08:00
{
this->setNormal(normal);
this->setMouseOver(mouseover);
this->setSelected(selected);
this->setClickFunc(func);
2017-10-21 19:09:31 +08:00
}
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, Function func)
2018-03-08 15:03:12 +08:00
: m_Callback(nullptr)
, m_eBtnState(Button::NORMAL)
2017-12-15 21:51:07 +08:00
, m_bEnable(true)
, m_bIsSelected(false)
, m_pNormal(nullptr)
, m_pMouseover(nullptr)
, m_pSelected(nullptr)
, m_pDisabled(nullptr)
2017-10-21 19:09:31 +08:00
{
this->setNormal(normal);
this->setMouseOver(mouseover);
this->setSelected(selected);
this->setDisabled(disabled);
this->setClickFunc(func);
2017-10-21 19:09:31 +08:00
}
bool e2d::Button::isEnable() const
{
return m_bEnable;
}
void e2d::Button::setNormal(Node * normal)
2017-10-21 19:09:31 +08:00
{
if (normal != m_pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2017-10-21 19:09:31 +08:00
if (m_pNormal)
{
this->removeChild(m_pNormal);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (normal)
{
this->addChild(normal);
2018-03-01 19:28:22 +08:00
this->setSize(normal->getWidth(), normal->getHeight());
}
2017-10-21 19:09:31 +08:00
m_pNormal = normal;
_updateVisiable();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setMouseOver(Node * mouseover)
2017-10-21 19:09:31 +08:00
{
if (mouseover != m_pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2017-10-21 19:09:31 +08:00
if (m_pMouseover)
{
this->removeChild(m_pMouseover);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (mouseover)
{
this->addChild(mouseover);
}
2017-10-21 19:09:31 +08:00
m_pMouseover = mouseover;
_updateVisiable();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setSelected(Node * selected)
2017-10-21 19:09:31 +08:00
{
if (selected != m_pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2017-10-21 19:09:31 +08:00
if (m_pSelected)
{
this->removeChild(m_pSelected);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (selected)
{
this->addChild(selected);
}
2017-10-21 19:09:31 +08:00
m_pSelected = selected;
_updateVisiable();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setDisabled(Node * disabled)
2017-10-21 19:09:31 +08:00
{
if (disabled != m_pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2017-10-21 19:09:31 +08:00
if (m_pDisabled)
{
this->removeChild(m_pDisabled);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (disabled)
{
this->addChild(disabled);
}
2017-10-21 19:09:31 +08:00
m_pDisabled = disabled;
_updateVisiable();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setEnable(bool bEnable)
2017-10-21 19:09:31 +08:00
{
if (m_bEnable != bEnable)
{
m_bEnable = bEnable;
_updateVisiable();
}
2017-10-21 19:09:31 +08:00
}
void e2d::Button::setClickFunc(Function func)
2017-10-21 19:09:31 +08:00
{
WARN_IF(m_pNormal == nullptr, "Button cannot work without anything to show. Please set its normal displayed.");
m_Callback = func;
2017-10-21 19:09:31 +08:00
}
void e2d::Button::onFixedUpdate()
2017-12-16 15:49:48 +08:00
{
if (SceneManager::isTransitioning())
2018-02-06 21:11:54 +08:00
return;
2018-01-30 16:45:38 +08:00
2018-02-06 21:11:54 +08:00
if (m_bEnable && m_bVisiable && m_pNormal)
{
if (Input::isMouseLButtonRelease())
2018-01-30 16:45:38 +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>
if (m_bIsSelected &&
m_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
_runCallback();
}
// <20><><EFBFBD><EFBFBD> m_bIsSelected Ϊ false
m_bIsSelected = false;
}
if (Input::isMouseLButtonPress())
2018-01-30 16:45:38 +08:00
{
if (m_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
// <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;
return;
}
}
if (m_bIsSelected && Input::isMouseLButtonDown())
2018-01-30 16:45:38 +08:00
{
if (m_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
_setState(Button::SELECTED);
2018-01-30 16:45:38 +08:00
return;
}
}
else if (m_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
_setState(Button::MOUSEOVER);
2018-01-30 16:45:38 +08:00
return;
}
_setState(Button::NORMAL);
2018-01-30 16:45:38 +08:00
}
2017-12-16 15:49:48 +08:00
}
void e2d::Button::_setState(BTN_STATE state)
2018-02-04 21:24:27 +08:00
{
if (m_eBtnState != state)
2017-10-21 19:09:31 +08:00
{
2018-02-04 21:24:27 +08:00
m_eBtnState = state;
_updateVisiable();
2017-10-21 19:09:31 +08:00
}
}
2017-10-21 19:09:31 +08:00
void e2d::Button::_updateVisiable()
{
SAFE_SET(m_pNormal, setVisiable, false);
SAFE_SET(m_pMouseover, setVisiable, false);
SAFE_SET(m_pSelected, setVisiable, false);
SAFE_SET(m_pDisabled, setVisiable, false);
2017-10-21 19:09:31 +08:00
if (m_bEnable)
2017-10-21 19:09:31 +08:00
{
if (m_eBtnState == Button::SELECTED && m_pSelected)
2017-10-21 19:09:31 +08:00
{
m_pSelected->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
else if (m_eBtnState == Button::MOUSEOVER && m_pMouseover)
2017-10-21 19:09:31 +08:00
{
m_pMouseover->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
else
{
if (m_pNormal) m_pNormal->setVisiable(true);
}
}
else
{
if (m_pDisabled)
2017-10-21 19:09:31 +08:00
{
m_pDisabled->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
else
2017-10-21 19:09:31 +08:00
{
if (m_pNormal) m_pNormal->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
}
}
void e2d::Button::_runCallback()
{
if (m_Callback)
2017-10-21 19:09:31 +08:00
{
m_Callback();
2017-10-21 19:09:31 +08:00
}
}