2018-11-08 21:39:26 +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.
|
|
|
|
|
|
|
|
|
|
#include "Button.h"
|
2019-01-22 10:23:12 +08:00
|
|
|
#include "../core/MouseEvent.hpp"
|
2018-11-08 21:39:26 +08:00
|
|
|
|
|
|
|
|
namespace easy2d
|
|
|
|
|
{
|
|
|
|
|
namespace ui
|
|
|
|
|
{
|
|
|
|
|
Button::Button()
|
2018-11-25 15:00:15 +08:00
|
|
|
: enabled_(true)
|
2018-11-08 21:39:26 +08:00
|
|
|
, is_selected_(false)
|
2018-11-25 19:29:32 +08:00
|
|
|
, click_callback_(nullptr)
|
2018-11-08 21:39:26 +08:00
|
|
|
, status_(Status::Normal)
|
|
|
|
|
{
|
2018-11-25 15:00:15 +08:00
|
|
|
AddListener(MouseEvent::Hover, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
|
|
|
|
|
AddListener(MouseEvent::Out, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
|
|
|
|
|
AddListener(MouseEvent::Down, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
|
|
|
|
|
AddListener(MouseEvent::Up, std::bind(&Button::UpdateStatus, this, std::placeholders::_1));
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-25 19:29:32 +08:00
|
|
|
Button::Button(const Callback& click)
|
2018-11-25 15:00:15 +08:00
|
|
|
: Button()
|
2018-11-08 21:39:26 +08:00
|
|
|
{
|
2018-11-25 19:29:32 +08:00
|
|
|
this->SetClickCallback(click);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Button::Button(Callback const & click, Callback const & pressed, Callback const & mouse_over, Callback const & mouse_out)
|
|
|
|
|
: Button()
|
|
|
|
|
{
|
|
|
|
|
this->SetClickCallback(click);
|
|
|
|
|
this->SetPressedCallback(pressed);
|
|
|
|
|
this->SetMouseOverCallback(mouse_over);
|
|
|
|
|
this->SetMouseOutCallback(mouse_out);
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-14 01:34:41 +08:00
|
|
|
Button::~Button()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 21:39:26 +08:00
|
|
|
bool Button::IsEnable() const
|
|
|
|
|
{
|
|
|
|
|
return enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::SetEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
if (enabled_ != enabled)
|
|
|
|
|
{
|
|
|
|
|
enabled_ = enabled;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 15:00:15 +08:00
|
|
|
void Button::SetClickCallback(const Callback& func)
|
2018-11-08 21:39:26 +08:00
|
|
|
{
|
2018-11-25 19:29:32 +08:00
|
|
|
click_callback_ = func;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::SetPressedCallback(const Callback & func)
|
|
|
|
|
{
|
|
|
|
|
pressed_callback_ = func;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::SetMouseOverCallback(const Callback & func)
|
|
|
|
|
{
|
|
|
|
|
mouse_over_callback_ = func;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::SetMouseOutCallback(const Callback & func)
|
|
|
|
|
{
|
|
|
|
|
mouse_out_callback_ = func;
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::SetStatus(Status status)
|
|
|
|
|
{
|
|
|
|
|
if (status_ != status)
|
|
|
|
|
{
|
|
|
|
|
status_ = status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 15:00:15 +08:00
|
|
|
void Button::UpdateStatus(Event * e)
|
2018-11-08 21:39:26 +08:00
|
|
|
{
|
2018-11-25 15:00:15 +08:00
|
|
|
E2D_ASSERT(MouseEvent::Check(e));
|
2018-11-08 21:39:26 +08:00
|
|
|
|
2018-11-25 15:00:15 +08:00
|
|
|
MouseEvent* me = static_cast<MouseEvent*>(e);
|
2018-11-08 21:39:26 +08:00
|
|
|
if (enabled_)
|
|
|
|
|
{
|
2018-11-25 15:00:15 +08:00
|
|
|
if (me->type == MouseEvent::Hover)
|
2018-11-08 21:39:26 +08:00
|
|
|
{
|
2018-11-25 15:00:15 +08:00
|
|
|
SetStatus(Status::Hover);
|
2018-11-25 19:29:32 +08:00
|
|
|
|
|
|
|
|
if (mouse_over_callback_)
|
|
|
|
|
mouse_over_callback_();
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
2018-11-25 15:00:15 +08:00
|
|
|
else if (me->type == MouseEvent::Out)
|
2018-11-08 21:39:26 +08:00
|
|
|
{
|
2018-11-25 15:00:15 +08:00
|
|
|
SetStatus(Status::Normal);
|
2018-11-25 19:29:32 +08:00
|
|
|
|
|
|
|
|
if (mouse_out_callback_)
|
|
|
|
|
mouse_out_callback_();
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
2018-11-25 15:00:15 +08:00
|
|
|
else if (me->type == MouseEvent::Down && status_ == Status::Hover)
|
2018-11-08 21:39:26 +08:00
|
|
|
{
|
2018-11-25 19:29:32 +08:00
|
|
|
SetStatus(Status::Pressed);
|
|
|
|
|
|
|
|
|
|
if (pressed_callback_)
|
|
|
|
|
pressed_callback_();
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
2018-11-25 19:29:32 +08:00
|
|
|
else if (me->type == MouseEvent::Up && status_ == Status::Pressed)
|
2018-11-08 21:39:26 +08:00
|
|
|
{
|
2018-11-25 15:00:15 +08:00
|
|
|
SetStatus(Status::Hover);
|
|
|
|
|
|
2018-11-25 19:29:32 +08:00
|
|
|
if (click_callback_)
|
|
|
|
|
click_callback_();
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-25 15:00:15 +08:00
|
|
|
|
2018-11-08 21:39:26 +08:00
|
|
|
}
|
|
|
|
|
}
|