From 6a259c9ef9ec63470bd50fb7828e2873ed6100a2 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 17 Apr 2016 20:50:26 +0200 Subject: [PATCH] Sdk/LuaBinding: Expose BindComponent for user components Former-commit-id: 3d9f1d14f6b63ec72b61e15e6e549e46e958a92f --- SDK/{src => include}/NDK/LuaBinding.hpp | 17 ++++-- SDK/include/NDK/LuaBinding.inl | 45 ++++++++++++++ SDK/src/NDK/LuaBinding_SDK.cpp | 78 +++++-------------------- 3 files changed, 72 insertions(+), 68 deletions(-) rename SDK/{src => include}/NDK/LuaBinding.hpp (89%) create mode 100644 SDK/include/NDK/LuaBinding.inl diff --git a/SDK/src/NDK/LuaBinding.hpp b/SDK/include/NDK/LuaBinding.hpp similarity index 89% rename from SDK/src/NDK/LuaBinding.hpp rename to SDK/include/NDK/LuaBinding.hpp index 93363f7fb..a0e3e7719 100644 --- a/SDK/src/NDK/LuaBinding.hpp +++ b/SDK/include/NDK/LuaBinding.hpp @@ -27,12 +27,14 @@ namespace Ndk { - class LuaBinding + class NDK_API LuaBinding { public: LuaBinding(); ~LuaBinding() = default; + template void BindComponent(const Nz::String& name); + void RegisterClasses(Nz::LuaInstance& instance); private: @@ -42,8 +44,6 @@ namespace Ndk void BindSDK(); void BindUtility(); - template 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 m_componentBinding; @@ -115,6 +116,14 @@ namespace Ndk Nz::LuaClass graphicsComponent; #endif }; + + template + int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle); + + template + int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component); } +#include + #endif // NDK_LUABINDING_HPP diff --git a/SDK/include/NDK/LuaBinding.inl b/SDK/include/NDK/LuaBinding.inl new file mode 100644 index 000000000..5ca290398 --- /dev/null +++ b/SDK/include/NDK/LuaBinding.inl @@ -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 + +namespace Ndk +{ + template + void LuaBinding::BindComponent(const Nz::String& name) + { + NazaraAssert(!name.IsEmpty(), "Component name cannot be empty"); + + ComponentBinding binding; + binding.adder = &AddComponentOfType; + binding.getter = &PushComponentOfType; + 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 + int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle) + { + static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); + + T& component = handle->AddComponent(); + lua.Push(component.CreateHandle()); + return 1; + } + + template + int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component) + { + static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); + + T& rightComponent = static_cast(component); + lua.Push(rightComponent.CreateHandle()); + return 1; + } +} diff --git a/SDK/src/NDK/LuaBinding_SDK.cpp b/SDK/src/NDK/LuaBinding_SDK.cpp index da456c732..7f020934d 100644 --- a/SDK/src/NDK/LuaBinding_SDK.cpp +++ b/SDK/src/NDK/LuaBinding_SDK.cpp @@ -7,41 +7,6 @@ namespace Ndk { - namespace - { - int AddNilComponent(Nz::LuaInstance& lua, EntityHandle&) - { - lua.PushNil(); - return 1; - } - - template - int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle) - { - static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); - - T& component = handle->AddComponent(); - lua.Push(component.CreateHandle()); - return 1; - } - - int PushNilComponent(Nz::LuaInstance& lua, BaseComponent&) - { - lua.PushNil(); - return 1; - } - - template - int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component) - { - static_assert(std::is_base_of::value, "ComponentType must inherit BaseComponent"); - - T& rightComponent = static_cast(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(); - EnableComponentBinding(); + BindComponent("Node"); + BindComponent("Velocity"); #ifndef NDK_SERVER - EnableComponentBinding(); + BindComponent("Graphics"); #endif } - template - void LuaBinding::EnableComponentBinding() - { - ComponentBinding binding; - binding.adder = &AddComponentOfType; - binding.getter = &PushComponentOfType; - 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"); }