Node.pkg 15 KB

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