add: Closure function

This commit is contained in:
Nomango 2019-01-24 14:07:18 +08:00 committed by Nomango
parent de4751c2d3
commit fd3c70d5f0
11 changed files with 110 additions and 10 deletions

View File

@ -26,6 +26,7 @@
<ClInclude Include="..\..\src\core\Animation.h" /> <ClInclude Include="..\..\src\core\Animation.h" />
<ClInclude Include="..\..\src\core\audio.h" /> <ClInclude Include="..\..\src\core\audio.h" />
<ClInclude Include="..\..\src\core\Canvas.h" /> <ClInclude Include="..\..\src\core\Canvas.h" />
<ClInclude Include="..\..\src\core\closure.hpp" />
<ClInclude Include="..\..\src\core\Color.h" /> <ClInclude Include="..\..\src\core\Color.h" />
<ClInclude Include="..\..\src\core\d2dhelper.hpp" /> <ClInclude Include="..\..\src\core\d2dhelper.hpp" />
<ClInclude Include="..\..\src\core\DebugNode.h" /> <ClInclude Include="..\..\src\core\DebugNode.h" />

View File

@ -197,6 +197,9 @@
<ClInclude Include="..\..\src\core\IntrusivePtr.hpp"> <ClInclude Include="..\..\src\core\IntrusivePtr.hpp">
<Filter>core</Filter> <Filter>core</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\core\closure.hpp">
<Filter>core</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="core"> <Filter Include="core">

View File

@ -26,6 +26,7 @@
<ClInclude Include="..\..\src\core\Animation.h" /> <ClInclude Include="..\..\src\core\Animation.h" />
<ClInclude Include="..\..\src\core\audio.h" /> <ClInclude Include="..\..\src\core\audio.h" />
<ClInclude Include="..\..\src\core\Canvas.h" /> <ClInclude Include="..\..\src\core\Canvas.h" />
<ClInclude Include="..\..\src\core\closure.hpp" />
<ClInclude Include="..\..\src\core\Color.h" /> <ClInclude Include="..\..\src\core\Color.h" />
<ClInclude Include="..\..\src\core\d2dhelper.hpp" /> <ClInclude Include="..\..\src\core\d2dhelper.hpp" />
<ClInclude Include="..\..\src\core\DebugNode.h" /> <ClInclude Include="..\..\src\core\DebugNode.h" />

View File

@ -197,6 +197,9 @@
<ClInclude Include="..\..\src\core\IntrusivePtr.hpp"> <ClInclude Include="..\..\src\core\IntrusivePtr.hpp">
<Filter>core</Filter> <Filter>core</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\core\closure.hpp">
<Filter>core</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="core"> <Filter Include="core">

View File

@ -26,6 +26,7 @@
<ClInclude Include="..\..\src\core\Animation.h" /> <ClInclude Include="..\..\src\core\Animation.h" />
<ClInclude Include="..\..\src\core\audio.h" /> <ClInclude Include="..\..\src\core\audio.h" />
<ClInclude Include="..\..\src\core\Canvas.h" /> <ClInclude Include="..\..\src\core\Canvas.h" />
<ClInclude Include="..\..\src\core\closure.hpp" />
<ClInclude Include="..\..\src\core\Color.h" /> <ClInclude Include="..\..\src\core\Color.h" />
<ClInclude Include="..\..\src\core\d2dhelper.hpp" /> <ClInclude Include="..\..\src\core\d2dhelper.hpp" />
<ClInclude Include="..\..\src\core\DebugNode.h" /> <ClInclude Include="..\..\src\core\DebugNode.h" />

View File

@ -197,6 +197,9 @@
<ClInclude Include="..\..\src\core\IntrusivePtr.hpp"> <ClInclude Include="..\..\src\core\IntrusivePtr.hpp">
<Filter>core</Filter> <Filter>core</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\core\closure.hpp">
<Filter>core</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="core"> <Filter Include="core">

View File

@ -26,8 +26,8 @@ namespace easy2d
{ {
Scene::Scene() Scene::Scene()
{ {
AddListener(WindowEvent::Activate, std::bind(&Scene::OnActivate, this)); AddListener(WindowEvent::Activate, Closure(this, &Scene::OnActivate));
AddListener(WindowEvent::Deavtivate, std::bind(&Scene::OnDeactivate, this)); AddListener(WindowEvent::Deavtivate, Closure(this, &Scene::OnDeactivate));
scene_ = this; scene_ = this;
} }
@ -46,11 +46,11 @@ namespace easy2d
E2D_LOG(L"Scene exited"); E2D_LOG(L"Scene exited");
} }
void Scene::OnDeactivate() void Scene::OnDeactivate(Event const&)
{ {
} }
void Scene::OnActivate() void Scene::OnActivate(Event const&)
{ {
} }

View File

@ -39,9 +39,9 @@ namespace easy2d
virtual void OnExit(); virtual void OnExit();
// 窗口获得焦点 // 窗口获得焦点
virtual void OnActivate(); virtual void OnActivate(Event const&);
// 窗口失去焦点 // 窗口失去焦点
virtual void OnDeactivate(); virtual void OnDeactivate(Event const&);
}; };
} }

87
src/core/closure.hpp Normal file
View File

