From 192a5204054fe2877786ab53c7f7663370f8f9c9 Mon Sep 17 00:00:00 2001 From: Nomango Date: Sun, 4 Oct 2020 03:58:44 +0800 Subject: [PATCH] update Button component --- src/kiwano/base/component/Button.cpp | 50 ++++++++-------------------- src/kiwano/base/component/Button.h | 45 +++++++++++-------------- 2 files changed, 34 insertions(+), 61 deletions(-) diff --git a/src/kiwano/base/component/Button.cpp b/src/kiwano/base/component/Button.cpp index 9425d5fd..13ad89a0 100644 --- a/src/kiwano/base/component/Button.cpp +++ b/src/kiwano/base/component/Button.cpp @@ -19,8 +19,6 @@ // THE SOFTWARE. #include -#include -#include 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()) { - SetStatus(Status::Hover); + OnEvent(Event::MouseEntered); + status_ = Status::Hover; } else if (evt->IsType()) { - SetStatus(Status::Normal); + OnEvent(Event::MouseExited); + status_ = Status::Normal; } else if (evt->IsType() && status_ == Status::Hover) { - SetStatus(Status::Pressed); + OnEvent(Event::Pressed); + status_ = Status::Pressed; } else if (evt->IsType() && status_ == Status::Pressed) { - SetStatus(Status::Hover); + OnEvent(Event::Released); + status_ = Status::Hover; } else if (evt->IsType()) { - 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 diff --git a/src/kiwano/base/component/Button.h b/src/kiwano/base/component/Button.h index 007a9d4f..633593c7 100644 --- a/src/kiwano/base/component/Button.h +++ b/src/kiwano/base/component/Button.h @@ -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& cb); + /// \~chinese /// @brief 按钮状态变化时 void OnEvent(Event evt) override; private: Callback cb_; + Function 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& cb) { - return status_; + clicked_cb_ = cb; } } // namespace kiwano