Sdk/LuaBinding: Expose BindComponent for user components
Former-commit-id: 3d9f1d14f6b63ec72b61e15e6e549e46e958a92f
This commit is contained in:
129
SDK/include/NDK/LuaBinding.hpp
Normal file
129
SDK/include/NDK/LuaBinding.hpp
Normal file
@@ -0,0 +1,129 @@
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_LUABINDING_HPP
|
||||
#define NDK_LUABINDING_HPP
|
||||
|
||||
#include <Nazara/Core.hpp>
|
||||
#include <Nazara/Lua.hpp>
|
||||
#include <Nazara/Math.hpp>
|
||||
#include <Nazara/Network.hpp>
|
||||
#include <Nazara/Utility.hpp>
|
||||
#include <NDK/Application.hpp>
|
||||
#include <NDK/Components.hpp>
|
||||
#include <NDK/Entity.hpp>
|
||||
#include <NDK/Systems.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
#include <Nazara/Audio.hpp>
|
||||
#include <Nazara/Graphics.hpp>
|
||||
#include <Nazara/Renderer.hpp>
|
||||
#include <NDK/Console.hpp>
|
||||
#endif
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class NDK_API LuaBinding
|
||||
{
|
||||
public:
|
||||
LuaBinding();
|
||||
~LuaBinding() = default;
|
||||
|
||||
template<typename T> void BindComponent(const Nz::String& name);
|
||||
|
||||
void RegisterClasses(Nz::LuaInstance& instance);
|
||||
|
||||
private:
|
||||
void BindCore();
|
||||
void BindMath();
|
||||
void BindNetwork();
|
||||
void BindSDK();
|
||||
void BindUtility();
|
||||
|
||||
void RegisterCore(Nz::LuaInstance& instance);
|
||||
void RegisterMath(Nz::LuaInstance& instance);
|
||||
void RegisterNetwork(Nz::LuaInstance& instance);
|
||||
void RegisterSDK(Nz::LuaInstance& instance);
|
||||
void RegisterUtility(Nz::LuaInstance& instance);
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
void BindAudio();
|
||||
void BindGraphics();
|
||||
void BindRenderer();
|
||||
|
||||
void RegisterAudio(Nz::LuaInstance& instance);
|
||||
void RegisterGraphics(Nz::LuaInstance& instance);
|
||||
void RegisterRenderer(Nz::LuaInstance& instance);
|
||||
#endif
|
||||
|
||||
// Core
|
||||
Nz::LuaClass<Nz::Clock> clockClass;
|
||||
Nz::LuaClass<Nz::Directory> directoryClass;
|
||||
Nz::LuaClass<Nz::File> fileClass;
|
||||
Nz::LuaClass<Nz::Stream> streamClass;
|
||||
|
||||
// Math
|
||||
Nz::LuaClass<Nz::EulerAnglesd> eulerAnglesClass;
|
||||
Nz::LuaClass<Nz::Quaterniond> quaternionClass;
|
||||
Nz::LuaClass<Nz::Vector2d> vector2dClass;
|
||||
Nz::LuaClass<Nz::Vector3d> vector3dClass;
|
||||
|
||||
// Network
|
||||
Nz::LuaClass<Nz::AbstractSocket> abstractSocketClass;
|
||||
Nz::LuaClass<Nz::IpAddress> ipAddressClass;
|
||||
|
||||
// Utility
|
||||
Nz::LuaClass<Nz::AbstractImage*> abstractImage;
|
||||
Nz::LuaClass<Nz::Node> nodeClass;
|
||||
|
||||
// SDK
|
||||
Nz::LuaClass<Application*> application;
|
||||
Nz::LuaClass<EntityHandle> entityClass;
|
||||
Nz::LuaClass<NodeComponentHandle> nodeComponent;
|
||||
Nz::LuaClass<VelocityComponentHandle> velocityComponent;
|
||||
Nz::LuaClass<WorldHandle> worldClass;
|
||||
|
||||
using AddComponentFunc = int(*)(Nz::LuaInstance&, EntityHandle&);
|
||||
using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&);
|
||||
|
||||
struct ComponentBinding
|
||||
{
|
||||
AddComponentFunc adder;
|
||||
ComponentIndex index;
|
||||
GetComponentFunc getter;
|
||||
Nz::String name;
|
||||
};
|
||||
|
||||
std::vector<ComponentBinding> m_componentBinding;
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
// Audio
|
||||
Nz::LuaClass<Nz::Music> musicClass;
|
||||
Nz::LuaClass<Nz::Sound> soundClass;
|
||||
Nz::LuaClass<Nz::SoundBufferRef> soundBuffer;
|
||||
Nz::LuaClass<Nz::SoundEmitter> soundEmitter;
|
||||
|
||||
// Graphics
|
||||
Nz::LuaClass<Nz::InstancedRenderableRef> instancedRenderable;
|
||||
Nz::LuaClass<Nz::ModelRef> modelClass;
|
||||
|
||||
// SDK
|
||||
Nz::LuaClass<ConsoleHandle> consoleClass;
|
||||
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
|
||||
45
SDK/include/NDK/LuaBinding.inl
Normal file
45
SDK/include/NDK/LuaBinding.inl
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user