Node.pkg 14 KB

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