Magic_Game/core/Node/Button.cpp

261 lines
5.2 KiB
C++
Raw Normal View History

2018-09-05 13:17:07 +08:00
#include "..\e2dcomponent.h"
2018-04-21 21:24:46 +08:00
#include "..\e2dmanager.h"
2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
2017-10-21 19:09:31 +08:00
#define SAFE_SET(pointer, func, ...) if (pointer) { pointer->##func(__VA_ARGS__); }
#define SET_BUTTON_NODE(Old, New) \
if (New != Old) \
{ \
2018-09-04 22:42:34 +08:00
if (Old) this->RemoveChild(Old); \
if (New) \
{ \
2018-09-04 22:42:34 +08:00
New->SetAnchor(anchor_.x, anchor_.y); \
this->AddChild(New); \
} \
Old = New; \
2018-09-04 22:42:34 +08:00
UpdateVisible(); \
} \
e2d::Button::Button()
2018-09-04 22:42:34 +08:00
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(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-09-04 22:42:34 +08:00
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
this->SetNormal(normal);
this->SetCallbackOnClick(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-09-04 22:42:34 +08:00
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
this->SetNormal(normal);
this->SetSelected(selected);
this->SetCallbackOnClick(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-09-04 22:42:34 +08:00
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
this->SetNormal(normal);
this->SetMouseOver(mouseover);
this->SetSelected(selected);
this->SetCallbackOnClick(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-09-04 22:42:34 +08:00
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
this->SetNormal(normal);
this->SetMouseOver(mouseover);
this->SetSelected(selected);
this->SetDisabled(disabled);
this->SetCallbackOnClick(func);
2017-10-21 19:09:31 +08:00
}
2018-09-04 22:42:34 +08:00
bool e2d::Button::IsEnable() const
{
2018-09-04 22:42:34 +08:00
return enabled_;
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetNormal(Node * normal)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
SET_BUTTON_NODE(normal_, normal);
if (normal)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
this->SetSize(normal->GetWidth(), normal->GetHeight());
2017-10-21 19:09:31 +08:00
}
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetMouseOver(Node * mouseover)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
SET_BUTTON_NODE(mouseover_, mouseover);
2017-10-21 19:09:31 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetSelected(Node * selected)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
SET_BUTTON_NODE(selected_, selected);
2017-10-21 19:09:31 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetDisabled(Node * disabled)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
SET_BUTTON_NODE(disabled_, disabled);
2017-10-21 19:09:31 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetEnabled(bool enabled)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
if (enabled_ != enabled)
{
2018-09-04 22:42:34 +08:00
enabled_ = enabled;
UpdateVisible();
}
2017-10-21 19:09:31 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetCallbackOnClick(const Function& func)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
callback_ = func;
2017-10-21 19:09:31 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetAnchor(float anchor_x, float anchor_y)
2018-07-28 20:48:25 +08:00
{
2018-09-04 22:42:34 +08:00
Node::SetAnchor(anchor_x, anchor_y);
SAFE_SET(normal_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(mouseover_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(selected_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(disabled_, SetAnchor, anchor_x, anchor_y);
2018-07-28 20:48:25 +08:00
}
2018-09-04 22:42:34 +08:00
bool e2d::Button::Dispatch(const MouseEvent & e, bool handled)
2017-12-16 15:49:48 +08:00
{
2018-09-04 22:42:34 +08:00
if (!handled && enabled_ && visible_ && normal_)
2018-02-06 21:11:54 +08:00
{
2018-09-04 22:42:34 +08:00
bool contains = normal_->ContainsPoint(e.GetPos());
if (e.GetType() == MouseEvent::Type::LeftUp && is_selected_ && contains)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
OnClick();
is_selected_ = false;
SetStatus(Status::Normal);
return true;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
else if (e.GetType() == MouseEvent::Type::LeftDown)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
is_selected_ = contains;
SetStatus(contains ? Status::Selected : Status::Normal);
if (contains)
return true;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
else if (e.GetType() == MouseEvent::Type::LeftUp)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
is_selected_ = false;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
else if (e.GetType() == MouseEvent::Type::Move && is_selected_ && contains)
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
SetStatus(Status::Selected);
return true;
2018-07-28 22:22:58 +08:00
}
else
{
2018-09-04 22:42:34 +08:00
if (!e.IsLButtonDown() && is_selected_)
2018-07-28 22:22:58 +08:00
{
2018-09-04 22:42:34 +08:00
is_selected_ = false;
2018-07-28 22:22:58 +08:00
}
2018-09-04 22:42:34 +08:00
SetStatus(contains ? Status::Mouseover : Status::Normal);
if (contains)
return true;
2018-01-30 16:45:38 +08:00
}
}
2018-09-04 22:42:34 +08:00
return Node::Dispatch(e, handled);
2018-07-28 22:22:58 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Button::Visit()
2018-07-28 22:22:58 +08:00
{
2018-09-04 22:42:34 +08:00
Node::Visit();
2018-07-28 22:22:58 +08:00
2018-09-04 22:42:34 +08:00
if (visible_ &&
!enabled_ &&
normal_ &&
normal_->ContainsPoint(Input::GetInstance()->GetMousePos()))
2018-07-28 22:22:58 +08:00
{
2018-09-04 22:42:34 +08:00
Window::GetInstance()->SetCursor(Window::Cursor::No);
2018-07-28 22:22:58 +08:00
}
2018-09-04 22:42:34 +08:00
else if (status_ == Status::Mouseover || status_ == Status::Selected)
{
2018-09-04 22:42:34 +08:00
Window::GetInstance()->SetCursor(Window::Cursor::Hand);
}
2017-12-16 15:49:48 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Button::SetStatus(Status status)
2018-02-04 21:24:27 +08:00
{
2018-09-04 22:42:34 +08:00
if (status_ != status)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
status_ = status;
UpdateVisible();
2017-10-21 19:09:31 +08:00
}
}
2017-10-21 19:09:31 +08:00
2018-09-04 22:42:34 +08:00
void e2d::Button::UpdateVisible()
{
2018-09-04 22:42:34 +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-09-04 22:42:34 +08:00
if (enabled_)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
if (status_ == Status::Selected && selected_)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
selected_->SetVisible(true);
2017-10-21 19:09:31 +08:00
}
2018-09-04 22:42:34 +08:00
else if (status_ == Status::Mouseover && mouseover_)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
mouseover_->SetVisible(true);
2017-10-21 19:09:31 +08:00
}
else
{
2018-09-04 22:42:34 +08:00
if (normal_) normal_->SetVisible(true);
}
}
else
{
2018-09-04 22:42:34 +08:00
if (disabled_)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
disabled_->SetVisible(true);
2017-10-21 19:09:31 +08:00
}
else
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
if (normal_) normal_->SetVisible(true);
2017-10-21 19:09:31 +08:00
}
}
}
2018-09-04 22:42:34 +08:00
void e2d::Button::OnClick()
{
2018-09-04 22:42:34 +08:00
if (callback_)
2017-10-21 19:09:31 +08:00
{
2018-09-04 22:42:34 +08:00
callback_();
2017-10-21 19:09:31 +08:00
}
}