Magic_Game/core/Node/Button.cpp

285 lines
5.2 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()
: _func(nullptr)
2018-05-08 17:40:36 +08:00
, _eBtnState(ButtonState::NORMAL)
, _bEnable(true)
, _bIsSelected(false)
, _pNormal(nullptr)
, _pMouseover(nullptr)
, _pSelected(nullptr)
, _pDisabled(nullptr)
2017-10-21 19:09:31 +08:00
{
}
2018-05-08 11:37:11 +08:00
e2d::Button::Button(Node * normal, const Function& func)
: _func(nullptr)
2018-05-08 17:40:36 +08:00
, _eBtnState(ButtonState::NORMAL)
, _bEnable(true)
, _bIsSelected(false)
, _pNormal(nullptr)
, _pMouseover(nullptr)
, _pSelected(nullptr)
, _pDisabled(nullptr)
2017-10-21 19:09:31 +08:00
{
this->setNormal(normal);
this->setClickFunc(func);
2017-10-21 19:09:31 +08:00
}
2018-05-08 11:37:11 +08:00
e2d::Button::Button(Node * normal, Node * selected, const Function& func)
: _func(nullptr)
2018-05-08 17:40:36 +08:00
, _eBtnState(ButtonState::NORMAL)
, _bEnable(true)
, _bIsSelected(false)
, _pNormal(nullptr)
, _pMouseover(nullptr)
, _pSelected(nullptr)
, _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
}
2018-05-08 11:37:11 +08:00
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, const Function& func)
: _func(nullptr)
2018-05-08 17:40:36 +08:00
, _eBtnState(ButtonState::NORMAL)
, _bEnable(true)
, _bIsSelected(false)
, _pNormal(nullptr)
, _pMouseover(nullptr)
, _pSelected(nullptr)
, _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
}
2018-05-08 11:37:11 +08:00
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, const Function& func)
: _func(nullptr)
2018-05-08 17:40:36 +08:00
, _eBtnState(ButtonState::NORMAL)
, _bEnable(true)
, _bIsSelected(false)
, _pNormal(nullptr)
, _pMouseover(nullptr)
, _pSelected(nullptr)
, _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
{
2018-05-08 17:40:36 +08:00
return _bEnable;
}
void e2d::Button::setNormal(Node * normal)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
if (normal != _pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2018-05-08 17:40:36 +08:00
if (_pNormal)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
this->removeChild(_pNormal);
2017-10-21 19:09:31 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (normal)
{
this->addChild(normal);
2018-03-01 19:28:22 +08:00
this->setSize(normal->getWidth(), normal->getHeight());
}
2018-05-08 17:40:36 +08:00
_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
{
2018-05-08 17:40:36 +08:00
if (mouseover != _pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2018-05-08 17:40:36 +08:00
if (_pMouseover)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
this->removeChild(_pMouseover);
2017-10-21 19:09:31 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (mouseover)
{
this->addChild(mouseover);
}
2018-05-08 17:40:36 +08:00
_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
{
2018-05-08 17:40:36 +08:00
if (selected != _pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2018-05-08 17:40:36 +08:00
if (_pSelected)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
this->removeChild(_pSelected);
2017-10-21 19:09:31 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (selected)
{
this->addChild(selected);
}
2018-05-08 17:40:36 +08:00
_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
{
2018-05-08 17:40:36 +08:00
if (disabled != _pNormal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
2018-05-08 17:40:36 +08:00
if (_pDisabled)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
this->removeChild(_pDisabled);
2017-10-21 19:09:31 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (disabled)
{
this->addChild(disabled);
}
2018-05-08 17:40:36 +08:00
_pDisabled = disabled;
_updateVisiable();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setEnable(bool enable)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
if (_bEnable != enable)
{
2018-05-08 17:40:36 +08:00
_bEnable = enable;
_updateVisiable();
}
2017-10-21 19:09:31 +08:00
}
2018-05-08 11:37:11 +08:00
void e2d::Button::setClickFunc(const Function& func)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
WARN_IF(_pNormal == nullptr, "Button cannot work without anything to show. Please set its normal displayed.");
_func = 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-05-08 17:40:36 +08:00
if (_bEnable && _bVisiable && _pNormal)
2018-02-06 21:11:54 +08:00
{
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>
2018-05-08 17:40:36 +08:00
if (_bIsSelected &&
_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
_runCallback();
}
2018-05-08 17:40:36 +08:00
// <20><><EFBFBD><EFBFBD> _bIsSelected Ϊ false
_bIsSelected = false;
2018-01-30 16:45:38 +08:00
}
if (Input::isMouseLButtonPress())
2018-01-30 16:45:38 +08:00
{
2018-05-08 17:40:36 +08:00
if (_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
2018-05-08 17:40:36 +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> _bIsSelected Ϊ true
_bIsSelected = true;
2018-01-30 16:45:38 +08:00
return;
}
}
2018-05-08 17:40:36 +08:00
if (_bIsSelected && Input::isMouseLButtonDown())
2018-01-30 16:45:38 +08:00
{
2018-05-08 17:40:36 +08:00
if (_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
_setState(ButtonState::SELECTED);
Window::setCursor(Cursor::HAND);
2018-01-30 16:45:38 +08:00
return;
}
}
2018-05-08 17:40:36 +08:00
else if (_pNormal->isPointIn(Input::getMousePos()))
2018-01-30 16:45:38 +08:00
{
_setState(ButtonState::MOUSEOVER);
Window::setCursor(Cursor::HAND);
2018-01-30 16:45:38 +08:00
return;
}
_setState(ButtonState::NORMAL);
2018-01-30 16:45:38 +08:00
}
2018-05-08 17:40:36 +08:00
if (_bVisiable && !_bEnable && _pNormal && _pNormal->isPointIn(Input::getMousePos()))
{
Window::setCursor(Cursor::NO);
}
2017-12-16 15:49:48 +08:00
}
void e2d::Button::_setState(ButtonState state)
2018-02-04 21:24:27 +08:00
{
2018-05-08 17:40:36 +08:00
if (_eBtnState != state)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
_eBtnState = state;
_updateVisiable();
2017-10-21 19:09:31 +08:00
}
}
2017-10-21 19:09:31 +08:00
void e2d::Button::_updateVisiable()
{
2018-05-08 17:40:36 +08:00
SAFE_SET(_pNormal, setVisiable, false);
SAFE_SET(_pMouseover, setVisiable, false);
SAFE_SET(_pSelected, setVisiable, false);
SAFE_SET(_pDisabled, setVisiable, false);
2017-10-21 19:09:31 +08:00
2018-05-08 17:40:36 +08:00
if (_bEnable)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
if (_eBtnState == ButtonState::SELECTED && _pSelected)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
_pSelected->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
2018-05-08 17:40:36 +08:00
else if (_eBtnState == ButtonState::MOUSEOVER && _pMouseover)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
_pMouseover->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
else
{
2018-05-08 17:40:36 +08:00
if (_pNormal) _pNormal->setVisiable(true);
}
}
else
{
2018-05-08 17:40:36 +08:00
if (_pDisabled)
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
_pDisabled->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
else
2017-10-21 19:09:31 +08:00
{
2018-05-08 17:40:36 +08:00
if (_pNormal) _pNormal->setVisiable(true);
2017-10-21 19:09:31 +08:00
}
}
}
void e2d::Button::_runCallback()
{
if (_func)
2017-10-21 19:09:31 +08:00
{
_func();
2017-10-21 19:09:31 +08:00
}
}