2
0

Node.pkg 14 KB

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