Node.pkg 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 SetPosition(const Vector2& position);
  25. void SetRotation(const Quaternion& rotation);
  26. void SetRotation(float rotation);
  27. void SetDirection(const Vector3& direction);
  28. void SetScale(float scale);
  29. void SetScale(const Vector3& scale);
  30. void SetScale(const Vector2& scale);
  31. void SetTransform(const Vector3& position, const Quaternion& rotation);
  32. void SetTransform(const Vector2& position, float rotation);
  33. void SetTransform(const Vector3& position, const Quaternion& rotation, float scale);
  34. void SetTransform(const Vector2& position, float rotation, float scale);
  35. void SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  36. void SetTransform(const Vector2& position, float rotation, const Vector2& scale);
  37. void SetWorldPosition(const Vector3& position);
  38. void SetWorldPosition(const Vector2& position);
  39. void SetWorldRotation(const Quaternion& rotation);
  40. void SetWorldRotation(float rotation);
  41. void SetWorldDirection(const Vector3& direction);
  42. void SetWorldScale(float scale);
  43. void SetWorldScale(const Vector3& scale);
  44. void SetWorldScale(const Vector2& scale);
  45. void SetWorldTransform(const Vector3& position, const Quaternion& rotation);
  46. void SetWorldTransform(const Vector2& position, float rotation);
  47. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, float scale);
  48. void SetWorldTransform(const Vector2& position, float rotation, float scale);
  49. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  50. void SetWorldTransform(const Vector2& position, float rotation, const Vector3& scale);
  51. void Translate(const Vector3& delta, TransformSpace space = TS_LOCAL);
  52. void Translate(const Vector2& delta, TransformSpace space = TS_LOCAL);
  53. void Rotate(const Quaternion& delta, TransformSpace space = TS_LOCAL);
  54. void Rotate(float delta, TransformSpace space = TS_LOCAL);
  55. void RotateAround(const Vector3& point, const Quaternion& delta, TransformSpace space = TS_LOCAL);
  56. void RotateAround(const Vector2& point, float delta, TransformSpace space = TS_LOCAL);
  57. void Pitch(float angle, TransformSpace space = TS_LOCAL);
  58. void Yaw(float angle, TransformSpace space = TS_LOCAL);
  59. void Roll(float angle, TransformSpace space = TS_LOCAL);
  60. bool LookAt(const Vector3& target, const Vector3& upAxis = Vector3::UP, TransformSpace space = TS_WORLD);
  61. void Scale(float scale);
  62. void Scale(const Vector3& scale);
  63. void Scale(const Vector2& scale);
  64. void SetEnabled(bool enable);
  65. void SetEnabled(bool enable, bool recursive);
  66. void SetOwner(Connection* owner);
  67. void MarkDirty();
  68. Node* CreateChild(const String name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0);
  69. void AddChild(Node* node, unsigned index = M_MAX_UNSIGNED);
  70. void RemoveChild(Node* node);
  71. void RemoveAllChildren();
  72. void RemoveChildren(bool removeReplicated, bool removeLocal, bool recursive);
  73. void RemoveComponent(Component* component);
  74. void RemoveComponent(ShortStringHash type);
  75. void RemoveComponent(const String type);
  76. void RemoveAllComponents();
  77. void RemoveComponents(bool removeReplicated, bool removeLocal);
  78. Node* Clone(CreateMode mode = REPLICATED);
  79. void Remove();
  80. void SetParent(Node* parent);
  81. void SetVar(ShortStringHash key, const Variant& value);
  82. void AddListener(Component* component);
  83. void RemoveListener(Component* component);
  84. // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  85. Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
  86. int CreateScriptObject(const String scriptObjectType);
  87. int CreateScriptObject(const String fileName, const String scriptObjectType);
  88. int GetScriptObject() const;
  89. int GetScriptObject(const String scriptObjectType) const;
  90. unsigned GetID() const;
  91. const String GetName() const;
  92. StringHash GetNameHash() const;
  93. Node* GetParent() const;
  94. Scene* GetScene() const;
  95. bool IsEnabled() const;
  96. Connection* GetOwner() const;
  97. const Vector3& GetPosition() const;
  98. const Quaternion& GetRotation() const;
  99. Vector3 GetDirection() const;
  100. Vector3 GetUp() const;
  101. Vector3 GetRight() const;
  102. const Vector3& GetScale() const;
  103. Matrix3x4 GetTransform() const;
  104. Vector3 GetWorldPosition() const;
  105. Quaternion GetWorldRotation() const;
  106. Vector3 GetWorldDirection() const;
  107. Vector3 GetWorldUp() const;
  108. Vector3 GetWorldRight() const;
  109. Vector3 GetWorldScale() const;
  110. const Matrix3x4& GetWorldTransform() const;
  111. Vector3 LocalToWorld(const Vector3& position) const;
  112. Vector3 LocalToWorld(const Vector4& vector) const;
  113. Vector2 LocalToWorld(const Vector2& vector) const;
  114. Vector3 WorldToLocal(const Vector3& position) const;
  115. Vector3 WorldToLocal(const Vector4& vector) const;
  116. Vector2 WorldToLocal(const Vector2& vector) const;
  117. bool IsDirty() const;
  118. unsigned GetNumChildren(bool recursive = false) const;
  119. Node* GetChild(const String name, bool recursive = false) const;
  120. Node* GetChild(StringHash nameHash, bool recursive = false) const;
  121. Node* GetChild(unsigned index) const;
  122. unsigned GetNumComponents() const;
  123. unsigned GetNumNetworkComponents() const;
  124. bool HasComponent(ShortStringHash type) const;
  125. bool HasComponent(const String type) const;
  126. const Variant& GetVar(ShortStringHash key) const;
  127. const VariantMap& GetVars() const;
  128. // template <class T> T* GetComponent() const;
  129. Component* GetComponent(const String type) const;
  130. void SetID(unsigned id);
  131. void SetScene(Scene* scene);
  132. void ResetScene();
  133. bool Load(Deserializer& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  134. bool LoadXML(const XMLElement& source, SceneResolver& resolver, bool loadChildren = true, bool rewriteIDs = false, CreateMode mode = REPLICATED);
  135. Node* CreateChild(unsigned id, CreateMode mode);
  136. void AddComponent(Component* component, unsigned id, CreateMode mode);
  137. tolua_property__get_set unsigned ID;
  138. tolua_property__get_set String name;
  139. tolua_readonly tolua_property__get_set StringHash nameHash;
  140. tolua_property__get_set Node* parent;
  141. tolua_property__get_set Scene* scene;
  142. tolua_property__is_set bool enabled;
  143. tolua_property__get_set Connection* owner;
  144. tolua_property__get_set Vector3& position;
  145. tolua_property__get_set Quaternion& rotation;
  146. tolua_property__get_set Vector3 direction;
  147. tolua_readonly tolua_property__get_set Vector3 up;
  148. tolua_readonly tolua_property__get_set Vector3 right;
  149. tolua_property__get_set Vector3& scale;
  150. tolua_readonly tolua_property__get_set Matrix3x4 transform;
  151. tolua_property__get_set Vector3 worldPosition;
  152. tolua_property__get_set Quaternion worldRotation;
  153. tolua_property__get_set Vector3 worldDirection;
  154. tolua_readonly tolua_property__get_set Vector3 worldUp;
  155. tolua_readonly tolua_property__get_set Vector3 worldRight;
  156. tolua_property__get_set Vector3 worldScale;
  157. tolua_readonly tolua_property__get_set Matrix3x4& worldTransform;
  158. tolua_readonly tolua_property__is_set bool dirty;
  159. tolua_readonly tolua_property__get_set unsigned numComponents;
  160. tolua_readonly tolua_property__get_set unsigned numNetworkComponents;
  161. };
  162. ${
  163. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00
  164. static int tolua_SceneLuaAPI_Node_new00(lua_State* tolua_S)
  165. {
  166. return ToluaNewObject<Node>(tolua_S);
  167. }
  168. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_new00_local
  169. static int tolua_SceneLuaAPI_Node_new00_local(lua_State* tolua_S)
  170. {
  171. return ToluaNewObjectGC<Node>(tolua_S);
  172. }
  173. static bool NodeSaveXML(const Node* node, File* file)
  174. {
  175. return file ? node->SaveXML(*file) : false;
  176. }
  177. // Disable generated CreateComponent function.
  178. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateComponent00
  179. static int tolua_SceneLuaAPI_Node_CreateComponent00(lua_State* tolua_S)
  180. {
  181. #ifndef TOLUA_RELEASE
  182. tolua_Error tolua_err;
  183. if (
  184. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  185. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  186. !tolua_isnumber(tolua_S,3,1,&tolua_err) ||
  187. !tolua_isnumber(tolua_S,4,1,&tolua_err) ||
  188. !tolua_isnoobj(tolua_S,5,&tolua_err)
  189. )
  190. goto tolua_lerror;
  191. else
  192. #endif
  193. {
  194. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  195. const String type = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  196. CreateMode mode = ((CreateMode) (int) tolua_tonumber(tolua_S,3,REPLICATED));
  197. unsigned id = ((unsigned) tolua_tonumber(tolua_S,4,0));
  198. #ifndef TOLUA_RELEASE
  199. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'CreateComponent'", NULL);
  200. #endif
  201. {
  202. Component* tolua_ret = (Component*) self->CreateComponent(type,mode,id);
  203. tolua_pushusertype(tolua_S,(void*)tolua_ret,type.CString());
  204. }
  205. }
  206. return 1;
  207. #ifndef TOLUA_RELEASE
  208. tolua_lerror:
  209. tolua_error(tolua_S,"#ferror in function 'CreateComponent'.",&tolua_err);
  210. return 0;
  211. #endif
  212. }
  213. // Disable generated GetComponent function.
  214. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetComponent00
  215. static int tolua_SceneLuaAPI_Node_GetComponent00(lua_State* tolua_S)
  216. {
  217. #ifndef TOLUA_RELEASE
  218. tolua_Error tolua_err;
  219. if (
  220. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  221. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  222. !tolua_isnoobj(tolua_S,3,&tolua_err)
  223. )
  224. goto tolua_lerror;
  225. else
  226. #endif
  227. {
  228. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  229. const String type = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  230. #ifndef TOLUA_RELEASE
  231. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetComponent'", NULL);
  232. #endif
  233. {
  234. Component* tolua_ret = (Component*) self->GetComponent(type);
  235. tolua_pushusertype(tolua_S,(void*)tolua_ret,type.CString());
  236. }
  237. }
  238. return 1;
  239. #ifndef TOLUA_RELEASE
  240. tolua_lerror:
  241. tolua_error(tolua_S,"#ferror in function 'GetComponent'.",&tolua_err);
  242. return 0;
  243. #endif
  244. }
  245. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject00
  246. static int tolua_SceneLuaAPI_Node_CreateScriptObject00(lua_State* tolua_S)
  247. {
  248. #ifndef TOLUA_RELEASE
  249. tolua_Error tolua_err;
  250. if (
  251. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  252. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  253. !tolua_isnoobj(tolua_S,3,&tolua_err)
  254. )
  255. goto tolua_lerror;
  256. else
  257. #endif
  258. {
  259. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  260. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  261. #ifndef TOLUA_RELEASE
  262. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  263. #endif
  264. {
  265. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  266. if (!instance)
  267. lua_pushnil(tolua_S);
  268. else
  269. {
  270. instance->CreateObject(scriptObjectType);
  271. // Push script object to Lua stack.
  272. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  273. }
  274. }
  275. }
  276. return 1;
  277. #ifndef TOLUA_RELEASE
  278. tolua_lerror:
  279. tolua_error(tolua_S,"#ferror in function 'CreateScriptObject'.",&tolua_err);
  280. return 0;
  281. #endif
  282. }
  283. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject01
  284. static int tolua_SceneLuaAPI_Node_CreateScriptObject01(lua_State* tolua_S)
  285. {
  286. tolua_Error tolua_err;
  287. if (
  288. !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
  289. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  290. !tolua_isurho3dstring(tolua_S,3,0,&tolua_err) ||
  291. !tolua_isnoobj(tolua_S,4,&tolua_err)
  292. )
  293. goto tolua_lerror;
  294. else
  295. {
  296. Node* self = (Node*) tolua_tousertype(tolua_S,1,0);
  297. const String fileName = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  298. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,3,0));
  299. #ifndef TOLUA_RELEASE
  300. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
  301. #endif
  302. {
  303. ResourceCache* cache = self->GetSubsystem<ResourceCache>();
  304. LuaFile* scriptFile = cache->GetResource<LuaFile>(fileName);
  305. if (!scriptFile)
  306. {
  307. lua_pushnil(tolua_S);
  308. return 1;
  309. }
  310. LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
  311. if (!instance)
  312. lua_pushnil(tolua_S);
  313. else
  314. {
  315. instance->CreateObject(scriptFile, scriptObjectType);
  316. // Push script object to Lua stack.
  317. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  318. }
  319. }
  320. }
  321. return 1;
  322. tolua_lerror:
  323. return tolua_SceneLuaAPI_Node_CreateScriptObject00(tolua_S);
  324. }
  325. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject00
  326. static int tolua_SceneLuaAPI_Node_GetScriptObject00(lua_State* tolua_S)
  327. {
  328. #ifndef TOLUA_RELEASE
  329. tolua_Error tolua_err;
  330. if (
  331. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  332. !tolua_isnoobj(tolua_S,2,&tolua_err)
  333. )
  334. goto tolua_lerror;
  335. else
  336. #endif
  337. {
  338. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  339. #ifndef TOLUA_RELEASE
  340. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  341. #endif
  342. {
  343. LuaScriptInstance* instance = self->GetComponent<LuaScriptInstance>();
  344. if (!instance)
  345. lua_pushnil(tolua_S);
  346. else
  347. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  348. }
  349. }
  350. return 1;
  351. #ifndef TOLUA_RELEASE
  352. tolua_lerror:
  353. tolua_error(tolua_S,"#ferror in function 'GetScriptObject'.",&tolua_err);
  354. return 0;
  355. #endif
  356. }
  357. #define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject01
  358. static int tolua_SceneLuaAPI_Node_GetScriptObject01(lua_State* tolua_S)
  359. {
  360. tolua_Error tolua_err;
  361. if (
  362. !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
  363. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  364. !tolua_isnoobj(tolua_S,3,&tolua_err)
  365. )
  366. goto tolua_lerror;
  367. else
  368. {
  369. const Node* self = (const Node*) tolua_tousertype(tolua_S,1,0);
  370. const String scriptObjectType = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  371. #ifndef TOLUA_RELEASE
  372. if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
  373. #endif
  374. {
  375. int scriptObjectRef = LUA_REFNIL;
  376. PODVector<LuaScriptInstance*> instances;
  377. self->GetComponents<LuaScriptInstance>(instances, false);
  378. for (unsigned i = 0; i < instances.Size(); ++i)
  379. {
  380. if (instances[i]->GetScriptObjectType() == scriptObjectType)
  381. {
  382. scriptObjectRef = instances[i]->GetScriptObjectRef();
  383. break;
  384. }
  385. }
  386. if (scriptObjectRef == LUA_REFNIL)
  387. lua_pushnil(tolua_S);
  388. else
  389. lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, scriptObjectRef);
  390. }
  391. }
  392. return 1;
  393. tolua_lerror:
  394. return tolua_SceneLuaAPI_Node_GetScriptObject00(tolua_S);
  395. }
  396. $}