Node.pkg 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. $#include "File.h"
  2. $#include "LuaFile.h"
  3. $#include "LuaScriptInstance.h"
  4. $#include "Node.h"
  5. $#include "ResourceCache.h"
  6. enum CreateMode
  7. {
  8. REPLICATED = 0,
  9. LOCAL = 1
  10. };
  11. enum TransformSpace
  12. {
  13. TS_LOCAL = 0,
  14. TS_PARENT,
  15. TS_WORLD
  16. };
  17. class Node : public Animatable
  18. {
  19. Node();
  20. virtual ~Node();
  21. tolua_outside bool NodeSaveXML @ SaveXML(File* dest) const;
  22. void SetName(const String name);
  23. void SetPosition(const Vector3& position);
  24. void SetPosition(const Vector2& position);
  25. void SetRotation(const Quaternion& rotation);
  26. void SetRotation(float rotation);
  27. void SetDirection(const Vector3& direction);
  28. void SetScale(float scale);
  29. void SetScale(const Vector3& scale);
  30. void SetScale(const Vector2& scale);
  31. void SetTransform(const Vector3& position, const Quaternion& rotation);
  32. void SetTransform(const Vector2& position, float rotation);
  33. void SetTransform(const Vector3& position, const Quaternion& rotation, float scale);
  34. void SetTransform(const Vector2& position, float rotation, float scale);
  35. void SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  36. void SetTransform(const Vector2& position, float rotation, const Vector2& scale);
  37. void SetWorldPosition(const Vector3& position);
  38. void SetWorldPosition(const Vector2& position);
  39. void SetWorldRotation(const Quaternion& rotation);
  40. void SetWorldRotation(float rotation);
  41. void SetWorldDirection(const Vector3& direction);
  42. void SetWorldScale(float scale);
  43. void SetWorldScale(const Vector3& scale);
  44. void SetWorldScale(const Vector2& scale);
  45. void SetWorldTransform(const Vector3& position, const Quaternion& rotation);
  46. void SetWorldTransform(const Vector2& position, float rotation);
  47. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, float scale);
  48. void SetWorldTransform(const Vector2& position, float rotation, float scale);
  49. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  50. void SetWorldTransform(const Vector2& position, float rotation, const Vector3& scale);
  51. void Translate(const Vector3& delta, TransformSpace space = TS_LOCAL);
  52. void Translate(const Vector2& delta, TransformSpace space = TS_LOCAL);
  53. void Rotate(const Quaternion& delta, TransformSpace space = TS_LOCAL);
  54. void Rotate(float delta, TransformSpace space = TS_LOCAL);
  55. void RotateAround(const Vector3& point, const Quaternion& delta, TransformSpace space = TS_LOCAL);
  56. void RotateAround(const Vector2& point, float delta, TransformSpace space = TS_LOCAL);
  57. void Pitch(float angle, TransformSpace space = TS_LOCAL);
  58. void Yaw(float angle, TransformSpace space = TS_LOCAL);
  59. void Roll(float angle, TransformSpace space = TS_LOCAL);
  60. bool LookAt(const Vector3& target, const Vector3& upAxis = Vector3::UP, TransformSpace space = TS_WORLD);
  61. void Scale(float scale);
  62. void Scale(const Vector3& scale);
  63. void Scale(const Vector2& scale);
  64. void SetEnabled(bool enable);
  65. void SetEnabled(bool enable, bool recursive);
  66. void SetOwner(Connection* owner);
  67. void MarkDirty();
  68. Node* CreateChild(const String name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0);
  69. void AddChild(Node* node, unsigned index = M_MAX_UNSIGNED);
  70. void RemoveChild(Node* node);
  71. void RemoveAllChildren();
  72. void RemoveChildren(bool removeReplicated, bool removeLocal, bool recursive);
  73. void RemoveComponent(Component* component);
  74. void RemoveComponent(ShortStringHash type);
  75. void RemoveComponent(const String type);
  76. void RemoveAllComponents();
  77. void RemoveComponents(bool removeReplicated, bool removeLocal);
  78. Node* Clone(CreateMode mode = REPLICATED);
  79. void Remove();
  80. void SetParent(Node* parent);
  81. void SetVar(ShortStringHash key, const Variant& value);
  82. void AddListener(Component* component);
  83. void RemoveListener(Component* component);
  84. // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  85. Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
  86. Component* CloneComponent(Component* component, unsigned id = 0);
  87. Component* CloneComponent(Component* component, CreateMode mode, unsigned id = 0);
  88. int CreateScriptObject(const String scriptObjectType);
  89. int CreateScriptObject(const String fileName, const String scriptObjectType);
  90. int GetScriptObject() const;
  91. int GetScriptObject(const String scriptObjectType) const;
  92. unsigned GetID() const;
  93. const String GetName() const;
  94. StringHash GetNameHash() const;
  95. Node* GetParent() const;
  96. Scene* GetScene() const;
  97. bool IsEnabled() const;
  98. Connection* GetOwner() const;
  99. const Vector3& GetPosition() const;
  100. const Quaternion& GetRotation() const;
  101. Vector3 GetDirection() const;
  102. Vector3 GetUp() const;
  103. Vector3 GetRight() const;
  104. const Vector3& GetScale() const;
  105. Matrix3x4 GetTransform() const;
  106. Vector3 GetWorldPosition() const;
  107. Quaternion GetWorldRotation() const;
  108. Vector3 GetWorldDirection() const;
  109. Vector3 GetWorldUp() const;
  110. Vector3 GetWorldRight() const;
  111. Vector3 GetWorldScale() const;
  112. const Matrix3x4& GetWorldTransform() const;
  113. Vector3 LocalToWorld(const Vector3& position) const;
  114. Vector3 LocalToWorld(const Vector4& vector) const;
  115. Vector2 LocalToWorld(const Vector2& vector) const;
  116. Vector3 WorldToLocal(const Vector3& position) const;
  117. Vector3 WorldToLocal(const Vector4& vector) const;
  118. Vector2 WorldToLocal(const Vector2& vector) const;
  119. bool IsDirty() const;
  120. unsigned GetNumChildren(bool recursive = false) const;
  121. Node* GetChild(const String name, bool recursive = false) const;
  122. Node* GetChild(StringHash nameHash, bool recursive = false) const;
  123. Node* GetChild(unsigned index) const;
  124. unsigned GetNumComponents() const;
  125. unsigned GetNumNetworkComponents() const;
  126. bool HasComponent(ShortStringHash type) const;
  127. bool HasComponent(const String type) const;
  128. const Variant& GetVar(ShortStringHash key) const;
  129. const VariantMap& GetVars() const;
  130. // template <class T> T* GetComponent() const;
  131. Component* GetComponent(const String type) const;
  132. void SetID(unsigned id);
  133. void SetScene(Scene* scene);
  134. void ResetScene();
  135. bool Load(Deserializer& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  136. bool LoadXML(const XMLElement& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  137. Node* CreateChild(unsigned id, CreateMode mode);
  138. void AddComponent(Component* component, unsigned id, CreateMode mode);
  139. tolua_property__get_set unsigned ID;
  140. tolua_property__get_set String name;
  141. tolua_readonly tolua_property__get_set StringHash nameHash;
  142. tolua_property__get_set Node* parent;
  143. tolua_property__get_set Scene* scene;
  144. tolua_property__is_set bool enabled;
  145. tolua_property__get_set Connection* owner;
  146. tolua_property__get_set Vector3& position;
  147. tolua_property__get_set Quaternion& rotation;
  148. tolua_property__get_set Vector3 direction;
  149. tolua_readonly tolua_property__get_set Vector3 up;
  150. tolua_readonly tolua_property__get_set Vector3 right;
  151. tolua_property__get_set Vector3& scale;
  152. tolua_readonly tolua_property__get_set Matrix3x4 transform;
  153. tolua_property__get_set Vector3 worldPosition;
  154. tolua_property__get_set Quaternion worldRotation;
  155. tolua_property__get_set Vector3 worldDirection;
  156. tolua_readonly tolua_property__get_set Vector3 worldUp;
  157. tolua_readonly tolua_property__get_set Vector3 worldRight;
  158. tolua_property__get_set Vector3 worldScale;
  159. tolua_readonly tolua_property__get_set Matrix3x4& worldTransform;
  160. tolua_readonly tolua_property__is_set bool dirty;
  161. tolua_readonly tolua_property__get_set unsigned numComponents;
  162. tolua_readonly tolua_property__get_set unsigned numNetworkComponents;
  163. };
  164. ${
  165. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00
  166. static int tolua_SceneLuaAPI_Node_new00(lua_State* tolua_S)
  167. {
  168. return ToluaNewObject<Node>(tolua_S);
  169. }
  170. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00_local
  171. static int tolua_SceneLuaAPI_Node_new00_local(lua_State* tolua_S)
  172. {
  173. return ToluaNewObjectGC<Node>(tolua_S);
  174. }
  175. static bool NodeSaveXML(const Node* node, File* file)
  176. {
  177. return file ? node->SaveXML(*file) : false;
  178. }
  179. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject00
  180. static int tolua_SceneLuaAPI_Node_CreateScriptObject00(lua_State* tolua_S)
  181. {
  182. #ifndef TOLUA_RELEASE
  183. tolua_Error tolua_err;
  184. if (
  185. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  186. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  187. !tolua_isnoobj(tolua_S,3,&tolua_err)
  188. )
  189. goto tolua_lerror;
  190. else
  191. #endif
  192. {
  193. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  194. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  195. #ifndef TOLUA_RELEASE
  196. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  197. #endif
  198. {
  199. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  200. if (!instance)
  201. lua_pushnil(tolua_S);
  202. else
  203. {
  204. instance->CreateObject(scriptObjectType);
  205. // Push script object to Lua stack.
  206. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  207. }
  208. }
  209. }
  210. return 1;
  211. #ifndef TOLUA_RELEASE
  212. tolua_lerror:
  213. tolua_error(tolua_S,"#ferror in function 'CreateScriptObject'.",&tolua_err);
  214. return 0;
  215. #endif
  216. }
  217. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject01
  218. static int tolua_SceneLuaAPI_Node_CreateScriptObject01(lua_State* tolua_S)
  219. {
  220. tolua_Error tolua_err;
  221. if (
  222. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  223. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  224. !tolua_isurho3dstring(tolua_S,3,0,&tolua_err) ||
  225. !tolua_isnoobj(tolua_S,4,&tolua_err)
  226. )
  227. goto tolua_lerror;
  228. else
  229. {
  230. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  231. const String fileName = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  232. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,3,0));
  233. #ifndef TOLUA_RELEASE
  234. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  235. #endif
  236. {
  237. ResourceCache* cache = self->GetSubsystem<ResourceCache>();
  238. LuaFile* scriptFile = cache->GetResource<LuaFile>(fileName);
  239. if (!scriptFile)
  240. {
  241. lua_pushnil(tolua_S);
  242. return 1;
  243. }
  244. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  245. if (!instance)
  246. lua_pushnil(tolua_S);
  247. else
  248. {
  249. instance->CreateObject(scriptFile, scriptObjectType);
  250. // Push script object to Lua stack.
  251. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  252. }
  253. }
  254. }
  255. return 1;
  256. tolua_lerror:
  257. return tolua_SceneLuaAPI_Node_CreateScriptObject00(tolua_S);
  258. }
  259. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject00
  260. static int tolua_SceneLuaAPI_Node_GetScriptObject00(lua_State* tolua_S)
  261. {
  262. #ifndef TOLUA_RELEASE
  263. tolua_Error tolua_err;
  264. if (
  265. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  266. !tolua_isnoobj(tolua_S,2,&tolua_err)
  267. )
  268. goto tolua_lerror;
  269. else
  270. #endif
  271. {
  272. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  273. #ifndef TOLUA_RELEASE
  274. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  275. #endif
  276. {
  277. LuaScriptInstance* instance = self->GetComponent<LuaScriptInstance>();
  278. if (!instance)
  279. lua_pushnil(tolua_S);
  280. else
  281. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  282. }
  283. }
  284. return 1;
  285. #ifndef TOLUA_RELEASE
  286. tolua_lerror:
  287. tolua_error(tolua_S,"#ferror in function 'GetScriptObject'.",&tolua_err);
  288. return 0;
  289. #endif
  290. }
  291. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject01
  292. static int tolua_SceneLuaAPI_Node_GetScriptObject01(lua_State* tolua_S)
  293. {
  294. tolua_Error tolua_err;
  295. if (
  296. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  297. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  298. !tolua_isnoobj(tolua_S,3,&tolua_err)
  299. )
  300. goto tolua_lerror;
  301. else
  302. {
  303. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  304. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  305. #ifndef TOLUA_RELEASE
  306. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  307. #endif
  308. {
  309. int scriptObjectRef = LUA_REFNIL;
  310. PODVector<LuaScriptInstance*> instances;
  311. self->GetComponents<LuaScriptInstance>(instances, false);
  312. for (unsigned i = 0; i < instances.Size(); ++i)
  313. {
  314. if (instances[i]->GetScriptObjectType() == scriptObjectType)
  315. {
  316. scriptObjectRef = instances[i]->GetScriptObjectRef();
  317. break;
  318. }
  319. }
  320. if (scriptObjectRef == LUA_REFNIL)
  321. lua_pushnil(tolua_S);
  322. else
  323. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, scriptObjectRef);
  324. }
  325. }
  326. return 1;
  327. tolua_lerror:
  328. return tolua_SceneLuaAPI_Node_GetScriptObject00(tolua_S);
  329. }
  330. $}