Node.pkg 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. $#include "File.h"
  2. $#include "LuaFile.h"
  3. $#include "LuaScriptInstance.h"
  4. $#include "Node.h"
  5. $#include "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;
  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 SetEnabled(bool enable, bool recursive);
  70. void SetOwner(Connection* owner);
  71. void MarkDirty();
  72. Node* CreateChild(const String name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0);
  73. void AddChild(Node* node, unsigned index = M_MAX_UNSIGNED);
  74. void RemoveChild(Node* node);
  75. void RemoveAllChildren();
  76. void RemoveChildren(bool removeReplicated, bool removeLocal, bool recursive);
  77. void RemoveComponent(Component* component);
  78. void RemoveComponent(StringHash type);
  79. void RemoveComponent(const String type);
  80. void RemoveAllComponents();
  81. void RemoveComponents(bool removeReplicated, bool removeLocal);
  82. Node* Clone(CreateMode mode = REPLICATED);
  83. void Remove();
  84. void SetParent(Node* parent);
  85. void SetVar(StringHash key, const Variant& value);
  86. void AddListener(Component* component);
  87. void RemoveListener(Component* component);
  88. // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  89. Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
  90. Component* CloneComponent(Component* component, unsigned id = 0);
  91. Component* CloneComponent(Component* component, CreateMode mode, unsigned id = 0);
  92. int CreateScriptObject(const String scriptObjectType);
  93. int CreateScriptObject(const String fileName, const String scriptObjectType);
  94. int GetScriptObject() const;
  95. int GetScriptObject(const String scriptObjectType) const;
  96. unsigned GetID() const;
  97. const String GetName() const;
  98. StringHash GetNameHash() const;
  99. Node* GetParent() const;
  100. Scene* GetScene() const;
  101. bool IsEnabled() const;
  102. Connection* GetOwner() const;
  103. const Vector3& GetPosition() const;
  104. Vector2 GetPosition2D() const;
  105. const Quaternion& GetRotation() const;
  106. float GetRotation2D() const;
  107. Vector3 GetDirection() const;
  108. Vector3 GetUp() const;
  109. Vector3 GetRight() const;
  110. const Vector3& GetScale() const;
  111. Vector2 GetScale2D() const;
  112. Matrix3x4 GetTransform() const;
  113. Vector3 GetWorldPosition() const;
  114. Vector2 GetWorldPosition2D() const;
  115. Quaternion GetWorldRotation() const;
  116. float GetWorldRotation2D() const;
  117. Vector3 GetWorldDirection() const;
  118. Vector3 GetWorldUp() const;
  119. Vector3 GetWorldRight() const;
  120. Vector3 GetWorldScale() const;
  121. Vector2 GetWorldScale2D() const;
  122. const Matrix3x4& GetWorldTransform() const;
  123. Vector3 LocalToWorld(const Vector3& position) const;
  124. Vector3 LocalToWorld(const Vector4& vector) const;
  125. Vector2 LocalToWorld2D(const Vector2& vector) const;
  126. Vector3 WorldToLocal(const Vector3& position) const;
  127. Vector3 WorldToLocal(const Vector4& vector) const;
  128. Vector2 WorldToLocal2D(const Vector2& vector) const;
  129. bool IsDirty() const;
  130. unsigned GetNumChildren(bool recursive = false) const;
  131. Node* GetChild(const String name, bool recursive = false) const;
  132. Node* GetChild(StringHash nameHash, bool recursive = false) const;
  133. Node* GetChild(unsigned index) const;
  134. unsigned GetNumComponents() const;
  135. unsigned GetNumNetworkComponents() const;
  136. bool HasComponent(StringHash type) const;
  137. bool HasComponent(const String type) const;
  138. const Variant& GetVar(StringHash key) const;
  139. const VariantMap& GetVars() const;
  140. // template <class T> T* GetComponent() const;
  141. Component* GetComponent(const String type) const;
  142. void SetID(unsigned id);
  143. void SetScene(Scene* scene);
  144. void ResetScene();
  145. bool Load(Deserializer& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  146. bool LoadXML(const XMLElement& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  147. Node* CreateChild(unsigned id, CreateMode mode);
  148. void AddComponent(Component* component, unsigned id, CreateMode mode);
  149. tolua_property__get_set unsigned ID;
  150. tolua_property__get_set String name;
  151. tolua_readonly tolua_property__get_set StringHash nameHash;
  152. tolua_property__get_set Node* parent;
  153. tolua_property__get_set Scene* scene;
  154. tolua_property__is_set bool enabled;
  155. tolua_property__get_set Connection* owner;
  156. tolua_property__get_set Vector3& position;
  157. tolua_property__get_set Vector2 position2D;
  158. tolua_property__get_set Quaternion& rotation;
  159. tolua_property__get_set float rotation2D;
  160. tolua_property__get_set Vector3 direction;
  161. tolua_readonly tolua_property__get_set Vector3 up;
  162. tolua_readonly tolua_property__get_set Vector3 right;
  163. tolua_property__get_set Vector3& scale;
  164. tolua_property__get_set Vector2 scale2D;
  165. tolua_readonly tolua_property__get_set Matrix3x4 transform;
  166. tolua_property__get_set Vector3 worldPosition;
  167. tolua_property__get_set Vector2 worldPosition2D;
  168. tolua_property__get_set Quaternion worldRotation;
  169. tolua_property__get_set float worldRotation2D;
  170. tolua_property__get_set Vector3 worldDirection;
  171. tolua_readonly tolua_property__get_set Vector3 worldUp;
  172. tolua_readonly tolua_property__get_set Vector3 worldRight;
  173. tolua_property__get_set Vector3 worldScale;
  174. tolua_property__get_set Vector2 worldScale2D;
  175. tolua_readonly tolua_property__get_set Matrix3x4& worldTransform;
  176. tolua_readonly tolua_property__is_set bool dirty;
  177. tolua_readonly tolua_property__get_set unsigned numComponents;
  178. tolua_readonly tolua_property__get_set unsigned numNetworkComponents;
  179. };
  180. ${
  181. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00
  182. static int tolua_SceneLuaAPI_Node_new00(lua_State* tolua_S)
  183. {
  184. return ToluaNewObject<Node>(tolua_S);
  185. }
  186. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00_local
  187. static int tolua_SceneLuaAPI_Node_new00_local(lua_State* tolua_S)
  188. {
  189. return ToluaNewObjectGC<Node>(tolua_S);
  190. }
  191. static bool NodeSaveXML(const Node* node, File* file)
  192. {
  193. return file ? node->SaveXML(*file) : false;
  194. }
  195. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject00
  196. static int tolua_SceneLuaAPI_Node_CreateScriptObject00(lua_State* tolua_S)
  197. {
  198. #ifndef TOLUA_RELEASE
  199. tolua_Error tolua_err;
  200. if (
  201. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  202. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  203. !tolua_isnoobj(tolua_S,3,&tolua_err)
  204. )
  205. goto tolua_lerror;
  206. else
  207. #endif
  208. {
  209. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  210. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  211. #ifndef TOLUA_RELEASE
  212. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  213. #endif
  214. {
  215. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  216. if (!instance)
  217. lua_pushnil(tolua_S);
  218. else
  219. {
  220. instance->CreateObject(scriptObjectType);
  221. // Push script object to Lua stack.
  222. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  223. }
  224. }
  225. }
  226. return 1;
  227. #ifndef TOLUA_RELEASE
  228. tolua_lerror:
  229. tolua_error(tolua_S,"#ferror in function 'CreateScriptObject'.",&tolua_err);
  230. return 0;
  231. #endif
  232. }
  233. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject01
  234. static int tolua_SceneLuaAPI_Node_CreateScriptObject01(lua_State* tolua_S)
  235. {
  236. tolua_Error tolua_err;
  237. if (
  238. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  239. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  240. !tolua_isurho3dstring(tolua_S,3,0,&tolua_err) ||
  241. !tolua_isnoobj(tolua_S,4,&tolua_err)
  242. )
  243. goto tolua_lerror;
  244. else
  245. {
  246. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  247. const String fileName = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  248. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,3,0));
  249. #ifndef TOLUA_RELEASE
  250. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  251. #endif
  252. {
  253. ResourceCache* cache = self->GetSubsystem<ResourceCache>();
  254. LuaFile* scriptFile = cache->GetResource<LuaFile>(fileName);
  255. if (!scriptFile)
  256. {
  257. lua_pushnil(tolua_S);
  258. return 1;
  259. }
  260. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  261. if (!instance)
  262. lua_pushnil(tolua_S);
  263. else
  264. {
  265. instance->CreateObject(scriptFile, scriptObjectType);
  266. // Push script object to Lua stack.
  267. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  268. }
  269. }
  270. }
  271. return 1;
  272. tolua_lerror:
  273. return tolua_SceneLuaAPI_Node_CreateScriptObject00(tolua_S);
  274. }
  275. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject00
  276. static int tolua_SceneLuaAPI_Node_GetScriptObject00(lua_State* tolua_S)
  277. {
  278. #ifndef TOLUA_RELEASE
  279. tolua_Error tolua_err;
  280. if (
  281. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  282. !tolua_isnoobj(tolua_S,2,&tolua_err)
  283. )
  284. goto tolua_lerror;
  285. else
  286. #endif
  287. {
  288. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  289. #ifndef TOLUA_RELEASE
  290. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  291. #endif
  292. {
  293. LuaScriptInstance* instance = self->GetComponent<LuaScriptInstance>();
  294. if (!instance)
  295. lua_pushnil(tolua_S);
  296. else
  297. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  298. }
  299. }
  300. return 1;
  301. #ifndef TOLUA_RELEASE
  302. tolua_lerror:
  303. tolua_error(tolua_S,"#ferror in function 'GetScriptObject'.",&tolua_err);
  304. return 0;
  305. #endif
  306. }
  307. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject01
  308. static int tolua_SceneLuaAPI_Node_GetScriptObject01(lua_State* tolua_S)
  309. {
  310. tolua_Error tolua_err;
  311. if (
  312. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  313. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  314. !tolua_isnoobj(tolua_S,3,&tolua_err)
  315. )
  316. goto tolua_lerror;
  317. else
  318. {
  319. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  320. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  321. #ifndef TOLUA_RELEASE
  322. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  323. #endif
  324. {
  325. int scriptObjectRef = LUA_REFNIL;
  326. PODVector<LuaScriptInstance*> instances;
  327. self->GetComponents<LuaScriptInstance>(instances, false);
  328. for (unsigned i = 0; i < instances.Size(); ++i)
  329. {
  330. if (instances[i]->GetScriptObjectType() == scriptObjectType)
  331. {
  332. scriptObjectRef = instances[i]->GetScriptObjectRef();
  333. break;
  334. }
  335. }
  336. if (scriptObjectRef == LUA_REFNIL)
  337. lua_pushnil(tolua_S);
  338. else
  339. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, scriptObjectRef);
  340. }
  341. }
  342. return 1;
  343. tolua_lerror:
  344. return tolua_SceneLuaAPI_Node_GetScriptObject00(tolua_S);
  345. }
  346. $}