Node.pkg 14 KB

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