Vector2i.cpp 3.5 KB

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