Vector2i.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Vector2i.h"
  29. namespace Rml {
  30. namespace Lua {
  31. template <>
  32. void ExtraInit<Vector2i>(lua_State* L, int metatable_index)
  33. {
  34. lua_pushcfunction(L, Vector2inew);
  35. lua_setfield(L, metatable_index - 1, "new");
  36. lua_pushcfunction(L, Vector2i__mul);
  37. lua_setfield(L, metatable_index, "__mul");
  38. lua_pushcfunction(L, Vector2i__div);
  39. lua_setfield(L, metatable_index, "__div");
  40. lua_pushcfunction(L, Vector2i__add);
  41. lua_setfield(L, metatable_index, "__add");
  42. lua_pushcfunction(L, Vector2i__sub);
  43. lua_setfield(L, metatable_index, "__sub");
  44. lua_pushcfunction(L, Vector2i__eq);
  45. lua_setfield(L, metatable_index, "__eq");
  46. // stack is in the same state as it was before it entered this function
  47. return;
  48. }
  49. int Vector2inew(lua_State* L)
  50. {
  51. int x = (int)luaL_checkinteger(L, 1);
  52. int y = (int)luaL_checkinteger(L, 2);
  53. Vector2i* vect = new Vector2i(x, y);
  54. LuaType<Vector2i>::push(L, vect, true); // true means it will be deleted when it is garbage collected
  55. return 1;
  56. }
  57. int Vector2i__mul(lua_State* L)
  58. {
  59. Vector2i* lhs = LuaType<Vector2i>::check(L, 1);
  60. RMLUI_CHECK_OBJ(lhs);
  61. int rhs = (int)luaL_checkinteger(L, 2);
  62. Vector2i* res = new Vector2i(0, 0);
  63. (*res) = (*lhs) * rhs;
  64. LuaType<Vector2i>::push(L, res, true);
  65. return 1;
  66. }
  67. int Vector2i__div(lua_State* L)
  68. {
  69. Vector2i* lhs = LuaType<Vector2i>::check(L, 1);
  70. RMLUI_CHECK_OBJ(lhs);
  71. int rhs = (int)luaL_checkinteger(L, 2);
  72. Vector2i* res = new Vector2i(0, 0);
  73. (*res) = (*lhs) / rhs;
  74. LuaType<Vector2i>::push(L, res, true);
  75. return 1;
  76. }
  77. int Vector2i__add(lua_State* L)
  78. {
  79. Vector2i* lhs = LuaType<Vector2i>::check(L, 1);
  80. RMLUI_CHECK_OBJ(lhs);
  81. Vector2i* rhs = LuaType<Vector2i>::check(L, 2);
  82. RMLUI_CHECK_OBJ(rhs);
  83. Vector2i* res = new Vector2i(0, 0);
  84. (*res) = (*lhs) + (*rhs);
  85. LuaType<Vector2i>::push(L, res, true);
  86. return 1;
  87. }
  88. int Vector2i__sub(lua_State* L)
  89. {
  90. Vector2i* lhs = LuaType<Vector2i>::check(L, 1);
  91. RMLUI_CHECK_OBJ(lhs);
  92. Vector2i* rhs = LuaType<Vector2i>::check(L, 2);
  93. RMLUI_CHECK_OBJ(rhs);
  94. Vector2i* res = new Vector2i(0, 0);
  95. (*res) = (*lhs) - (*rhs);
  96. LuaType<Vector2i>::push(L, res, true);
  97. return 1;
  98. }
  99. int Vector2i__eq(lua_State* L)
  100. {
  101. Vector2i* lhs = LuaType<Vector2i>::check(L, 1);
  102. RMLUI_CHECK_OBJ(lhs);
  103. Vector2i* rhs = LuaType<Vector2i>::check(L, 2);
  104. RMLUI_CHECK_OBJ(rhs);
  105. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  106. return 1;
  107. }
  108. int Vector2iGetAttrx(lua_State* L)
  109. {
  110. Vector2i* self = LuaType<Vector2i>::check(L, 1);
  111. RMLUI_CHECK_OBJ(self);
  112. lua_pushinteger(L, self->x);
  113. return 1;
  114. }
  115. int Vector2iGetAttry(lua_State* L)
  116. {
  117. Vector2i* self = LuaType<Vector2i>::check(L, 1);
  118. RMLUI_CHECK_OBJ(self);
  119. lua_pushinteger(L, self->y);
  120. return 1;
  121. }
  122. int Vector2iGetAttrmagnitude(lua_State* L)
  123. {
  124. Vector2i* self = LuaType<Vector2i>::check(L, 1);
  125. RMLUI_CHECK_OBJ(self);
  126. lua_pushnumber(L, self->Magnitude());
  127. return 1;
  128. }
  129. int Vector2iSetAttrx(lua_State* L)
  130. {
  131. Vector2i* self = LuaType<Vector2i>::check(L, 1);
  132. RMLUI_CHECK_OBJ(self);
  133. int value = (int)luaL_checkinteger(L, 2);
  134. self->x = value;
  135. return 0;
  136. }
  137. int Vector2iSetAttry(lua_State* L)
  138. {
  139. Vector2i* self = LuaType<Vector2i>::check(L, 1);
  140. RMLUI_CHECK_OBJ(self);
  141. int value = (int)luaL_checkinteger(L, 2);
  142. self->y = value;
  143. return 0;
  144. }
  145. RegType<Vector2i> Vector2iMethods[] = {
  146. {nullptr, nullptr},
  147. };
  148. luaL_Reg Vector2iGetters[] = {
  149. RMLUI_LUAGETTER(Vector2i, x),
  150. RMLUI_LUAGETTER(Vector2i, y),
  151. RMLUI_LUAGETTER(Vector2i, magnitude),
  152. {nullptr, nullptr},
  153. };
  154. luaL_Reg Vector2iSetters[] = {
  155. RMLUI_LUASETTER(Vector2i, x),
  156. RMLUI_LUASETTER(Vector2i, y),
  157. {nullptr, nullptr},
  158. };
  159. RMLUI_LUATYPE_DEFINE(Vector2i)
  160. } // namespace Lua
  161. } // namespace Rml