ToluaUtils.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Copyright (c) 2008-2014 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. #pragma once
  23. #include "Context.h"
  24. #include "OctreeQuery.h"
  25. #ifdef URHO3D_PHYSICS
  26. #include "PhysicsWorld.h"
  27. #endif
  28. #ifdef URHO3D_URHO2D
  29. #include "PhysicsWorld2D.h"
  30. #endif
  31. #include "Vector2.h"
  32. #include "Vector3.h"
  33. struct lua_State;
  34. namespace Urho3D
  35. {
  36. class Context;
  37. class Pass;
  38. class SoundSource;
  39. class UIElement;
  40. }
  41. using namespace Urho3D;
  42. /// Check is String.
  43. #define tolua_isurho3dstring tolua_isstring
  44. /// Push String.
  45. #define tolua_pushurho3dstring(x, y) tolua_pushstring(x, y.CString())
  46. /// Convert to String.
  47. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str);
  48. /// Convert to String.
  49. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str);
  50. /// Set context.
  51. void SetContext(lua_State* L, Context* context);
  52. /// Return context.
  53. Context* GetContext(lua_State* L);
  54. /// Create object.
  55. template<typename T> int ToluaNewObject(lua_State* tolua_S)
  56. {
  57. T* object = Mtolua_new(T(GetContext(tolua_S)));
  58. tolua_pushusertype(tolua_S, (void*)object,T::GetTypeNameStatic().CString());
  59. return 1;
  60. }
  61. /// Create object with garbage collection.
  62. template<typename T> int ToluaNewObjectGC(lua_State* tolua_S)
  63. {
  64. T* object = Mtolua_new(T(GetContext(tolua_S)));
  65. tolua_pushusertype(tolua_S, (void*)object,T::GetTypeNameStatic().CString());
  66. tolua_register_gc(tolua_S, lua_gettop(tolua_S));
  67. return 1;
  68. }
  69. /// Return subsystem.
  70. template<typename T> int ToluaGetSubsystem(lua_State* tolua_S)
  71. {
  72. T* subsystem = GetContext(tolua_S)->GetSubsystem<T>();
  73. tolua_pushusertype(tolua_S, (void*)subsystem, T::GetTypeNameStatic().CString());
  74. return 1;
  75. }
  76. /// Check is Vector<T>.
  77. template<typename T> int ToluaIsVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  78. /// Check is Vector<String>.
  79. template<> int ToluaIsVector<String>(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  80. /// Convert to Vector<T>.
  81. template<typename T> void* ToluaToVector(lua_State* L, int narg, void* def);
  82. /// Convert to Vector<String>.
  83. template<> void* ToluaToVector<String>(lua_State* L, int narg, void* def);
  84. /// Push Vector<T> to Lua as a table.
  85. template<typename T> int ToluaPushVector(lua_State*L, void* data, const char* type);
  86. /// Push Vector<String> to Lua as a table.
  87. template<> int ToluaPushVector<String>(lua_State* L, void* data, const char* type);
  88. /// Push Vector<StringHash> to Lua as a table.
  89. template<> int ToluaPushVector<StringHash>(lua_State* L, void* data, const char* type);
  90. /// Check is PODVector<T>.
  91. template<typename T> int ToluaIsPODVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  92. /// Check is PODVector<unsigned>.
  93. template<> int ToluaIsPODVector<unsigned>(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  94. /// Check is PODVector<Vector2>.
  95. template<> int ToluaIsPODVector<Vector2>(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  96. /// Convert to PODVector<T>.
  97. template<typename T> void* ToluaToPODVector(lua_State* L, int narg, void* def);
  98. /// Convert PODVector<unsigned>.
  99. template<> void* ToluaToPODVector<unsigned>(lua_State* L, int narg, void* def);
  100. /// Convert PODVector<Vector2>.
  101. template<> void* ToluaToPODVector<Vector2>(lua_State* L, int narg, void* def);
  102. /// Push PODVector<T> to Lua as a table.
  103. template<typename T> int ToluaPushPODVector(lua_State* L, void* data, const char* type);
  104. /// Push PODVector<int> to Lua as a table.
  105. template<> int ToluaPushPODVector<int>(lua_State* L, void* data, const char* type);
  106. /// Push PODVector<unsigned> to Lua as a table.
  107. template<> int ToluaPushPODVector<unsigned>(lua_State* L, void* data, const char* type);
  108. /// Push PODVector<SoundSource*> to Lua as a table.
  109. template<> int ToluaPushPODVector<SoundSource*>(lua_State* L, void* data, const char* type);
  110. /// Push PODVector<UIElement*> to Lua as a table.
  111. template<> int ToluaPushPODVector<UIElement*>(lua_State* L, void* data, const char* type);
  112. #ifdef URHO3D_PHYSICS
  113. /// Push PODVector<RigidBody*> to Lua as a table.
  114. template<> int ToluaPushPODVector<RigidBody*>(lua_State* L, void* data, const char* type);
  115. #endif
  116. #ifdef URHO3D_URHO2D
  117. /// Push PODVector<RigidBody2D*> to Lua as a table.
  118. template<> int ToluaPushPODVector<RigidBody2D*>(lua_State* L, void* data, const char* type);
  119. #endif
  120. /// Push PODVector<Vector3> to Lua as a table.
  121. template<> int ToluaPushPODVector<Vector3>(lua_State* L, void* data, const char* type);
  122. /// Push PODVector<IntVector2> to Lua as a table.
  123. template<> int ToluaPushPODVector<IntVector2>(lua_State* L, void* data, const char* type);
  124. /// Push PODVector<OctreeQueryResult> to Lua as a table.
  125. template<> int ToluaPushPODVector<OctreeQueryResult>(lua_State* L, void* data, const char* type);
  126. #ifdef URHO3D_PHYSICS
  127. /// Push PODVector<PhysicsRaycastResult> to Lua as a table.
  128. template<> int ToluaPushPODVector<PhysicsRaycastResult>(lua_State* L, void* data, const char* type);
  129. #endif
  130. #ifdef URHO3D_URHO2D
  131. /// Push PODVector<PhysicsRaycastResult2D> to Lua as a table.
  132. template<> int ToluaPushPODVector<PhysicsRaycastResult2D>(lua_State* L, void* data, const char* type);
  133. #endif
  134. /// Push PODVector<RayQueryResult> to Lua as a table.
  135. template<> int ToluaPushPODVector<RayQueryResult>(lua_State* L, void* data, const char* type);
  136. /// Push PODVector<Pass*> to Lua as a table.
  137. template<> int ToluaPushPODVector<Pass*>(lua_State* L, void* data, const char* type);
  138. /// Push Object to Lua.
  139. void ToluaPushObject(lua_State*L, void* data, const char* type);