ToluaUtils.h 6.5 KB

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