Vector2 Lua Binding

Added Nz::Vector2 Lua binding


Former-commit-id: 727ad086fe62ce131ffdbc24ce17d020cd0fede7
This commit is contained in:
GigAnon 2016-01-20 18:34:31 +01:00
parent 83b365f139
commit 95f6977c1f
2 changed files with 117 additions and 8 deletions

View File

@ -41,13 +41,14 @@ namespace Ndk
abstractHashClass.SetMethod("End",
[](Nz::LuaInstance& lua, Nz::AbstractHash& hash) -> int
{
Nz::ByteArray data(hash.End()); // Exceptions? What's the expected behavior of that method?
Nz::ByteArray data(hash.End());
lua.PushString(data.ToString());
return 1;
});
abstractHashClass.SetMethod("GetDigestLength", &Nz::AbstractHash::GetDigestLength);
abstractHashClass.SetMethod("GetHashName",
[](Nz::LuaInstance& lua, Nz::AbstractHash& hash) -> int
{

View File

@ -8,10 +8,118 @@ namespace Ndk
{
void LuaAPI::Register_Math(Nz::LuaInstance& instance)
{
/*********************************** Nz::Vector3 **********************************/
Nz::LuaClass<Nz::Vector3d> vectorClass("Vector3");
/*********************************** Nz::Vector2 **********************************/
Nz::LuaClass<Nz::Vector2d> vector2dClass("Vector2");
vectorClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Vector3d* {
vector2dClass.SetConstructor(
[](Nz::LuaInstance& lua) -> Nz::Vector2d*
{
unsigned int argCount = std::min(lua.GetStackTop(), 2U);
switch (argCount)
{
case 0:
case 2:
return new Nz::Vector2d(lua.CheckNumber(1, 0.0), lua.CheckNumber(2, 0.0));
case 1:
{
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))));
break;
}
}
lua.Error("No matching overload for Vector2 constructor");
return nullptr;
});
vector2dClass.SetMethod("__tostring", &Nz::Vector2d::ToString);
vector2dClass.SetGetter(
[](Nz::LuaInstance& lua, Nz::Vector2d& instance)
{
switch (lua.GetType(1))
{
case Nz::LuaType_Number:
lua.Push(instance[lua.CheckInteger(1)]);
return true;
case Nz::LuaType_String:
{
std::size_t length;
const char* xy = lua.CheckString(1, &length);
if (length != 1)
break;
switch (xy[0])
{
case 'x':
lua.Push(instance.x);
return true;
case 'y':
lua.Push(instance.y);
return true;
}
break;
}
}
return false;
});
vector2dClass.SetSetter(
[](Nz::LuaInstance& lua, Nz::Vector2d& instance)
{
switch (lua.GetType(1))
{
case Nz::LuaType_Number:
{
long long index = lua.CheckInteger(1);
if (index < 1 || index > 2)
return false;
instance[index] = lua.CheckNumber(2);
return true;
}
case Nz::LuaType_String:
{
std::size_t length;
const char* xy = lua.CheckString(1, &length);
if (length != 1)
break;
double value = lua.CheckNumber(2);
switch (xy[0])
{
case 'x':
instance.x = value;
return true;
case 'y':
instance.y = value;
return true;
}
break;
}
}
return false;
});
vector2dClass.Register(instance);
/*********************************** Nz::Vector3 **********************************/
Nz::LuaClass<Nz::Vector3d> vector3dClass("Vector3");
vector3dClass.SetConstructor([] (Nz::LuaInstance& lua) -> Nz::Vector3d* {
unsigned int argCount = std::min(lua.GetStackTop(), 3U);
switch (argCount)
{
@ -46,9 +154,9 @@ namespace Ndk
return nullptr;
});
vectorClass.SetMethod("__tostring", &Nz::Vector3d::ToString);
vector3dClass.SetMethod("__tostring", &Nz::Vector3d::ToString);
vectorClass.SetGetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance)
vector3dClass.SetGetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance)
{
switch (lua.GetType(1))
{
@ -85,7 +193,7 @@ namespace Ndk
return false;
});
vectorClass.SetSetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance)
vector3dClass.SetSetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance)
{
switch (lua.GetType(1))
{
@ -130,6 +238,6 @@ namespace Ndk
return false;
});
vectorClass.Register(instance);
vector3dClass.Register(instance);
}
}