tolua++urho3d.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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* def)
  27. {
  28. const char* s = tolua_tostring(L, narg, def);
  29. return s ? s : "";
  30. }
  31. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& def)
  32. {
  33. return tolua_tourho3dstring(L, narg, def.CString());
  34. }
  35. int tolua_pushurho3dconstpodvectorintvector2(lua_State* L, void* data, const char* type)
  36. {
  37. #ifndef TOLUA_RELEASE
  38. assert(strcmp(type, "const PODVector<IntVector2>") == 0);
  39. #endif
  40. const PODVector<IntVector2>& vector = *((const PODVector<IntVector2>*)data);
  41. lua_newtable(L);
  42. for (unsigned i = 0; i < vector.Size(); ++i)
  43. {
  44. void* tolua_obj = Mtolua_new((IntVector2)(vector[i]));
  45. tolua_pushusertype(L,tolua_obj,"IntVector2");
  46. tolua_register_gc(L,lua_gettop(L));
  47. lua_rawseti(L, -2, i + 1);
  48. }
  49. return 1;
  50. }
  51. int tolua_pushurho3dpodvectoruielement(lua_State* L, void* data, const char* type)
  52. {
  53. #ifndef TOLUA_RELEASE
  54. assert(strcmp(type, "const PODVector<UIElement*>") == 0);
  55. #endif
  56. const PODVector<UIElement*>& vector = *((const PODVector<UIElement*>*)data);
  57. lua_newtable(L);
  58. for (unsigned i = 0; i < vector.Size(); ++i)
  59. {
  60. tolua_pushusertype(L, vector[i], "UIElement");
  61. lua_rawseti(L, -2, i + 1);
  62. }
  63. return 1;
  64. }
  65. int tolua_isurho3dconstpodvectorunsigned(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  66. {
  67. if (lua_istable(L, lo))
  68. {
  69. int length = lua_objlen(L, lo);
  70. for (int i = 1; i <= length; ++i)
  71. {
  72. lua_pushinteger(L, i);
  73. lua_gettable(L, lo);
  74. if (!lua_isnumber(L, -1))
  75. {
  76. lua_pop(L, 1);
  77. err->index = lo;
  78. err->array = 0;
  79. err->type = type;
  80. return 0;
  81. }
  82. lua_pop(L, 1);
  83. }
  84. return 1;
  85. }
  86. err->index = lo;
  87. err->array = 0;
  88. err->type = type;
  89. return 0;
  90. }
  91. void* tolua_tourho3dconstpodvectorunsigned(lua_State* L, int narg, void* def)
  92. {
  93. if (!lua_istable(L, narg))
  94. return 0;
  95. static Vector<unsigned> result;
  96. result.Clear();
  97. int length = lua_objlen(L, narg);
  98. for (int i = 1; i <= length; ++i)
  99. {
  100. lua_pushinteger(L, i);
  101. lua_gettable(L, narg);
  102. if (!lua_isnumber(L, -1))
  103. {
  104. lua_pop(L, 1);
  105. return 0;
  106. }
  107. unsigned value = (unsigned)tolua_tonumber(L, -1, 0);
  108. result.Push(value);
  109. lua_pop(L, 1);
  110. }
  111. return &result;
  112. }
  113. int tolua_pushurho3dconstpodvectorunsigned(lua_State* L, void* data, const char* type)
  114. {
  115. #ifndef TOLUA_RELEASE
  116. assert(strcmp(type, "const PODVector<unsigned>") == 0);
  117. #endif
  118. const PODVector<unsigned>& vector = *((const PODVector<unsigned>*)data);
  119. lua_newtable(L);
  120. for (unsigned i = 0; i < vector.Size(); ++i)
  121. {
  122. lua_pushinteger(L, vector[i]);
  123. lua_rawseti(L, -2, i + 1);
  124. }
  125. return 1;
  126. }
  127. int tolua_isurho3dconstvectorstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  128. {
  129. if (lua_istable(L, lo))
  130. {
  131. int length = lua_objlen(L, lo);
  132. for (int i = 1; i <= length; ++i)
  133. {
  134. lua_pushinteger(L, i);
  135. lua_gettable(L, lo);
  136. if (!lua_isstring(L, -1))
  137. {
  138. lua_pop(L, 1);
  139. err->index = lo;
  140. err->array = 0;
  141. err->type = type;
  142. return 0;
  143. }
  144. lua_pop(L, 1);
  145. }
  146. return 1;
  147. }
  148. err->index = lo;
  149. err->array = 0;
  150. err->type = type;
  151. return 0;
  152. }
  153. void* tolua_tourho3dconstvectorstring(lua_State* L, int narg, void* def)
  154. {
  155. if (!lua_istable(L, narg))
  156. return 0;
  157. static Vector<String> result;
  158. result.Clear();
  159. int length = lua_objlen(L, narg);
  160. for (int i = 1; i <= length; ++i)
  161. {
  162. lua_pushinteger(L, i);
  163. lua_gettable(L, narg);
  164. if (!lua_isstring(L, -1))
  165. {
  166. lua_pop(L, 1);
  167. return 0;
  168. }
  169. String string = tolua_tourho3dstring(L, -1, "");
  170. result.Push(string);
  171. lua_pop(L, 1);
  172. }
  173. return &result;
  174. }
  175. int tolua_pushurho3dconstvectorstring(lua_State*L, void* data, const char* type)
  176. {
  177. #ifndef TOLUA_RELEASE
  178. assert(strcmp(type, "const Vector<String>") == 0);
  179. #endif
  180. const Vector<String>& vectorstring = *((const Vector<String>*)data);
  181. lua_newtable(L);
  182. for (unsigned i = 0; i < vectorstring.Size(); ++i)
  183. {
  184. tolua_pushurho3dstring(L, vectorstring[i]);
  185. lua_rawseti(L, -2, i + 1);
  186. }
  187. return 1;
  188. }