refactoring project

This commit is contained in:
Nomango 2020-05-24 20:46:52 +08:00
parent 76d488b4c5
commit 6d1cf35730
17 changed files with 305 additions and 198 deletions

View File

@ -9,9 +9,11 @@
<ClInclude Include="..\..\src\kiwano\2d\action\ActionWalk.h" />
<ClInclude Include="..\..\src\kiwano\2d\action\ActionTween.h" />
<ClInclude Include="..\..\src\kiwano\2d\action\Animation.h" />
<ClInclude Include="..\..\src\kiwano\2d\Button.h" />
<ClInclude Include="..\..\src\kiwano\2d\Component.h" />
<ClInclude Include="..\..\src\kiwano\2d\Actor.h" />
<ClInclude Include="..\..\src\kiwano\2d\GifSprite.h" />
<ClInclude Include="..\..\src\kiwano\base\component\Button.h" />
<ClInclude Include="..\..\src\kiwano\base\component\Component.h" />
<ClInclude Include="..\..\src\kiwano\base\component\ComponentManager.h" />
<ClInclude Include="..\..\src\kiwano\base\Director.h" />
<ClInclude Include="..\..\src\kiwano\base\Module.h" />
<ClInclude Include="..\..\src\kiwano\base\ObjectBase.h" />
@ -44,7 +46,6 @@
<ClInclude Include="..\..\src\kiwano\2d\DebugActor.h" />
<ClInclude Include="..\..\src\kiwano\2d\ShapeActor.h" />
<ClInclude Include="..\..\src\kiwano\2d\LayerActor.h" />
<ClInclude Include="..\..\src\kiwano\2d\Actor.h" />
<ClInclude Include="..\..\src\kiwano\2d\Stage.h" />
<ClInclude Include="..\..\src\kiwano\2d\Sprite.h" />
<ClInclude Include="..\..\src\kiwano\2d\TextActor.h" />
@ -115,18 +116,19 @@
<ClCompile Include="..\..\src\kiwano\2d\action\ActionWalk.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\action\ActionTween.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\action\Animation.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Button.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Actor.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Component.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\DebugActor.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\ShapeActor.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\GifSprite.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\LayerActor.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Actor.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Stage.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Sprite.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\TextActor.cpp" />
<ClCompile Include="..\..\src\kiwano\2d\Transition.cpp" />
<ClCompile Include="..\..\src\kiwano\base\component\Button.cpp" />
<ClCompile Include="..\..\src\kiwano\base\component\Component.cpp" />
<ClCompile Include="..\..\src\kiwano\base\component\ComponentManager.cpp" />
<ClCompile Include="..\..\src\kiwano\base\Director.cpp" />
<ClCompile Include="..\..\src\kiwano\base\Module.cpp" />
<ClCompile Include="..\..\src\kiwano\base\ObjectBase.cpp" />

View File

@ -34,6 +34,9 @@
<Filter Include="event">
<UniqueIdentifier>{b0d28d20-bfaf-4816-8415-28c20ecf9fac}</UniqueIdentifier>
</Filter>
<Filter Include="base\component">
<UniqueIdentifier>{d15f4de1-7c2c-40d6-a3ce-68860b95a61e}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\kiwano\2d\Canvas.h">
@ -69,9 +72,6 @@
<ClInclude Include="..\..\src\kiwano\2d\GifSprite.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\2d\Actor.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\2d\Stage.h">
<Filter>2d</Filter>
</ClInclude>
@ -132,9 +132,6 @@
<ClInclude Include="..\..\src\kiwano\platform\win32\libraries.h">
<Filter>platform\win32</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\2d\Button.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\Singleton.h">
<Filter>core</Filter>
</ClInclude>
@ -261,9 +258,6 @@
<ClInclude Include="..\..\src\kiwano\render\ShapeMaker.h">
<Filter>render</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\2d\Component.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\Allocator.h">
<Filter>core</Filter>
</ClInclude>
@ -345,6 +339,18 @@
<ClInclude Include="..\..\src\kiwano\event\WindowEvent.h">
<Filter>event</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\2d\Actor.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\base\component\Button.h">
<Filter>base\component</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\base\component\Component.h">
<Filter>base\component</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\base\component\ComponentManager.h">
<Filter>base\component</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">
@ -365,9 +371,6 @@
<ClCompile Include="..\..\src\kiwano\2d\GifSprite.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\2d\Actor.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\2d\Stage.cpp">
<Filter>2d</Filter>
</ClCompile>
@ -422,9 +425,6 @@
<ClCompile Include="..\..\src\kiwano\platform\win32\libraries.cpp">
<Filter>platform\win32</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\2d\Button.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\platform\win32\WindowImpl.cpp">
<Filter>platform\win32</Filter>
</ClCompile>
@ -503,9 +503,6 @@
<ClCompile Include="..\..\src\kiwano\render\ShapeMaker.cpp">
<Filter>render</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\2d\Component.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\core\Allocator.cpp">
<Filter>core</Filter>
</ClCompile>
@ -566,6 +563,18 @@
<ClCompile Include="..\..\src\kiwano\event\WindowEvent.cpp">
<Filter>event</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\2d\Actor.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\base\component\Button.cpp">
<Filter>base\component</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\base\component\Component.cpp">
<Filter>base\component</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\base\component\ComponentManager.cpp">
<Filter>base\component</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="suppress_warning.ruleset" />

