Vector2f.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "Vector2f.h"
  29. #include <Rocket/Core/Vector2.h>
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Lua {
  33. int Vector2fnew(lua_State* L)
  34. {
  35. float x = (float)luaL_checknumber(L,1);
  36. float y = (float)luaL_checknumber(L,2);
  37. Vector2f* vect = new Vector2f(x,y);
  38. LuaType<Vector2f>::push(L,vect,true); //true means it will be deleted when it is garbage collected
  39. return 1;
  40. }
  41. int Vector2f__mul(lua_State* L)
  42. {
  43. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  44. LUACHECKOBJ(lhs);
  45. float rhs = (float)luaL_checknumber(L,2);
  46. Vector2f* res = new Vector2f(*lhs);
  47. (*res) *= rhs;
  48. LuaType<Vector2f>::push(L,res,true);
  49. return 1;
  50. }
  51. int Vector2f__div(lua_State* L)
  52. {
  53. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  54. LUACHECKOBJ(lhs);
  55. float rhs = (float)luaL_checknumber(L,2);
  56. Vector2f* res = new Vector2f(*lhs);
  57. (*res) /= rhs;
  58. LuaType<Vector2f>::push(L,res,true);
  59. return 1;
  60. }
  61. int Vector2f__add(lua_State* L)
  62. {
  63. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  64. LUACHECKOBJ(lhs);
  65. Vector2f* rhs = LuaType<Vector2f>::check(L,2);
  66. LUACHECKOBJ(rhs);
  67. Vector2f* res = new Vector2f(*lhs);
  68. (*res) += (*rhs);
  69. LuaType<Vector2f>::push(L,res,true);
  70. return 1;
  71. }
  72. int Vector2f__sub(lua_State* L)
  73. {
  74. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  75. LUACHECKOBJ(lhs);
  76. Vector2f* rhs = LuaType<Vector2f>::check(L,2);
  77. LUACHECKOBJ(rhs);
  78. Vector2f* res = new Vector2f(*lhs);
  79. (*res) -= (*rhs);
  80. LuaType<Vector2f>::push(L,res,true);
  81. return 1;
  82. }
  83. int Vector2f__eq(lua_State* L)
  84. {
  85. Vector2f* lhs = LuaType<Vector2f>::check(L,1);
  86. LUACHECKOBJ(lhs);
  87. Vector2f* rhs = LuaType<Vector2f>::check(L,2);
  88. LUACHECKOBJ(rhs);
  89. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  90. return 1;
  91. }
  92. int Vector2fDotProduct(lua_State* L, Vector2f* obj)
  93. {
  94. Vector2f* rhs = LuaType<Vector2f>::check(L,1);
  95. LUACHECKOBJ(rhs);
  96. float res = obj->DotProduct(*rhs);
  97. lua_pushnumber(L,res);
  98. return 1;
  99. }
  100. int Vector2fNormalise(lua_State* L, Vector2f* obj)
  101. {
  102. Vector2f* res = new Vector2f();
  103. (*res) = obj->Normalise();
  104. LuaType<Vector2f>::push(L,res,true);
  105. return 1;
  106. }
  107. int Vector2fRotate(lua_State* L, Vector2f* obj)
  108. {
  109. float num = (float)luaL_checknumber(L,1);
  110. Vector2f* res = new Vector2f();
  111. (*res) = obj->Rotate(num);
  112. LuaType<Vector2f>::push(L,res,true);
  113. return 1;
  114. }
  115. int Vector2fGetAttrx(lua_State*L)
  116. {
  117. Vector2f* self = LuaType<Vector2f>::check(L,1);
  118. LUACHECKOBJ(self);
  119. lua_pushnumber(L,self->x);
  120. return 1;
  121. }
  122. int Vector2fGetAttry(lua_State*L)
  123. {
  124. Vector2f* self = LuaType<Vector2f>::check(L,1);
  125. LUACHECKOBJ(self);
  126. lua_pushnumber(L,self->y);
  127. return 1;
  128. }
  129. int Vector2fGetAttrmagnitude(lua_State*L)
  130. {
  131. Vector2f* self = LuaType<Vector2f>::check(L,1);
  132. LUACHECKOBJ(self);
  133. lua_pushnumber(L,self->Magnitude());
  134. return 1;
  135. }
  136. int Vector2fSetAttrx(lua_State*L)
  137. {
  138. Vector2f* self = LuaType<Vector2f>::check(L,1);
  139. LUACHECKOBJ(self);
  140. float value = (float)luaL_checknumber(L,2);
  141. self->x = value;
  142. return 0;
  143. }
  144. int Vector2fSetAttry(lua_State*L)
  145. {
  146. Vector2f* self = LuaType<Vector2f>::check(L,1);
  147. LUACHECKOBJ(self);
  148. float value = (float)luaL_checknumber(L,2);
  149. self->y = value;
  150. return 0;
  151. }
  152. RegType<Vector2f> Vector2fMethods[] =
  153. {
  154. LUAMETHOD(Vector2f,DotProduct)
  155. LUAMETHOD(Vector2f,Normalise)
  156. LUAMETHOD(Vector2f,Rotate)
  157. { NULL, NULL },
  158. };
  159. luaL_reg Vector2fGetters[]=
  160. {
  161. LUAGETTER(Vector2f,x)
  162. LUAGETTER(Vector2f,y)
  163. LUAGETTER(Vector2f,magnitude)
  164. { NULL, NULL },
  165. };
  166. luaL_reg Vector2fSetters[]=
  167. {
  168. LUASETTER(Vector2f,x)
  169. LUASETTER(Vector2f,y)
  170. { NULL, NULL },
  171. };
  172. }
  173. }
  174. }