Ndk/Components: Added On[Attached|Detached] events

Former-commit-id: 46e5b5720a8496dfe536181918ae0a995e028fc1
This commit is contained in:
Lynix 2015-04-23 14:26:48 +02:00
parent 581496ce44
commit ad290a18f4
3 changed files with 19 additions and 1 deletions

View File

@ -43,8 +43,10 @@ namespace Ndk
static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc); static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc);
private: private:
virtual void OnAttached();
virtual void OnComponentAttached(BaseComponent& component); virtual void OnComponentAttached(BaseComponent& component);
virtual void OnComponentDetached(BaseComponent& component); virtual void OnComponentDetached(BaseComponent& component);
virtual void OnDetached();
void SetEntity(Entity* entity); void SetEntity(Entity* entity);
static bool Initialize(); static bool Initialize();

View File

@ -38,7 +38,15 @@ namespace Ndk
inline void BaseComponent::SetEntity(Entity* entity) inline void BaseComponent::SetEntity(Entity* entity)
{ {
m_entity = entity; if (m_entity != entity)
{
if (m_entity)
OnDetached();
m_entity = entity;
if (m_entity)
OnAttached();
}
} }
inline bool BaseComponent::Initialize() inline bool BaseComponent::Initialize()

View File

@ -8,6 +8,10 @@ namespace Ndk
{ {
BaseComponent::~BaseComponent() = default; BaseComponent::~BaseComponent() = default;
void BaseComponent::OnAttached()
{
}
void BaseComponent::OnComponentAttached(BaseComponent& component) void BaseComponent::OnComponentAttached(BaseComponent& component)
{ {
NazaraUnused(component); NazaraUnused(component);
@ -18,6 +22,10 @@ namespace Ndk
NazaraUnused(component); NazaraUnused(component);
} }
void BaseComponent::OnDetached()
{
}
std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries; std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries;
std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex; std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex;
} }