300 lines
5.5 KiB
C++
300 lines
5.5 KiB
C++
#include "..\e2dnode.h"
|
|
#include "..\e2dmanager.h"
|
|
|
|
#define SAFE_SET(pointer, func, ...) if (pointer) { pointer->##func(__VA_ARGS__); }
|
|
|
|
|
|
e2d::Button::Button()
|
|
: _func(nullptr)
|
|
, _state(ButtonState::Normal)
|
|
, _enabled(true)
|
|
, _isSelected(false)
|
|
, _normal(nullptr)
|
|
, _mouseover(nullptr)
|
|
, _selected(nullptr)
|
|
, _disabled(nullptr)
|
|
{
|
|
}
|
|
|
|
e2d::Button::Button(Node * normal, const Function& func)
|
|
: _func(nullptr)
|
|
, _state(ButtonState::Normal)
|
|
, _enabled(true)
|
|
, _isSelected(false)
|
|
, _normal(nullptr)
|
|
, _mouseover(nullptr)
|
|
, _selected(nullptr)
|
|
, _disabled(nullptr)
|
|
{
|
|
this->setNormal(normal);
|
|
this->setClickFunc(func);
|
|
}
|
|
|
|
e2d::Button::Button(Node * normal, Node * selected, const Function& func)
|
|
: _func(nullptr)
|
|
, _state(ButtonState::Normal)
|
|
, _enabled(true)
|
|
, _isSelected(false)
|
|
, _normal(nullptr)
|
|
, _mouseover(nullptr)
|
|
, _selected(nullptr)
|
|
, _disabled(nullptr)
|
|
{
|
|
this->setNormal(normal);
|
|
this->setSelected(selected);
|
|
this->setClickFunc(func);
|
|
}
|
|
|
|
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, const Function& func)
|
|
: _func(nullptr)
|
|
, _state(ButtonState::Normal)
|
|
, _enabled(true)
|
|
, _isSelected(false)
|
|
, _normal(nullptr)
|
|
, _mouseover(nullptr)
|
|
, _selected(nullptr)
|
|
, _disabled(nullptr)
|
|
{
|
|
this->setNormal(normal);
|
|
this->setMouseOver(mouseover);
|
|
this->setSelected(selected);
|
|
this->setClickFunc(func);
|
|
}
|
|
|
|
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, const Function& func)
|
|
: _func(nullptr)
|
|
, _state(ButtonState::Normal)
|
|
, _enabled(true)
|
|
, _isSelected(false)
|
|
, _normal(nullptr)
|
|
, _mouseover(nullptr)
|
|
, _selected(nullptr)
|
|
, _disabled(nullptr)
|
|
{
|
|
this->setNormal(normal);
|
|
this->setMouseOver(mouseover);
|
|
this->setSelected(selected);
|
|
this->setDisabled(disabled);
|
|
this->setClickFunc(func);
|
|
}
|
|
|
|
bool e2d::Button::isEnable() const
|
|
{
|
|
return _enabled;
|
|
}
|
|
|
|
void e2d::Button::setNormal(Node * normal)
|
|
{
|
|
if (normal != _normal)
|
|
{
|
|
// ÒÆ³ý¾ÉµÄ
|
|
if (_normal)
|
|
{
|
|
this->removeChild(_normal);
|
|
}
|
|
// Ìí¼ÓеÄ
|
|
if (normal)
|
|
{
|
|
normal->setPivot(_pivotX, _pivotY);
|
|
this->addChild(normal);
|
|
this->setSize(normal->getWidth(), normal->getHeight());
|
|
}
|
|
_normal = normal;
|
|
|
|
_updateVisible();
|
|
}
|
|
}
|
|
|
|
void e2d::Button::setMouseOver(Node * mouseover)
|
|
{
|
|
if (mouseover != _normal)
|
|
{
|
|
// ÒÆ³ý¾ÉµÄ
|
|
if (_mouseover)
|
|
{
|
|
this->removeChild(_mouseover);
|
|
}
|
|
// Ìí¼ÓеÄ
|
|
if (mouseover)
|
|
{
|
|
mouseover->setPivot(_pivotX, _pivotY);
|
|
this->addChild(mouseover);
|
|
}
|
|
_mouseover = mouseover;
|
|
_updateVisible();
|
|
}
|
|
}
|
|
|
|
void e2d::Button::setSelected(Node * selected)
|
|
{
|
|
if (selected != _normal)
|
|
{
|
|
// ÒÆ³ý¾ÉµÄ
|
|
if (_selected)
|
|
{
|
|
this->removeChild(_selected);
|
|
}
|
|
// Ìí¼ÓеÄ
|
|
if (selected)
|
|
{
|
|
selected->setPivot(_pivotX, _pivotY);
|
|
this->addChild(selected);
|
|
}
|
|
_selected = selected;
|
|
_updateVisible();
|
|
}
|
|
}
|
|
|
|
void e2d::Button::setDisabled(Node * disabled)
|
|
{
|
|
if (disabled != _normal)
|
|
{
|
|
// ÒÆ³ý¾ÉµÄ
|
|
if (_disabled)
|
|
{
|
|
this->removeChild(_disabled);
|
|
}
|
|
// Ìí¼ÓеÄ
|
|
if (disabled)
|
|
{
|
|
disabled->setPivot(_pivotX, _pivotY);
|
|
this->addChild(disabled);
|
|
}
|
|
_disabled = disabled;
|
|
_updateVisible();
|
|
}
|
|
}
|
|
|
|
void e2d::Button::setEnabled(bool enabled)
|
|
{
|
|
if (_enabled != enabled)
|
|
{
|
|
_enabled = enabled;
|
|
_updateVisible();
|
|
}
|
|
}
|
|
|
|
void e2d::Button::setClickFunc(const Function& func)
|
|
{
|
|
_func = func;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void e2d::Button::_update()
|
|
{
|
|
Node::_update();
|
|
|
|
if (SceneManager::getInstance()->isTransitioning())
|
|
return;
|
|
|
|
auto input = Input::getInstance();
|
|
auto window = Window::getInstance();
|
|
|
|
if (_enabled && _visible && _normal)
|
|
{
|
|
if (input->isRelease(MouseCode::Left))
|
|
{
|
|
// Êó±ê×ó¼ü̧Æðʱ£¬ÅжÏÊó±ê×ø±êÊÇ·ñÔÚ°´Å¥ÄÚ²¿
|
|
if (_isSelected &&
|
|
_normal->containsPoint(input->getMousePos()))
|
|
{
|
|
_isSelected = false;
|
|
_runCallback();
|
|
}
|
|
}
|
|
|
|
if (input->isPress(MouseCode::Left))
|
|
{
|
|
if (_normal->containsPoint(input->getMousePos()))
|
|
{
|
|
// Êó±ê×ó¼ü°´Ï£¬ÇÒλÓÚ°´Å¥ÄÚʱ£¬±ê¼Ç _isSelected Ϊ true
|
|
_isSelected = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (_isSelected && input->isDown(MouseCode::Left))
|
|
{
|
|
if (_normal->containsPoint(input->getMousePos()))
|
|
{
|
|
_setState(ButtonState::Selected);
|
|
window->setCursor(Window::Cursor::Hand);
|
|
return;
|
|
}
|
|
}
|
|
else if (_normal->containsPoint(input->getMousePos()))
|
|
{
|
|
_setState(ButtonState::Mouseover);
|
|
window->setCursor(Window::Cursor::Hand);
|
|
return;
|
|
}
|
|
|
|
_setState(ButtonState::Normal);
|
|
}
|
|
|
|
if (_visible && !_enabled && _normal && _normal->containsPoint(input->getMousePos()))
|
|
{
|
|
window->setCursor(Window::Cursor::No);
|
|
}
|
|
}
|
|
|
|
void e2d::Button::_setState(ButtonState state)
|
|
{
|
|
if (_state != state)
|
|
{
|
|
_state = state;
|
|
_updateVisible();
|
|
}
|
|
}
|
|
|
|
void e2d::Button::_updateVisible()
|
|
{
|
|
SAFE_SET(_normal, setVisible, false);
|
|
SAFE_SET(_mouseover, setVisible, false);
|
|
SAFE_SET(_selected, setVisible, false);
|
|
SAFE_SET(_disabled, setVisible, false);
|
|
|
|
if (_enabled)
|
|
{
|
|
if (_state == ButtonState::Selected && _selected)
|
|
{
|
|
_selected->setVisible(true);
|
|
}
|
|
else if (_state == ButtonState::Mouseover && _mouseover)
|
|
{
|
|
_mouseover->setVisible(true);
|
|
}
|
|
else
|
|
{
|
|
if (_normal) _normal->setVisible(true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_disabled)
|
|
{
|
|
_disabled->setVisible(true);
|
|
}
|
|
else
|
|
{
|
|
if (_normal) _normal->setVisible(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
void e2d::Button::_runCallback()
|
|
{
|
|
if (_func)
|
|
{
|
|
_func();
|
|
}
|
|
}
|