Node.pkg 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. $#include "File.h"
  2. $#include "Node.h"
  3. enum CreateMode
  4. {
  5. REPLICATED = 0,
  6. LOCAL = 1
  7. };
  8. class Node : public Serializable
  9. {
  10. Node(Context* context);
  11. virtual ~Node();
  12. tolua_outside bool NodeSaveXML @ SaveXML(File* dest) const;
  13. void SetName(const String name);
  14. void SetPosition(const Vector3& position);
  15. tolua_outside void NodeSetPositionXYZ @ SetPositionXYZ(float x, float y, float z);
  16. void SetRotation(const Quaternion& rotation);
  17. tolua_outside void NodeSetRotationXYZ @ SetRotationXYZ(float x, float y, float z);
  18. void SetDirection(const Vector3& direction);
  19. tolua_outside void NodeSetDirectionXYZ @ SetDirectionXYZ(float x, float y, float z);
  20. void SetScale(float scale);
  21. void SetScale(const Vector3& scale);
  22. tolua_outside void NodeSetScaleXYZ @ SetScaleXYZ(float x, float y, float z);
  23. void SetTransform(const Vector3& position, const Quaternion& rotation);
  24. void SetTransform(const Vector3& position, const Quaternion& rotation, float scale);
  25. void SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  26. void SetWorldPosition(const Vector3& position);
  27. tolua_outside void NodeSetWorldPositionXYZ @ SetWorldPositionXYZ(float x, float y, float z);
  28. void SetWorldRotation(const Quaternion& rotation);
  29. tolua_outside void NodeSetWorldRotationXYZ @ SetWorldRotationXYZ(float x, float y, float z);
  30. void SetWorldDirection(const Vector3& direction);
  31. tolua_outside void NodeSetWorldDirectionXYZ @ SetWorldDirectionXYZ(float x, float y, float z);
  32. void SetWorldScale(float scale);
  33. void SetWorldScale(const Vector3& scale);
  34. tolua_outside void NodeSetWorldScaleXYZ @ SetWorldScaleXYZ(float x, float y, float z);
  35. void SetWorldTransform(const Vector3& position, const Quaternion& rotation);
  36. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, float scale);
  37. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  38. void Translate(const Vector3& delta);
  39. tolua_outside void NodeTranslateXYZ @ TranslateXYZ(float x, float y, float z);
  40. void TranslateRelative(const Vector3& delta);
  41. tolua_outside void NodeTranslateRelativeXYZ @ TranslateRelativeXYZ(float x, float y, float z);
  42. void Rotate(const Quaternion& delta, bool fixedAxis = false);
  43. tolua_outside void NodeRotateXYZ @ RotateXYZ(float x, float y, float z, bool fixedAxis = false);
  44. void Pitch(float angle, bool fixedAxis = false);
  45. void Yaw(float angle, bool fixedAxis = false);
  46. void Roll(float angle, bool fixedAxis = false);
  47. void LookAt(const Vector3& target, const Vector3& upAxis = Vector3::UP);
  48. tolua_outside void NodeLookAtXYZ @ LookAtXYZ(float x, float y, float z, float upX = 0.0f, float upY = 1.0f, float upZ = 0.0f);
  49. void Scale(float scale);
  50. void Scale(const Vector3& scale);
  51. tolua_outside void NodeScaleXYZ @ ScaleXYZ(float x, float y, float z);
  52. void SetEnabled(bool enable);
  53. void SetEnabled(bool enable, bool recursive);
  54. void SetOwner(Connection* owner);
  55. void MarkDirty();
  56. Node* CreateChild(const String name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0);
  57. void AddChild(Node* node);
  58. void RemoveChild(Node* node);
  59. void RemoveAllChildren();
  60. void RemoveChildren(bool removeReplicated, bool removeLocal, bool recursive);
  61. void RemoveComponent(Component* component);
  62. void RemoveComponent(ShortStringHash type);
  63. void RemoveComponent(const String type);
  64. void RemoveAllComponents();
  65. void RemoveComponents(bool removeReplicated, bool removeLocal);
  66. Node* Clone(CreateMode mode = REPLICATED);
  67. void Remove();
  68. void SetParent(Node* parent);
  69. void SetVar(ShortStringHash key, const Variant& value);
  70. void AddListener(Component* component);
  71. void RemoveListener(Component* component);
  72. // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  73. Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
  74. unsigned GetID() const;
  75. const String& GetName() const;
  76. StringHash GetNameHash() const;
  77. Node* GetParent() const;
  78. Scene* GetScene() const;
  79. bool IsEnabled() const;
  80. Connection* GetOwner() const;
  81. const Vector3& GetPosition() const;
  82. tolua_outside void NodeGetPositionXYZ @ GetPositionXYZ(float* x = 0.0f, float* y = 0.0f, float* z = 0.0f) const;
  83. const Quaternion& GetRotation() const;
  84. tolua_outside void NodeGetRotationXYZ @ GetRotationXYZ(float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  85. tolua_outside void NodeGetRotationWXYZ @ GetRotationWXYZ(float* *w = 0.0f, float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  86. Vector3 GetDirection() const;
  87. tolua_outside void NodeGetDirectionXYZ @ GetDirectionXYZ(float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  88. const Vector3& GetScale() const;
  89. tolua_outside void NodeGetScaleXYZ @ GetScaleXYZ(float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  90. Matrix3x4 GetTransform() const;
  91. Vector3 GetWorldPosition() const;
  92. tolua_outside void NodeGetWorldPositionXYZ @ GetWorldPositionXYZ(float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  93. Quaternion GetWorldRotation() const;
  94. tolua_outside void NodeGetWorldRotationXYZ @ GetWorldRotationXYZ(float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  95. tolua_outside void NodeGetWorldRotationWXYZ @ GetWorldRotationWXYZ(float* *w = 0.0f, float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  96. Vector3 GetWorldDirection() const;
  97. tolua_outside void NodeGetWorldDirectionXYZ @ GetWorldDirectionXYZ(float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  98. Vector3 GetWorldScale() const;
  99. tolua_outside void NodeGetWorldScaleXYZ @ GetWorldScaleXYZ(float* *x = 0.0f, float* *y = 0.0f, float* *z = 0.0f) const;
  100. const Matrix3x4& GetWorldTransform() const;
  101. Vector3 LocalToWorld(const Vector3& position) const;
  102. Vector3 LocalToWorld(const Vector4& vector) const;
  103. Vector3 WorldToLocal(const Vector3& position) const;
  104. Vector3 WorldToLocal(const Vector4& vector) const;
  105. bool IsDirty() const;
  106. unsigned GetNumChildren(bool recursive = false) const;
  107. Node* GetChild(unsigned index) const;
  108. Node* GetChild(const String name, bool recursive = false) const;
  109. Node* GetChild(StringHash nameHash, bool recursive = false) const;
  110. unsigned GetNumComponents() const;
  111. unsigned GetNumNetworkComponents() const;
  112. bool HasComponent(ShortStringHash type) const;
  113. bool HasComponent(const String type) const;
  114. const Variant& GetVar(ShortStringHash key) const;
  115. const VariantMap& GetVars() const;
  116. // template <class T> T* GetComponent() const;
  117. Component* GetComponent(const String type) const;
  118. void SetID(unsigned id);
  119. void SetScene(Scene* scene);
  120. void ResetScene();
  121. bool Load(Deserializer& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  122. bool LoadXML(const XMLElement& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  123. Node* CreateChild(unsigned id, CreateMode mode);
  124. void AddComponent(Component* component, unsigned id, CreateMode mode);
  125. tolua_property__get_set unsigned ID;
  126. tolua_property__get_set String& name;
  127. tolua_readonly tolua_property__get_set StringHash nameHash;
  128. tolua_property__get_set Node* parent;
  129. tolua_property__get_set Scene* scene;
  130. tolua_property__is_set bool enabled;
  131. tolua_property__get_set Connection* owner;
  132. tolua_property__get_set Vector3& position;
  133. tolua_property__get_set Quaternion& rotation;
  134. tolua_property__get_set Vector3 direction;
  135. tolua_property__get_set Vector3& scale;
  136. tolua_readonly tolua_property__get_set Matrix3x4 transform;
  137. tolua_property__get_set Vector3 worldPosition;
  138. tolua_property__get_set Quaternion worldRotation;
  139. tolua_property__get_set Vector3 worldDirection;
  140. tolua_property__get_set Vector3 worldScale;
  141. tolua_readonly tolua_property__get_set Matrix3x4& worldTransform;
  142. tolua_readonly tolua_property__is_set bool dirty;
  143. tolua_readonly tolua_property__get_set unsigned numComponents;
  144. tolua_readonly tolua_property__get_set unsigned numNetworkComponents;
  145. };
  146. ${
  147. static bool NodeSaveXML(const Node* node, File* file)
  148. {
  149. return file ? node->SaveXML(*file) : false;
  150. }
  151. static void NodeSetPositionXYZ(Node* node, float x, float y, float z)
  152. {
  153. node->SetPosition(Vector3(x, y, z));
  154. }
  155. static void NodeSetRotationXYZ(Node* node, float x, float y, float z)
  156. {
  157. node->SetRotation(Quaternion(x, y, z));
  158. }
  159. static void NodeSetDirectionXYZ(Node* node, float x, float y, float z)
  160. {
  161. node->SetDirection(Vector3(x, y, z));
  162. }
  163. static void NodeSetScaleXYZ(Node* node, float x, float y, float z)
  164. {
  165. node->SetScale(Vector3(x, y, z));
  166. }
  167. static void NodeSetWorldPositionXYZ(Node* node, float x, float y, float z)
  168. {
  169. node->SetWorldPosition(Vector3(x, y, z));
  170. }
  171. static void NodeSetWorldRotationXYZ(Node* node, float x, float y, float z)
  172. {
  173. node->SetWorldRotation(Quaternion(x, y, z));
  174. }
  175. static void NodeSetWorldDirectionXYZ(Node* node, float x, float y, float z)
  176. {
  177. node->SetWorldDirection(Vector3(x, y, z));
  178. }
  179. static void NodeSetWorldScaleXYZ(Node* node, float x, float y, float z)
  180. {
  181. node->SetWorldScale(Vector3(x, y, z));
  182. }
  183. static void NodeTranslateXYZ(Node* node, float x, float y, float z)
  184. {
  185. node->Translate(Vector3(x, y, z));
  186. }
  187. static void NodeTranslateRelativeXYZ(Node* node, float x, float y, float z)
  188. {
  189. node->TranslateRelative(Vector3(x, y, z));
  190. }
  191. static void NodeRotateXYZ(Node* node, float x, float y, float z, bool fixedAxis = false)
  192. {
  193. node->Rotate(Quaternion(x, y, z), fixedAxis);
  194. }
  195. static void NodeLookAtXYZ(Node* node, float x, float y, float z, float upX = 0.0f, float upY = 1.0f, float upZ = 0.0f)
  196. {
  197. node->LookAt(Vector3(x, y, z), Vector3(upX, upY, upZ));
  198. }
  199. static void NodeScaleXYZ(Node* node, float x, float y, float z)
  200. {
  201. node->Scale(Vector3(x, y, z));
  202. }
  203. static void NodeGetPositionXYZ(const Node* node, float* x, float* y, float* z)
  204. {
  205. const Vector3& position = node->GetPosition();
  206. *x = position.x_;
  207. *y = position.y_;
  208. *z = position.z_;
  209. }
  210. static void NodeGetRotationXYZ(const Node* node, float* x, float* y, float* z)
  211. {
  212. const Quaternion& rotation = node->GetRotation();
  213. *x = rotation.x_;
  214. *y = rotation.y_;
  215. *z = rotation.z_;
  216. }
  217. static void NodeGetRotationWXYZ(const Node* node, float* w, float* x, float* y, float* z)
  218. {
  219. const Quaternion& rotation = node->GetRotation();
  220. *w = rotation.w_;
  221. *x = rotation.x_;
  222. *y = rotation.y_;
  223. *z = rotation.z_;
  224. }
  225. static void NodeGetDirectionXYZ(const Node* node, float* x, float* y, float* z)
  226. {
  227. const Vector3& direction = node->GetDirection();
  228. *x = direction.x_;
  229. *y = direction.y_;
  230. *z = direction.z_;
  231. }
  232. static void NodeGetScaleXYZ(const Node* node, float* x, float* y, float* z)
  233. {
  234. const Vector3& scale = node->GetScale();
  235. *x = scale.x_;
  236. *y = scale.y_;
  237. *z = scale.z_;
  238. }
  239. static void NodeGetWorldPositionXYZ(const Node* node, float* x, float* y, float* z)
  240. {
  241. Vector3 worldPosition = node->GetWorldPosition();
  242. *x = worldPosition.x_;
  243. *y = worldPosition.y_;
  244. *z = worldPosition.z_;
  245. }
  246. static void NodeGetWorldRotationXYZ(const Node* node, float* x, float* y, float* z)
  247. {
  248. Quaternion worldRotation = node->GetWorldRotation();
  249. *x = worldRotation.x_;
  250. *y = worldRotation.y_;
  251. *z = worldRotation.z_;
  252. }
  253. static void NodeGetWorldRotationWXYZ(const Node* node, float* w, float* x, float* y, float* z)
  254. {
  255. Quaternion worldRotation = node->GetWorldRotation();
  256. *w = worldRotation.w_;
  257. *x = worldRotation.x_;
  258. *y = worldRotation.y_;
  259. *z = worldRotation.z_;
  260. }
  261. static void NodeGetWorldDirectionXYZ(const Node* node, float* x, float* y, float* z)
  262. {
  263. Vector3 worldDirection = node->GetWorldDirection();
  264. *x = worldDirection.x_;
  265. *y = worldDirection.y_;
  266. *z = worldDirection.z_;
  267. }
  268. static void NodeGetWorldScaleXYZ(const Node* node, float* x, float* y, float* z)
  269. {
  270. Vector3 worldScale = node->GetWorldScale();
  271. *x = worldScale.x_;
  272. *y = worldScale.y_;
  273. *z = worldScale.z_;
  274. }
  275. // Disable generated CreateComponent funciton.
  276. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateComponent00
  277. static int tolua_SceneLuaAPI_Node_CreateComponent00(lua_State* tolua_S)
  278. {
  279. #ifndef TOLUA_RELEASE
  280. tolua_Error tolua_err;
  281. if (
  282. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  283. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  284. !tolua_isnumber(tolua_S,3,1,&tolua_err) ||
  285. !tolua_isnumber(tolua_S,4,1,&tolua_err) ||
  286. !tolua_isnoobj(tolua_S,5,&tolua_err)
  287. )
  288. goto tolua_lerror;
  289. else
  290. #endif
  291. {
  292. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  293. const String type = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  294. CreateMode mode = ((CreateMode) (int) tolua_tonumber(tolua_S,3,REPLICATED));
  295. unsigned id = ((unsigned) tolua_tonumber(tolua_S,4,0));
  296. #ifndef TOLUA_RELEASE
  297. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'CreateComponent'", NULL);
  298. #endif
  299. {
  300. Component* tolua_ret = (Component*) self->CreateComponent(type,mode,id);
  301. tolua_pushusertype(tolua_S,(void*)tolua_ret,type.CString());
  302. }
  303. }
  304. return 1;
  305. #ifndef TOLUA_RELEASE
  306. tolua_lerror:
  307. tolua_error(tolua_S,"#ferror in function 'CreateComponent'.",&tolua_err);
  308. return 0;
  309. #endif
  310. }
  311. // Disable generated GetComponent funciton.
  312. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetComponent00
  313. static int tolua_SceneLuaAPI_Node_GetComponent00(lua_State* tolua_S)
  314. {
  315. #ifndef TOLUA_RELEASE
  316. tolua_Error tolua_err;
  317. if (
  318. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  319. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  320. !tolua_isnoobj(tolua_S,3,&tolua_err)
  321. )
  322. goto tolua_lerror;
  323. else
  324. #endif
  325. {
  326. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  327. const String type = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  328. #ifndef TOLUA_RELEASE
  329. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetComponent'", NULL);
  330. #endif
  331. {
  332. Component* tolua_ret = (Component*) self->GetComponent(type);
  333. tolua_pushusertype(tolua_S,(void*)tolua_ret,type.CString());
  334. }
  335. }
  336. return 1;
  337. #ifndef TOLUA_RELEASE
  338. tolua_lerror:
  339. tolua_error(tolua_S,"#ferror in function 'GetComponent'.",&tolua_err);
  340. return 0;
  341. #endif
  342. }
  343. $}