Sdk: Add binding of SDK

Former-commit-id: 4bad2b5db52a2fe459ad7802eb4a894fe3bf006e
This commit is contained in:
Lynix
2016-03-31 21:16:48 +02:00
parent 809c340edc
commit 1ecc52c718
5 changed files with 872 additions and 43 deletions

View File

@@ -10,5 +10,37 @@ namespace Ndk
{
void LuaAPI::Register_Graphics(Nz::LuaInstance& instance)
{
Nz::LuaClass<Nz::InstancedRenderableRef> instancedRenderable("InstancedRenderable");
Nz::LuaClass<Nz::ModelRef> modelClass("Model");
modelClass.Inherit<Nz::InstancedRenderableRef>(instancedRenderable, [] (Nz::ModelRef* model) -> Nz::InstancedRenderableRef*
{
return reinterpret_cast<Nz::InstancedRenderableRef*>(model); //TODO: Make a ObjectRefCast
});
modelClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::ModelRef*
{
return new Nz::ModelRef(new Nz::Model);
});
//modelClass.SetMethod("GetMaterial", &Nz::Model::GetMaterial);
modelClass.SetMethod("GetMaterialCount", &Nz::Model::GetMaterialCount);
//modelClass.SetMethod("GetMesh", &Nz::Model::GetMesh);
modelClass.SetMethod("GetSkin", &Nz::Model::GetSkin);
modelClass.SetMethod("GetSkinCount", &Nz::Model::GetSkinCount);
modelClass.SetMethod("IsAnimated", &Nz::Model::IsAnimated);
modelClass.SetMethod("LoadFromFile", &Nz::Model::LoadFromFile, Nz::ModelParameters());
modelClass.SetMethod("Reset", &Nz::Model::Reset);
//modelClass.SetMethod("SetMaterial", &Nz::Model::SetMaterial);
//modelClass.SetMethod("SetMesh", &Nz::Model::SetMesh);
//modelClass.SetMethod("SetSequence", &Nz::Model::SetSequence);
modelClass.SetMethod("SetSkin", &Nz::Model::SetSkin);
modelClass.SetMethod("SetSkinCount", &Nz::Model::SetSkinCount);
instancedRenderable.Register(instance);
modelClass.Register(instance);
}
}

View File

