Vector2i.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "precompiled.h"
  2. #include "Vector2i.h"
  3. namespace Rocket {
  4. namespace Core {
  5. namespace Lua {
  6. template<>
  7. void LuaType<Vector2i>::extra_init(lua_State* L, int metatable_index)
  8. {
  9. lua_pushcfunction(L,Vector2i__call);
  10. lua_setfield(L,metatable_index,"__call");
  11. lua_pushcfunction(L,Vector2i__mul);
  12. lua_setfield(L,metatable_index,"__mul");
  13. lua_pushcfunction(L,Vector2i__div);
  14. lua_setfield(L,metatable_index,"__div");
  15. lua_pushcfunction(L,Vector2i__add);
  16. lua_setfield(L,metatable_index,"__add");
  17. lua_pushcfunction(L,Vector2i__sub);
  18. lua_setfield(L,metatable_index,"__sub");
  19. lua_pushcfunction(L,Vector2i__eq);
  20. lua_setfield(L,metatable_index,"__eq");
  21. //stack is in the same state as it was before it entered this function
  22. return;
  23. }
  24. int Vector2i__call(lua_State* L)
  25. {
  26. int x = luaL_checkint(L,1);
  27. int y = luaL_checkint(L,2);
  28. Vector2i* vect = new Vector2i(x,y);
  29. LuaType<Vector2i>::push(L,vect,true); //true means it will be deleted when it is garbage collected
  30. return 1;
  31. }
  32. int Vector2i__mul(lua_State* L)
  33. {
  34. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  35. int rhs = luaL_checkint(L,2);
  36. Vector2i* res = new Vector2i(*lhs);
  37. (*res) *= rhs;
  38. LuaType<Vector2i>::push(L,res,true);
  39. return 1;
  40. }
  41. int Vector2i__div(lua_State* L)
  42. {
  43. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  44. int rhs = luaL_checkint(L,2);
  45. Vector2i* res = new Vector2i(*lhs);
  46. (*res) /= rhs;
  47. LuaType<Vector2i>::push(L,res,true);
  48. return 1;
  49. }
  50. int Vector2i__add(lua_State* L)
  51. {
  52. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  53. Vector2i* rhs = LuaType<Vector2i>::check(L,1);
  54. Vector2i* res = new Vector2i(*lhs);
  55. (*res) += (*rhs);
  56. LuaType<Vector2i>::push(L,res,true);
  57. return 1;
  58. }
  59. int Vector2i__sub(lua_State* L)
  60. {
  61. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  62. Vector2i* rhs = LuaType<Vector2i>::check(L,1);
  63. Vector2i* res = new Vector2i(*lhs);
  64. (*res) -= (*rhs);
  65. LuaType<Vector2i>::push(L,res,true);
  66. return 1;
  67. }
  68. int Vector2i__eq(lua_State* L)
  69. {
  70. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  71. Vector2i* rhs = LuaType<Vector2i>::check(L,1);
  72. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  73. return 1;
  74. }
  75. int Vector2iGetAttrx(lua_State*L)
  76. {
  77. Vector2i* self = LuaType<Vector2i>::check(L,1);
  78. lua_pushinteger(L,self->x);
  79. return 1;
  80. }
  81. int Vector2iGetAttry(lua_State*L)
  82. {
  83. Vector2i* self = LuaType<Vector2i>::check(L,1);
  84. lua_pushinteger(L,self->y);
  85. return 1;
  86. }
  87. int Vector2iGetAttrmagnitude(lua_State*L)
  88. {
  89. Vector2i* self = LuaType<Vector2i>::check(L,1);
  90. lua_pushnumber(L,self->Magnitude());
  91. return 1;
  92. }
  93. int Vector2iSetAttrx(lua_State*L)
  94. {
  95. Vector2i* self = LuaType<Vector2i>::check(L,1);
  96. int value = luaL_checkint(L,2);
  97. self->x = value;
  98. return 0;
  99. }
  100. int Vector2iSetAttry(lua_State*L)
  101. {
  102. Vector2i* self = LuaType<Vector2i>::check(L,1);
  103. int value = luaL_checkint(L,2);
  104. self->y = value;
  105. return 0;
  106. }
  107. RegType<Vector2i> Vector2iMethods[] =
  108. {
  109. { NULL, NULL },
  110. };
  111. luaL_reg Vector2iGetters[]=
  112. {
  113. LUAGETTER(Vector2i,x)
  114. LUAGETTER(Vector2i,y)
  115. LUAGETTER(Vector2i,magnitude)
  116. { NULL, NULL },
  117. };
  118. luaL_reg Vector2iSetters[]=
  119. {
  120. LUASETTER(Vector2i,x)
  121. LUASETTER(Vector2i,y)
  122. { NULL, NULL },
  123. };
  124. template<> const char* GetTClassName<Vector2i>() { return "Vector2i"; }
  125. template<> RegType<Vector2i>* GetMethodTable<Vector2i>() { return Vector2iMethods; }
  126. template<> luaL_reg* GetAttrTable<Vector2i>() { return Vector2iGetters; }
  127. template<> luaL_reg* SetAttrTable<Vector2i>() { return Vector2iSetters; }
  128. }
  129. }
  130. }