NodeProxy.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include <string>
  22. #include <map>
  23. #include "../Macro.h"
  24. #include "base/CCRef.h"
  25. #include "base/ccTypes.h"
  26. #include "base/CCVector.h"
  27. #include "base/CCMap.h"
  28. #include "math/CCMath.h"
  29. #include "assembler/AssemblerBase.hpp"
  30. #include "MemPool.hpp"
  31. #include <functional>
  32. namespace se {
  33. class Object;
  34. }
  35. RENDERER_BEGIN
  36. class ModelBatcher;
  37. class Scene;
  38. struct TRS;
  39. struct ParentInfo;
  40. struct Skew;
  41. /**
  42. * @addtogroup scene
  43. * @{
  44. */
  45. /**
  46. * @brief NodeProxy is a cpp delegator of js Node.\n
  47. * It synchronize the hierarchy from js node tree, update the transform each frame, and manages assembler which represent the render component.\n
  48. * JS API: renderer.NodeProxy
  49. @code
  50. let node = new cc.Node();
  51. // NodeProxy is automatically created by cc.Node
  52. let proxy = node._proxy;
  53. @endcode
  54. */
  55. class NodeProxy : public Ref
  56. {
  57. public:
  58. typedef std::function<void(NodeProxy*, ModelBatcher*, Scene*)> TraverseFunc;
  59. /*
  60. * @brief Visit the node but do not transform position.
  61. */
  62. static void render(NodeProxy* node, ModelBatcher* batcher, Scene* scene);
  63. /*
  64. * @brief Visit the node as a ordinary node but not a root node.
  65. */
  66. static void visit(NodeProxy* node, ModelBatcher* batcher, Scene* scene);
  67. /*
  68. * @brief Reset global render order.
  69. */
  70. static void resetGlobalRenderOrder() { _globalRenderOrder = 0; }
  71. /*
  72. * @brief The default constructor.
  73. */
  74. NodeProxy(std::size_t unitID, std::size_t index, const std::string& id, const std::string& name);
  75. /*
  76. * @brief The destructor.
  77. */
  78. ~NodeProxy();
  79. /*
  80. * @brief destroy node data immediately .
  81. */
  82. void destroyImmediately();
  83. /*
  84. * @brief If js node has been destroy.
  85. */
  86. bool isValid() { return _trs != nullptr; }
  87. /// @{
  88. /// @name Hierarchy
  89. /**
  90. * @brief Adds child node proxy to the node proxy.
  91. * @param[in] child A child node proxy pointer.
  92. */
  93. void addChild(NodeProxy * child);
  94. /**
  95. * @brief Removes child node proxy from the node proxy.
  96. * @param[in] child A child node proxy pointer.
  97. */
  98. void removeChild(NodeProxy* child);
  99. /**
  100. * @brief Removes all child node proxies from the current one.
  101. */
  102. void removeAllChildren();
  103. /**
  104. * @brief Update parent by parent unitId and index.
  105. */
  106. void notifyUpdateParent();
  107. /**
  108. * @brief Sets the node proxy parent.
  109. * @param[in] parent.
  110. */
  111. inline void setParent(NodeProxy* parent) { _parent = parent; };
  112. /**
  113. * @brief Gets the node proxy parent.
  114. * @return Parent.
  115. */
  116. inline NodeProxy* getParent() const { return _parent; };
  117. /**
  118. * @brief Gets the node proxy all children.
  119. * @return Children container.
  120. */
  121. inline const Vector<NodeProxy*>& getChildren() const { return _children; };
  122. /**
  123. * @brief Gets the node proxy child count.
  124. * @return Child count.
  125. */
  126. inline size_t getChildrenCount() const { return _children.size(); };
  127. /**
  128. * @brief Gets a child node by name.
  129. * @return Child node.
  130. */
  131. NodeProxy* getChildByName(std::string childName);
  132. /**
  133. * @brief Gets a child node by runtime id.
  134. * @return Child node.
  135. */
  136. NodeProxy* getChildByID(std::string id);
  137. /**
  138. * @brief Sets the node proxy's local zorder.
  139. * @param[in] zOrder The value of zorder.
  140. */
  141. void setLocalZOrder(int zOrder);
  142. /// @} end of Hierarchy
  143. /*
  144. * @brief Gets the world matrix.
  145. * @return World matrix.
  146. */
  147. inline const cocos2d::Mat4& getWorldMatrix() const { return *_worldMat; };
  148. /*
  149. * @brief Gets the local matrix.
  150. * @return Local matrix.
  151. */
  152. inline const cocos2d::Mat4& getLocalMatrix() const { return *_localMat; };
  153. /*
  154. * @brief Gets the position.
  155. * @param[out] out The position vector
  156. */
  157. void getPosition(cocos2d::Vec3* out) const;
  158. /*
  159. * @brief Gets the rotation.
  160. * @param[out] out The rotation quaternion.
  161. */
  162. void getRotation(cocos2d::Quaternion* out) const;
  163. /*
  164. * @brief Gets the scale.
  165. * @param[out] out The scale vector.
  166. */
  167. void getScale(cocos2d::Vec3* out) const;
  168. /*
  169. * @brief Gets world rotation.
  170. * @param[out] out The rotation quaternion.
  171. */
  172. void getWorldRotation(cocos2d::Quaternion* out) const;
  173. /*
  174. * @brief Gets the position in world coordinates.
  175. * @param[out] out The world position vector.
  176. */
  177. void getWorldPosition(cocos2d::Vec3* out) const;
  178. /*
  179. * @brief Gets the matrix contains the world rotation and translation.
  180. * @param[out] out The matrix to store datas.
  181. */
  182. void getWorldRT(cocos2d::Mat4* out) const;
  183. /**
  184. * @brief Gets the node's opacity.
  185. */
  186. inline uint8_t getOpacity() const { return *_opacity; };
  187. /**
  188. * @brief Sets the node's opacity.
  189. */
  190. void setOpacity(uint8_t opacity);
  191. /**
  192. * @brief Updates opacity from parent.
  193. */
  194. void updateRealOpacity();
  195. /**
  196. * @brief Gets the node's realOpacity.
  197. */
  198. inline const uint8_t getRealOpacity() const {return _realOpacity;};
  199. /**
  200. * @brief Gets the node's group id, this controls which camera can see the node.
  201. */
  202. inline int getCullingMask() const { return *_cullingMask; };
  203. /**
  204. * @brief Sets the node's group id.
  205. * @param[in] groupID The group id
  206. */
  207. inline void setCullingMask(int cullingMask) { *_cullingMask = cullingMask; };
  208. /**
  209. * @brief Gets the node's name.
  210. * This equals to the one in JS node, helps to debug in cpp.
  211. * @return name
  212. */
  213. inline const std::string& getName() const { return _name; };
  214. /**
  215. * @brief Sets the node's name.
  216. * The name should be updated once JS node's name changes.
  217. * @param[in] name
  218. */
  219. inline void setName(const std::string& name) { _name = name; };
  220. /**
  221. * @brief Sets the node's 3D state.
  222. * @param[in] is3DNode
  223. */
  224. inline void set3DNode(bool is3DNode) { *_is3DNode = is3DNode ? 0x1 : 0x0; };
  225. /**
  226. * @brief Sets a system handle to the node proxy, system handle will be invoked during node's visit process.
  227. * @param[in] handle The system handle pointer.
  228. */
  229. void setAssembler(AssemblerBase* assembler);
  230. /**
  231. * @brief Removes a system handle from node proxy by system id.
  232. * @param[in] sysid The system id.
  233. */
  234. void clearAssembler();
  235. /**
  236. * @brief Gets the system handle by system id.
  237. * @param[in] sysid The system id.
  238. * @return The system handle object or nullptr if not exist
  239. */
  240. AssemblerBase* getAssembler() const;
  241. /*
  242. * @brief Enables visit.
  243. */
  244. void enableVisit(bool value) { _needVisit = value; }
  245. /*
  246. * @brief Disables visit.
  247. */
  248. void disableVisit() { _needVisit = false; }
  249. /*
  250. * @brief Updates local matrix.
  251. */
  252. void updateLocalMatrix();
  253. /*
  254. * @brief Updates world matrix.
  255. */
  256. void updateWorldMatrix();
  257. /*
  258. * @brief Updates world matrix with provide matrix.
  259. */
  260. void updateWorldMatrix(const cocos2d::Mat4& worldMatrix);
  261. /*
  262. * @brief Enables calc world matrix.
  263. */
  264. void enableUpdateWorldMatrix(bool value) { _updateWorldMatrix = value; }
  265. /*
  266. * @brief Gets node runtime id
  267. */
  268. const std::string& getID() const { return _id; }
  269. /*
  270. * @brief Gets node dirty
  271. */
  272. uint32_t* getDirty() const { return _dirty; }
  273. /*
  274. * @brief Is node flag dirty
  275. */
  276. bool isDirty(uint32_t flag) const { return *_dirty & flag; }
  277. /*
  278. * @brief Gets render order
  279. */
  280. uint32_t getRenderOrder () { return _renderOrder; }
  281. /*
  282. * @brief switch traverse interface to visit
  283. */
  284. void switchTraverseToVisit() { traverseHandle = visit; }
  285. /*
  286. * @brief switch traverse interface to render
  287. */
  288. void switchTraverseToRender() { traverseHandle = render; }
  289. /*
  290. * @brief traverse handle
  291. */
  292. TraverseFunc traverseHandle = nullptr;
  293. protected:
  294. void updateLevel();
  295. void childrenAlloc();
  296. void detachChild(NodeProxy* child, ssize_t childIndex);
  297. void reorderChildren();
  298. private:
  299. bool _needVisit = true;
  300. bool _updateWorldMatrix = true;
  301. bool _needRender = false;
  302. uint8_t _realOpacity = 255;
  303. std::string _id = "";
  304. std::string _name = "";
  305. std::size_t _level = 0;
  306. uint32_t* _dirty = nullptr;
  307. TRS* _trs = nullptr;
  308. cocos2d::Mat4* _localMat = nullptr;
  309. cocos2d::Mat4* _worldMat = nullptr;
  310. ParentInfo* _parentInfo = nullptr;
  311. int32_t* _localZOrder = nullptr;
  312. int32_t* _cullingMask = nullptr;
  313. uint8_t* _opacity = nullptr;
  314. uint8_t* _is3DNode = nullptr;
  315. Skew* _skew = nullptr;
  316. std::size_t _unitID = 0;
  317. std::size_t _index = 0;
  318. Sign* _signData = nullptr;
  319. NodeProxy* _parent = nullptr; ///< weak reference to parent node
  320. cocos2d::Vector<NodeProxy*> _children; ///< array of children nodes
  321. AssemblerBase* _assembler = nullptr;
  322. uint32_t _renderOrder = 0;
  323. static uint32_t _globalRenderOrder;
  324. };
  325. // end of scene group
  326. /// @}
  327. RENDERER_END