Node.pkg 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. $#include "IO/File.h"
  2. $#include "LuaScript/LuaFile.h"
  3. $#include "LuaScript/LuaScriptInstance.h"
  4. $#include "Scene/Node.h"
  5. $#include "Resource/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 String indentation = "\t") const;
  22. tolua_outside bool NodeSaveJSON @ SaveJSON(File* dest, const String indentation = "\t") const;
  23. void SetName(const String name);
  24. void AddTag(const String tag);
  25. void AddTags(const String tags, char separator);
  26. bool RemoveTag(const String tag);
  27. void RemoveAllTags();
  28. void SetPosition(const Vector3& position);
  29. void SetPosition2D(const Vector2& position);
  30. void SetPosition2D(float x, float y);
  31. void SetRotation(const Quaternion& rotation);
  32. void SetRotation2D(float rotation);
  33. void SetDirection(const Vector3& direction);
  34. void SetScale(float scale);
  35. void SetScale(const Vector3& scale);
  36. void SetScale2D(const Vector2& scale);
  37. void SetScale2D(float x, float y);
  38. void SetTransform(const Vector3& position, const Quaternion& rotation);
  39. void SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  40. void SetTransform(const Vector3& position, const Quaternion& rotation, float scale);
  41. void SetTransform(const Matrix3x4& transform);
  42. void SetTransform2D(const Vector2& position, float rotation);
  43. void SetTransform2D(const Vector2& position, float rotation, const Vector2& scale);
  44. void SetTransform2D(const Vector2& position, float rotation, float scale);
  45. void SetWorldPosition(const Vector3& position);
  46. void SetWorldPosition2D(const Vector2& position);
  47. void SetWorldPosition2D(float x, float y);
  48. void SetWorldRotation(const Quaternion& rotation);
  49. void SetWorldRotation2D(float rotation);
  50. void SetWorldDirection(const Vector3& direction);
  51. void SetWorldScale(float scale);
  52. void SetWorldScale(const Vector3& scale);
  53. void SetWorldScale2D(const Vector2& scale);
  54. void SetWorldScale2D(float x, float y);
  55. void SetWorldTransform(const Vector3& position, const Quaternion& rotation);
  56. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  57. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, float scale);
  58. void SetWorldTransform2D(const Vector2& position, float rotation);
  59. void SetWorldTransform2D(const Vector2& position, float rotation, const Vector2& scale);
  60. void SetWorldTransform2D(const Vector2& position, float rotation, float scale);
  61. void Translate(const Vector3& delta, TransformSpace space = TS_LOCAL);
  62. void Translate2D(const Vector2& delta, TransformSpace space = TS_LOCAL);
  63. void Rotate(const Quaternion& delta, TransformSpace space = TS_LOCAL);
  64. void Rotate2D(float delta, TransformSpace space = TS_LOCAL);
  65. void RotateAround(const Vector3& point, const Quaternion& delta, TransformSpace space = TS_LOCAL);
  66. void RotateAround2D(const Vector2& point, float delta, TransformSpace space = TS_LOCAL);
  67. void Pitch(float angle, TransformSpace space = TS_LOCAL);
  68. void Yaw(float angle, TransformSpace space = TS_LOCAL);
  69. void Roll(float angle, TransformSpace space = TS_LOCAL);
  70. bool LookAt(const Vector3& target, const Vector3& upAxis = Vector3::UP, TransformSpace space = TS_WORLD);
  71. void Scale(float scale);
  72. void Scale(const Vector3& scale);
  73. void Scale2D(const Vector2& scale);
  74. void SetEnabled(bool enable);
  75. void SetDeepEnabled(bool enable);
  76. void ResetDeepEnabled();
  77. void SetEnabledRecursive(bool enable);
  78. void SetOwner(Connection* owner);
  79. void MarkDirty();
  80. Node* CreateChild(const String name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0, bool temporary = false);
  81. Node* CreateTemporaryChild(const String name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0);
  82. void AddChild(Node* node, unsigned index = M_MAX_UNSIGNED);
  83. void RemoveChild(Node* node);
  84. void RemoveAllChildren();
  85. void RemoveChildren(bool removeReplicated, bool removeLocal, bool recursive);
  86. void RemoveComponent(Component* component);
  87. void RemoveComponent(StringHash type);
  88. void RemoveComponent(const String type);
  89. void RemoveComponents(bool removeReplicated, bool removeLocal);
  90. void RemoveComponents(const String type);
  91. void RemoveAllComponents();
  92. void ReorderComponent(Component* component, unsigned index);
  93. Node* Clone(CreateMode mode = REPLICATED);
  94. void Remove();
  95. void SetParent(Node* parent);
  96. void SetVar(StringHash key, const Variant& value);
  97. void AddListener(Component* component);
  98. void RemoveListener(Component* component);
  99. // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  100. Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
  101. // template <class T> T* GetOrCreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  102. Component* GetOrCreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
  103. Component* CloneComponent(Component* component, unsigned id = 0);
  104. Component* CloneComponent(Component* component, CreateMode mode, unsigned id = 0);
  105. int CreateScriptObject(const String scriptObjectType);
  106. int CreateScriptObject(const String fileName, const String scriptObjectType);
  107. int GetScriptObject() const;
  108. int GetScriptObject(const String scriptObjectType) const;
  109. unsigned GetID() const;
  110. const String GetName() const;
  111. StringHash GetNameHash() const;
  112. Node* GetParent() const;
  113. Scene* GetScene() const;
  114. bool IsChildOf(Node* node) const;
  115. bool IsEnabled() const;
  116. bool IsEnabledSelf() const;
  117. Connection* GetOwner() const;
  118. const Vector3& GetPosition() const;
  119. Vector2 GetPosition2D() const;
  120. const Quaternion& GetRotation() const;
  121. float GetRotation2D() const;
  122. Vector3 GetDirection() const;
  123. Vector3 GetUp() const;
  124. Vector3 GetRight() const;
  125. const Vector3& GetScale() const;
  126. Vector2 GetScale2D() const;
  127. Matrix3x4 GetTransform() const;
  128. Vector3 GetWorldPosition() const;
  129. Vector2 GetWorldPosition2D() const;
  130. Quaternion GetWorldRotation() const;
  131. float GetWorldRotation2D() const;
  132. Vector3 GetWorldDirection() const;
  133. Vector3 GetWorldUp() const;
  134. Vector3 GetWorldRight() const;
  135. Vector3 GetWorldScale() const;
  136. Vector3 GetSignedWorldScale() const;
  137. Vector2 GetWorldScale2D() const;
  138. const Matrix3x4& GetWorldTransform() const;
  139. Vector3 LocalToWorld(const Vector3& position) const;
  140. Vector3 LocalToWorld(const Vector4& vector) const;
  141. Vector2 LocalToWorld2D(const Vector2& vector) const;
  142. Vector3 WorldToLocal(const Vector3& position) const;
  143. Vector3 WorldToLocal(const Vector4& vector) const;
  144. Vector2 WorldToLocal2D(const Vector2& vector) const;
  145. bool IsDirty() const;
  146. unsigned GetNumChildren(bool recursive = false) const;
  147. Node* GetChild(const String name, bool recursive = false) const;
  148. Node* GetChild(StringHash nameHash, bool recursive = false) const;
  149. Node* GetChild(unsigned index) const;
  150. unsigned GetNumComponents() const;
  151. unsigned GetNumNetworkComponents() const;
  152. bool HasComponent(StringHash type) const;
  153. bool HasComponent(const String type) const;
  154. const Variant& GetVar(StringHash key) const;
  155. const VariantMap& GetVars() const;
  156. // template <class T> T* GetComponent() const;
  157. Component* GetComponent(const String type, bool recursive = false) const;
  158. // template <class T> T* GetParentComponent() const;
  159. Component* GetParentComponent(const String type, bool recursive = false) const;
  160. // template <class T> void GetComponents(PODVector<T*>& dest, bool recursive = false) const;
  161. tolua_outside const PODVector<Component*>& NodeGetComponentsWithType @ GetComponents(const String type, bool recursive = false);
  162. // void GetChildren(PODVector<Node*>& dest, bool recursive = false) const;
  163. tolua_outside const PODVector<Node*>& NodeGetChildren @ GetChildren(bool recursive = false);
  164. // template <class T> void GetChildrenWithComponent(PODVector<Node*>& dest, bool recursive = false) const;
  165. tolua_outside const PODVector<Node*>& NodeGetChildrenWithComponent @ GetChildrenWithComponent(const String type, bool recursive = false);
  166. bool Load(Deserializer& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  167. bool LoadXML(const XMLElement& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  168. bool LoadJSON(const JSONValue& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  169. Node* CreateChild(unsigned id, CreateMode mode, bool temporary = false);
  170. void AddComponent(Component* component, unsigned id, CreateMode mode);
  171. bool HasTag(const String tag) const;
  172. const StringVector& GetTags() const;
  173. // void GetChildrenWithTag(PODVector<Node*>& dest, const String& tag, bool recursive = false) const;
  174. tolua_outside const PODVector<Node*>& NodeGetChildrenWithTag @ GetChildrenWithTag(const String& tag, bool recursive = false) const;
  175. void SetID(unsigned id);
  176. tolua_readonly tolua_property__get_set unsigned ID;
  177. tolua_property__get_set String name;
  178. tolua_readonly tolua_property__get_set StringHash nameHash;
  179. tolua_property__get_set Node* parent;
  180. tolua_readonly tolua_property__get_set Scene* scene;
  181. tolua_property__is_set bool enabled;
  182. tolua_readonly tolua_property__is_set bool enabledSelf;
  183. tolua_property__get_set Connection* owner;
  184. tolua_property__get_set Vector3& position;
  185. tolua_property__get_set Vector2 position2D;
  186. tolua_property__get_set Quaternion& rotation;
  187. tolua_property__get_set float rotation2D;
  188. tolua_property__get_set Vector3 direction;
  189. tolua_readonly tolua_property__get_set Vector3 up;
  190. tolua_readonly tolua_property__get_set Vector3 right;
  191. tolua_property__get_set Vector3& scale;
  192. tolua_property__get_set Vector2 scale2D;
  193. tolua_readonly tolua_property__get_set Matrix3x4 transform;
  194. tolua_property__get_set Vector3 worldPosition;
  195. tolua_property__get_set Vector2 worldPosition2D;
  196. tolua_property__get_set Quaternion worldRotation;
  197. tolua_property__get_set float worldRotation2D;
  198. tolua_property__get_set Vector3 worldDirection;
  199. tolua_readonly tolua_property__get_set Vector3 worldUp;
  200. tolua_readonly tolua_property__get_set Vector3 worldRight;
  201. tolua_property__get_set Vector3 worldScale;
  202. tolua_readonly tolua_property__get_set Vector3 signedWorldScale;
  203. tolua_property__get_set Vector2 worldScale2D;
  204. tolua_readonly tolua_property__get_set Matrix3x4& worldTransform;
  205. tolua_readonly tolua_property__is_set bool dirty;
  206. tolua_readonly tolua_property__get_set unsigned numComponents;
  207. tolua_readonly tolua_property__get_set unsigned numNetworkComponents;
  208. };
  209. ${
  210. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00
  211. static int tolua_SceneLuaAPI_Node_new00(lua_State* tolua_S)
  212. {
  213. return ToluaNewObject<Node>(tolua_S);
  214. }
  215. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00_local
  216. static int tolua_SceneLuaAPI_Node_new00_local(lua_State* tolua_S)
  217. {
  218. return ToluaNewObjectGC<Node>(tolua_S);
  219. }
  220. static bool NodeSaveXML(const Node* node, File* file, const String& indentation)
  221. {
  222. return file ? node->SaveXML(*file, indentation) : false;
  223. }
  224. static bool NodeSaveJSON(const Node* node, File* file, const String& indentation)
  225. {
  226. return file ? node->SaveJSON(*file, indentation) : false;
  227. }
  228. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject00
  229. static int tolua_SceneLuaAPI_Node_CreateScriptObject00(lua_State* tolua_S)
  230. {
  231. #ifndef TOLUA_RELEASE
  232. tolua_Error tolua_err;
  233. if (
  234. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  235. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  236. !tolua_isnoobj(tolua_S,3,&tolua_err)
  237. )
  238. goto tolua_lerror;
  239. else
  240. #endif
  241. {
  242. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  243. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  244. #ifndef TOLUA_RELEASE
  245. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  246. #endif
  247. {
  248. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  249. if (!instance)
  250. lua_pushnil(tolua_S);
  251. else
  252. {
  253. instance->CreateObject(scriptObjectType);
  254. // Push script object to Lua stack.
  255. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  256. }
  257. }
  258. }
  259. return 1;
  260. #ifndef TOLUA_RELEASE
  261. tolua_lerror:
  262. tolua_error(tolua_S,"#ferror in function 'CreateScriptObject'.",&tolua_err);
  263. return 0;
  264. #endif
  265. }
  266. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject01
  267. static int tolua_SceneLuaAPI_Node_CreateScriptObject01(lua_State* tolua_S)
  268. {
  269. tolua_Error tolua_err;
  270. if (
  271. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  272. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  273. !tolua_isurho3dstring(tolua_S,3,0,&tolua_err) ||
  274. !tolua_isnoobj(tolua_S,4,&tolua_err)
  275. )
  276. goto tolua_lerror;
  277. else
  278. {
  279. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  280. const String fileName = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  281. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,3,0));
  282. #ifndef TOLUA_RELEASE
  283. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  284. #endif
  285. {
  286. ResourceCache* cache = self->GetSubsystem<ResourceCache>();
  287. LuaFile* scriptFile = cache->GetResource<LuaFile>(fileName);
  288. if (!scriptFile)
  289. {
  290. lua_pushnil(tolua_S);
  291. return 1;
  292. }
  293. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  294. if (!instance)
  295. lua_pushnil(tolua_S);
  296. else
  297. {
  298. instance->CreateObject(scriptFile, scriptObjectType);
  299. // Push script object to Lua stack.
  300. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  301. }
  302. }
  303. }
  304. return 1;
  305. tolua_lerror:
  306. return tolua_SceneLuaAPI_Node_CreateScriptObject00(tolua_S);
  307. }
  308. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject00
  309. static int tolua_SceneLuaAPI_Node_GetScriptObject00(lua_State* tolua_S)
  310. {
  311. #ifndef TOLUA_RELEASE
  312. tolua_Error tolua_err;
  313. if (
  314. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  315. !tolua_isnoobj(tolua_S,2,&tolua_err)
  316. )
  317. goto tolua_lerror;
  318. else
  319. #endif
  320. {
  321. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  322. #ifndef TOLUA_RELEASE
  323. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  324. #endif
  325. {
  326. LuaScriptInstance* instance = self->GetComponent<LuaScriptInstance>();
  327. if (!instance)
  328. lua_pushnil(tolua_S);
  329. else
  330. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  331. }
  332. }
  333. return 1;
  334. #ifndef TOLUA_RELEASE
  335. tolua_lerror:
  336. tolua_error(tolua_S,"#ferror in function 'GetScriptObject'.",&tolua_err);
  337. return 0;
  338. #endif
  339. }
  340. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject01
  341. static int tolua_SceneLuaAPI_Node_GetScriptObject01(lua_State* tolua_S)
  342. {
  343. tolua_Error tolua_err;
  344. if (
  345. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  346. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  347. !tolua_isnoobj(tolua_S,3,&tolua_err)
  348. )
  349. goto tolua_lerror;
  350. else
  351. {
  352. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  353. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  354. #ifndef TOLUA_RELEASE
  355. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  356. #endif
  357. {
  358. int scriptObjectRef = LUA_REFNIL;
  359. PODVector<LuaScriptInstance*> instances;
  360. self->GetComponents<LuaScriptInstance>(instances, false);
  361. for (unsigned i = 0; i < instances.Size(); ++i)
  362. {
  363. if (instances[i]->GetScriptObjectType() == scriptObjectType)
  364. {
  365. scriptObjectRef = instances[i]->GetScriptObjectRef();
  366. break;
  367. }
  368. }
  369. if (scriptObjectRef == LUA_REFNIL)
  370. lua_pushnil(tolua_S);
  371. else
  372. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, scriptObjectRef);
  373. }
  374. }
  375. return 1;
  376. tolua_lerror:
  377. return tolua_SceneLuaAPI_Node_GetScriptObject00(tolua_S);
  378. }
  379. static const PODVector<Component*>& NodeGetComponentsWithType(Node* node, String type, bool recursive)
  380. {
  381. static PODVector<Component*> result;
  382. result.Clear();
  383. node->GetComponents(result, type, recursive);
  384. return result;
  385. }
  386. static const PODVector<Node*>& NodeGetChildren(Node* node, bool recursive)
  387. {
  388. static PODVector<Node*> result;
  389. result.Clear();
  390. node->GetChildren(result, recursive);
  391. return result;
  392. }
  393. static const PODVector<Node*>& NodeGetChildrenWithComponent(Node* node, String type, bool recursive)
  394. {
  395. static PODVector<Node*> result;
  396. result.Clear();
  397. node->GetChildrenWithComponent(result, type, recursive);
  398. return result;
  399. }
  400. static const PODVector<Node*>& NodeGetChildrenWithTag(const Node* node, const String& tag, bool recursive)
  401. {
  402. static PODVector<Node*> result;
  403. result.Clear();
  404. node->GetChildrenWithTag(result, tag, recursive);
  405. return result;
  406. }
  407. $}