Vector2i.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 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 "precompiled.h"
  29. #include "Vector2i.h"
  30. namespace Rml {
  31. namespace Core {
  32. namespace Lua {
  33. template<> void ExtraInit<Vector2i>(lua_State* L, int metatable_index)
  34. {
  35. lua_pushcfunction(L,Vector2inew);
  36. lua_setfield(L,metatable_index-1,"new");
  37. lua_pushcfunction(L,Vector2i__mul);
  38. lua_setfield(L,metatable_index,"__mul");
  39. lua_pushcfunction(L,Vector2i__div);
  40. lua_setfield(L,metatable_index,"__div");
  41. lua_pushcfunction(L,Vector2i__add);
  42. lua_setfield(L,metatable_index,"__add");
  43. lua_pushcfunction(L,Vector2i__sub);
  44. lua_setfield(L,metatable_index,"__sub");
  45. lua_pushcfunction(L,Vector2i__eq);
  46. lua_setfield(L,metatable_index,"__eq");
  47. //stack is in the same state as it was before it entered this function
  48. return;
  49. }
  50. int Vector2inew(lua_State* L)
  51. {
  52. int x = luaL_checkinteger(L,1);
  53. int y = luaL_checkinteger(L,2);
  54. Vector2i* vect = new Vector2i(x,y);
  55. LuaType<Vector2i>::push(L,vect,true); //true means it will be deleted when it is garbage collected
  56. return 1;
  57. }
  58. int Vector2i__mul(lua_State* L)
  59. {
  60. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  61. LUACHECKOBJ(lhs);
  62. int rhs = luaL_checkinteger(L,2);
  63. Vector2i* res = new Vector2i(0,0);
  64. (*res) = (*lhs) * rhs;
  65. LuaType<Vector2i>::push(L,res,true);
  66. return 1;
  67. }
  68. int Vector2i__div(lua_State* L)
  69. {
  70. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  71. LUACHECKOBJ(lhs);
  72. int rhs = luaL_checkinteger(L,2);
  73. Vector2i* res = new Vector2i(0,0);
  74. (*res) = (*lhs) / rhs;
  75. LuaType<Vector2i>::push(L,res,true);
  76. return 1;
  77. }
  78. int Vector2i__add(lua_State* L)
  79. {
  80. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  81. LUACHECKOBJ(lhs);
  82. Vector2i* rhs = LuaType<Vector2i>::check(L,2);
  83. LUACHECKOBJ(rhs);
  84. Vector2i* res = new Vector2i(0,0);
  85. (*res) = (*lhs) + (*rhs);
  86. LuaType<Vector2i>::push(L,res,true);
  87. return 1;
  88. }
  89. int Vector2i__sub(lua_State* L)
  90. {
  91. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  92. LUACHECKOBJ(lhs);
  93. Vector2i* rhs = LuaType<Vector2i>::check(L,2);
  94. LUACHECKOBJ(rhs);
  95. Vector2i* res = new Vector2i(0,0);
  96. (*res) = (*lhs) - (*rhs);
  97. LuaType<Vector2i>::push(L,res,true);
  98. return 1;
  99. }
  100. int Vector2i__eq(lua_State* L)
  101. {
  102. Vector2i* lhs = LuaType<Vector2i>::check(L,1);
  103. LUACHECKOBJ(lhs);
  104. Vector2i* rhs = LuaType<Vector2i>::check(L,2);
  105. LUACHECKOBJ(rhs);
  106. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  107. return 1;
  108. }
  109. int Vector2iGetAttrx(lua_State*L)
  110. {
  111. Vector2i* self = LuaType<Vector2i>::check(L,1);
  112. LUACHECKOBJ(self);
  113. lua_pushinteger(L,self->x);
  114. return 1;
  115. }
  116. int Vector2iGetAttry(lua_State*L)
  117. {
  118. Vector2i* self = LuaType<Vector2i>::check(L,1);
  119. LUACHECKOBJ(self);
  120. lua_pushinteger(L,self->y);
  121. return 1;
  122. }
  123. int Vector2iGetAttrmagnitude(lua_State*L)
  124. {
  125. Vector2i* self = LuaType<Vector2i>::check(L,1);
  126. LUACHECKOBJ(self);
  127. lua_pushnumber(L,self->Magnitude());
  128. return 1;
  129. }
  130. int Vector2iSetAttrx(lua_State*L)
  131. {
  132. Vector2i* self = LuaType<Vector2i>::check(L,1);
  133. LUACHECKOBJ(self);
  134. int value = luaL_checkinteger(L,2);
  135. self->x = value;
  136. return 0;
  137. }
  138. int Vector2iSetAttry(lua_State*L)
  139. {
  140. Vector2i* self = LuaType<Vector2i>::check(L,1);
  141. LUACHECKOBJ(self);
  142. int value = luaL_checkinteger(L,2);
  143. self->y = value;
  144. return 0;
  145. }
  146. RegType<Vector2i> Vector2iMethods[] =
  147. {
  148. { nullptr, nullptr },
  149. };
  150. luaL_Reg Vector2iGetters[]=
  151. {
  152. LUAGETTER(Vector2i,x)
  153. LUAGETTER(Vector2i,y)
  154. LUAGETTER(Vector2i,magnitude)
  155. { nullptr, nullptr },
  156. };
  157. luaL_Reg Vector2iSetters[]=
  158. {
  159. LUASETTER(Vector2i,x)
  160. LUASETTER(Vector2i,y)
  161. { nullptr, nullptr },
  162. };
  163. LUACORETYPEDEFINE(Vector2i)
  164. }
  165. }
  166. }