Vector2f.cpp 5.6 KB

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