Sdk/LuaBinding: Expose BindComponent for user components
Former-commit-id: 3d9f1d14f6b63ec72b61e15e6e549e46e958a92f
This commit is contained in:
parent
922e49accb
commit
6a259c9ef9
|
|
@ -27,12 +27,14 @@
|
|||
|
||||
namespace Ndk
|
||||
{
|
||||
class LuaBinding
|
||||
class NDK_API LuaBinding
|
||||
{
|
||||
public:
|
||||
LuaBinding();
|
||||
~LuaBinding() = default;
|
||||
|
||||
template<typename T> void BindComponent(const Nz::String& name);
|
||||
|
||||
void RegisterClasses(Nz::LuaInstance& instance);
|
||||
|
||||
private:
|
||||
|
|
@ -42,8 +44,6 @@ namespace Ndk
|
|||
void BindSDK();
|
||||
void BindUtility();
|
||||
|
||||
template<typename T> void EnableComponentBinding();
|
||||
|
||||
void RegisterCore(Nz::LuaInstance& instance);
|
||||
void RegisterMath(Nz::LuaInstance& instance);
|
||||
void RegisterNetwork(Nz::LuaInstance& instance);
|
||||
|
|
@ -93,8 +93,9 @@ namespace Ndk
|
|||
struct ComponentBinding
|
||||
{
|
||||
AddComponentFunc adder;
|
||||
ComponentIndex index;
|
||||
GetComponentFunc getter;
|
||||
bool valid = false;
|
||||
Nz::String name;
|
||||
};
|
||||
|
||||
std::vector<ComponentBinding> m_componentBinding;
|
||||
|
|
@ -115,6 +116,14 @@ namespace Ndk
|
|||
Nz::LuaClass<GraphicsComponentHandle> graphicsComponent;
|
||||
#endif
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle);
|
||||
|
||||
template<typename T>
|
||||
int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component);
|
||||
}
|
||||
|
||||
#include <NDK/LuaBinding.inl>
|
||||
|
||||
#endif // NDK_LUABINDING_HPP
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/LuaBinding.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
template<typename T>
|
||||
void LuaBinding::BindComponent(const Nz::String& name)
|
||||
{
|
||||
NazaraAssert(!name.IsEmpty(), "Component name cannot be empty");
|
||||
|
||||
ComponentBinding binding;
|
||||
binding.adder = &AddComponentOfType<T>;
|
||||
binding.getter = &PushComponentOfType<T>;
|
||||
binding.index = T::componentIndex;
|
||||
binding.name = name;
|
||||
|
||||
if (m_componentBinding.size() <= T::componentIndex)
|
||||
m_componentBinding.resize(T::componentIndex + 1);
|
||||
|
||||
m_componentBinding[T::componentIndex] = std::move(binding);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle)
|
||||
{
|
||||
static_assert(std::is_base_of<BaseComponent, T>::value, "ComponentType must inherit BaseComponent");
|
||||
|
||||
T& component = handle->AddComponent<T>();
|
||||
lua.Push(component.CreateHandle());
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component)
|
||||
{
|
||||
static_assert(std::is_base_of<BaseComponent, T>::value, "ComponentType must inherit BaseComponent");
|
||||
|
||||
T& rightComponent = static_cast<T&>(component);
|
||||
lua.Push(rightComponent.CreateHandle());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,41 +7,6 @@
|
|||
|
||||
namespace Ndk
|
||||
{
|
||||
namespace
|
||||
{
|
||||
int AddNilComponent(Nz::LuaInstance& lua, EntityHandle&)
|
||||
{
|
||||
lua.PushNil();
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle)
|
||||
{
|
||||
static_assert(std::is_base_of<BaseComponent, T>::value, "ComponentType must inherit BaseComponent");
|
||||
|
||||
T& component = handle->AddComponent<T>();
|
||||
lua.Push(component.CreateHandle());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int PushNilComponent(Nz::LuaInstance& lua, BaseComponent&)
|
||||
{
|
||||
lua.PushNil();
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component)
|
||||
{
|
||||
static_assert(std::is_base_of<BaseComponent, T>::value, "ComponentType must inherit BaseComponent");
|
||||
|
||||
T& rightComponent = static_cast<T&>(component);
|
||||
lua.Push(rightComponent.CreateHandle());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void LuaBinding::BindSDK()
|
||||
{
|
||||
/*********************************** Ndk::Application **********************************/
|
||||
|
|
@ -110,7 +75,7 @@ namespace Ndk
|
|||
}
|
||||
|
||||
ComponentBinding& binding = m_componentBinding[componentIndex];
|
||||
if (!binding.valid)
|
||||
if (binding.name.IsEmpty())
|
||||
{
|
||||
lua.Error("This component is not available to the LuaAPI");
|
||||
return 0;
|
||||
|
|
@ -137,7 +102,7 @@ namespace Ndk
|
|||
}
|
||||
|
||||
ComponentBinding& binding = m_componentBinding[componentIndex];
|
||||
if (!binding.valid)
|
||||
if (binding.name.IsEmpty())
|
||||
{
|
||||
lua.Error("This component is not available to the LuaAPI");
|
||||
return 0;
|
||||
|
|
@ -195,29 +160,16 @@ namespace Ndk
|
|||
|
||||
|
||||
// Components functions
|
||||
m_componentBinding.resize(BaseComponent::GetMaxComponentIndex() + 1);
|
||||
m_componentBinding.resize(BaseComponent::GetMaxComponentIndex());
|
||||
|
||||
EnableComponentBinding<NodeComponent>();
|
||||
EnableComponentBinding<VelocityComponent>();
|
||||
BindComponent<NodeComponent>("Node");
|
||||
BindComponent<VelocityComponent>("Velocity");
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
EnableComponentBinding<GraphicsComponent>();
|
||||
BindComponent<GraphicsComponent>("Graphics");
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LuaBinding::EnableComponentBinding()
|
||||
{
|
||||
ComponentBinding binding;
|
||||
binding.adder = &AddComponentOfType<T>;
|
||||
binding.getter = &PushComponentOfType<T>;
|
||||
binding.valid = true;
|
||||
|
||||
NazaraAssert(T::componentIndex < m_componentBinding.size(), "Component index is over component binding size");
|
||||
|
||||
m_componentBinding[T::componentIndex] = std::move(binding);
|
||||
}
|
||||
|
||||
void LuaBinding::RegisterSDK(Nz::LuaInstance& instance)
|
||||
{
|
||||
// Classes
|
||||
|
|
@ -235,18 +187,16 @@ namespace Ndk
|
|||
// Enums
|
||||
|
||||
// ComponentType (fake enumeration to expose component indexes)
|
||||
instance.PushTable();
|
||||
instance.PushTable(0, m_componentBinding.size());
|
||||
{
|
||||
#ifndef NDK_SERVER
|
||||
instance.PushInteger(GraphicsComponent::componentIndex);
|
||||
instance.SetField("Graphics");
|
||||
#endif
|
||||
for (const ComponentBinding& entry : m_componentBinding)
|
||||
{
|
||||
if (entry.name.IsEmpty())
|
||||
continue;
|
||||
|
||||
instance.PushInteger(NodeComponent::componentIndex);
|
||||
instance.SetField("Node");
|
||||
|
||||
instance.PushInteger(VelocityComponent::componentIndex);
|
||||
instance.SetField("Velocity");
|
||||
instance.PushInteger(entry.index);
|
||||
instance.SetField(entry.name);
|
||||
}
|
||||
}
|
||||
instance.SetGlobal("ComponentType");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue