2018-04-21 21:24:46 +08:00
|
|
|
|
#include "..\e2dnode.h"
|
|
|
|
|
|
#include "..\e2dmanager.h"
|
2017-10-21 19:09:31 +08:00
|
|
|
|
|
2018-04-22 17:08:41 +08:00
|
|
|
|
#define SAFE_SET(pointer, func, ...) if (pointer) { pointer->##func(__VA_ARGS__); }
|
2018-02-14 20:38:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
e2d::Button::Button()
|
2018-05-08 20:03:29 +08:00
|
|
|
|
: _func(nullptr)
|
2018-07-28 22:22:58 +08:00
|
|
|
|
, _status(Status::Normal)
|
2018-07-04 17:00:21 +08:00
|
|
|
|
, _enabled(true)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
, _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)
|
2018-05-08 20:03:29 +08:00
|
|
|
|
: _func(nullptr)
|
2018-07-28 22:22:58 +08:00
|
|
|
|
, _status(Status::Normal)
|
2018-07-04 17:00:21 +08:00
|
|
|
|
, _enabled(true)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
, _isSelected(false)
|
|
|
|
|
|
, _normal(nullptr)
|
|
|
|
|
|
, _mouseover(nullptr)
|
|
|
|
|
|
, _selected(nullptr)
|
|
|
|
|
|
, _disabled(nullptr)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
this->setNormal(normal);
|
2018-03-31 18:12:01 +08:00
|
|
|
|
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)
|
2018-05-08 20:03:29 +08:00
|
|
|
|
: _func(nullptr)
|
2018-07-28 22:22:58 +08:00
|
|
|
|
, _status(Status::Normal)
|
2018-07-04 17:00:21 +08:00
|
|
|
|
, _enabled(true)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
, _isSelected(false)
|
|
|
|
|
|
, _normal(nullptr)
|
|
|
|
|
|
, _mouseover(nullptr)
|
|
|
|
|
|
, _selected(nullptr)
|
|
|
|
|
|
, _disabled(nullptr)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
this->setNormal(normal);
|
|
|
|
|
|
this->setSelected(selected);
|
2018-03-31 18:12:01 +08:00
|
|
|
|
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)
|
2018-05-08 20:03:29 +08:00
|
|
|
|
: _func(nullptr)
|
2018-07-28 22:22:58 +08:00
|
|
|
|
, _status(Status::Normal)
|
2018-07-04 17:00:21 +08:00
|
|
|
|
, _enabled(true)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
, _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);
|
2018-03-31 18:12:01 +08:00
|
|
|
|
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)
|
2018-05-08 20:03:29 +08:00
|
|
|
|
: _func(nullptr)
|
2018-07-28 22:22:58 +08:00
|
|
|
|
, _status(Status::Normal)
|
2018-07-04 17:00:21 +08:00
|
|
|
|
, _enabled(true)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
, _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);
|
2018-03-31 18:12:01 +08:00
|
|
|
|
this->setClickFunc(func);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
bool e2d::Button::isEnable() const
|
2017-11-07 22:20:46 +08:00
|
|
|
|
{
|
2018-07-04 17:00:21 +08:00
|
|
|
|
return _enabled;
|
2017-11-07 22:20:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Button::setNormal(Node * normal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (normal != _normal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_normal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
this->removeChild(_normal);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (normal)
|
|
|
|
|
|
{
|
2018-07-28 20:48:25 +08:00
|
|
|
|
normal->setPivot(_pivotX, _pivotY);
|
2017-11-07 22:20:46 +08:00
|
|
|
|
this->addChild(normal);
|
2018-03-01 19:28:22 +08:00
|
|
|
|
this->setSize(normal->getWidth(), normal->getHeight());
|
2017-11-07 22:20:46 +08:00
|
|
|
|
}
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_normal = normal;
|
2017-11-09 18:22:41 +08:00
|
|
|
|
|
2018-07-07 01:48:39 +08:00
|
|
|
|
_updateVisible();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Button::setMouseOver(Node * mouseover)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (mouseover != _normal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_mouseover)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
this->removeChild(_mouseover);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (mouseover)
|
|
|
|
|
|
{
|
2018-07-28 20:48:25 +08:00
|
|
|
|
mouseover->setPivot(_pivotX, _pivotY);
|
2017-11-07 22:20:46 +08:00
|
|
|
|
this->addChild(mouseover);
|
|
|
|
|
|
}
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_mouseover = mouseover;
|
2018-07-07 01:48:39 +08:00
|
|
|
|
_updateVisible();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Button::setSelected(Node * selected)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (selected != _normal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_selected)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
this->removeChild(_selected);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (selected)
|
|
|
|
|
|
{
|
2018-07-28 20:48:25 +08:00
|
|
|
|
selected->setPivot(_pivotX, _pivotY);
|
2017-11-07 22:20:46 +08:00
|
|
|
|
this->addChild(selected);
|
|
|
|
|
|
}
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_selected = selected;
|
2018-07-07 01:48:39 +08:00
|
|
|
|
_updateVisible();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Button::setDisabled(Node * disabled)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (disabled != _normal)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ɵ<EFBFBD>
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_disabled)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
this->removeChild(_disabled);
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|
|
|
|
|
if (disabled)
|
|
|
|
|
|
{
|
2018-07-28 20:48:25 +08:00
|
|
|
|
disabled->setPivot(_pivotX, _pivotY);
|
2017-11-07 22:20:46 +08:00
|
|
|
|
this->addChild(disabled);
|
|
|
|
|
|
}
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_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)
|
2017-11-07 22:20:46 +08:00
|
|
|
|
{
|
2018-07-04 17:00:21 +08:00
|
|
|
|
_enabled = enabled;
|
2018-07-07 01:48:39 +08:00
|
|
|
|
_updateVisible();
|
2017-11-07 22:20:46 +08:00
|
|
|
|
}
|
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 20:03:29 +08:00
|
|
|
|
_func = func;
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-28 20:48:25 +08:00
|
|
|
|
void e2d::Button::setPivot(float pivotX, float pivotY)
|
|
|
|
|
|
{
|
|
|
|
|
|
Node::setPivot(pivotX, pivotY);
|
|
|
|
|
|
SAFE_SET(_normal, setPivot, pivotX, pivotY);
|
|
|
|
|
|
SAFE_SET(_mouseover, setPivot, pivotX, pivotY);
|
|
|
|
|
|
SAFE_SET(_selected, setPivot, pivotX, pivotY);
|
|
|
|
|
|
SAFE_SET(_disabled, setPivot, pivotX, pivotY);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-28 22:22:58 +08:00
|
|
|
|
void e2d::Button::dispatch(const MouseEvent & e)
|
2017-12-16 15:49:48 +08:00
|
|
|
|
{
|
2018-07-07 01:48:39 +08:00
|
|
|
|
if (_enabled && _visible && _normal)
|
2018-02-06 21:11:54 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
bool contains = _normal->containsPoint(e.getPos());
|
|
|
|
|
|
if (e.getType() == MouseEvent::Type::LeftUp && _isSelected && contains)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
_isSelected = false;
|
|
|
|
|
|
_runCallback();
|
|
|
|
|
|
_setStatus(Status::Normal);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
2018-07-28 22:22:58 +08:00
|
|
|
|
else if (e.getType() == MouseEvent::Type::LeftDown)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
_isSelected = contains;
|
|
|
|
|
|
_setStatus(contains ? Status::Selected : Status::Normal);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
2018-07-28 22:22:58 +08:00
|
|
|
|
else if (e.getType() == MouseEvent::Type::LeftUp)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
_isSelected = false;
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
2018-07-28 22:22:58 +08:00
|
|
|
|
else if (e.getType() == MouseEvent::Type::Move && _isSelected && contains)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
_setStatus(Status::Selected);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!e.isLButtonDown() && _isSelected)
|
|
|
|
|
|
{
|
|
|
|
|
|
_isSelected = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
_setStatus(contains ? Status::Mouseover : Status::Normal);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-05-07 17:03:59 +08:00
|
|
|
|
|
2018-07-28 22:22:58 +08:00
|
|
|
|
Node::dispatch(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::Button::_render()
|
|
|
|
|
|
{
|
|
|
|
|
|
Node::_render();
|
|
|
|
|
|
|
|
|
|
|
|
if (_visible &&
|
|
|
|
|
|
!_enabled &&
|
|
|
|
|
|
_normal &&
|
|
|
|
|
|
_normal->containsPoint(Input::getInstance()->getMousePos()))
|
|
|
|
|
|
{
|
|
|
|
|
|
Window::getInstance()->setCursor(Window::Cursor::No);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (_status == Status::Mouseover || _status == Status::Selected)
|
2018-05-07 17:03:59 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
Window::getInstance()->setCursor(Window::Cursor::Hand);
|
2018-05-07 17:03:59 +08:00
|
|
|
|
}
|
2017-12-16 15:49:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-28 22:22:58 +08:00
|
|
|
|
void e2d::Button::_setStatus(Status status)
|
2018-02-04 21:24:27 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
if (_status != status)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-07-28 22:22:58 +08:00
|
|
|
|
_status = status;
|
2018-07-07 01:48:39 +08:00
|
|
|
|
_updateVisible();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
}
|
2017-10-21 19:09:31 +08:00
|
|
|
|
|
2018-07-07 01:48:39 +08:00
|
|
|
|
void e2d::Button::_updateVisible()
|
2017-11-07 22:20:46 +08:00
|
|
|
|
{
|
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-07-28 22:22:58 +08:00
|
|
|
|
if (_status == Status::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-07-28 22:22:58 +08:00
|
|
|
|
else if (_status == Status::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
|
|
|
|
}
|
2017-11-07 22:20:46 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-07-07 01:48:39 +08:00
|
|
|
|
if (_normal) _normal->setVisible(true);
|
2017-11-07 22:20:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
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
|
|
|
|
}
|
2017-11-07 22:20:46 +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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Button::_runCallback()
|
2017-11-07 22:20:46 +08:00
|
|
|
|
{
|
2018-05-08 20:03:29 +08:00
|
|
|
|
if (_func)
|
2017-10-21 19:09:31 +08:00
|
|
|
|
{
|
2018-05-08 20:03:29 +08:00
|
|
|
|
_func();
|
2017-10-21 19:09:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|