Просмотр исходного кода

add getter/setter to LuaVector3Box

mikymod 12 лет назад
Родитель
Сommit
93068b5fe3
1 измененных файлов с 58 добавлено и 37 удалено
  1. 58 37
      engine/lua/LuaVector3Box.cpp

+ 58 - 37
engine/lua/LuaVector3Box.cpp

@@ -27,10 +27,64 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "LuaEnvironment.h"
 #include "LuaStack.h"
 #include "Vector3.h"
+#include "StringUtils.h"
 
 namespace crown
 {
 
+//-----------------------------------------------------------------------------
+CE_EXPORT int vector3box_get_value(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Vector3& v = stack.get_vector3box(1);
+	const char* s = stack.get_string(2);
+
+	if (string::strcmp(s, "x") == 0)
+	{
+		stack.push_float(v.x);
+		return 1;
+	}
+	else if (string::strcmp(s, "y") == 0)
+	{
+		stack.push_float(v.y);
+		return 1;
+	}
+	else if (string::strcmp(s, "z") == 0)
+	{
+		stack.push_float(v.z);
+		return 1;
+	}
+
+	// Never happens
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int vector3box_set_value(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Vector3& v = stack.get_vector3box(1);
+	const char* s = stack.get_string(2);
+	float value = stack.get_float(3);
+
+	if (string::strcmp(s, "x") == 0)
+	{
+		v.x = value;
+	}
+	else if (string::strcmp(s, "y") == 0)
+	{
+		v.y = value;
+	}
+	else if (string::strcmp(s, "z") == 0)
+	{
+		v.z = value;
+	}
+
+	return 0;
+}
+
 //-----------------------------------------------------------------------------
 CE_EXPORT int vector3box(lua_State* L)
 {
@@ -38,6 +92,10 @@ CE_EXPORT int vector3box(lua_State* L)
 
 	Vector3& v = stack.push_vector3box();
 
+	// Associates a metatable to userdata
+	stack.get_global_metatable("Vector3Box_i_mt");
+	stack.set_metatable();
+
 	if (stack.num_args() == 2)
 	{
 		Vector3 tv = stack.get_vector3(1);
@@ -86,49 +144,12 @@ CE_EXPORT int vector3box_unbox(lua_State* L)
 	return 1;
 }
 
-// //-----------------------------------------------------------------------------
-// CE_EXPORT int vector3box_x(lua_State* L)
-// {
-// 	LuaStack stack(L);
-
-// 	Vector3* v = (Vector3*) stack.get_vector3box(1);
-
-// 	stack.push_float(v->x);
-// 	return 1;
-// }
-
-// //-----------------------------------------------------------------------------
-// CE_EXPORT int vector3box_y(lua_State* L)
-// {
-// 	LuaStack stack(L);
-
-// 	Vector3* v = (Vector3*) stack.get_vector3box(1);
-
-// 	stack.push_float(v->y);
-// 	return 1;
-// }
-
-// //-----------------------------------------------------------------------------
-// CE_EXPORT int vector3box_z(lua_State* L)
-// {
-// 	LuaStack stack(L);
-
-// 	Vector3* v = (Vector3*) stack.get_vector3box(1);
-
-// 	stack.push_float(v->z);
-// 	return 1;
-// }
-
-
 //-----------------------------------------------------------------------------
 void load_vector3box(LuaEnvironment& env)
 {
 	env.load_module_function("Vector3Box", "new", vector3box);
 	env.load_module_function("Vector3Box", "store", vector3box_store);
 	env.load_module_function("Vector3Box", "unbox", vector3box_unbox);
-	// env.load_module_function("Vector3Box", "x", vector3box_x);
-	// env.load_module_function("Vector3Box", "y", vector3box_y);
-	// env.load_module_function("Vector3Box", "z", vector3box_z);
 }
 
 } // namespace crown