Node.pkg 17 KB

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