@ -0,0 +1,87 @@
// 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.
#pragma once
#include <functional>
#define CLOSURE_PARAMS_TYPE_1 typename Param1
#define CLOSURE_PARAMS_1 Param1
#define CLOSURE_PLACEHOLDERS_1 std::placeholders::_1
#define CLOSURE_PARAMS_TYPE_2 typename Param1, typename Param2
#define CLOSURE_PARAMS_2 Param1, Param2
#define CLOSURE_PLACEHOLDERS_2 std::placeholders::_1, std::placeholders::_2
#define CLOSURE_PARAMS_TYPE_3 typename Param1, typename Param2, typename Param3
#define CLOSURE_PARAMS_3 Param1, Param2, Param3
#define CLOSURE_PLACEHOLDERS_3 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3
#define CLOSURE_PARAMS_TYPE_4 typename Param1, typename Param2, typename Param3, typename Param4
#define CLOSURE_PARAMS_4 Param1, Param2, Param3, Param4
#define CLOSURE_PLACEHOLDERS_4 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4
#define CLOSURE_PARAMS_TYPE_5 typename Param1, typename Param2, typename Param3, typename Param4, typename Param5
#define CLOSURE_PARAMS_5 Param1, Param2, Param3, Param4, Param5
#define CLOSURE_PLACEHOLDERS_5 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5
#define CLOSURE_PARAMS_TYPE_6 typename Param1, typename Param2, typename Param3, typename Param4, typename Param5, typename Param6
#define CLOSURE_PARAMS_6 Param1, Param2, Param3, Param4, Param5, Param6
#define CLOSURE_PLACEHOLDERS_6 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6
#define CLOSURE_EXPAND(x) x
#define CLOSURE_DECLARE(TYPE, PARAMS, PHS)\
template<typename T, typename R, CLOSURE_EXPAND(TYPE)>\
inline std::function<R(CLOSURE_EXPAND(PARAMS))> Closure(T* ptr, R(T::*func)(CLOSURE_EXPAND(PARAMS)))\
{\
return std::bind(func, ptr, CLOSURE_EXPAND(PHS));\
}
namespace easy2d
{
template<typename T, typename R>
inline std::function<R(void)> Closure(T* ptr, R(T::*func)(void))
{
return std::bind(func, ptr);
}
CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_1, CLOSURE_PARAMS_1, CLOSURE_PLACEHOLDERS_1);
CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_2, CLOSURE_PARAMS_2, CLOSURE_PLACEHOLDERS_2);
CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_3, CLOSURE_PARAMS_3, CLOSURE_PLACEHOLDERS_3);
CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_4, CLOSURE_PARAMS_4, CLOSURE_PLACEHOLDERS_4);
CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_5, CLOSURE_PARAMS_5, CLOSURE_PLACEHOLDERS_5);
CLOSURE_DECLARE(CLOSURE_PARAMS_TYPE_6, CLOSURE_PARAMS_6, CLOSURE_PLACEHOLDERS_6);
}
#undef CLOSURE_PARAMS_TYPE_1
#undef CLOSURE_PARAMS_1
#undef CLOSURE_PLACEHOLDERS_1
#undef CLOSURE_PARAMS_TYPE_2
#undef CLOSURE_PARAMS_2
#undef CLOSURE_PLACEHOLDERS_2
#undef CLOSURE_PARAMS_TYPE_3
#undef CLOSURE_PARAMS_3
#undef CLOSURE_PLACEHOLDERS_3
#undef CLOSURE_PARAMS_TYPE_4
#undef CLOSURE_PARAMS_4
#undef CLOSURE_PLACEHOLDERS_4
#undef CLOSURE_PARAMS_TYPE_5
#undef CLOSURE_PARAMS_5
#undef CLOSURE_PLACEHOLDERS_5
#undef CLOSURE_PARAMS_TYPE_6
#undef CLOSURE_PARAMS_6
#undef CLOSURE_PLACEHOLDERS_6
#undef CLOSURE_EXPAND
#undef CLOSURE_DECLARE

View File

@ -21,6 +21,7 @@
#pragma once #pragma once
#include "RefCounter.hpp" #include "RefCounter.hpp"
#include "IntrusivePtr.hpp" #include "IntrusivePtr.hpp"
#include "closure.hpp"
#include "../math/vector.hpp" #include "../math/vector.hpp"
#include "../math/Rect.hpp" #include "../math/Rect.hpp"
#include "../math/Matrix.hpp" #include "../math/Matrix.hpp"

View File

@ -30,10 +30,10 @@ namespace easy2d
, click_callback_(nullptr) , click_callback_(nullptr)
, status_(Status::Normal) , status_(Status::Normal)
{ {
AddListener(MouseEvent::Hover, std::bind(&Button::UpdateStatus, this, std::placeholders::_1)); AddListener(MouseEvent::Hover, Closure(this, &Button::UpdateStatus));
AddListener(MouseEvent::Out, std::bind(&Button::UpdateStatus, this, std::placeholders::_1)); AddListener(MouseEvent::Out, Closure(this, &Button::UpdateStatus));
AddListener(MouseEvent::Down, std::bind(&Button::UpdateStatus, this, std::placeholders::_1)); AddListener(MouseEvent::Down, Closure(this, &Button::UpdateStatus));
AddListener(MouseEvent::Up, std::bind(&Button::UpdateStatus, this, std::placeholders::_1)); AddListener(MouseEvent::Up, Closure(this, &Button::UpdateStatus));
} }
Button::Button(const Callback& click) Button::Button(const Callback& click)