From a76add7880c67a7398c7260cc9c7fc25e76815e1 Mon Sep 17 00:00:00 2001 From: Haibo Date: Sun, 25 Nov 2018 19:29:32 +0800 Subject: [PATCH] update Button --- core/base/Node.cpp | 4 +-- core/base/Node.h | 4 +-- core/base/render.cpp | 10 ++++---- core/base/time.cpp | 58 ++++++++++++++++++++++---------------------- core/base/time.h | 8 +++--- core/ui/Button.cpp | 49 +++++++++++++++++++++++++++++++------ core/ui/Button.h | 31 ++++++++++++++++++++--- 7 files changed, 111 insertions(+), 53 deletions(-) diff --git a/core/base/Node.cpp b/core/base/Node.cpp index e284d82a..f6c816a9 100644 --- a/core/base/Node.cpp +++ b/core/base/Node.cpp @@ -190,12 +190,12 @@ namespace easy2d return transform_matrix_inverse_; } - spNode Node::GetParent() const + Node* Node::GetParent() const { return parent_; } - spScene Node::GetScene() const + Scene* Node::GetScene() const { return scene_; } diff --git a/core/base/Node.h b/core/base/Node.h index 9b24b9bf..b377bad3 100644 --- a/core/base/Node.h +++ b/core/base/Node.h @@ -132,10 +132,10 @@ namespace easy2d Matrix const& GetTransformInverseMatrix() const; // 获取父节点 - spNode GetParent() const; + Node* GetParent() const; // 获取所在场景 - spScene GetScene() const; + Scene* GetScene() const; // 设置是否显示 void SetVisible( diff --git a/core/base/render.cpp b/core/base/render.cpp index 10c23d62..591ec79c 100644 --- a/core/base/render.cpp +++ b/core/base/render.cpp @@ -82,11 +82,6 @@ namespace easy2d HRESULT GraphicsDevice::EndDraw() { HRESULT hr = S_OK; - - if (debug_) - { - status_.duration = time::Now() - status_.start; - } if (!window_occluded_) { @@ -100,6 +95,11 @@ namespace easy2d hr = S_OK; } } + + if (debug_) + { + status_.duration = time::Now() - status_.start; + } return hr; } diff --git a/core/base/time.cpp b/core/base/time.cpp index b53192cd..79ef17d7 100644 --- a/core/base/time.cpp +++ b/core/base/time.cpp @@ -99,7 +99,7 @@ namespace easy2d // Duration //------------------------------------------------------- - const Duration Millisecond = Duration(1); + const Duration Millisecond = Duration{ 1LL }; const Duration Second = 1000 * Millisecond; const Duration Minute = 60 * Second; const Duration Hour = 60 * Minute; @@ -123,29 +123,29 @@ namespace easy2d { } - Duration::Duration(int64_t milliseconds) + Duration::Duration(long long milliseconds) : milliseconds_(milliseconds) { } float Duration::Seconds() const { - int64_t sec = milliseconds_ / Second.milliseconds_; - int64_t ms = milliseconds_ % Second.milliseconds_; + long long sec = milliseconds_ / Second.milliseconds_; + long long ms = milliseconds_ % Second.milliseconds_; return static_cast(sec) + static_cast(ms) / 1000.f; } float Duration::Minutes() const { - int64_t min = milliseconds_ / Minute.milliseconds_; - int64_t ms = milliseconds_ % Minute.milliseconds_; + long long min = milliseconds_ / Minute.milliseconds_; + long long ms = milliseconds_ % Minute.milliseconds_; return static_cast(min) + static_cast(ms) / (60 * 1000.f); } float Duration::Hours() const { - int64_t hour = milliseconds_ / Hour.milliseconds_; - int64_t ms = milliseconds_ % Hour.milliseconds_; + long long hour = milliseconds_ / Hour.milliseconds_; + long long ms = milliseconds_ % Hour.milliseconds_; return static_cast(hour) + static_cast(ms) / (60 * 60 * 1000.f); } @@ -157,10 +157,10 @@ namespace easy2d } std::wstring result; - int64_t hour = milliseconds_ / Hour.milliseconds_; - int64_t min = milliseconds_ / Minute.milliseconds_ - hour * 60; - int64_t sec = milliseconds_ / Second.milliseconds_ - (hour * 60 * 60 + min * 60); - int64_t ms = milliseconds_ % Second.milliseconds_; + long long hour = milliseconds_ / Hour.milliseconds_; + long long min = milliseconds_ / Minute.milliseconds_ - hour * 60; + long long sec = milliseconds_ / Second.milliseconds_ - (hour * 60 * 60 + min * 60); + long long ms = milliseconds_ % Second.milliseconds_; if (milliseconds_ < 0) result.append(L"-"); @@ -256,42 +256,42 @@ namespace easy2d const Duration easy2d::time::Duration::operator*(unsigned long long val) const { - return Duration(static_cast(milliseconds_ * val)); + return Duration(static_cast(milliseconds_ * val)); } const Duration easy2d::time::Duration::operator/(unsigned long long val) const { - return Duration(static_cast(milliseconds_ / val)); + return Duration(static_cast(milliseconds_ / val)); } const Duration Duration::operator*(float val) const { - return Duration(static_cast(milliseconds_ * val)); + return Duration(static_cast(milliseconds_ * val)); } const Duration Duration::operator/(float val) const { - return Duration(static_cast(milliseconds_ / val)); + return Duration(static_cast(milliseconds_ / val)); } const Duration Duration::operator*(double val) const { - return Duration(static_cast(milliseconds_ * val)); + return Duration(static_cast(milliseconds_ * val)); } const Duration Duration::operator*(long double val) const { - return Duration(static_cast(milliseconds_ * val)); + return Duration(static_cast(milliseconds_ * val)); } const Duration Duration::operator/(double val) const { - return Duration(static_cast(milliseconds_ / val)); + return Duration(static_cast(milliseconds_ / val)); } const Duration Duration::operator/(long double val) const { - return Duration(static_cast(milliseconds_ / val)); + return Duration(static_cast(milliseconds_ / val)); } Duration & Duration::operator+=(const Duration &other) @@ -314,55 +314,55 @@ namespace easy2d Duration & Duration::operator/=(int val) { - milliseconds_ = static_cast(milliseconds_ / val); + milliseconds_ = static_cast(milliseconds_ / val); return (*this); } Duration & easy2d::time::Duration::operator*=(unsigned long long val) { - milliseconds_ = static_cast(milliseconds_ * val); + milliseconds_ = static_cast(milliseconds_ * val); return (*this); } Duration & easy2d::time::Duration::operator/=(unsigned long long val) { - milliseconds_ = static_cast(milliseconds_ * val); + milliseconds_ = static_cast(milliseconds_ * val); return (*this); } Duration & Duration::operator*=(float val) { - milliseconds_ = static_cast(milliseconds_ * val); + milliseconds_ = static_cast(milliseconds_ * val); return (*this); } Duration & Duration::operator/=(float val) { - milliseconds_ = static_cast(milliseconds_ / val); + milliseconds_ = static_cast(milliseconds_ / val); return (*this); } Duration & Duration::operator*=(double val) { - milliseconds_ = static_cast(milliseconds_ * val); + milliseconds_ = static_cast(milliseconds_ * val); return (*this); } Duration & Duration::operator*=(long double val) { - milliseconds_ = static_cast(milliseconds_ * val); + milliseconds_ = static_cast(milliseconds_ * val); return (*this); } Duration & Duration::operator/=(double val) { - milliseconds_ = static_cast(milliseconds_ / val); + milliseconds_ = static_cast(milliseconds_ / val); return (*this); } Duration & Duration::operator/=(long double val) { - milliseconds_ = static_cast(milliseconds_ / val); + milliseconds_ = static_cast(milliseconds_ / val); return (*this); } diff --git a/core/base/time.h b/core/base/time.h index 1ee16f90..a41d12d1 100644 --- a/core/base/time.h +++ b/core/base/time.h @@ -44,12 +44,12 @@ namespace easy2d public: Duration(); - explicit Duration( - int64_t milliseconds + Duration( + long long milliseconds ); // 转化为毫秒 - inline int64_t Milliseconds() const { return milliseconds_; } + inline long long Milliseconds() const { return milliseconds_; } // 转化为秒 float Seconds() const; @@ -117,7 +117,7 @@ namespace easy2d friend std::wistream& operator>> (std::wistream &, Duration &); private: - int64_t milliseconds_; + long long milliseconds_; }; extern const Duration Millisecond; // 毫秒 diff --git a/core/ui/Button.cpp b/core/ui/Button.cpp index d9b58961..b6501069 100644 --- a/core/ui/Button.cpp +++ b/core/ui/Button.cpp @@ -28,7 +28,7 @@ namespace easy2d Button::Button() : enabled_(true) , is_selected_(false) - , callback_(nullptr) + , click_callback_(nullptr) , status_(Status::Normal) { AddListener(MouseEvent::Hover, std::bind(&Button::UpdateStatus, this, std::placeholders::_1)); @@ -37,10 +37,19 @@ namespace easy2d AddListener(MouseEvent::Up, std::bind(&Button::UpdateStatus, this, std::placeholders::_1)); } - Button::Button(const Callback& func) + Button::Button(const Callback& click) : Button() { - this->SetClickCallback(func); + 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); } Button::~Button() @@ -62,7 +71,22 @@ namespace easy2d void Button::SetClickCallback(const Callback& func) { - callback_ = func; + 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; } void Button::SetStatus(Status status) @@ -83,21 +107,30 @@ namespace easy2d if (me->type == MouseEvent::Hover) { SetStatus(Status::Hover); + + if (mouse_over_callback_) + mouse_over_callback_(); } else if (me->type == MouseEvent::Out) { SetStatus(Status::Normal); + + if (mouse_out_callback_) + mouse_out_callback_(); } else if (me->type == MouseEvent::Down && status_ == Status::Hover) { - SetStatus(Status::Selected); + SetStatus(Status::Pressed); + + if (pressed_callback_) + pressed_callback_(); } - else if (me->type == MouseEvent::Up && status_ == Status::Selected) + else if (me->type == MouseEvent::Up && status_ == Status::Pressed) { SetStatus(Status::Hover); - if (callback_) - callback_(); + if (click_callback_) + click_callback_(); } } } diff --git a/core/ui/Button.h b/core/ui/Button.h index e7978e9d..d32d20b2 100644 --- a/core/ui/Button.h +++ b/core/ui/Button.h @@ -35,7 +35,14 @@ namespace easy2d Button(); explicit Button( - Callback const& func /* 按钮回调函数 */ + Callback const& click /* 按钮点击回调函数 */ + ); + + explicit Button( + Callback const& click, /* 按钮点击回调函数 */ + Callback const& pressed, /* 按钮按下回调函数 */ + Callback const& mouse_over, /* 按钮移入回调函数 */ + Callback const& mouse_out /* 按钮移出回调函数 */ ); virtual ~Button(); @@ -53,8 +60,23 @@ namespace easy2d const Callback& func ); + // 设置按钮被按下时的回调函数 + void SetPressedCallback( + const Callback& func + ); + + // 设置鼠标移入按钮时的回调函数 + void SetMouseOverCallback( + const Callback& func + ); + + // 设置鼠标移出按钮时的回调函数 + void SetMouseOutCallback( + const Callback& func + ); + private: - enum class Status { Normal, Hover, Selected }; + enum class Status { Normal, Hover, Pressed }; void SetStatus( Status status @@ -66,7 +88,10 @@ namespace easy2d bool enabled_; bool is_selected_; Status status_; - Callback callback_; + Callback click_callback_; + Callback pressed_callback_; + Callback mouse_over_callback_; + Callback mouse_out_callback_; }; } } \ No newline at end of file