Magic_Game/core/components/Button.cpp

283 lines
6.4 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-10-16 14:13:15 +08:00
easy2d::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-10-16 14:13:15 +08:00
easy2d::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-10-16 14:13:15 +08:00
easy2d::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-10-16 14:13:15 +08:00
easy2d::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-10-16 14:13:15 +08:00
easy2d::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-10-16 14:13:15 +08:00
bool easy2d::Button::IsEnable() const
{
2018-09-04 22:42:34 +08:00
return enabled_;
}
2018-10-16 14:13:15 +08:00
void easy2d::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-10-16 14:13:15 +08:00
void easy2d::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-10-16 14:13:15 +08:00
void easy2d::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-10-16 14:13:15 +08:00
void easy2d::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-10-16 14:13:15 +08:00
void easy2d::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-10-16 14:13:15 +08:00
void easy2d::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-10-16 14:13:15 +08:00
void easy2d::Button::SetPivot(float pivot_x, float pivot_y)
2018-07-28 20:48:25 +08:00
{
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-10-16 14:13:15 +08:00
bool easy2d::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-10-03 18:04:04 +08:00
bool contains = normal_->ContainsPoint(e.GetPosition());
2018-09-04 22:42:34 +08:00
if (e.GetType() == MouseEvent::Type::LeftUp && is_selected_ && contains)
2018-01-30 16:45:38 +08:00
{
2018-09-07 23:57:32 +08:00
if (callback_)
{
callback_();
}
2018-09-04 22:42:34 +08:00
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-16 16:07:51 +08:00
else if (e.GetType() == MouseEvent::Type::MoveBy && 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-10-16 14:13:15 +08:00
void easy2d::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_ &&
2018-10-03 18:04:04 +08:00
normal_->ContainsPoint(Device::GetInput()->GetMousePos()))
2018-07-28 22:22:58 +08:00
{
2018-10-03 18:04:04 +08:00
HCURSOR hcursor = ::LoadCursor(nullptr, IDC_NO);
if (hcursor)
{
::SetCursor(hcursor);
}
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-10-03 18:04:04 +08:00
HCURSOR hcursor = ::LoadCursor(nullptr, IDC_HAND);
if (hcursor)
{
::SetCursor(hcursor);
}
}
2017-12-16 15:49:48 +08:00
}
2018-10-16 14:13:15 +08:00
void easy2d::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-10-16 14:13:15 +08:00
void easy2d::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
}
}
}