Magic_Game/core/Node/Button.cpp

286 lines
5.1 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-24 20:37:34 +08:00
, _state(ButtonState::Normal)
2018-07-04 17:00:21 +08:00
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
, _selected(nullptr)
, _disabled(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-24 20:37:34 +08:00
, _state(ButtonState::Normal)
2018-07-04 17:00:21 +08:00
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
, _selected(nullptr)
, _disabled(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-24 20:37:34 +08:00
, _state(ButtonState::Normal)
2018-07-04 17:00:21 +08:00
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
, _selected(nullptr)
, _disabled(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-24 20:37:34 +08:00
, _state(ButtonState::Normal)
2018-07-04 17:00:21 +08:00
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
, _selected(nullptr)
, _disabled(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-24 20:37:34 +08:00
, _state(ButtonState::Normal)
2018-07-04 17:00:21 +08:00
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
, _selected(nullptr)
, _disabled(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-07-04 17:00:21 +08:00
return _enabled;
}
void e2d::Button::setNormal(Node * normal)
2017-10-21 19:09:31 +08:00
{
if (normal != _normal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
if (_normal)
2017-10-21 19:09:31 +08:00
{
this->removeChild(_normal);
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());
}
_normal = normal;
2018-07-07 01:48:39 +08:00
_updateVisible();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setMouseOver(Node * mouseover)
2017-10-21 19:09:31 +08:00
{
if (mouseover != _normal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
if (_mouseover)
2017-10-21 19:09:31 +08:00
{
this->removeChild(_mouseover);
2017-10-21 19:09:31 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (mouseover)
{
this->addChild(mouseover);
}
_mouseover = mouseover;
2018-07-07 01:48:39 +08:00
_updateVisible();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setSelected(Node * selected)
2017-10-21 19:09:31 +08:00
{
if (selected != _normal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
if (_selected)
2017-10-21 19:09:31 +08:00
{
this->removeChild(_selected);
2017-10-21 19:09:31 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (selected)
{
this->addChild(selected);
}
_selected = selected;
2018-07-07 01:48:39 +08:00
_updateVisible();
2017-10-21 19:09:31 +08:00
}
}
void e2d::Button::setDisabled(Node * disabled)
2017-10-21 19:09:31 +08:00
{
if (disabled != _normal)
2017-10-21 19:09:31 +08:00
{
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
if (_disabled)
2017-10-21 19:09:31 +08:00
{
this->removeChild(_disabled);
2017-10-21 19:09:31 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
if (disabled)
{
this->addChild(disabled);
}
_disabled = disabled;
2018-07-07 01:48:39 +08:00
_updateVisible();
2017-10-21 19:09:31 +08:00
}
}
2018-07-04 17:00:21 +08:00
void e2d::Button::setEnabled(bool enabled)
2017-10-21 19:09:31 +08:00
{
2018-07-04 17:00:21 +08:00
if (_enabled != enabled)
{
2018-07-04 17:00:21 +08:00
_enabled = enabled;
2018-07-07 01:48:39 +08:00
_updateVisible();
}
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
{
_func = func;
2017-10-21 19:09:31 +08:00
}
2018-05-19 00:00:12 +08:00
void e2d::Button::_fixedUpdate()
2017-12-16 15:49:48 +08:00
{
if (SceneManager::getInstance()->isTransitioning())
2018-02-06 21:11:54 +08:00
return;
2018-01-30 16:45:38 +08:00
2018-07-04 15:33:09 +08:00
auto input = Input::getInstance();
auto window = Window::getInstance();
2018-07-07 01:48:39 +08:00
if (_enabled && _visible && _normal)
2018-02-06 21:11:54 +08:00
{
if (input->isRelease(MouseCode::Left))
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 (_isSelected &&
2018-07-04 15:33:09 +08:00
_normal->containsPoint(input->getMousePos()))
2018-01-30 16:45:38 +08:00
{
_runCallback();
}
// <20><><EFBFBD><EFBFBD> _isSelected Ϊ false
_isSelected = false;
2018-01-30 16:45:38 +08:00
}
if (input->isPress(MouseCode::Left))
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
if (_normal->containsPoint(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> _isSelected Ϊ true
_isSelected = true;
2018-01-30 16:45:38 +08:00
return;
}
}
if (_isSelected && input->isDown(MouseCode::Left))
2018-01-30 16:45:38 +08:00
{
2018-07-04 15:33:09 +08:00
if (_normal->containsPoint(input->getMousePos()))
2018-01-30 16:45:38 +08:00
{
2018-05-24 20:37:34 +08:00
_setState(ButtonState::Selected);
2018-07-04 15:33:09 +08:00
window->setCursor(Window::Cursor::Hand);
2018-01-30 16:45:38 +08:00
return;
}
}
2018-07-04 15:33:09 +08:00
else if (_normal->containsPoint(input->getMousePos()))
2018-01-30 16:45:38 +08:00
{
2018-05-24 20:37:34 +08:00
_setState(ButtonState::Mouseover);
2018-07-04 15:33:09 +08:00
window->setCursor(Window::Cursor::Hand);
2018-01-30 16:45:38 +08:00
return;
}
2018-05-24 20:37:34 +08:00
_setState(ButtonState::Normal);
2018-01-30 16:45:38 +08:00
}
2018-07-07 01:48:39 +08:00
if (_visible && !_enabled && _normal && _normal->containsPoint(input->getMousePos()))
{
2018-07-04 15:33:09 +08:00
window->setCursor(Window::Cursor::No);
}
2017-12-16 15:49:48 +08:00
}
void e2d::Button::_setState(ButtonState state)
2018-02-04 21:24:27 +08:00
{
if (_state != state)
2017-10-21 19:09:31 +08:00
{
_state = state;
2018-07-07 01:48:39 +08:00
_updateVisible();
2017-10-21 19:09:31 +08:00
}
}
2017-10-21 19:09:31 +08:00
2018-07-07 01:48:39 +08:00
void e2d::Button::_updateVisible()
{
2018-07-07 01:48:39 +08:00
SAFE_SET(_normal, setVisible, false);
SAFE_SET(_mouseover, setVisible, false);
SAFE_SET(_selected, setVisible, false);
SAFE_SET(_disabled, setVisible, false);
2017-10-21 19:09:31 +08:00
2018-07-04 17:00:21 +08:00
if (_enabled)
2017-10-21 19:09:31 +08:00
{
2018-05-24 20:37:34 +08:00
if (_state == ButtonState::Selected && _selected)
2017-10-21 19:09:31 +08:00
{
2018-07-07 01:48:39 +08:00
_selected->setVisible(true);
2017-10-21 19:09:31 +08:00
}
2018-05-24 20:37:34 +08:00
else if (_state == ButtonState::Mouseover && _mouseover)
2017-10-21 19:09:31 +08:00
{
2018-07-07 01:48:39 +08:00
_mouseover->setVisible(true);
2017-10-21 19:09:31 +08:00
}
else
{
2018-07-07 01:48:39 +08:00
if (_normal) _normal->setVisible(true);
}
}
else
{
if (_disabled)
2017-10-21 19:09:31 +08:00
{
2018-07-07 01:48:39 +08:00
_disabled->setVisible(true);
2017-10-21 19:09:31 +08:00
}
else
2017-10-21 19:09:31 +08:00
{
2018-07-07 01:48:39 +08:00
if (_normal) _normal->setVisible(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
}
}