UILayout.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __LAYOUT_H__
  22. #define __LAYOUT_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. #include "renderer/CCCustomCommand.h"
  26. #include "renderer/CCGroupCommand.h"
  27. /**
  28. * @addtogroup ui
  29. * @{
  30. */
  31. NS_CC_BEGIN
  32. class DrawNode;
  33. class LayerColor;
  34. class LayerGradient;
  35. class StencilStateManager;
  36. struct CC_DLL ResourceData;
  37. namespace ui {
  38. class LayoutManager;
  39. class Scale9Sprite;
  40. /**
  41. *@brief Layout interface for creating LayoutManger and do actual layout.
  42. * @js NA
  43. */
  44. class CC_GUI_DLL LayoutProtocol
  45. {
  46. public:
  47. /**
  48. *@brief Default constructor.
  49. */
  50. LayoutProtocol(){}
  51. /**
  52. *@brief Default destructor.
  53. */
  54. virtual ~LayoutProtocol(){}
  55. /**
  56. * @brief Create a custom layout manager.
  57. *
  58. * @return A LayoutManager descendants instance.
  59. */
  60. virtual LayoutManager* createLayoutManager() = 0;
  61. /**
  62. * @brief Return the content size of layout.
  63. *
  64. * @return A content size in Size.
  65. */
  66. virtual Size getLayoutContentSize()const = 0;
  67. /**
  68. * @brief Get all elements of the layout.
  69. *
  70. * @return A vector of Node pointers.
  71. */
  72. virtual const Vector<Node*>& getLayoutElements()const = 0;
  73. /**
  74. * @brief The main function to do the layout job.
  75. * Different layout manager should implement its own layout algorithm.
  76. *
  77. */
  78. virtual void doLayout() = 0;
  79. };
  80. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  81. #ifdef RELATIVE
  82. #undef RELATIVE
  83. #endif
  84. #endif
  85. /**
  86. *@brief A container for holding a few child widgets.
  87. *
  88. * The child widgets could be rearranged according to the layout type and it also enables clipping, set background image and color.
  89. *
  90. * There are mainly four types of layout:
  91. * - Absolute layout: This the default layout type, child elements are arranged by their absolute position.
  92. * - Horizontal layout: child elements are arranged horizontally.
  93. * - Vertical layout: child elements are arranged vertically.
  94. * - Relative layout: child elements are arranged relative to certain rules.
  95. *
  96. */
  97. class CC_GUI_DLL Layout : public Widget, public LayoutProtocol
  98. {
  99. DECLARE_CLASS_GUI_INFO
  100. public:
  101. /**
  102. * Layout type, default is ABSOLUTE.
  103. */
  104. enum class Type
  105. {
  106. ABSOLUTE,
  107. VERTICAL,
  108. HORIZONTAL,
  109. RELATIVE
  110. };
  111. /**
  112. * Clipping Type, default is STENCIL.
  113. */
  114. enum class ClippingType
  115. {
  116. STENCIL,
  117. SCISSOR
  118. };
  119. /**
  120. * Background color type, default is NONE.
  121. */
  122. enum class BackGroundColorType
  123. {
  124. NONE,
  125. SOLID,
  126. GRADIENT
  127. };
  128. /**
  129. * Default constructor
  130. * @js ctor
  131. * @lua new
  132. */
  133. Layout();
  134. /**
  135. * Default destructor
  136. * @js NA
  137. * @lua NA
  138. */
  139. virtual ~Layout();
  140. /**
  141. * Create a empty layout.
  142. */
  143. static Layout* create();
  144. /**
  145. * Sets a background image for layout.
  146. *
  147. * @param fileName image file path.
  148. * @param texType @see TextureResType.
  149. */
  150. void setBackGroundImage(const std::string& fileName,TextureResType texType = TextureResType::LOCAL);
  151. /**
  152. * Sets a background image capinsets for layout, it only affects the scale9 enabled background image
  153. *
  154. * @param capInsets The capInsets in Rect.
  155. *
  156. */
  157. void setBackGroundImageCapInsets(const Rect& capInsets);
  158. /**
  159. * Query background image's capInsets size.
  160. *@return The background image capInsets.
  161. */
  162. const Rect& getBackGroundImageCapInsets()const;
  163. /**
  164. * Sets Color Type for layout's background
  165. *
  166. * @param type @see `BackGroundColorType`
  167. */
  168. void setBackGroundColorType(BackGroundColorType type);
  169. /**
  170. * Query the layout's background color type.
  171. *@return The layout's background color type.
  172. */
  173. BackGroundColorType getBackGroundColorType()const;
  174. /**
  175. * Enable background image scale9 rendering.
  176. *
  177. * @param enabled True means enable scale9 rendering for background image, false otherwise.
  178. */
  179. void setBackGroundImageScale9Enabled(bool enabled);
  180. /**
  181. * Query background image scale9 enable status.
  182. *@return Whether background image is scale9 enabled or not.
  183. */
  184. bool isBackGroundImageScale9Enabled()const;
  185. /**
  186. * Set background color for layout
  187. * The color only applies to layout when it's color type is BackGroundColorType::SOLIDE
  188. *
  189. * @param color Color in Color3B.
  190. */
  191. void setBackGroundColor(const Color3B &color);
  192. /**
  193. * Query the layout's background color.
  194. *@return Background color in Color3B.
  195. */
  196. const Color3B& getBackGroundColor()const;
  197. /**
  198. * Set start and end background color for layout.
  199. * This setting only take effect when the layout's color type is BackGroundColorType::GRADIENT
  200. *
  201. * @param startColor Color value in Color3B.
  202. * @param endColor Color value in Color3B.
  203. */
  204. void setBackGroundColor(const Color3B &startColor, const Color3B &endColor);
  205. /**
  206. * Get the gradient background start color.
  207. *@return Gradient background start color value.
  208. */
  209. const Color3B& getBackGroundStartColor()const;
  210. /**
  211. * Get the gradient background end color.
  212. * @return Gradient background end color value.
  213. */
  214. const Color3B& getBackGroundEndColor()const;
  215. /**
  216. * Sets background color opacity of layout.
  217. *
  218. * @param opacity The opacity in `GLubyte`.
  219. */
  220. void setBackGroundColorOpacity(GLubyte opacity);
  221. /**
  222. * Get the layout's background color opacity.
  223. *@return Background color opacity value.
  224. */
  225. GLubyte getBackGroundColorOpacity()const;
  226. /**
  227. * Sets background color vector for layout.
  228. * This setting only take effect when layout's color type is BackGroundColorType::GRADIENT
  229. *
  230. * @param vector The color vector in `Vec2`.
  231. */
  232. void setBackGroundColorVector(const Vec2 &vector);
  233. /**
  234. * Get the layout's background color vector.
  235. *@return Background color vector.
  236. */
  237. const Vec2& getBackGroundColorVector()const;
  238. /**
  239. * Set layout's background image color.
  240. *@param color Background color value in `Color3B`.
  241. */
  242. void setBackGroundImageColor(const Color3B& color);
  243. /**
  244. * Set opacity of background image.
  245. *@param opacity Background image opacity in GLubyte.
  246. */
  247. void setBackGroundImageOpacity(GLubyte opacity);
  248. /**
  249. * Get color of layout's background image.
  250. *@return Layout's background image color.
  251. */
  252. const Color3B& getBackGroundImageColor()const;
  253. /**
  254. * Get the opacity of layout's background image.
  255. * @return The opacity of layout's background image.
  256. */
  257. GLubyte getBackGroundImageOpacity()const;
  258. /**
  259. * Remove the background image of layout.
  260. */
  261. void removeBackGroundImage();
  262. /**
  263. * Gets background image texture size.
  264. *
  265. * @return background image texture size.
  266. */
  267. const Size& getBackGroundImageTextureSize() const;
  268. /**
  269. * Toggle layout clipping.
  270. *
  271. * If you do need clipping, you pass true to this function.
  272. *
  273. * @param enabled Pass true to enable clipping, false otherwise.
  274. */
  275. virtual void setClippingEnabled(bool enabled);
  276. /**
  277. * Change the clipping type of layout.
  278. * On default, the clipping type is `ClippingType::STENCIL`.
  279. * @see `ClippingType`
  280. *@param type The clipping type of layout.
  281. */
  282. void setClippingType(ClippingType type);
  283. /**
  284. *
  285. * @see `setClippingType(ClippingType)`
  286. */
  287. ClippingType getClippingType()const;
  288. /**
  289. * Gets if layout is clipping enabled.
  290. *
  291. * @return if layout is clipping enabled.
  292. */
  293. virtual bool isClippingEnabled()const;
  294. /**
  295. * Returns the "class name" of widget.
  296. */
  297. virtual std::string getDescription() const override;
  298. /**
  299. * Change the layout type.
  300. *@param type Layout type.
  301. */
  302. virtual void setLayoutType(Type type);
  303. /**
  304. * Query layout type.
  305. *@return Get the layout type.
  306. */
  307. virtual Type getLayoutType() const;
  308. virtual void addChild(Node* child)override;
  309. virtual void addChild(Node * child, int localZOrder)override;
  310. /**
  311. * Adds a child to the container with z order and tag
  312. *
  313. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  314. *
  315. * @param child A child node
  316. * @param localZOrder Z order for drawing priority. Please refer to setLocalZOrder(int)
  317. * @param tag A integer to identify the node easily. Please refer to setTag(int)
  318. */
  319. virtual void addChild(Node* child, int localZOrder, int tag) override;
  320. virtual void addChild(Node* child, int localZOrder, const std::string &name) override;
  321. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  322. virtual void removeChild(Node* child, bool cleanup = true) override;
  323. /**
  324. * Removes all children from the container with a cleanup.
  325. *
  326. * @see `removeAllChildrenWithCleanup(bool)`
  327. */
  328. virtual void removeAllChildren() override;
  329. /**
  330. * Removes all children from the container, and do a cleanup to all running actions depending on the cleanup parameter.
  331. *
  332. * @param cleanup true if all running actions on all children nodes should be cleanup, false otherwise.
  333. * @js removeAllChildren
  334. * @lua removeAllChildren
  335. */
  336. virtual void removeAllChildrenWithCleanup(bool cleanup) override;
  337. /**
  338. * force refresh widget layout
  339. */
  340. virtual void forceDoLayout();
  341. /**
  342. * request to refresh widget layout
  343. */
  344. virtual void requestDoLayout();
  345. /**
  346. * @lua NA
  347. */
  348. virtual void onEnter() override;
  349. /**
  350. * @lua NA
  351. */
  352. virtual void onExit() override;
  353. virtual void setGlobalZOrder(float globalZOrder) override;
  354. /**
  355. * If a layout is loop focused which means that the focus movement will be inside the layout
  356. *@param loop pass true to let the focus movement loop inside the layout
  357. */
  358. void setLoopFocus(bool loop);
  359. /**
  360. *@return If focus loop is enabled, then it will return true, otherwise it returns false. The default value is false.
  361. */
  362. bool isLoopFocus()const;
  363. /**
  364. *@param pass To specify whether the layout pass its focus to its child
  365. */
  366. void setPassFocusToChild(bool pass);
  367. /**
  368. * @return To query whether the layout will pass the focus to its children or not. The default value is true
  369. */
  370. bool isPassFocusToChild()const;
  371. /**
  372. * When a widget is in a layout, you could call this method to get the next focused widget within a specified direction.
  373. * If the widget is not in a layout, it will return itself
  374. *@param direction the direction to look for the next focused widget in a layout
  375. *@param current the current focused widget
  376. *@return the next focused widget in a layout
  377. */
  378. virtual Widget* findNextFocusedWidget(FocusDirection direction, Widget* current) override;
  379. /**
  380. * To specify a user-defined functor to decide which child widget of the layout should get focused
  381. * @param FocusDirection the finding direction
  382. * @param this previous focused widget
  383. * @return return the index of widget in the layout
  384. */
  385. std::function<int(FocusDirection, Widget*)> onPassFocusToChild;
  386. /**
  387. * Override function. Set camera mask, the node is visible by the camera whose camera flag & node's camera mask is true.
  388. * @param mask Mask being set
  389. * @param applyChildren If true call this function recursively from this node to its children.
  390. */
  391. virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
  392. ResourceData getRenderFile();
  393. CC_CONSTRUCTOR_ACCESS:
  394. //override "init" method of widget.
  395. virtual bool init() override;
  396. protected:
  397. //override "onSizeChanged" method of widget.
  398. virtual void onSizeChanged() override;
  399. //init background image renderer.
  400. void addBackGroundImage();
  401. void supplyTheLayoutParameterLackToChild(Widget* child);
  402. virtual Widget* createCloneInstance() override;
  403. virtual void copySpecialProperties(Widget* model) override;
  404. virtual void copyClonedWidgetChildren(Widget* model) override;
  405. void stencilClippingVisit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags);
  406. void scissorClippingVisit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags);
  407. void setStencilClippingSize(const Size& size);
  408. const Rect& getClippingRect();
  409. virtual void doLayout()override;
  410. virtual LayoutManager* createLayoutManager()override;
  411. virtual Size getLayoutContentSize()const override;
  412. virtual const Vector<Node*>& getLayoutElements()const override;
  413. //clipping
  414. void onBeforeVisitScissor();
  415. void onAfterVisitScissor();
  416. void updateBackGroundImageColor();
  417. void updateBackGroundImageOpacity();
  418. void updateBackGroundImageRGBA();
  419. /**
  420. *get the content size of the layout, it will accumulate all its children's content size
  421. */
  422. Size getLayoutAccumulatedSize() const;
  423. /**
  424. * When the layout get focused, it the layout pass the focus to its child, it will use this method to determine which child
  425. * will get the focus. The current algorithm to determine which child will get focus is nearest-distance-priority algorithm
  426. *@param direction The next focused widget direction
  427. *@return The index of child widget in the container
  428. */
  429. int findNearestChildWidgetIndex(FocusDirection direction, Widget* baseWidget);
  430. /**
  431. * When the layout get focused, it the layout pass the focus to its child, it will use this method to determine which child
  432. * will get the focus. The current algorithm to determine which child will get focus is farthest-distance-priority algorithm
  433. *@param direction The next focused widget direction
  434. *@return The index of child widget in the container
  435. */
  436. int findFarthestChildWidgetIndex(FocusDirection direction, Widget* baseWidget);
  437. /**
  438. * calculate the nearest distance between the baseWidget and the children of the layout
  439. *@param the base widget which will be used to calculate the distance between the layout's children and itself
  440. *@return return the nearest distance between the baseWidget and the layout's children
  441. */
  442. float calculateNearestDistance(Widget* baseWidget);
  443. /**
  444. * calculate the farthest distance between the baseWidget and the children of the layout
  445. *@param the base widget which will be used to calculate the distance between the layout's children and itself
  446. *@return return the farthest distance between the baseWidget and the layout's children
  447. */
  448. float calculateFarthestDistance(Widget* baseWidget);
  449. /**
  450. * when a layout pass the focus to it's child, use this method to determine which algorithm to use, nearest or farthest distance algorithm or not
  451. */
  452. void findProperSearchingFunctor(FocusDirection dir, Widget* baseWidget);
  453. /**
  454. * find the first non-layout widget in this layout
  455. */
  456. Widget *findFirstNonLayoutWidget();
  457. /**
  458. * find the first focus enabled widget index in the layout, it will recursive searching the child widget
  459. */
  460. int findFirstFocusEnabledWidgetIndex();
  461. /**
  462. * find a focus enabled child Widget in the layout by index
  463. */
  464. Widget* findFocusEnabledChildWidgetByIndex(ssize_t index);
  465. /**
  466. * get the center point of a widget in world space
  467. */
  468. Vec2 getWorldCenterPoint(Widget* node)const;
  469. /**
  470. * this method is called internally by nextFocusedWidget. When the dir is Right/Down, then this method will be called
  471. *@param direction the direction.
  472. *@param current the current focused widget
  473. *@return the next focused widget
  474. */
  475. Widget* getNextFocusedWidget(FocusDirection direction,Widget *current);
  476. /**
  477. * this method is called internally by nextFocusedWidget. When the dir is Left/Up, then this method will be called
  478. *@param direction the direction.
  479. *@param current the current focused widget
  480. *@return the next focused widget
  481. */
  482. Widget* getPreviousFocusedWidget(FocusDirection direction, Widget *current);
  483. /**
  484. * find the nth element in the _children array. Only the Widget descendant object will be returned
  485. *@param index The index of a element in the _children array
  486. */
  487. Widget* getChildWidgetByIndex(ssize_t index)const;
  488. /**
  489. * whether it is the last element according to all their parents
  490. */
  491. bool isLastWidgetInContainer(Widget* widget, FocusDirection direction)const;
  492. /**Lookup any parent widget with a layout type as the direction,
  493. * if the layout is loop focused, then return true, otherwise
  494. * It returns false
  495. */
  496. bool isWidgetAncestorSupportLoopFocus(Widget* widget, FocusDirection direction)const;
  497. /**
  498. * pass the focus to the layout's next focus enabled child
  499. */
  500. Widget* passFocusToChild(FocusDirection direction, Widget* current);
  501. /**
  502. * If there are no focus enabled child in the layout, it will return false, otherwise it returns true
  503. */
  504. bool checkFocusEnabledChild()const;
  505. protected:
  506. //background
  507. bool _backGroundScale9Enabled;
  508. Scale9Sprite* _backGroundImage;
  509. std::string _backGroundImageFileName;
  510. Rect _backGroundImageCapInsets;
  511. BackGroundColorType _colorType;
  512. TextureResType _bgImageTexType;
  513. Size _backGroundImageTextureSize;
  514. Color3B _backGroundImageColor;
  515. GLubyte _backGroundImageOpacity;
  516. LayerColor* _colorRender;
  517. LayerGradient* _gradientRender;
  518. Color3B _cColor;
  519. Color3B _gStartColor;
  520. Color3B _gEndColor;
  521. Vec2 _alongVector;
  522. GLubyte _cOpacity;
  523. //clipping
  524. bool _clippingEnabled;
  525. Type _layoutType;
  526. ClippingType _clippingType;
  527. DrawNode* _clippingStencil;
  528. bool _scissorOldState;
  529. Rect _clippingOldRect;
  530. Rect _clippingRect;
  531. Layout* _clippingParent;
  532. bool _clippingRectDirty;
  533. //clipping
  534. StencilStateManager *_stencilStateManager;
  535. GroupCommand _groupCommand;
  536. CustomCommand _beforeVisitCmdStencil;
  537. CustomCommand _afterDrawStencilCmd;
  538. CustomCommand _afterVisitCmdStencil;
  539. CustomCommand _beforeVisitCmdScissor;
  540. CustomCommand _afterVisitCmdScissor;
  541. bool _doLayoutDirty;
  542. bool _isInterceptTouch;
  543. //whether enable loop focus or not
  544. bool _loopFocus;
  545. //on default, it will pass the focus to the next nearest widget
  546. bool _passFocusToChild;
  547. //when finding the next focused widget, use this variable to pass focus between layout & widget
  548. bool _isFocusPassing;
  549. };
  550. }
  551. NS_CC_END
  552. // end of ui group
  553. /// @}
  554. #endif /* defined(__Layout__) */