Vector2f.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "Vector2f.h"
  30. #include <RmlUi/Core/Vector2.h>
  31. namespace Rml {
  32. namespace Core {
  33. namespace Lua {
  34. template<> void ExtraInit<Vector2f>(lua_State* L, int metatable_index)
  35. {
  36. lua_pushcfunction(L,Vector2fnew);
  37. lua_setfield(L,metatable_index-1,"new");
  38. lua_pushcfunction(L,Vector2f__mul);
  39. lua_setfield(L,metatable_index,"__mul");
  40. lua_pushcfunction(L,Vector2f__div);
  41. lua_setfield(L,metatable_index,"__div");
  42. lua_pushcfunction(L,Vector2f__add);
  43. lua_setfield(L,metatable_index,"__add");
  44. lua_pushcfunction(L,Vector2f__sub);
  45. lua_setfield(L,metatable_index,"__sub");
  46. lua_pushcfunction(L,Vector2f__eq);
  47. lua_setfield(L,metatable_index,"__eq");
  48. //stack is in the same state as it was before it entered this function
  49. return;
  50. }
  51. int Vector2fnew(lua_State* L)
  52. {
  53. float x = (float)luaL_checknumber(L,1);
  54. float y = (float)luaL_checknumber(L,2);
  55. Vector2f* vect = new Vector2f(x,y);
  56. LuaType<Vector2f>::push(L,vect,true); //true means it will be deleted when it is garbage collected
  57. return 1;
  58. }
  59. int Vector2f__mul(lua_State* L)
  60. {
  61. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  62. LUACHECKOBJ(lhs);
  63. float rhs = (float)luaL_checknumber(L,2);
  64. Vector2f* res = new Vector2f(0.f,0.f);
  65. (*res) = (*lhs) * rhs;
  66. LuaType<Vector2f>::push(L,res,true);
  67. return 1;
  68. }
  69. int Vector2f__div(lua_State* L)
  70. {
  71. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  72. LUACHECKOBJ(lhs);
  73. float rhs = (float)luaL_checknumber(L,2);
  74. Vector2f* res = new Vector2f(0.f,0.f);
  75. (*res) = (*lhs) / rhs;
  76. LuaType<Vector2f>::push(L,res,true);
  77. return 1;
  78. }
  79. int Vector2f__add(lua_State* L)
  80. {
  81. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  82. LUACHECKOBJ(lhs);
  83. Vector2f* rhs = LuaType<Vector2f>::check(L,2);
  84. LUACHECKOBJ(rhs);
  85. Vector2f* res = new Vector2f(0.f,0.f);
  86. (*res) = (*lhs) + (*rhs);
  87. LuaType<Vector2f>::push(L,res,true);
  88. return 1;
  89. }
  90. int Vector2f__sub(lua_State* L)
  91. {
  92. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  93. LUACHECKOBJ(lhs);
  94. Vector2f* rhs = LuaType<Vector2f>::check(L,2);
  95. LUACHECKOBJ(rhs);
  96. Vector2f* res = new Vector2f(0.f,0.f);
  97. (*res) = (*lhs) - (*rhs);
  98. LuaType<Vector2f>::push(L,res,true);
  99. return 1;
  100. }
  101. int Vector2f__eq(lua_State* L)
  102. {
  103. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  104. LUACHECKOBJ(lhs);
  105. Vector2f* rhs = LuaType<Vector2f>::check(L,2);
  106. LUACHECKOBJ(rhs);
  107. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  108. return 1;
  109. }
  110. int Vector2fDotProduct(lua_State* L, Vector2f* obj)
  111. {
  112. Vector2f* rhs = LuaType<Vector2f>::check(L,1);
  113. LUACHECKOBJ(rhs);
  114. float res = obj->DotProduct(*rhs);
  115. lua_pushnumber(L,res);
  116. return 1;
  117. }
  118. int Vector2fNormalise(lua_State* L, Vector2f* obj)
  119. {
  120. Vector2f* res = new Vector2f();
  121. (*res) = obj->Normalise();
  122. LuaType<Vector2f>::push(L,res,true);
  123. return 1;
  124. }
  125. int Vector2fRotate(lua_State* L, Vector2f* obj)
  126. {
  127. float num = (float)luaL_checknumber(L,1);
  128. Vector2f* res = new Vector2f();
  129. (*res) = obj->Rotate(num);
  130. LuaType<Vector2f>::push(L,res,true);
  131. return 1;
  132. }
  133. int Vector2fGetAttrx(lua_State*L)
  134. {
  135. Vector2f* self = LuaType<Vector2f>::check(L,1);
  136. LUACHECKOBJ(self);
  137. lua_pushnumber(L,self->x);
  138. return 1;
  139. }
  140. int Vector2fGetAttry(lua_State*L)
  141. {
  142. Vector2f* self = LuaType<Vector2f>::check(L,1);
  143. LUACHECKOBJ(self);
  144. lua_pushnumber(L,self->y);
  145. return 1;
  146. }
  147. int Vector2fGetAttrmagnitude(lua_State*L)
  148. {
  149. Vector2f* self = LuaType<Vector2f>::check(L,1);
  150. LUACHECKOBJ(self);
  151. lua_pushnumber(L,self->Magnitude());
  152. return 1;
  153. }
  154. int Vector2fSetAttrx(lua_State*L)
  155. {
  156. Vector2f* self = LuaType<Vector2f>::check(L,1);
  157. LUACHECKOBJ(self);
  158. float value = (float)luaL_checknumber(L,2);
  159. self->x = value;
  160. return 0;
  161. }
  162. int Vector2fSetAttry(lua_State*L)
  163. {
  164. Vector2f* self = LuaType<Vector2f>::check(L,1);
  165. LUACHECKOBJ(self);
  166. float value = (float)luaL_checknumber(L,2);
  167. self->y = value;
  168. return 0;
  169. }
  170. RegType<Vector2f> Vector2fMethods[] =
  171. {
  172. LUAMETHOD(Vector2f,DotProduct)
  173. LUAMETHOD(Vector2f,Normalise)
  174. LUAMETHOD(Vector2f,Rotate)
  175. { nullptr, nullptr },
  176. };
  177. luaL_Reg Vector2fGetters[]=
  178. {
  179. LUAGETTER(Vector2f,x)
  180. LUAGETTER(Vector2f,y)
  181. LUAGETTER(Vector2f,magnitude)
  182. { nullptr, nullptr },
  183. };
  184. luaL_Reg Vector2fSetters[]=
  185. {
  186. LUASETTER(Vector2f,x)
  187. LUASETTER(Vector2f,y)
  188. { nullptr, nullptr },
  189. };
  190. LUACORETYPEDEFINE(Vector2f)
  191. }
  192. }
  193. }