View File

@ -46,7 +46,8 @@ ActorPtr Actor::Create()
}
Actor::Actor()
: visible_(true)
: ComponentManager(this)
, visible_(true)
, visible_in_rt_(true)
, update_pausing_(false)
, hover_(false)
@ -79,7 +80,7 @@ void Actor::Update(Duration dt)
{
ActionScheduler::Update(this, dt);
TaskScheduler::Update(dt);
UpdateComponents(dt);
ComponentManager::Update(dt);
if (!update_pausing_)
{
@ -112,7 +113,7 @@ void Actor::Render(RenderContext& ctx)
if (CheckVisibility(ctx))
{
PrepareToRender(ctx);
RenderComponents(ctx);
ComponentManager::Render(ctx);
OnRender(ctx);
}
}
@ -132,7 +133,7 @@ void Actor::Render(RenderContext& ctx)
if (CheckVisibility(ctx))
{
PrepareToRender(ctx);
RenderComponents(ctx);
ComponentManager::Render(ctx);
OnRender(ctx);
}
@ -242,19 +243,7 @@ void Actor::DoDeserialize(Deserializer* deserializer)
bool Actor::HandleEvent(Event* evt)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsEnable())
{
component->HandleEvent(evt);
}
}
}
ComponentManager::DispatchToComponents(evt);
if (!EventDispatcher::DispatchEvent(evt))
return false;
@ -304,40 +293,6 @@ bool Actor::HandleEvent(Event* evt)
return true;
}
void Actor::UpdateComponents(Duration dt)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsEnable())
{
component->OnUpdate(dt);
}
}
}
}
void Actor::RenderComponents(RenderContext& ctx)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsEnable())
{
component->OnRender(ctx);
}
}
}
}
const Matrix3x2& Actor::GetTransformMatrix() const
{
UpdateTransform();
@ -657,72 +612,6 @@ void Actor::RemoveFromParent()
}
}
Component* Actor::AddComponent(ComponentPtr component)
{
KGE_ASSERT(component && "AddComponent failed, NULL pointer exception");
if (component)
{
component->InitComponent(this);
components_.PushBack(component);
}
return component.Get();
}
ComponentList& Actor::GetAllComponents()
{
return components_;
}
const ComponentList& Actor::GetAllComponents() const
{
return components_;
}
void Actor::RemoveComponent(ComponentPtr component)
{
auto iter = std::find(components_.begin(), components_.end(), component);
if (iter != components_.end())
{
component->DestroyComponent();
components_.Remove(component);
}
}
void Actor::RemoveComponents(const String& name)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsName(name))
{
component->DestroyComponent();
components_.Remove(component);
}
}
}
}
void Actor::RemoveAllComponents()
{
// Destroy all components
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
component->DestroyComponent();
}
}
components_.Clear();
}
void Actor::RemoveChild(ActorPtr child)
{
KGE_ASSERT(child && "Actor::RemoveChild failed, NULL pointer exception");

View File

@ -20,12 +20,12 @@
#pragma once
#include <kiwano/math/Math.h>
#include <kiwano/base/ObjectBase.h>
#include <kiwano/core/Time.h>
#include <kiwano/base/ObjectBase.h>
#include <kiwano/base/component/ComponentManager.h>
#include <kiwano/event/EventDispatcher.h>
#include <kiwano/utils/TaskScheduler.h>
#include <kiwano/2d/action/ActionScheduler.h>
#include <kiwano/2d/Component.h>
namespace kiwano
{
@ -65,6 +65,7 @@ class KGE_API Actor
, public TaskScheduler
, public ActionScheduler
, public EventDispatcher
, public ComponentManager
, protected IntrusiveListValue<ActorPtr>
{
friend class Director;
@ -370,32 +371,6 @@ public:
/// @brief 从父角色移除
void RemoveFromParent();
/// \~chinese
/// @brief 添加组件
/// @param component 组件
Component* AddComponent(ComponentPtr component);
/// \~chinese
/// @brief 获取所有组件
ComponentList& GetAllComponents();
/// \~chinese
/// @brief 获取所有组件
const ComponentList& GetAllComponents() const;
/// \~chinese
/// @brief 移除组件
void RemoveComponent(ComponentPtr component);
/// \~chinese
/// @brief 移除组件
/// @param name 组件名称
void RemoveComponents(const String& name);
/// \~chinese
/// @brief 移除所有组件
void RemoveAllComponents();
/// \~chinese
/// @brief 暂停角色更新
void PauseUpdating();
@ -499,14 +474,6 @@ protected:
/// @brief 处理事件
bool HandleEvent(Event* evt);
/// \~chinese
/// @brief 更新组件
void UpdateComponents(Duration dt);
/// \~chinese
/// @brief 渲染组件
void RenderComponents(RenderContext& ctx);
private:
bool visible_;
bool update_pausing_;
@ -524,7 +491,6 @@ private:
Point anchor_;
Size size_;
ActorList children_;
ComponentList components_;
UpdateCallback cb_update_;
Transform transform_;

View File

@ -34,10 +34,6 @@ class ActionScheduler;
KGE_DECLARE_SMART_PTR(Action);
/// \~chinese
/// @brief 动画列表
typedef IntrusiveList<ActionPtr> ActionList;
/**
* \~chinese
* \defgroup Actions

View File

@ -28,6 +28,10 @@ namespace kiwano
* @{
*/
/// \~chinese
/// @brief 动画列表
typedef IntrusiveList<ActionPtr> ActionList;
/**
* \~chinese
* @brief

View File

@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <kiwano/2d/Button.h>
#include <kiwano/base/component/Button.h>
#include <kiwano/2d/Stage.h>
#include <kiwano/platform/Application.h>

View File

@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <kiwano/2d/Component.h>
#include <kiwano/base/component/Component.h>
#include <kiwano/2d/Actor.h>
namespace kiwano

View File

@ -29,6 +29,7 @@ namespace kiwano
class Actor;
class Event;
class ComponentManager;
KGE_DECLARE_SMART_PTR(Component);
@ -42,10 +43,6 @@ KGE_DECLARE_SMART_PTR(Component);
* @{
*/
/// \~chinese
/// @brief 组件列表
typedef IntrusiveList<ComponentPtr> ComponentList;
/**
* \~chinese
* @brief ×éźţ
@ -54,7 +51,7 @@ class KGE_API Component
: public ObjectBase
, protected IntrusiveListValue<ComponentPtr>
{
friend class Actor;
friend class ComponentManager;
friend IntrusiveList<ComponentPtr>;
public:

View File

@ -0,0 +1,148 @@
// Copyright (c) 2016-2018 Kiwano - 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 <kiwano/base/component/ComponentManager.h>
namespace kiwano
{
ComponentManager::ComponentManager(Actor* target)
: target_(target)
{
}
Component* ComponentManager::AddComponent(ComponentPtr component)
{
KGE_ASSERT(component && "AddComponent failed, NULL pointer exception");
if (component)
{
component->InitComponent(target_);
components_.PushBack(component);
}
return component.Get();
}
ComponentList& ComponentManager::GetAllComponents()
{
return components_;
}
const ComponentList& ComponentManager::GetAllComponents() const
{
return components_;
}
void ComponentManager::RemoveComponent(ComponentPtr component)
{
auto iter = std::find(components_.begin(), components_.end(), component);
if (iter != components_.end())
{
component->DestroyComponent();
components_.Remove(component);
}
}
void ComponentManager::RemoveComponents(const String& name)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsName(name))
{
component->DestroyComponent();
components_.Remove(component);
}
}
}
}
void ComponentManager::RemoveAllComponents()
{
// Destroy all components
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
component->DestroyComponent();
}
}
components_.Clear();
}
void ComponentManager::Update(Duration dt)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsEnable())
{
component->OnUpdate(dt);
}
}
}
}
void ComponentManager::Render(RenderContext& ctx)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsEnable())
{
component->OnRender(ctx);
}
}
}
}
void ComponentManager::DispatchToComponents(Event* evt)
{
if (!components_.IsEmpty())
{
ComponentPtr next;
for (auto component = components_.GetFirst(); component; component = next)
{
next = component->GetNext();
if (component->IsEnable())
{
component->HandleEvent(evt);
}
}
}
}
} // namespace kiwano

View File

@ -0,0 +1,92 @@
// Copyright (c) 2016-2018 Kiwano - 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 <kiwano/core/IntrusiveList.h>
#include <kiwano/base/component/Component.h>
namespace kiwano
{
/**
* \addtogroup Component
* @{
*/
/// \~chinese
/// @brief 组件列表
typedef IntrusiveList<ComponentPtr> ComponentList;
/**
* \~chinese
* @brief
*/
class KGE_API ComponentManager
{
public:
/// \~chinese
/// @brief 添加组件
/// @param component 组件
Component* AddComponent(ComponentPtr component);
/// \~chinese
/// @brief 获取所有组件
ComponentList& GetAllComponents();
/// \~chinese
/// @brief 获取所有组件
const ComponentList& GetAllComponents() const;
/// \~chinese
/// @brief 移除组件
void RemoveComponent(ComponentPtr component);
/// \~chinese
/// @brief 移除组件
/// @param name 组件名称
void RemoveComponents(const String& name);
/// \~chinese
/// @brief 移除所有组件
void RemoveAllComponents();
/// \~chinese
/// @brief 更新组件
void Update(Duration dt);
/// \~chinese
/// @brief 渲染组件
void Render(RenderContext& ctx);
/// \~chinese
/// @brief 分发事件
void DispatchToComponents(Event* evt);
protected:
ComponentManager(Actor* target);
private:
Actor* target_;
ComponentList components_;
};
/** @} */
} // namespace kiwano

View File

@ -23,6 +23,11 @@
namespace kiwano
{
/// \~chinese
/// @brief 监听器列表
typedef IntrusiveList<EventListenerPtr> ListenerList;
/**
* \~chinese
* @brief

View File

@ -31,10 +31,6 @@ class EventDispatcher;
KGE_DECLARE_SMART_PTR(EventListener);
/// \~chinese
/// @brief 监听器列表
typedef IntrusiveList<EventListenerPtr> ListenerList;
/**
* \~chinese
* @brief

View File

@ -56,6 +56,9 @@
#include <kiwano/base/ObjectBase.h>
#include <kiwano/base/Director.h>
#include <kiwano/base/Module.h>
#include <kiwano/base/component/Component.h>
#include <kiwano/base/component/ComponentManager.h>
#include <kiwano/base/component/Button.h>
//
@ -91,7 +94,6 @@
//
#include <kiwano/2d/Actor.h>
#include <kiwano/2d/Button.h>
#include <kiwano/2d/Canvas.h>
#include <kiwano/2d/DebugActor.h>
#include <kiwano/2d/GifSprite.h>

View File

@ -28,10 +28,6 @@ class TaskScheduler;
KGE_DECLARE_SMART_PTR(Task);
/// \~chinese
/// @brief ÈÎÎñÁбí
typedef IntrusiveList<TaskPtr> TaskList;
/// \~chinese
/// @brief 任务
/// @details 任务用于每隔一段时间执行一次回调函数,且可以指定执行总次数

View File

@ -23,6 +23,11 @@
namespace kiwano
{
/// \~chinese
/// @brief ÈÎÎñÁбí
typedef IntrusiveList<TaskPtr> TaskList;
/**
* \~chinese
* @brief