tolua++urho3d.cpp 5.9 KB

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