tolua++urho3d.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Ptr.h"
  24. #include "tolua++.h"
  25. #include "tolua++urho3d.h"
  26. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str)
  27. {
  28. const char* s = tolua_tostring(L, narg, str);
  29. return s ? s : "";
  30. }
  31. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str)
  32. {
  33. return tolua_tourho3dstring(L, narg, str.CString());
  34. }
  35. template<> int tolua_isurho3dvector<String>(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  36. {
  37. if (lua_istable(L, lo))
  38. {
  39. int length = lua_objlen(L, lo);
  40. for (int i = 1; i <= length; ++i)
  41. {
  42. lua_pushinteger(L, i);
  43. lua_gettable(L, lo);
  44. if (!lua_isstring(L, -1))
  45. {
  46. lua_pop(L, 1);
  47. err->index = lo;
  48. err->array = 0;
  49. err->type = type;
  50. return 0;
  51. }
  52. lua_pop(L, 1);
  53. }
  54. return 1;
  55. }
  56. err->index = lo;
  57. err->array = 0;
  58. err->type = type;
  59. return 0;
  60. }
  61. template<> void* tolua_tourho3dvector<String>(lua_State* L, int narg, void* def)
  62. {
  63. if (!lua_istable(L, narg))
  64. return 0;
  65. static Vector<String> result;
  66. result.Clear();
  67. int length = lua_objlen(L, narg);
  68. for (int i = 1; i <= length; ++i)
  69. {
  70. lua_pushinteger(L, i);
  71. lua_gettable(L, narg);
  72. if (!lua_isstring(L, -1))
  73. {
  74. lua_pop(L, 1);
  75. return 0;
  76. }
  77. String string = tolua_tourho3dstring(L, -1, "");
  78. result.Push(string);
  79. lua_pop(L, 1);
  80. }
  81. return &result;
  82. }
  83. template<> int tolua_pushurho3dvector<String>(lua_State* L, void* data, const char* type)
  84. {
  85. const Vector<String>& vectorstring = *((const Vector<String>*)data);
  86. lua_newtable(L);
  87. for (unsigned i = 0; i < vectorstring.Size(); ++i)
  88. {
  89. tolua_pushurho3dstring(L, vectorstring[i]);
  90. lua_rawseti(L, -2, i + 1);
  91. }
  92. return 1;
  93. }
  94. template<> int tolua_isurho3dpodvector<unsigned>(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  95. {
  96. if (lua_istable(L, lo))
  97. {
  98. int length = lua_objlen(L, lo);
  99. for (int i = 1; i <= length; ++i)
  100. {
  101. lua_pushinteger(L, i);
  102. lua_gettable(L, lo);
  103. if (!lua_isnumber(L, -1))
  104. {
  105. lua_pop(L, 1);
  106. err->index = lo;
  107. err->array = 0;
  108. err->type = type;
  109. return 0;
  110. }
  111. lua_pop(L, 1);
  112. }
  113. return 1;
  114. }
  115. err->index = lo;
  116. err->array = 0;
  117. err->type = type;
  118. return 0;
  119. }
  120. template<> void* tolua_tourho3dpodvector<unsigned>(lua_State* L, int narg, void* def)
  121. {
  122. if (!lua_istable(L, narg))
  123. return 0;
  124. static Vector<unsigned> result;
  125. result.Clear();
  126. int length = lua_objlen(L, narg);
  127. for (int i = 1; i <= length; ++i)
  128. {
  129. lua_pushinteger(L, i);
  130. lua_gettable(L, narg);
  131. if (!lua_isnumber(L, -1))
  132. {
  133. lua_pop(L, 1);
  134. return 0;
  135. }
  136. unsigned value = (unsigned)tolua_tonumber(L, -1, 0);
  137. result.Push(value);
  138. lua_pop(L, 1);
  139. }
  140. return &result;
  141. }
  142. template<> int tolua_pushurho3dpodvector<int>(lua_State* L, void* data, const char* type)
  143. {
  144. const PODVector<int>& vector = *((const PODVector<int>*)data);
  145. lua_newtable(L);
  146. for (unsigned i = 0; i < vector.Size(); ++i)
  147. {
  148. lua_pushinteger(L, vector[i]);
  149. lua_rawseti(L, -2, i + 1);
  150. }
  151. return 1;
  152. }
  153. template<> int tolua_pushurho3dpodvector<IntVector2>(lua_State* L, void* data, const char* type)
  154. {
  155. const PODVector<IntVector2>& vector = *((const PODVector<IntVector2>*)data);
  156. lua_newtable(L);
  157. for (unsigned i = 0; i < vector.Size(); ++i)
  158. {
  159. void* tolua_obj = Mtolua_new((IntVector2)(vector[i]));
  160. tolua_pushusertype(L,tolua_obj,"IntVector2");
  161. tolua_register_gc(L,lua_gettop(L));
  162. lua_rawseti(L, -2, i + 1);
  163. }
  164. return 1;
  165. }
  166. template<> int tolua_pushurho3dpodvector<OctreeQueryResult>(lua_State* L, void* data, const char* type)
  167. {
  168. const PODVector<OctreeQueryResult>& vector = *((const PODVector<OctreeQueryResult>*)data);
  169. lua_newtable(L);
  170. for (unsigned i = 0; i < vector.Size(); ++i)
  171. {
  172. void* tolua_obj = Mtolua_new((OctreeQueryResult)(vector[i]));
  173. tolua_pushusertype(L,tolua_obj,"OctreeQueryResult");
  174. tolua_register_gc(L,lua_gettop(L));
  175. lua_rawseti(L, -2, i + 1);
  176. }
  177. return 1;
  178. }
  179. template<> int tolua_pushurho3dpodvector<PhysicsRaycastResult>(lua_State* L, void* data, const char* type)
  180. {
  181. const PODVector<PhysicsRaycastResult>& vector = *((const PODVector<PhysicsRaycastResult>*)data);
  182. lua_newtable(L);
  183. for (unsigned i = 0; i < vector.Size(); ++i)
  184. {
  185. void* tolua_obj = Mtolua_new((PhysicsRaycastResult)(vector[i]));
  186. tolua_pushusertype(L,tolua_obj,"PhysicsRaycastResult");
  187. tolua_register_gc(L,lua_gettop(L));
  188. lua_rawseti(L, -2, i + 1);
  189. }
  190. return 1;
  191. }
  192. template<> int tolua_pushurho3dpodvector<RayQueryResult>(lua_State* L, void* data, const char* type)
  193. {
  194. const PODVector<RayQueryResult>& vector = *((const PODVector<RayQueryResult>*)data);
  195. lua_newtable(L);
  196. for (unsigned i = 0; i < vector.Size(); ++i)
  197. {
  198. void* tolua_obj = Mtolua_new((RayQueryResult)(vector[i]));
  199. tolua_pushusertype(L,tolua_obj,"RayQueryResult");
  200. tolua_register_gc(L,lua_gettop(L));
  201. lua_rawseti(L, -2, i + 1);
  202. }
  203. return 1;
  204. }
  205. template<> int tolua_pushurho3dpodvector<UIElement*>(lua_State* L, void* data, const char* type)
  206. {
  207. const PODVector<UIElement*>& vector = *((const PODVector<UIElement*>*)data);
  208. lua_newtable(L);
  209. for (unsigned i = 0; i < vector.Size(); ++i)
  210. {
  211. tolua_pushusertype(L, vector[i], "UIElement");
  212. lua_rawseti(L, -2, i + 1);
  213. }
  214. return 1;
  215. }
  216. template<> int tolua_pushurho3dpodvector<unsigned>(lua_State* L, void* data, const char* type)
  217. {
  218. const PODVector<unsigned>& vector = *((const PODVector<unsigned>*)data);
  219. lua_newtable(L);
  220. for (unsigned i = 0; i < vector.Size(); ++i)
  221. {
  222. lua_pushinteger(L, vector[i]);
  223. lua_rawseti(L, -2, i + 1);
  224. }
  225. return 1;
  226. }
  227. template<typename T> int PushPODVector(lua_State* L, const PODVector<T>& vector, const char* type)
  228. {
  229. lua_newtable(L);
  230. for (unsigned i = 0; i < vector.Size(); ++i)
  231. {
  232. void* tolua_obj = Mtolua_new((T)(vector[i]));
  233. tolua_pushusertype(L, tolua_obj, type);
  234. tolua_register_gc(L, lua_gettop(L));
  235. lua_rawseti(L, -2, i + 1);
  236. }
  237. return 1;
  238. }
  239. template<> int tolua_pushurho3dpodvector<Vector3>(lua_State* L, void* data, const char* type)
  240. {
  241. const PODVector<Vector3>& vector = *((const PODVector<Vector3>*)data);
  242. lua_newtable(L);
  243. for (unsigned i = 0; i < vector.Size(); ++i)
  244. {
  245. void* tolua_obj = Mtolua_new((Vector3)(vector[i]));
  246. tolua_pushusertype(L,tolua_obj,"Vector3");
  247. tolua_register_gc(L,lua_gettop(L));
  248. lua_rawseti(L, -2, i + 1);
  249. }
  250. return 1;
  251. }