update Button component
This commit is contained in:
parent
f9348b8551
commit
192a520405
|
|
@ -19,8 +19,6 @@
|
||||||
// THE SOFTWARE.
|
// THE SOFTWARE.
|
||||||
|
|
||||||
#include <kiwano/base/component/Button.h>
|
#include <kiwano/base/component/Button.h>
|
||||||
#include <kiwano/2d/Stage.h>
|
|
||||||
#include <kiwano/platform/Application.h>
|
|
||||||
|
|
||||||
namespace kiwano
|
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)
|
void ButtonBase::InitComponent(Actor* actor)
|
||||||
{
|
{
|
||||||
Component::InitComponent(actor);
|
Component::InitComponent(actor);
|
||||||
|
|
@ -82,23 +51,27 @@ void ButtonBase::HandleEvent(kiwano::Event* evt)
|
||||||
{
|
{
|
||||||
if (evt->IsType<MouseHoverEvent>())
|
if (evt->IsType<MouseHoverEvent>())
|
||||||
{
|
{
|
||||||
SetStatus(Status::Hover);
|
OnEvent(Event::MouseEntered);
|
||||||
|
status_ = Status::Hover;
|
||||||
}
|
}
|
||||||
else if (evt->IsType<MouseOutEvent>())
|
else if (evt->IsType<MouseOutEvent>())
|
||||||
{
|
{
|
||||||
SetStatus(Status::Normal);
|
OnEvent(Event::MouseExited);
|
||||||
|
status_ = Status::Normal;
|
||||||
}
|
}
|
||||||
else if (evt->IsType<MouseDownEvent>() && status_ == Status::Hover)
|
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)
|
else if (evt->IsType<MouseUpEvent>() && status_ == Status::Pressed)
|
||||||
{
|
{
|
||||||
SetStatus(Status::Hover);
|
OnEvent(Event::Released);
|
||||||
|
status_ = Status::Hover;
|
||||||
}
|
}
|
||||||
else if (evt->IsType<MouseClickEvent>())
|
else if (evt->IsType<MouseClickEvent>())
|
||||||
{
|
{
|
||||||
OnEvent(Event::Click);
|
OnEvent(Event::Clicked);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,6 +88,11 @@ void Button::OnEvent(Event evt)
|
||||||
{
|
{
|
||||||
cb_(this, evt);
|
cb_(this, evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (evt == Event::Clicked && clicked_cb_)
|
||||||
|
{
|
||||||
|
clicked_cb_();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace kiwano
|
} // namespace kiwano
|
||||||
|
|
|
||||||
|
|
@ -42,19 +42,11 @@ public:
|
||||||
/// @brief 按钮事件
|
/// @brief 按钮事件
|
||||||
enum class Event
|
enum class Event
|
||||||
{
|
{
|
||||||
Click, ///< 被点击
|
MouseEntered, ///< 鼠标浮于按钮上
|
||||||
Pressed, ///< 被按下
|
MouseExited, ///< 鼠标从按钮上移出
|
||||||
MouseOver, ///< 鼠标浮于按钮上
|
Pressed, ///< 鼠标按下
|
||||||
MouseOut, ///< 鼠标从按钮上移出
|
Released, ///< 鼠标抬起
|
||||||
};
|
Clicked, ///< 鼠标点击
|
||||||
|
|
||||||
/// \~chinese
|
|
||||||
/// @brief 按钮状态
|
|
||||||
enum class Status
|
|
||||||
{
|
|
||||||
Normal, ///< 普通
|
|
||||||
Hover, ///< 鼠标在按钮内
|
|
||||||
Pressed, ///< 被按下
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ButtonBase();
|
ButtonBase();
|
||||||
|
|
@ -65,15 +57,7 @@ public:
|
||||||
/// @brief 按钮事件发生时
|
/// @brief 按钮事件发生时
|
||||||
virtual void OnEvent(Event evt) = 0;
|
virtual void OnEvent(Event evt) = 0;
|
||||||
|
|
||||||
/// \~chinese
|
|
||||||
/// @brief 获取按钮状态
|
|
||||||
Status GetStatus() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// \~chinese
|
|
||||||
/// @brief 设置按钮状态
|
|
||||||
void SetStatus(Status status);
|
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 初始化组件
|
/// @brief 初始化组件
|
||||||
void InitComponent(Actor* actor) override;
|
void InitComponent(Actor* actor) override;
|
||||||
|
|
@ -87,7 +71,12 @@ protected:
|
||||||
void HandleEvent(kiwano::Event* evt) override;
|
void HandleEvent(kiwano::Event* evt) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Status status_;
|
enum class Status
|
||||||
|
{
|
||||||
|
Normal,
|
||||||
|
Hover,
|
||||||
|
Pressed
|
||||||
|
} status_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -113,24 +102,30 @@ public:
|
||||||
/// @param cb 按钮回调函数
|
/// @param cb 按钮回调函数
|
||||||
void SetCallback(const Callback& cb);
|
void SetCallback(const Callback& cb);
|
||||||
|
|
||||||
|
/// \~chinese
|
||||||
|
/// @brief 设置按钮回调函数
|
||||||
|
/// @param cb 按钮回调函数
|
||||||
|
void SetCallbackOnClicked(const Function<void()>& cb);
|
||||||
|
|
||||||
/// \~chinese
|
/// \~chinese
|
||||||
/// @brief 按钮状态变化时
|
/// @brief 按钮状态变化时
|
||||||
void OnEvent(Event evt) override;
|
void OnEvent(Event evt) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Callback cb_;
|
Callback cb_;
|
||||||
|
Function<void()> clicked_cb_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
inline void Button::SetCallback(const Callback& cb)
|
inline void Button::SetCallback(const Button::Callback& cb)
|
||||||
{
|
{
|
||||||
cb_ = cb;
|
cb_ = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline ButtonBase::Status ButtonBase::GetStatus() const
|
inline void Button::SetCallbackOnClicked(const Function<void()>& cb)
|
||||||
{
|
{
|
||||||
return status_;
|
clicked_cb_ = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace kiwano
|
} // namespace kiwano
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue