From de5a51462eb46c3cfeab650a81722e1760d1a1cf Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Mon, 8 Apr 2019 14:15:27 +0800 Subject: [PATCH] update Closure minor fixes fixes minor fixes fixes fixes --- Easy2D/2d/Action.h | 2 +- Easy2D/2d/ActionTween.h | 6 +- Easy2D/2d/Image.cpp | 2 +- Easy2D/2d/Image.h | 4 +- Easy2D/2d/Layer.cpp | 14 +- Easy2D/2d/Node.cpp | 25 ++- Easy2D/2d/Node.h | 74 ++++++--- Easy2D/base/AsyncTask.cpp | 6 +- Easy2D/base/AsyncTask.h | 7 +- Easy2D/base/EventListener.h | 3 +- Easy2D/base/Timer.h | 3 +- Easy2D/base/logs.cpp | 4 +- Easy2D/base/logs.h | 2 +- Easy2D/common/closure.hpp | 275 ++++++++++++++++++++++++++------ Easy2D/imgui/ImGuiLayer.h | 2 +- Easy2D/network/HttpClient.cpp | 4 +- Easy2D/network/HttpRequest.h | 2 +- Easy2D/platform/Application.cpp | 2 +- Easy2D/platform/Application.h | 4 +- Easy2D/ui/Button.cpp | 8 +- Easy2D/ui/Button.h | 2 +- samples/Box2DSample/MainScene.h | 2 +- samples/ImGuiSample/MainScene.h | 4 +- samples/Samples/Demo4.h | 4 +- samples/Samples/Demo5.h | 10 +- samples/Samples/main.cpp | 2 +- 26 files changed, 351 insertions(+), 122 deletions(-) diff --git a/Easy2D/2d/Action.h b/Easy2D/2d/Action.h index e479711a..3741c49c 100644 --- a/Easy2D/2d/Action.h +++ b/Easy2D/2d/Action.h @@ -23,7 +23,7 @@ namespace easy2d { - using ActionCallback = std::function; + using ActionCallback = Closure; class ActionManager; diff --git a/Easy2D/2d/ActionTween.h b/Easy2D/2d/ActionTween.h index c21636e8..83a1e788 100644 --- a/Easy2D/2d/ActionTween.h +++ b/Easy2D/2d/ActionTween.h @@ -26,7 +26,7 @@ namespace easy2d { // 缓动函数 - using EaseFunc = std::function; + using EaseFunc = Closure; // 缓动函数枚举 // See https://easings.net for more information @@ -380,8 +380,8 @@ namespace easy2d public: // 创建淡出动作 explicit ActionFadeOut( - Duration duration, /* 持续时长 */ - EaseFunc func = Ease::Linear /* 速度变化 */ + Duration duration, /* 持续时长 */ + EaseFunc func = nullptr /* 速度变化 */ ); }; diff --git a/Easy2D/2d/Image.cpp b/Easy2D/2d/Image.cpp index fcee87a1..a69c3709 100644 --- a/Easy2D/2d/Image.cpp +++ b/Easy2D/2d/Image.cpp @@ -158,7 +158,7 @@ namespace easy2d return crop_rect_.origin; } - Rect const& Image::GetCropRect() const + Rect Image::GetCropRect() const { return crop_rect_; } diff --git a/Easy2D/2d/Image.h b/Easy2D/2d/Image.h index 9770e9b7..2afd023e 100644 --- a/Easy2D/2d/Image.h +++ b/Easy2D/2d/Image.h @@ -88,7 +88,7 @@ namespace easy2d Point GetCropPos() const; // 获取裁剪矩形 - Rect const& GetCropRect() const; + Rect GetCropRect() const; ComPtr const& GetBitmap() const; @@ -98,7 +98,7 @@ namespace easy2d ); protected: - Rect crop_rect_; + Rect crop_rect_; ComPtr bitmap_; }; } diff --git a/Easy2D/2d/Layer.cpp b/Easy2D/2d/Layer.cpp index 7e870c78..8f648ccf 100644 --- a/Easy2D/2d/Layer.cpp +++ b/Easy2D/2d/Layer.cpp @@ -28,14 +28,14 @@ namespace easy2d { SetSize(Renderer::Instance().GetOutputSize()); - AddListener(Event::MouseBtnDown, Closure(this, &Layer::HandleMessages)); - AddListener(Event::MouseBtnUp, Closure(this, &Layer::HandleMessages)); - AddListener(Event::MouseMove, Closure(this, &Layer::HandleMessages)); - AddListener(Event::MouseWheel, Closure(this, &Layer::HandleMessages)); + AddListener(Event::MouseBtnDown, MakeClosure(this, &Layer::HandleMessages)); + AddListener(Event::MouseBtnUp, MakeClosure(this, &Layer::HandleMessages)); + AddListener(Event::MouseMove, MakeClosure(this, &Layer::HandleMessages)); + AddListener(Event::MouseWheel, MakeClosure(this, &Layer::HandleMessages)); - AddListener(Event::KeyDown, Closure(this, &Layer::HandleMessages)); - AddListener(Event::KeyUp, Closure(this, &Layer::HandleMessages)); - AddListener(Event::Char, Closure(this, &Layer::HandleMessages)); + AddListener(Event::KeyDown, MakeClosure(this, &Layer::HandleMessages)); + AddListener(Event::KeyUp, MakeClosure(this, &Layer::HandleMessages)); + AddListener(Event::Char, MakeClosure(this, &Layer::HandleMessages)); } Layer::~Layer() diff --git a/Easy2D/2d/Node.cpp b/Easy2D/2d/Node.cpp index e460686e..7c6fa209 100644 --- a/Easy2D/2d/Node.cpp +++ b/Easy2D/2d/Node.cpp @@ -302,6 +302,11 @@ namespace easy2d dirty_transform_ = true; } + void Node::SetAnchor(Point const& anchor) + { + this->SetAnchor(anchor.x, anchor.y); + } + void Node::SetWidth(float width) { this->SetSize(width, size_.y); @@ -407,6 +412,11 @@ namespace easy2d dirty_transform_ = true; } + void Node::SetScale(Point const& scale) + { + this->SetScale(scale.x, scale.y); + } + void Node::SetSkewX(float skew_x) { this->SetSkew(skew_x, transform_.skew.y); @@ -427,6 +437,11 @@ namespace easy2d dirty_transform_ = true; } + void Node::SetSkew(Point const& skew) + { + this->SetSkew(skew.x, skew.y); + } + void Node::SetRotation(float angle) { if (transform_.rotation == angle) @@ -522,26 +537,24 @@ namespace easy2d } } - bool Node::RemoveChild(NodePtr const& child) + void Node::RemoveChild(NodePtr const& child) { - return RemoveChild(child.Get()); + RemoveChild(child.Get()); } - bool Node::RemoveChild(Node * child) + void Node::RemoveChild(Node * child) { E2D_ASSERT(child && "Node::RemoveChild failed, NULL pointer exception"); if (children_.IsEmpty()) - return false; + return; if (child) { child->parent_ = nullptr; if (child->scene_) child->SetScene(nullptr); children_.Remove(NodePtr(child)); - return true; } - return false; } void Node::RemoveChildren(String const& child_name) diff --git a/Easy2D/2d/Node.h b/Easy2D/2d/Node.h index d3cf7eaf..f07a11ef 100644 --- a/Easy2D/2d/Node.h +++ b/Easy2D/2d/Node.h @@ -42,7 +42,7 @@ namespace easy2d friend class IntrusiveList; using Children = IntrusiveList; - using UpdateCallback = std::function; + using UpdateCallback = Closure; public: Node(); @@ -56,6 +56,9 @@ namespace easy2d // 获取显示状态 bool IsVisible() const { return visible_; } + // 获取响应状态 + bool IsResponsible() const { return responsible_; } + // 获取名称的 Hash 值 size_t GetHashName() const { return hash_name_; } @@ -63,7 +66,7 @@ namespace easy2d int GetZOrder() const { return z_order_; } // 获取坐标 - Point const& GetPosition() const { return transform_.position; } + Point GetPosition() const { return transform_.position; } // 获取 x 坐标 float GetPositionX() const { return transform_.position.x; } @@ -71,12 +74,18 @@ namespace easy2d // 获取 y 坐标 float GetPositionY() const { return transform_.position.y; } + // 获取横向缩放比例 + Point GetScale() const { return transform_.scale; } + // 获取横向缩放比例 float GetScaleX() const { return transform_.scale.x; } // 获取纵向缩放比例 float GetScaleY() const { return transform_.scale.y; } + // 获取错切角度 + Point GetSkew() const { return transform_.skew; } + // 获取横向错切角度 float GetSkewX() const { return transform_.skew.x; } @@ -93,7 +102,7 @@ namespace easy2d float GetHeight() const { return size_.y; } // 获取大小 - Size const& GetSize() const { return size_; } + Size GetSize() const { return size_; } // 获取缩放后的宽度 float GetScaledWidth() const { return size_.x * transform_.scale.x; } @@ -104,6 +113,9 @@ namespace easy2d // 获取缩放后的大小 Size GetScaledSize() const { return Size{ GetScaledWidth(), GetScaledHeight() }; } + // 获取锚点 + Point GetAnchor() const { return anchor_; } + // 获取 x 方向锚点 float GetAnchorX() const { return anchor_.x; } @@ -114,7 +126,7 @@ namespace easy2d float GetOpacity() const { return opacity_; } // 获取变换 - Transform const& GetTransform() const { return transform_; } + Transform GetTransform() const { return transform_; } // 获取边框 Rect GetBounds() const; @@ -195,6 +207,12 @@ namespace easy2d float scale_y ); + // 设置缩放比例 + // 默认为 (1.0, 1.0) + void SetScale( + Point const& scale + ); + // 设置缩放比例 // 默认为 1.0 void SetScale( @@ -220,6 +238,12 @@ namespace easy2d float skew_y ); + // 设置错切角度 + // 默认为 (0, 0) + void SetSkew( + Point const& skew + ); + // 设置旋转角度 // 默认为 0 void SetRotation( @@ -245,6 +269,12 @@ namespace easy2d float anchor_y ); + // 设置锚点位置 + // 默认为 (0, 0), 范围 [0, 1] + void SetAnchor( + Point const& anchor + ); + // 修改宽度 void SetWidth( float width @@ -317,12 +347,12 @@ namespace easy2d Children const& GetChildren() const; // 移除子节点 - bool RemoveChild( + void RemoveChild( NodePtr const& child ); // 移除子节点 - bool RemoveChild( + void RemoveChild( Node* child ); @@ -376,22 +406,22 @@ namespace easy2d void SetScene(Scene* scene); protected: - bool visible_; - bool hover_; - bool pressed_; - bool responsible_; - bool update_pausing_; - int z_order_; - float opacity_; - float display_opacity_; - size_t hash_name_; - Transform transform_; - Point anchor_; - Size size_; - Node* parent_; - Scene* scene_; - Children children_; - UpdateCallback cb_update_; + bool visible_; + bool hover_; + bool pressed_; + bool responsible_; + bool update_pausing_; + int z_order_; + float opacity_; + float display_opacity_; + size_t hash_name_; + Transform transform_; + Point anchor_; + Size size_; + Node* parent_; + Scene* scene_; + Children children_; + UpdateCallback cb_update_; mutable bool dirty_transform_; mutable bool dirty_transform_inverse_; diff --git a/Easy2D/base/AsyncTask.cpp b/Easy2D/base/AsyncTask.cpp index fc8473f1..492f9116 100644 --- a/Easy2D/base/AsyncTask.cpp +++ b/Easy2D/base/AsyncTask.cpp @@ -40,7 +40,7 @@ namespace easy2d void AsyncTask::Start() { - std::thread thread(Closure(this, &AsyncTask::TaskThread)); + std::thread thread(MakeClosure(this, &AsyncTask::TaskThread)); thread.detach(); // retain this object until finished @@ -77,7 +77,7 @@ namespace easy2d if (thread_cb_) { - AsyncTaskThread::Instance().PerformTaskCallback(Closure(this, &AsyncTask::Complete)); + AsyncTaskThread::Instance().PerformTaskCallback(MakeClosure(this, &AsyncTask::Complete)); } } @@ -102,7 +102,7 @@ namespace easy2d { } - void AsyncTaskThread::PerformTaskCallback(std::function func) + void AsyncTaskThread::PerformTaskCallback(Closure func) { if (app_) { diff --git a/Easy2D/base/AsyncTask.h b/Easy2D/base/AsyncTask.h index 898b2b93..5205032f 100644 --- a/Easy2D/base/AsyncTask.h +++ b/Easy2D/base/AsyncTask.h @@ -21,6 +21,7 @@ #pragma once #include "Object.h" #include "Component.h" +#include "../common/Closure.hpp" #include "../common/Singleton.hpp" #include #include @@ -29,8 +30,8 @@ namespace easy2d { E2D_DECLARE_SMART_PTR(AsyncTask); - typedef std::function AsyncTaskFunc; - typedef std::function AsyncTaskCallback; + typedef Closure AsyncTaskFunc; + typedef Closure AsyncTaskCallback; class AsyncTask : public Object @@ -74,7 +75,7 @@ namespace easy2d virtual void DestroyComponent(); - void PerformTaskCallback(std::function func); + void PerformTaskCallback(Closure func); private: Application* app_; diff --git a/Easy2D/base/EventListener.h b/Easy2D/base/EventListener.h index b52c2306..483c2557 100644 --- a/Easy2D/base/EventListener.h +++ b/Easy2D/base/EventListener.h @@ -21,6 +21,7 @@ #pragma once #include "../base/SmartPtr.hpp" #include "../common/helper.h" +#include "../common/Closure.hpp" #include "../common/IntrusiveList.hpp" #include "Object.h" #include "Event.hpp" @@ -28,7 +29,7 @@ namespace easy2d { - typedef std::function EventCallback; + typedef Closure EventCallback; class EventDispatcher; diff --git a/Easy2D/base/Timer.h b/Easy2D/base/Timer.h index 568d5412..d9b7e05e 100644 --- a/Easy2D/base/Timer.h +++ b/Easy2D/base/Timer.h @@ -20,6 +20,7 @@ #pragma once #include "../common/helper.h" +#include "../common/Closure.hpp" #include "../common/IntrusiveList.hpp" #include "Object.h" #include "time.h" @@ -39,7 +40,7 @@ namespace easy2d friend class TimerManager; friend class IntrusiveList; - using Callback = std::function; + using Callback = Closure; public: explicit Timer( diff --git a/Easy2D/base/logs.cpp b/Easy2D/base/logs.cpp index 4d5ca245..2a76e02d 100644 --- a/Easy2D/base/logs.cpp +++ b/Easy2D/base/logs.cpp @@ -163,9 +163,9 @@ namespace easy2d { if (enabled_ && has_console_) { - std::wstring output = MakeOutputString(prompt, format, args); + std::wstring output = MakeOutputStringf(prompt, format, args); - os << color << output; + os << color << output << std::flush; ::OutputDebugStringW(output.c_str()); ResetConsoleColor(); diff --git a/Easy2D/base/logs.h b/Easy2D/base/logs.h index 99f4061e..8e8f927b 100644 --- a/Easy2D/base/logs.h +++ b/Easy2D/base/logs.h @@ -239,7 +239,7 @@ namespace easy2d { std::wstring output = MakeOutputString(prompt, std::forward<_Args>(args)...); - os << color << output; + os << color << output << std::flush; ::OutputDebugStringW(output.c_str()); ResetConsoleColor(); diff --git a/Easy2D/common/closure.hpp b/Easy2D/common/closure.hpp index 40b0fc30..381b42c7 100644 --- a/Easy2D/common/closure.hpp +++ b/Easy2D/common/closure.hpp @@ -19,79 +19,262 @@ // THE SOFTWARE. #pragma once -#include +#include namespace easy2d { // - // Closure is a simple function for binding member functions + // Closure is a light weight std::function<>-like class // - template - std::function<_Ret(_Args...)> Closure(_Ty* _Ptr, _Ret(_Ty::*_Func)(_Args...)); - - - // - // Details of Closure - // - namespace __closure__detail + namespace __closure_detail { - // sequence & generater - - template - struct Seq + template + class Callable { - using NextType = Seq<_Num..., sizeof...(_Num)>; + public: + virtual ~Callable() {} + + virtual void AddRef() = 0; + virtual void Release() = 0; + virtual _Ret Invoke(_Args... args) const = 0; }; - template - struct Gen; - - template<> - struct Gen<> + template + class RefCountCallable + : public Callable<_Ret, _Args...> { - using SeqType = Seq<>; + public: + RefCountCallable() : ref_count_(0) {} + + virtual void AddRef() override + { + ++ref_count_; + } + + virtual void Release() override + { + --ref_count_; + if (ref_count_ <= 0) + { + delete this; + } + } + + private: + int ref_count_; }; - template - struct Gen<_Ty1, _Args...> - { - using SeqType = typename Gen<_Args...>::SeqType::NextType; - }; - - - // ClosureHelper - template - struct ClosureHelper + class ProxyCallable + : public RefCountCallable<_Ret, _Args...> { - template - static inline std::function<_Ret(_Args...)> MakeFunc(_Ty* _Ptr, _Ret(_Ty::*_Func)(_Args...), Seq<_Num...>) + public: + ProxyCallable(_Ty&& val) + : callee_(std::move(val)) { - return std::bind(_Func, _Ptr, std::_Ph<_Num + 1>()...); } - template - static inline std::function<_Ret(_Args...)> MakeFunc(_Ty* _Ptr, _Ret(_Ty::* _Func)(_Args...) const, Seq<_Num...>) + virtual _Ret Invoke(_Args... args) const override { - return std::bind(_Func, _Ptr, std::_Ph<_Num + 1>()...); + return callee_(std::forward<_Args>(args)...); } + + static inline Callable<_Ret, _Args...>* Make(_Ty&& val) + { + return new (std::nothrow) ProxyCallable<_Ty, _Ret, _Args...>(std::move(val)); + } + + private: + _Ty callee_; + }; + + template + class ProxyMemCallable + : public RefCountCallable<_Ret, _Args...> + { + public: + typedef _Ret(_Ty::* _FuncType)(_Args...); + + ProxyMemCallable(void* ptr, _FuncType func) + : ptr_(ptr) + , func_(func) + { + } + + virtual _Ret Invoke(_Args... args) const override + { + return (static_cast<_Ty*>(ptr_)->*func_)(std::forward<_Args>(args)...); + } + + static inline Callable<_Ret, _Args...>* Make(void* ptr, _FuncType func) + { + return new (std::nothrow) ProxyMemCallable<_Ty, _Ret, _Args...>(ptr, func); + } + + private: + void* ptr_; + _FuncType func_; + }; + + template + class ProxyConstMemCallable + : public RefCountCallable<_Ret, _Args...> + { + public: + typedef _Ret(_Ty::* _FuncType)(_Args...) const; + + ProxyConstMemCallable(void* ptr, _FuncType func) + : ptr_(ptr) + , func_(func) + { + } + + virtual _Ret Invoke(_Args... args) const override + { + return (static_cast<_Ty*>(ptr_)->*func_)(std::forward<_Args>(args)...); + } + + static inline Callable<_Ret, _Args...>* Make(void* ptr, _FuncType func) + { + return new (std::nothrow) ProxyConstMemCallable<_Ty, _Ret, _Args...>(ptr, func); + } + + private: + void* ptr_; + _FuncType func_; }; } - template - inline std::function<_Ret(_Args...)> Closure(_Ty* _Ptr, _Ret(_Ty::*_Func)(_Args...)) + // + // exceptions + // + class bad_function_call : public std::exception { - using namespace __closure__detail; - return ClosureHelper<_Ty, _Ret, _Args...>:: - MakeFunc(_Ptr, _Func, typename Gen<_Args...>::SeqType{}); + public: + bad_function_call() {} + + virtual const char* what() const override + { + return "bad function call"; + } + }; + + + // + // Closure details + // + template + class Closure; + + template + class Closure<_Ret(_Args...)> + { + public: + Closure() + : callable_(nullptr) + { + } + + Closure(std::nullptr_t) + : callable_(nullptr) + { + } + + Closure(const Closure& rhs) + : callable_(rhs.callable_) + { + if (callable_) callable_->AddRef(); + } + + Closure(Closure&& rhs) + : callable_(rhs.callable_) + { + rhs.callable_ = nullptr; + } + + template + Closure(_Ty val) + { + callable_ = __closure_detail::ProxyCallable<_Ty, _Ret, _Args...>::Make(std::move(val)); + if (callable_) callable_->AddRef(); + } + + template + Closure(void* ptr, _Ret(_Ty::* func)(_Args...)) + { + callable_ = __closure_detail::ProxyMemCallable<_Ty, _Ret, _Args...>::Make(ptr, func); + if (callable_) callable_->AddRef(); + } + + template + Closure(void* ptr, _Ret(_Ty::* func)(_Args...) const) + { + callable_ = __closure_detail::ProxyConstMemCallable<_Ty, _Ret, _Args...>::Make(ptr, func); + if (callable_) callable_->AddRef(); + } + + ~Closure() + { + tidy(); + } + + inline void swap(const Closure& rhs) + { + std::swap(callable_, rhs.callable_); + } + + inline _Ret operator()(_Args... args) const + { + if (!callable_) + throw bad_function_call(); + return callable_->Invoke(std::forward<_Args>(args)...); + } + + inline operator bool() const + { + return !!callable_; + } + + inline Closure& operator=(const Closure& rhs) + { + tidy(); + callable_ = rhs.callable_; + if (callable_) callable_->AddRef(); + return (*this); + } + + inline Closure& operator=(Closure&& rhs) + { + tidy(); + callable_ = rhs.callable_; + rhs.callable_ = nullptr; + return (*this); + } + + private: + inline void tidy() + { + if (callable_) + { + callable_->Release(); + callable_ = nullptr; + } + } + + private: + __closure_detail::Callable<_Ret, _Args...>* callable_; + }; + + template + inline Closure<_Ret(_Args...)> MakeClosure(void* ptr, _Ret(_Ty::* func)(_Args...)) + { + return Closure<_Ret(_Args...)>(ptr, func); } template - inline std::function<_Ret(_Args...)> Closure(_Ty* _Ptr, _Ret(_Ty::*_Func)(_Args...) const) + inline Closure<_Ret(_Args...)> MakeClosure(void* ptr, _Ret(_Ty::* func)(_Args...) const) { - using namespace __closure__detail; - return ClosureHelper<_Ty, _Ret, _Args...>:: - MakeFunc(_Ptr, _Func, typename Gen<_Args...>::SeqType{}); + return Closure<_Ret(_Args...)>(ptr, func); } } diff --git a/Easy2D/imgui/ImGuiLayer.h b/Easy2D/imgui/ImGuiLayer.h index e40529b8..43a7abe1 100644 --- a/Easy2D/imgui/ImGuiLayer.h +++ b/Easy2D/imgui/ImGuiLayer.h @@ -26,7 +26,7 @@ namespace easy2d E2D_DECLARE_SMART_PTR(ImGuiLayer); - using ImGuiPipeline = std::function; + using ImGuiPipeline = Closure; class ImGuiLayer : public Layer diff --git a/Easy2D/network/HttpClient.cpp b/Easy2D/network/HttpClient.cpp index 2302ffc7..6630f713 100644 --- a/Easy2D/network/HttpClient.cpp +++ b/Easy2D/network/HttpClient.cpp @@ -256,7 +256,7 @@ namespace easy2d ::curl_global_init(CURL_GLOBAL_ALL); - std::thread thread(Closure(this, &HttpClient::NetworkThread)); + std::thread thread(MakeClosure(this, &HttpClient::NetworkThread)); thread.detach(); } @@ -301,7 +301,7 @@ namespace easy2d if (app_) { - app_->PreformFunctionInMainThread(Closure(this, &HttpClient::DispatchResponseCallback)); + app_->PreformFunctionInMainThread(MakeClosure(this, &HttpClient::DispatchResponseCallback)); } } } diff --git a/Easy2D/network/HttpRequest.h b/Easy2D/network/HttpRequest.h index 0fc72ac1..3a74ff36 100644 --- a/Easy2D/network/HttpRequest.h +++ b/Easy2D/network/HttpRequest.h @@ -24,7 +24,7 @@ namespace easy2d { namespace network { - typedef std::function ResponseCallback; + typedef Closure ResponseCallback; class E2D_API HttpRequest : public Object diff --git a/Easy2D/platform/Application.cpp b/Easy2D/platform/Application.cpp index 9201f070..9280e77c 100644 --- a/Easy2D/platform/Application.cpp +++ b/Easy2D/platform/Application.cpp @@ -414,7 +414,7 @@ namespace easy2d ); } - void Application::PreformFunctionInMainThread(std::function function) + void Application::PreformFunctionInMainThread(Closure function) { std::lock_guard lock(perform_mutex_); functions_to_perform_.push(function); diff --git a/Easy2D/platform/Application.h b/Easy2D/platform/Application.h index 8e877780..3f584769 100644 --- a/Easy2D/platform/Application.h +++ b/Easy2D/platform/Application.h @@ -134,7 +134,7 @@ namespace easy2d // 在 Easy2D 主线程中执行函数 // 当在其他线程调用 Easy2D 函数时使用 void PreformFunctionInMainThread( - std::function function + Closure function ); // 显示控制台 @@ -163,6 +163,6 @@ namespace easy2d Array components_; std::mutex perform_mutex_; - Queue> functions_to_perform_; + Queue> functions_to_perform_; }; } diff --git a/Easy2D/ui/Button.cpp b/Easy2D/ui/Button.cpp index 3d696075..8852b03c 100644 --- a/Easy2D/ui/Button.cpp +++ b/Easy2D/ui/Button.cpp @@ -31,10 +31,10 @@ namespace easy2d { SetResponsible(true); - AddListener(Event::MouseHover, Closure(this, &Button::UpdateStatus)); - AddListener(Event::MouseOut, Closure(this, &Button::UpdateStatus)); - AddListener(Event::MouseBtnDown, Closure(this, &Button::UpdateStatus)); - AddListener(Event::MouseBtnUp, Closure(this, &Button::UpdateStatus)); + AddListener(Event::MouseHover, MakeClosure(this, &Button::UpdateStatus)); + AddListener(Event::MouseOut, MakeClosure(this, &Button::UpdateStatus)); + AddListener(Event::MouseBtnDown, MakeClosure(this, &Button::UpdateStatus)); + AddListener(Event::MouseBtnUp, MakeClosure(this, &Button::UpdateStatus)); } Button::Button(const Callback& click) diff --git a/Easy2D/ui/Button.h b/Easy2D/ui/Button.h index 423ee742..38aff570 100644 --- a/Easy2D/ui/Button.h +++ b/Easy2D/ui/Button.h @@ -27,7 +27,7 @@ namespace easy2d class E2D_API Button : public Sprite { - using Callback = std::function; + using Callback = Closure; public: Button(); diff --git a/samples/Box2DSample/MainScene.h b/samples/Box2DSample/MainScene.h index 24c1083f..b504d40a 100644 --- a/samples/Box2DSample/MainScene.h +++ b/samples/Box2DSample/MainScene.h @@ -19,7 +19,7 @@ public: SetResponsible(true); // 添加消息监听 - AddListener(Event::Click, Closure(this, &MainScene::Click)); + AddListener(Event::Click, MakeClosure(this, &MainScene::Click)); // 创建物理世界 world_ = new b2World(b2Vec2(0, 10)); diff --git a/samples/ImGuiSample/MainScene.h b/samples/ImGuiSample/MainScene.h index ded35755..376ac46e 100644 --- a/samples/ImGuiSample/MainScene.h +++ b/samples/ImGuiSample/MainScene.h @@ -29,10 +29,10 @@ public: }, L"DemoWindow"); // 添加一个简单窗口 - layer->AddItem(Closure(this, &MainScene::SimpleWindow), L"SimpleWindow"); + layer->AddItem(MakeClosure(this, &MainScene::SimpleWindow), L"SimpleWindow"); // 再添加一个窗口 - layer->AddItem(Closure(this, &MainScene::AnotherWindow), L"AnotherWindow"); + layer->AddItem(MakeClosure(this, &MainScene::AnotherWindow), L"AnotherWindow"); } void SimpleWindow() diff --git a/samples/Samples/Demo4.h b/samples/Samples/Demo4.h index 298e8945..70f8d7b1 100644 --- a/samples/Samples/Demo4.h +++ b/samples/Samples/Demo4.h @@ -48,8 +48,8 @@ public: ); // 添加按键监听 - AddListener(Event::KeyDown, Closure(this, &Tiger::OnKeyDown)); - AddListener(Event::KeyUp, Closure(this, &Tiger::OnKeyUp)); + AddListener(Event::KeyDown, MakeClosure(this, &Tiger::OnKeyDown)); + AddListener(Event::KeyUp, MakeClosure(this, &Tiger::OnKeyUp)); // 默认方向为 Left facing_left = true; diff --git a/samples/Samples/Demo5.h b/samples/Samples/Demo5.h index 732d986a..79353a30 100644 --- a/samples/Samples/Demo5.h +++ b/samples/Samples/Demo5.h @@ -16,7 +16,7 @@ public: Demo5() { // 添加按键监听 - AddListener(Event::KeyDown, Closure(this, &Demo5::OnKeyDown)); + AddListener(Event::KeyDown, MakeClosure(this, &Demo5::OnKeyDown)); // 创建说明文字 TextPtr text = new Text(L"按G发送GET请求\n按P发送POST请求\n按U发送PUT请求\n按D发送DELETE请求"); @@ -74,7 +74,7 @@ public: // 设置请求类型为 GET request->SetType(HttpRequest::Type::Get); // 设置请求完成后的回调函数 - request->SetResponseCallback(Closure(this, &Demo5::Complete)); + request->SetResponseCallback(MakeClosure(this, &Demo5::Complete)); // 发送 HTTP 请求 HttpClient::Instance().Send(request); @@ -100,7 +100,7 @@ public: request->SetType(HttpRequest::Type::Post); // 设置 POST 请求的数据 request->SetJsonData(request_data); - request->SetResponseCallback(Closure(this, &Demo5::Complete)); + request->SetResponseCallback(MakeClosure(this, &Demo5::Complete)); HttpClient::Instance().Send(request); } @@ -118,7 +118,7 @@ public: request->SetType(HttpRequest::Type::Put); // 设置 PUT 请求的数据 request->SetJsonData(request_data); - request->SetResponseCallback(Closure(this, &Demo5::Complete)); + request->SetResponseCallback(MakeClosure(this, &Demo5::Complete)); HttpClient::Instance().Send(request); } @@ -131,7 +131,7 @@ public: HttpRequestPtr request = new HttpRequest; request->SetUrl(L"http://httpbin.org/delete"); request->SetType(HttpRequest::Type::Delete); - request->SetResponseCallback(Closure(this, &Demo5::Complete)); + request->SetResponseCallback(MakeClosure(this, &Demo5::Complete)); HttpClient::Instance().Send(request); } diff --git a/samples/Samples/main.cpp b/samples/Samples/main.cpp index c616314c..68ff76f1 100644 --- a/samples/Samples/main.cpp +++ b/samples/Samples/main.cpp @@ -59,7 +59,7 @@ public: EnterScene(scene); // 添加按键监听 - scene->AddListener(Event::KeyUp, Closure(this, &DemoApp::KeyPressed)); + scene->AddListener(Event::KeyUp, MakeClosure(this, &DemoApp::KeyPressed)); // 显示提示文字 String intro_str = format_wstring(L"按键 1~%d 可切换示例\n", s_DemoNum);