From 321ea81c89d303b8b907aee06a4c22fcd03eb4c6 Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 30 Sep 2020 18:22:18 +0800 Subject: [PATCH] [deploy] update button component --- src/kiwano/base/component/Button.cpp | 55 ++++++------ src/kiwano/base/component/Button.h | 123 +++++++++++++-------------- 2 files changed, 83 insertions(+), 95 deletions(-) diff --git a/src/kiwano/base/component/Button.cpp b/src/kiwano/base/component/Button.cpp index 6971549d..9425d5fd 100644 --- a/src/kiwano/base/component/Button.cpp +++ b/src/kiwano/base/component/Button.cpp @@ -25,32 +25,17 @@ namespace kiwano { -Button::Button(const Callback& click) - : Button(click, nullptr, nullptr, nullptr) -{ -} - -Button::Button(const Callback& click, const Callback& pressed, const Callback& mouse_over, const Callback& mouse_out) - : status_(Status::Normal) - , click_callback_(click) - , pressed_callback_(pressed) - , mouse_over_callback_(mouse_over) - , mouse_out_callback_(mouse_out) -{ - SetName("__KGE_BUTTON__"); -} - -Button::Button() +ButtonBase::ButtonBase() : status_(Status::Normal) { SetName("__KGE_BUTTON__"); } -Button::~Button() +ButtonBase::~ButtonBase() { } -void Button::SetStatus(Status status) +void ButtonBase::SetStatus(Status status) { if (status_ != status) { @@ -58,10 +43,8 @@ void Button::SetStatus(Status status) if (status == Status::Normal) { + OnEvent(Event::MouseOut); Application::GetInstance().GetWindow()->SetCursor(CursorType::Arrow); - - if (mouse_out_callback_) - mouse_out_callback_(this, GetBoundActor()); } else if (status == Status::Hover) { @@ -69,21 +52,19 @@ void Button::SetStatus(Status status) if (old_status != Status::Pressed) { - if (mouse_over_callback_) - mouse_over_callback_(this, GetBoundActor()); + OnEvent(Event::MouseOver); } } else if (status == Status::Pressed) { - if (pressed_callback_) - pressed_callback_(this, GetBoundActor()); + OnEvent(Event::Pressed); } status_ = status; } } -void Button::InitComponent(Actor* actor) +void ButtonBase::InitComponent(Actor* actor) { Component::InitComponent(actor); if (actor) @@ -92,12 +73,12 @@ void Button::InitComponent(Actor* actor) } } -void Button::DestroyComponent() +void ButtonBase::DestroyComponent() { Component::DestroyComponent(); } -void Button::HandleEvent(Event* evt) +void ButtonBase::HandleEvent(kiwano::Event* evt) { if (evt->IsType()) { @@ -117,8 +98,22 @@ void Button::HandleEvent(Event* evt) } else if (evt->IsType()) { - if (click_callback_) - click_callback_(this, GetBoundActor()); + OnEvent(Event::Click); + } +} + +Button::Button() {} + +Button::Button(const Callback& cb) +{ + SetCallback(cb); +} + +void Button::OnEvent(Event evt) +{ + if (cb_) + { + cb_(this, evt); } } diff --git a/src/kiwano/base/component/Button.h b/src/kiwano/base/component/Button.h index 069a70e9..007a9d4f 100644 --- a/src/kiwano/base/component/Button.h +++ b/src/kiwano/base/component/Button.h @@ -33,62 +33,43 @@ KGE_DECLARE_SMART_PTR(Button); /** * \~chinese - * @brief 按钮 + * @brief 基础按钮组件 */ -class KGE_API Button : public Component +class KGE_API ButtonBase : public Component { public: /// \~chinese - /// @brief 按钮回调函数 - using Callback = Function; + /// @brief 按钮事件 + enum class Event + { + Click, ///< 被点击 + Pressed, ///< 被按下 + MouseOver, ///< 鼠标浮于按钮上 + MouseOut, ///< 鼠标从按钮上移出 + }; - /// \~chinese - /// @brief 创建按钮 - /// @param click 按钮点击回调函数 - Button(const Callback& click); - - /// \~chinese - /// @brief 创建按钮 - /// @param click 按钮点击回调函数 - /// @param pressed 按钮按下回调函数 - /// @param mouse_over 按钮移入回调函数 - /// @param mouse_out 按钮移出回调函数 - Button(const Callback& click, const Callback& pressed, const Callback& mouse_over, const Callback& mouse_out); - - Button(); - - virtual ~Button(); - - /// \~chinese - /// @brief 设置按钮点击后的回调函数 - void SetClickCallback(const Callback& func); - - /// \~chinese - /// @brief 设置按钮被按下时的回调函数 - void SetPressedCallback(const Callback& func); - - /// \~chinese - /// @brief 设置鼠标移入按钮时的回调函数 - void SetMouseOverCallback(const Callback& func); - - /// \~chinese - /// @brief 设置鼠标移出按钮时的回调函数 - void SetMouseOutCallback(const Callback& func); - -protected: /// \~chinese /// @brief 按钮状态 enum class Status { - Normal, ///< 普通 - Hover, ///< 鼠标在按钮内 - Pressed ///< 被按下 + Normal, ///< 普通 + Hover, ///< 鼠标在按钮内 + Pressed, ///< 被按下 }; + ButtonBase(); + + virtual ~ButtonBase(); + + /// \~chinese + /// @brief 按钮事件发生时 + virtual void OnEvent(Event evt) = 0; + /// \~chinese /// @brief 获取按钮状态 Status GetStatus() const; +protected: /// \~chinese /// @brief 设置按钮状态 void SetStatus(Status status); @@ -103,39 +84,51 @@ protected: /// \~chinese /// @brief 处理角色事件 - void HandleEvent(Event* evt) override; + void HandleEvent(kiwano::Event* evt) override; private: - Status status_; - Callback click_callback_; - Callback pressed_callback_; - Callback mouse_over_callback_; - Callback mouse_out_callback_; + Status status_; +}; + +/** + * \~chinese + * @brief 按钮组件 + */ +class KGE_API Button : public ButtonBase +{ +public: + /// \~chinese + /// @brief 按钮回调函数 + using Callback = Function; + + Button(); + + /// \~chinese + /// @brief 创建按钮 + /// @param cb 按钮回调函数 + Button(const Callback& cb); + + /// \~chinese + /// @brief 设置按钮回调函数 + /// @param cb 按钮回调函数 + void SetCallback(const Callback& cb); + + /// \~chinese + /// @brief 按钮状态变化时 + void OnEvent(Event evt) override; + +private: + Callback cb_; }; /** @} */ -inline void Button::SetClickCallback(const Callback& func) +inline void Button::SetCallback(const Callback& cb) { - click_callback_ = func; + cb_ = cb; } -inline void Button::SetPressedCallback(const Callback& func) -{ - pressed_callback_ = func; -} - -inline void Button::SetMouseOverCallback(const Callback& func) -{ - mouse_over_callback_ = func; -} - -inline void Button::SetMouseOutCallback(const Callback& func) -{ - mouse_out_callback_ = func; -} - -inline Button::Status Button::GetStatus() const +inline ButtonBase::Status ButtonBase::GetStatus() const { return status_; }