diff --git a/core/actions/Callback.cpp b/core/actions/Callback.cpp index f1a710fe..be27ea0e 100644 --- a/core/actions/Callback.cpp +++ b/core/actions/Callback.cpp @@ -20,26 +20,26 @@ #include "..\e2daction.h" -easy2d::Callback::Callback(const Function& func) : +easy2d::CallFunc::CallFunc(const Callback& func) : callback_(func) { } -easy2d::Callback * easy2d::Callback::Clone() const +easy2d::CallFunc * easy2d::CallFunc::Clone() const { - return new Callback(callback_); + return new CallFunc(callback_); } -easy2d::Callback * easy2d::Callback::Reverse() const +easy2d::CallFunc * easy2d::CallFunc::Reverse() const { - return new Callback(callback_); + return new CallFunc(callback_); } -void easy2d::Callback::Init() +void easy2d::CallFunc::Init() { } -void easy2d::Callback::Update() +void easy2d::CallFunc::Update() { callback_(); this->Stop(); diff --git a/core/components/Button.cpp b/core/components/Button.cpp index d3bf8d51..036238f5 100644 --- a/core/components/Button.cpp +++ b/core/components/Button.cpp @@ -49,7 +49,7 @@ easy2d::Button::Button() { } -easy2d::Button::Button(Node * normal, const Function& func) +easy2d::Button::Button(Node * normal, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -63,7 +63,7 @@ easy2d::Button::Button(Node * normal, const Function& func) this->SetCallbackOnClick(func); } -easy2d::Button::Button(Node * normal, Node * selected, const Function& func) +easy2d::Button::Button(Node * normal, Node * selected, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -78,7 +78,7 @@ easy2d::Button::Button(Node * normal, Node * selected, const Function& func) this->SetCallbackOnClick(func); } -easy2d::Button::Button(Node * normal, Node * mouseover, Node * selected, const Function& func) +easy2d::Button::Button(Node * normal, Node * mouseover, Node * selected, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -94,7 +94,7 @@ easy2d::Button::Button(Node * normal, Node * mouseover, Node * selected, const F this->SetCallbackOnClick(func); } -easy2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, const Function& func) +easy2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, const Callback& func) : callback_(nullptr) , status_(Status::Normal) , enabled_(true) @@ -149,7 +149,7 @@ void easy2d::Button::SetEnabled(bool enabled) } } -void easy2d::Button::SetCallbackOnClick(const Function& func) +void easy2d::Button::SetCallbackOnClick(const Callback& func) { callback_ = func; } diff --git a/core/e2daction.h b/core/e2daction.h index 2e91d35a..6c7076f9 100644 --- a/core/e2daction.h +++ b/core/e2daction.h @@ -580,22 +580,24 @@ namespace easy2d // 回调动作 - class Callback + class CallFunc : public Action { + typedef std::function Callback; + public: - explicit Callback( - const Function& func /* 函数对象 */ + explicit CallFunc( + const Callback& func /* 函数对象 */ ); // 获取该动作的拷贝对象 - virtual Callback * Clone() const override; + virtual CallFunc * Clone() const override; // 获取该动作的倒转 - virtual Callback * Reverse() const override; + virtual CallFunc * Reverse() const override; protected: - E2D_DISABLE_COPY(Callback); + E2D_DISABLE_COPY(CallFunc); // 初始化动作 virtual void Init() override; @@ -604,7 +606,7 @@ namespace easy2d virtual void Update() override; protected: - Function callback_; + Callback callback_; }; diff --git a/core/e2dcomponent.h b/core/e2dcomponent.h index d43a49ab..d946078e 100644 --- a/core/e2dcomponent.h +++ b/core/e2dcomponent.h @@ -30,25 +30,27 @@ namespace easy2d class Button : public Node { + typedef std::function Callback; + public: Button(); explicit Button( Node * normal, /* 普通状态 */ - const Function& func = nullptr /* 按钮点击后的回调函数 */ + const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); explicit Button( Node * normal, /* 普通状态 */ Node * selected, /* 鼠标按下状态 */ - const Function& func = nullptr /* 按钮点击后的回调函数 */ + const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); explicit Button( Node * normal, /* 普通状态 */ Node * mouseover, /* 鼠标移入状态 */ Node * selected, /* 鼠标按下状态 */ - const Function& func = nullptr /* 按钮点击后的回调函数 */ + const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); explicit Button( @@ -56,7 +58,7 @@ namespace easy2d Node * mouseover, /* 鼠标移入状态 */ Node * selected, /* 鼠标移入状态 */ Node * disabled, /* 按钮禁用状态 */ - const Function& func = nullptr /* 按钮点击后的回调函数 */ + const Callback& func = nullptr /* 按钮点击后的回调函数 */ ); // 获取按钮状态是启用还是禁用 @@ -89,7 +91,7 @@ namespace easy2d // 设置按钮点击后的回调函数 void SetCallbackOnClick( - const Function& func + const Callback& func ); // 设置支点位置 @@ -130,7 +132,7 @@ namespace easy2d bool enabled_; bool is_selected_; Status status_; - Function callback_; + Callback callback_; }; diff --git a/core/e2dobject.h b/core/e2dobject.h index 08264dc8..8272c537 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -201,14 +201,16 @@ namespace easy2d { friend class Node; + typedef std::function Callback; + public: explicit Task( - const Function& func, /* 执行函数 */ + const Callback& func, /* 执行函数 */ const std::wstring& name = L"" /* 任务名称 */ ); explicit Task( - const Function& func, /* 执行函数 */ + const Callback& func, /* 执行函数 */ float delay, /* 时间间隔(秒) */ int times = -1, /* 执行次数(设 -1 为永久执行) */ const std::wstring& name = L"" /* 任务名称 */ @@ -243,7 +245,7 @@ namespace easy2d std::wstring name_; Duration delay_; Time last_time_; - Function callback_; + Callback callback_; Node * target_; }; diff --git a/core/e2dutil.h b/core/e2dutil.h index 04f81a3b..b3f5ef41 100644 --- a/core/e2dutil.h +++ b/core/e2dutil.h @@ -336,41 +336,6 @@ namespace easy2d }; - // 函数对象 - class Function - { - public: - Function(); - - Function( - std::nullptr_t - ); - - Function( - std::function func - ); - - template - Function(Func func) - : func_(func) - { - } - - template - Function(Func&& func, Object&& obj) - : func_(std::bind(func, obj)) - { - } - - void operator() (void) const; - - E2D_OP_EXPLICIT operator bool() const; - - private: - std::function func_; - }; - - // 时间段 // // Usage: diff --git a/core/objects/Task.cpp b/core/objects/Task.cpp index 95d32bda..ba9bad21 100644 --- a/core/objects/Task.cpp +++ b/core/objects/Task.cpp @@ -21,7 +21,7 @@ #include "..\e2dobject.h" -easy2d::Task::Task(const Function & func, const std::wstring & name) +easy2d::Task::Task(const Callback & func, const std::wstring & name) : running_(true) , stopped_(false) , run_times_(0) @@ -32,7 +32,7 @@ easy2d::Task::Task(const Function & func, const std::wstring & name) { } -easy2d::Task::Task(const Function & func, float delay, int times, const std::wstring & name) +easy2d::Task::Task(const Callback & func, float delay, int times, const std::wstring & name) : running_(true) , stopped_(false) , run_times_(0) diff --git a/core/utils/Function.cpp b/core/utils/Function.cpp deleted file mode 100644 index 277f44ae..00000000 --- a/core/utils/Function.cpp +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2016-2018 Easy2D - Nomango -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include "..\e2dutil.h" - -easy2d::Function::Function() - : func_(nullptr) -{} - -easy2d::Function::Function(std::nullptr_t) - : func_(nullptr) -{ -} - -easy2d::Function::Function(std::function func) - : func_(func) -{ -} - -void easy2d::Function::operator()(void) const -{ - if (func_) - { - func_(); - } -} - -easy2d::Function::operator bool() const -{ - return static_cast(func_); -} \ No newline at end of file diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index 2df2f82b..e735d357 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -70,7 +70,6 @@ - diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 3ff6de27..a915d4df 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -108,9 +108,6 @@ utils - - utils - utils diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 0febad40..addb7c57 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -214,7 +214,6 @@ - diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 3ff6de27..a915d4df 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -108,9 +108,6 @@ utils - - utils - utils diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index d62bab54..2c038930 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -247,7 +247,6 @@ - diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 3ff6de27..a915d4df 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -108,9 +108,6 @@ utils - - utils - utils