ToluaUtils.h 5.9 KB

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