1
0

UILayout.h 20 KB

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