Node.pkg 16 KB

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