@@ -8,6 +8,243 @@ namespace Ndk
{
void LuaAPI::Register_Math(Nz::LuaInstance& instance)
{
/*********************************** Nz::EulerAngles **********************************/
Nz::LuaClass<Nz::EulerAnglesd> eulerAnglesClass("EulerAngles");
eulerAnglesClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::EulerAnglesd*
{
unsigned int argCount = std::min(lua.GetStackTop(), 3U);
switch (argCount)
{
case 0:
return new Nz::EulerAnglesd(0.0, 0.0, 0.0);
case 1:
return new Nz::EulerAnglesd(*(*static_cast<Nz::EulerAnglesd**>(lua.CheckUserdata(1, "EulerAngles"))));
case 3:
return new Nz::EulerAnglesd(lua.CheckNumber(1), lua.CheckNumber(2), lua.CheckNumber(3));
}
lua.Error("No matching overload for EulerAngles constructor");
return nullptr;
});
eulerAnglesClass.SetMethod("__tostring", &Nz::EulerAnglesd::ToString);
eulerAnglesClass.SetGetter([] (Nz::LuaInstance& lua, Nz::EulerAnglesd& instance)
{
std::size_t length;
const char* ypr = lua.CheckString(1, &length);
switch (length)
{
case 1:
{
switch (ypr[0])
{
case 'p':
lua.Push(instance.pitch);
return true;
case 'y':
lua.Push(instance.yaw);
return true;
case 'r':
lua.Push(instance.roll);
return true;
}
break;
}
case 3:
{
if (std::memcmp(ypr, "yaw", 3) != 0)
break;
lua.Push(instance.yaw);
return true;
}
case 4:
{
if (std::memcmp(ypr, "roll", 4) != 0)
break;
lua.Push(instance.roll);
return true;
}
case 5:
{
if (std::memcmp(ypr, "pitch", 5) != 0)
break;
lua.Push(instance.pitch);
return true;
}
}
return false;
});
eulerAnglesClass.SetSetter([] (Nz::LuaInstance& lua, Nz::EulerAnglesd& instance)
{
std::size_t length;
const char* ypr = lua.CheckString(1, &length);
double value = lua.CheckNumber(2);
switch (length)
{
case 1:
{
switch (ypr[0])
{
case 'p':
instance.pitch = value;
return true;
case 'y':
instance.yaw = value;
return true;
case 'r':
instance.roll = value;
return true;
}
break;
}
case 3:
{
if (std::memcmp(ypr, "yaw", 3) != 0)
break;
instance.yaw = value;
return true;
}
case 4:
{
if (std::memcmp(ypr, "roll", 4) != 0)
break;
instance.roll = value;
return true;
}
case 5:
{
if (std::memcmp(ypr, "pitch", 5) != 0)
break;
instance.pitch = value;
return true;
}
}
return false;
});
eulerAnglesClass.Register(instance);
/*********************************** Nz::Quaternion **********************************/
Nz::LuaClass<Nz::Quaterniond> quaternionClass("Quaternion");
quaternionClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Quaterniond*
{
unsigned int argCount = std::min(lua.GetStackTop(), 4U);
switch (argCount)
{
case 0:
return new Nz::Quaterniond(1.0, 0.0, 0.0, 0.0);
case 1:
{
if (lua.IsOfType(1, "EulerAngles"))
return new Nz::Quaterniond(*(*static_cast<Nz::EulerAnglesd**>(lua.ToUserdata(1))));
else if (lua.IsOfType(1, "Quaternion"))
return new Nz::Quaterniond(*(*static_cast<Nz::Quaterniond**>(lua.ToUserdata(1))));
}
case 2:
return new Nz::Quaterniond(lua.CheckNumber(1), *(*static_cast<Nz::Vector3d**>(lua.CheckUserdata(2, "Vector3"))));
case 4:
return new Nz::Quaterniond(lua.CheckNumber(1), lua.CheckNumber(2), lua.CheckNumber(3), lua.CheckNumber(4));
}
lua.Error("No matching overload for Quaternion constructor");
return nullptr;
});
quaternionClass.SetMethod("__tostring", &Nz::Quaterniond::ToString);
quaternionClass.SetGetter([] (Nz::LuaInstance& lua, Nz::Quaterniond& instance)
{
std::size_t length;
const char* wxyz = lua.CheckString(1, &length);
if (length != 1)
return false;
switch (wxyz[0])
{
case 'w':
lua.Push(instance.w);
return true;
case 'x':
lua.Push(instance.x);
return true;
case 'y':
lua.Push(instance.y);
return true;
case 'z':
lua.Push(instance.z);
return true;
}
return false;
});
quaternionClass.SetSetter([] (Nz::LuaInstance& lua, Nz::Quaterniond& instance)
{
std::size_t length;
const char* wxyz = lua.CheckString(1, &length);
if (length != 1)
return false;
double value = lua.CheckNumber(2);
switch (wxyz[0])
{
case 'w':
instance.w = value;
return true;
case 'x':
instance.x = value;
return true;
case 'y':
instance.y = value;
return true;
case 'z':
instance.z = value;
return true;
}
return false;
});
quaternionClass.Register(instance);
/*********************************** Nz::Vector2 **********************************/
Nz::LuaClass<Nz::Vector2d> vector2dClass("Vector2");
@@ -25,7 +262,7 @@ namespace Ndk
if (lua.IsOfType(1, Nz::LuaType_Number))
return new Nz::Vector2d(lua.CheckNumber(1));
else if (lua.IsOfType(1, "Vector2"))
return new Nz::Vector2d(*(*static_cast<Nz::Vector2d*>(lua.ToUserdata(1))));
return new Nz::Vector2d(*(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1))));
break;
}
@@ -42,8 +279,14 @@ namespace Ndk
switch (lua.GetType(1))
{
case Nz::LuaType_Number:
lua.Push(instance[lua.CheckInteger(1)]);
{
long long index = lua.CheckInteger(1);
if (index < 1 || index > 2)
return false;
lua.Push(instance[index - 1]);
return true;
}
case Nz::LuaType_String:
{
@@ -80,7 +323,7 @@ namespace Ndk
if (index < 1 || index > 2)
return false;
instance[index] = lua.CheckNumber(2);
instance[index - 1] = lua.CheckNumber(2);
return true;
}
@@ -128,11 +371,11 @@ namespace Ndk
case 1:
{
if (lua.IsOfType(1, Nz::LuaType_Number))
return new Nz::Vector3d(lua.CheckNumber(1), *static_cast<Nz::Vector2d*>(lua.ToUserdata(1)));
return new Nz::Vector3d(lua.CheckNumber(1), *(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1))));
else if (lua.IsOfType(1, "Vector2"))
return new Nz::Vector3d(*(*static_cast<Nz::Vector2d*>(lua.ToUserdata(1))));
return new Nz::Vector3d(*(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1))));
else if (lua.IsOfType(1, "Vector3"))
return new Nz::Vector3d(*(*static_cast<Nz::Vector3d*>(lua.ToUserdata(1))));
return new Nz::Vector3d(*(*static_cast<Nz::Vector3d**>(lua.ToUserdata(1))));
break;
}
@@ -140,7 +383,7 @@ namespace Ndk
case 2:
{
if (lua.IsOfType(1, Nz::LuaType_Number))
return new Nz::Vector3d(lua.CheckNumber(1), *static_cast<Nz::Vector2d*>(lua.CheckUserdata(1, "Vector2")));
return new Nz::Vector3d(lua.CheckNumber(1), *(*static_cast<Nz::Vector2d**>(lua.CheckUserdata(1, "Vector2"))));
else if (lua.IsOfType(1, "Vector2"))
return new Nz::Vector3d(*(*static_cast<Nz::Vector2d**>(lua.ToUserdata(1))), lua.CheckNumber(2));
@@ -159,8 +402,14 @@ namespace Ndk
switch (lua.GetType(1))
{
case Nz::LuaType_Number:
lua.Push(instance[lua.CheckInteger(1)]);
{
long long index = lua.CheckInteger(1);
if (index < 1 || index > 3)
return false;
lua.Push(instance[index - 1]);
return true;
}
case Nz::LuaType_String:
{
@@ -201,7 +450,7 @@ namespace Ndk
if (index < 1 || index > 3)
return false;
instance[index] = lua.CheckNumber(2);
instance[index - 1] = lua.CheckNumber(2);
return true;
}

313
SDK/src/NDK/LuaAPI_SDK.cpp Normal file
View File

@@ -0,0 +1,313 @@
// Copyright (C) 2016 Jérôme Leclercq, Arnaud Cadot
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <NDK/LuaAPI.hpp>
#include <Nazara/Renderer.hpp>
#include <Nazara/Utility.hpp>
#include <Nazara/Lua/LuaClass.hpp>
#include <NDK/Components.hpp>
#include <NDK/World.hpp>
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 AddComponent(Nz::LuaInstance& lua, EntityHandle& handle, ComponentIndex index)
{
std::vector<int(*)(Nz::LuaInstance&, EntityHandle&)> componentAdder(BaseComponent::GetMaxComponentIndex(), AddNilComponent);
componentAdder[GraphicsComponent::componentIndex] = &AddComponentOfType<GraphicsComponent>;
componentAdder[NodeComponent::componentIndex] = &AddComponentOfType<NodeComponent>;
if (index > componentAdder.size())
{
lua.Error("Invalid component index");
return 0;
}
return componentAdder[index](lua, handle);
}
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;
}
int PushComponent(Nz::LuaInstance& lua, BaseComponent& component)
{
std::vector<int(*)(Nz::LuaInstance&, BaseComponent&)> componentConverter(BaseComponent::GetMaxComponentIndex(), PushNilComponent);
componentConverter[GraphicsComponent::componentIndex] = &PushComponentOfType<GraphicsComponent>;
componentConverter[NodeComponent::componentIndex] = &PushComponentOfType<NodeComponent>;
ComponentIndex index = component.GetIndex();
if (index > componentConverter.size())
{
lua.Error("Invalid component index");
return 0;
}
return componentConverter[index](lua, component);
}
}
void LuaAPI::Register_SDK(Nz::LuaInstance& instance)
{
Nz::LuaClass<GraphicsComponentHandle> graphicsComponent("GraphicsComponent");
graphicsComponent.SetMethod("Attach", &GraphicsComponent::Attach, 0);
graphicsComponent.Register(instance);
Nz::LuaClass<Nz::Node> nodeClass("Node"); //< TODO: Move to Utility
nodeClass.SetMethod("GetBackward", &Nz::Node::GetBackward);
//nodeClass.SetMethod("GetChilds", &Nz::Node::GetChilds);
nodeClass.SetMethod("GetDown", &Nz::Node::GetDown);
nodeClass.SetMethod("GetForward", &Nz::Node::GetForward);
nodeClass.SetMethod("GetInheritPosition", &Nz::Node::GetInheritPosition);
nodeClass.SetMethod("GetInheritRotation", &Nz::Node::GetInheritRotation);
nodeClass.SetMethod("GetInheritScale", &Nz::Node::GetInheritScale);
nodeClass.SetMethod("GetInitialPosition", &Nz::Node::GetInitialPosition);
//nodeClass.SetMethod("GetInitialRotation", &Nz::Node::GetInitialRotation);
nodeClass.SetMethod("GetInitialScale", &Nz::Node::GetInitialScale);
nodeClass.SetMethod("GetLeft", &Nz::Node::GetLeft);
nodeClass.SetMethod("GetNodeType", &Nz::Node::GetNodeType);
//nodeClass.SetMethod("GetParent", &Nz::Node::GetParent);
nodeClass.SetMethod("GetPosition", &Nz::Node::GetPosition, Nz::CoordSys_Global);
nodeClass.SetMethod("GetRight", &Nz::Node::GetRight);
//nodeClass.SetMethod("GetRotation", &Nz::Node::GetRotation, Nz::CoordSys_Global);
nodeClass.SetMethod("GetScale", &Nz::Node::GetScale, Nz::CoordSys_Global);
//nodeClass.SetMethod("GetTransformMatrix", &Nz::Node::GetTransformMatrix);
nodeClass.SetMethod("GetUp", &Nz::Node::GetUp);
nodeClass.SetMethod("HasChilds", &Nz::Node::HasChilds);
nodeClass.SetMethod("GetBackward", &Nz::Node::GetBackward);
nodeClass.SetMethod("GetDown", &Nz::Node::GetDown);
nodeClass.SetMethod("GetForward", &Nz::Node::GetForward);
nodeClass.SetMethod("GetInheritPosition", &Nz::Node::GetInheritPosition);
nodeClass.SetMethod("GetInheritRotation", &Nz::Node::GetInheritRotation);
nodeClass.SetMethod("GetInheritScale", &Nz::Node::GetInheritScale);
nodeClass.SetMethod("GetInitialPosition", &Nz::Node::GetInitialPosition);
nodeClass.SetMethod("GetInitialRotation", &Nz::Node::GetInitialRotation);
nodeClass.SetMethod("GetInitialScale", &Nz::Node::GetInitialScale);
nodeClass.SetMethod("GetLeft", &Nz::Node::GetLeft);
nodeClass.SetMethod("GetNodeType", &Nz::Node::GetNodeType);
nodeClass.SetMethod("GetPosition", &Nz::Node::GetPosition, Nz::CoordSys_Global);
nodeClass.SetMethod("GetRight", &Nz::Node::GetRight);
nodeClass.SetMethod("GetRotation", &Nz::Node::GetRotation, Nz::CoordSys_Global);
nodeClass.SetMethod("GetScale", &Nz::Node::GetScale, Nz::CoordSys_Global);
nodeClass.SetMethod("GetUp", &Nz::Node::GetUp);
nodeClass.SetMethod("SetInitialPosition", (void(Nz::Node::*)(const Nz::Vector3f&)) &Nz::Node::SetInitialPosition);
nodeClass.SetMethod("SetInitialRotation", (void(Nz::Node::*)(const Nz::Quaternionf&)) &Nz::Node::SetInitialRotation);
nodeClass.SetMethod("SetPosition", (void(Nz::Node::*)(const Nz::Vector3f&, Nz::CoordSys)) &Nz::Node::SetPosition, Nz::CoordSys_Local);
nodeClass.SetMethod("SetRotation", (void(Nz::Node::*)(const Nz::Quaternionf&, Nz::CoordSys)) &Nz::Node::SetRotation, Nz::CoordSys_Local);
nodeClass.SetMethod("Move", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int
{
int argIndex = 1;
Nz::Vector3f offset = lua.Check<Nz::Vector3f>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
node.Move(offset, coordSys);
return 0;
});
nodeClass.SetMethod("Rotate", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int
{
int argIndex = 1;
Nz::Quaternionf rotation = lua.Check<Nz::Quaternionf>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
node.Rotate(rotation, coordSys);
return 0;
});
nodeClass.SetMethod("Scale", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int
{
unsigned int argCount = std::min(lua.GetStackTop(), 4U);
int argIndex = 1;
switch (argCount)
{
case 1:
{
if (lua.IsOfType(argIndex, Nz::LuaType_Number))
node.Scale(lua.Check<float>(&argIndex));
else
node.Scale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
case 3:
node.Scale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
lua.Error("No matching overload for method Scale");
return 0;
});
nodeClass.SetMethod("SetScale", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int
{
unsigned int argCount = std::min(lua.GetStackTop(), 4U);
int argIndex = 1;
switch (argCount)
{
case 1:
case 2:
{
if (lua.IsOfType(argIndex, Nz::LuaType_Number))
{
float scale = lua.Check<float>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
node.SetScale(scale, coordSys);
}
else
node.SetScale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
case 3:
case 4:
{
Nz::Vector3f scale = lua.Check<Nz::Vector3f>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
node.SetScale(scale, coordSys);
return 0;
}
}
lua.Error("No matching overload for method SetScale");
return 0;
});
nodeClass.SetMethod("SetInitialScale", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int
{
unsigned int argCount = std::min(lua.GetStackTop(), 4U);
int argIndex = 1;
switch (argCount)
{
case 1:
{
if (lua.IsOfType(argIndex, Nz::LuaType_Number))
node.SetInitialScale(lua.Check<float>(&argIndex));
else
node.SetInitialScale(lua.Check<Nz::Vector2f>(&argIndex));
return 0;
}
case 2:
case 3:
node.SetInitialScale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
lua.Error("No matching overload for method SetInitialScale");
return 0;
});
nodeClass.Register(instance);
Nz::LuaClass<NodeComponentHandle> nodeComponent("NodeComponent");
nodeComponent.Inherit<Nz::Node>(nodeClass, [] (NodeComponentHandle* handle) -> Nz::Node*
{
return handle->GetObject();
});
nodeComponent.Register(instance);
Nz::LuaClass<EntityHandle> entityClass("Entity");
entityClass.SetMethod("Enable", &Entity::Enable);
entityClass.SetMethod("GetId", &Entity::GetId);
entityClass.SetMethod("GetWorld", &Entity::GetWorld);
entityClass.SetMethod("Kill", &Entity::Kill);
entityClass.SetMethod("IsEnabled", &Entity::IsEnabled);
entityClass.SetMethod("IsValid", &Entity::IsValid);
entityClass.SetMethod("RemoveComponent", (void(Entity::*)(ComponentIndex)) &Entity::RemoveComponent);
entityClass.SetMethod("RemoveAllComponents", &Entity::RemoveAllComponents);
entityClass.SetMethod("__tostring", &EntityHandle::ToString);
entityClass.SetMethod("AddComponent", [] (Nz::LuaInstance& lua, EntityHandle& handle) -> int
{
int index = 1;
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
return AddComponent(lua, handle, componentIndex);
});
entityClass.SetMethod("GetComponent", [] (Nz::LuaInstance& lua, EntityHandle& handle) -> int
{
int index = 1;
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
if (!handle->HasComponent(componentIndex))
{
lua.PushNil();
return 1;
}
return PushComponent(lua, handle->GetComponent(componentIndex));
});
entityClass.Register(instance);
Nz::LuaClass<WorldHandle> worldClass("World");
worldClass.SetMethod("CreateEntity", &World::CreateEntity);
worldClass.SetMethod("CreateEntities", &World::CreateEntities);
worldClass.SetMethod("Clear", &World::Clear);
worldClass.Register(instance);
instance.PushTable(0, 2);
{
instance.PushInteger(GraphicsComponent::componentIndex);
instance.SetField("Graphics");
instance.PushInteger(NodeComponent::componentIndex);
instance.SetField("Node");
}
instance.SetGlobal("ComponentType");
}
}