Vector2f.cpp 5.8 KB

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