update Button component

This commit is contained in:
Nomango 2020-10-04 03:58:44 +08:00
parent f9348b8551
commit 192a520405
2 changed files with 34 additions and 61 deletions

View File

@ -19,8 +19,6 @@
// THE SOFTWARE.
#include <kiwano/base/component/Button.h>
#include <kiwano/2d/Stage.h>
#include <kiwano/platform/Application.h>
namespace kiwano
{
@ -35,35 +33,6 @@ ButtonBase::~ButtonBase()
{
}
void ButtonBase::SetStatus(Status status)
{
if (status_ != status)
{
Status old_status = status_;
if (status == Status::Normal)
{
OnEvent(Event::MouseOut);
Application::GetInstance().GetWindow()->SetCursor(CursorType::Arrow);
}
else if (status == Status::Hover)
{
Application::GetInstance().GetWindow()->SetCursor(CursorType::Hand);
if (old_status != Status::Pressed)
{
OnEvent(Event::MouseOver);
}
}
else if (status == Status::Pressed)
{
OnEvent(Event::Pressed);
}
status_ = status;
}
}
void ButtonBase::InitComponent(Actor* actor)
{
Component::InitComponent(actor);
@ -82,23 +51,27 @@ void ButtonBase::HandleEvent(kiwano::Event* evt)
{
if (evt->IsType<MouseHoverEvent>())
{
SetStatus(Status::Hover);
OnEvent(Event::MouseEntered);
status_ = Status::Hover;
}
else if (evt->IsType<MouseOutEvent>())
{
SetStatus(Status::Normal);
OnEvent(Event::MouseExited);
status_ = Status::Normal;
}
else if (evt->IsType<MouseDownEvent>() && status_ == Status::Hover)
{
SetStatus(Status::Pressed);
OnEvent(Event::Pressed);
status_ = Status::Pressed;
}
else if (evt->IsType<MouseUpEvent>() && status_ == Status::Pressed)
{
SetStatus(Status::Hover);
OnEvent(Event::Released);
status_ = Status::Hover;
}
else if (evt->IsType<MouseClickEvent>())
{
OnEvent(Event::Click);
OnEvent(Event::Clicked);
}
}
@ -115,6 +88,11 @@ void Button::OnEvent(Event evt)
{
cb_(this, evt);
}
if (evt == Event::Clicked && clicked_cb_)
{
clicked_cb_();
}
}
} // namespace kiwano

View File

@ -42,19 +42,11 @@ public:
/// @brief 按钮事件
enum class Event
{
Click, ///< 被点击
Pressed, ///< 被按下
MouseOver, ///< 鼠标浮于按钮上
MouseOut, ///< 鼠标从按钮上移出
};
/// \~chinese
/// @brief 按钮状态
enum class Status
{
Normal, ///< 普通
Hover, ///< 鼠标在按钮内
Pressed, ///< 被按下
MouseEntered, ///< 鼠标浮于按钮上
MouseExited, ///< 鼠标从按钮上移出
Pressed, ///< 鼠标按下
Released, ///< 鼠标抬起
Clicked, ///< 鼠标点击
};
ButtonBase();
@ -65,15 +57,7 @@ public:
/// @brief 按钮事件发生时
virtual void OnEvent(Event evt) = 0;
/// \~chinese
/// @brief 获取按钮状态
Status GetStatus() const;
protected:
/// \~chinese
/// @brief 设置按钮状态
void SetStatus(Status status);
/// \~chinese
/// @brief 初始化组件
void InitComponent(Actor* actor) override;
@ -87,7 +71,12 @@ protected:
void HandleEvent(kiwano::Event* evt) override;
private:
Status status_;
enum class Status
{
Normal,
Hover,
Pressed
} status_;
};
/**
@ -113,24 +102,30 @@ public:
/// @param cb 按钮回调函数
void SetCallback(const Callback& cb);
/// \~chinese
/// @brief 设置按钮回调函数
/// @param cb 按钮回调函数
void SetCallbackOnClicked(const Function<void()>& cb);
/// \~chinese
/// @brief 按钮状态变化时
void OnEvent(Event evt) override;
private:
Callback cb_;
Function<void()> clicked_cb_;
};
/** @} */
inline void Button::SetCallback(const Callback& cb)
inline void Button::SetCallback(const Button::Callback& cb)
{
cb_ = cb;
}
inline ButtonBase::Status ButtonBase::GetStatus() const
inline void Button::SetCallbackOnClicked(const Function<void()>& cb)
{
return status_;
clicked_cb_ = cb;
}
} // namespace kiwano