Magic_Game/core/components/Button.cpp

285 lines
6.5 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2018-09-05 13:17:07 +08:00
#include "..\e2dcomponent.h"
#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) \
{ \
New->SetPivot(GetPivotX(), GetPivotY()); \
2018-09-04 22:42:34 +08:00
this->AddChild(New); \
} \
Old = New; \
2018-09-04 22:42:34 +08:00
UpdateVisible(); \
} \
2018-11-08 00:21:59 +08:00
namespace easy2d
2017-10-21 19:09:31 +08:00
{
2018-11-08 00:21:59 +08:00
Button::Button()
: 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-11-08 00:21:59 +08:00
Button::Button(Node * normal, const Callback& func)
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
{
this->SetNormal(normal);
this->SetCallbackOnClick(func);
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
Button::Button(Node * normal, Node * selected, const Callback& func)
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
{
this->SetNormal(normal);
this->SetSelected(selected);
this->SetCallbackOnClick(func);
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
Button::Button(Node * normal, Node * mouseover, Node * selected, const Callback& func)
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
{
this->SetNormal(normal);
this->SetMouseOver(mouseover);
this->SetSelected(selected);
this->SetCallbackOnClick(func);
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, const Callback& func)
: callback_(nullptr)
, status_(Status::Normal)
, enabled_(true)
, is_selected_(false)
, normal_(nullptr)
, mouseover_(nullptr)
, selected_(nullptr)
, disabled_(nullptr)
{
this->SetNormal(normal);
this->SetMouseOver(mouseover);
this->SetSelected(selected);
this->SetDisabled(disabled);
this->SetCallbackOnClick(func);
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
bool Button::IsEnable() const
{
return enabled_;
}
2018-11-08 00:21:59 +08:00
void Button::SetNormal(Node * normal)
2017-10-21 19:09:31 +08:00
{
2018-11-08 00:21:59 +08:00
SET_BUTTON_NODE(normal_, normal);
if (normal)
{
this->SetSize(normal->GetWidth(), normal->GetHeight());
}
2017-10-21 19:09:31 +08:00
}
2018-11-08 00:21:59 +08:00
void Button::SetMouseOver(Node * mouseover)
{
SET_BUTTON_NODE(mouseover_, mouseover);
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
void Button::SetSelected(Node * selected)
{
SET_BUTTON_NODE(selected_, selected);
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
void Button::SetDisabled(Node * disabled)
{
SET_BUTTON_NODE(disabled_, disabled);
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
void Button::SetEnabled(bool enabled)
{
2018-11-08 00:21:59 +08:00
if (enabled_ != enabled)
{
enabled_ = enabled;
UpdateVisible();
}
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
void Button::SetCallbackOnClick(const Callback& func)
{
callback_ = func;
}
2017-10-21 19:09:31 +08:00
2018-11-08 00:21:59 +08:00
void Button::SetPivot(float pivot_x, float pivot_y)
{
Node::SetPivot(pivot_x, pivot_y);
SAFE_SET(normal_, SetPivot, pivot_x, pivot_y);
SAFE_SET(mouseover_, SetPivot, pivot_x, pivot_y);
SAFE_SET(selected_, SetPivot, pivot_x, pivot_y);
SAFE_SET(disabled_, SetPivot, pivot_x, pivot_y);
}
2018-07-28 20:48:25 +08:00
2018-11-08 00:21:59 +08:00
bool Button::Dispatch(const MouseEvent & e, bool handled)
2018-02-06 21:11:54 +08:00
{
2018-11-08 00:21:59 +08:00
if (!handled && enabled_ && IsVisible() && normal_)
2018-01-30 16:45:38 +08:00
{
2018-11-08 00:21:59 +08:00
bool contains = normal_->ContainsPoint(e.GetPosition());
if (e.GetType() == MouseEvent::Type::LeftUp && is_selected_ && contains)
2018-09-07 23:57:32 +08:00
{
2018-11-08 00:21:59 +08:00
if (callback_)
{
callback_();
}
is_selected_ = false;
SetStatus(Status::Normal);
return true;
2018-11-08 00:21:59 +08:00
}
else if (e.GetType() == MouseEvent::Type::LeftDown)
{
is_selected_ = contains;
SetStatus(contains ? Status::Selected : Status::Normal);
if (contains)
return true;
}
else if (e.GetType() == MouseEvent::Type::LeftUp)
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-11-08 00:21:59 +08:00
else if (e.GetType() == MouseEvent::Type::MoveBy && is_selected_ && contains)
{
SetStatus(Status::Selected);
return true;
}
else
{
if (!e.IsLButtonDown() && is_selected_)
{
is_selected_ = false;
}
2018-11-08 00:21:59 +08:00
SetStatus(contains ? Status::Mouseover : Status::Normal);
2018-11-08 00:21:59 +08:00
if (contains)
return true;
}
2018-01-30 16:45:38 +08:00
}
2018-07-28 22:22:58 +08:00
2018-11-08 00:21:59 +08:00
return Node::Dispatch(e, handled);
}
2018-07-28 22:22:58 +08:00
2018-11-08 00:21:59 +08:00
void Button::Visit()
2018-07-28 22:22:58 +08:00
{
2018-11-08 00:21:59 +08:00
Node::Visit();
if (IsVisible() &&
!enabled_ &&
normal_ &&
normal_->ContainsPoint(Device::GetInput()->GetMousePos()))
2018-10-03 18:04:04 +08:00
{
2018-11-08 00:21:59 +08:00
HCURSOR hcursor = ::LoadCursor(nullptr, IDC_NO);
if (hcursor)
{
::SetCursor(hcursor);
}
2018-10-03 18:04:04 +08:00
}
2018-11-08 00:21:59 +08:00
else if (status_ == Status::Mouseover || status_ == Status::Selected)
2018-10-03 18:04:04 +08:00
{
2018-11-08 00:21:59 +08:00
HCURSOR hcursor = ::LoadCursor(nullptr, IDC_HAND);
if (hcursor)
{
::SetCursor(hcursor);
}
2018-10-03 18:04:04 +08:00
}
}
2017-12-16 15:49:48 +08:00
2018-11-08 00:21:59 +08:00
void Button::SetStatus(Status status)
2017-10-21 19:09:31 +08:00
{
2018-11-08 00:21:59 +08:00
if (status_ != status)
2017-10-21 19:09:31 +08:00
{
2018-11-08 00:21:59 +08:00
status_ = status;
UpdateVisible();
}
}
2018-11-08 00:21:59 +08:00
void Button::UpdateVisible()
{
2018-11-08 00:21:59 +08:00
SAFE_SET(normal_, SetVisible, false);
SAFE_SET(mouseover_, SetVisible, false);
SAFE_SET(selected_, SetVisible, false);
SAFE_SET(disabled_, SetVisible, false);
if (enabled_)
2017-10-21 19:09:31 +08:00
{
2018-11-08 00:21:59 +08:00
if (status_ == Status::Selected && selected_)
{
selected_->SetVisible(true);
}
else if (status_ == Status::Mouseover && mouseover_)
{
mouseover_->SetVisible(true);
}
else
{
if (normal_) normal_->SetVisible(true);
}
2017-10-21 19:09:31 +08:00
}
else
2017-10-21 19:09:31 +08:00
{
2018-11-08 00:21:59 +08:00
if (disabled_)
{
disabled_->SetVisible(true);
}
else
{
if (normal_) normal_->SetVisible(true);
}
2017-10-21 19:09:31 +08:00
}
}
2018-11-08 00:21:59 +08:00
}