UILayoutComponent.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 __cocos2d_libs__LayoutComponent__
  22. #define __cocos2d_libs__LayoutComponent__
  23. #include "2d/CCComponent.h"
  24. #include "ui/GUIExport.h"
  25. NS_CC_BEGIN
  26. /**
  27. * @addtogroup ui
  28. * @{
  29. */
  30. namespace ui {
  31. /**
  32. *@brief A component class used for layout.
  33. * The LayoutComponent holds all the data for layouting.
  34. */
  35. class CC_GUI_DLL LayoutComponent : public Component
  36. {
  37. public:
  38. /**
  39. * Default constructor
  40. *
  41. * @lua new
  42. */
  43. LayoutComponent();
  44. /**
  45. * Default destructor
  46. *
  47. * @lua NA
  48. */
  49. ~LayoutComponent();
  50. virtual bool init()override;
  51. /**
  52. * Create a LayoutComponent instance with default settings.
  53. */
  54. CREATE_FUNC(LayoutComponent);
  55. /**
  56. * Bind a LayoutComponent to a specified node.
  57. * If the node has already binded a LayoutComponent named __LAYOUT_COMPONENT_NAME, just return the LayoutComponent.
  58. * Otherwise, create a new LayoutComponent and bind the LayoutComponent to the node.
  59. *@param node A Node* instance pointer.
  60. *@return The binded LayoutComponent instance pointer.
  61. */
  62. static LayoutComponent* bindLayoutComponent(Node* node);
  63. /**
  64. * Horizontal dock position type.
  65. */
  66. enum class HorizontalEdge
  67. {
  68. None,
  69. Left,
  70. Right,
  71. Center
  72. };
  73. /**
  74. * Vertical dock position type.
  75. */
  76. enum class VerticalEdge
  77. {
  78. None,
  79. Bottom,
  80. Top,
  81. Center
  82. };
  83. /**
  84. * Percent content size is used to adapt node's content size based on parent's content size.
  85. * If set to true then node's content size will be changed based on the value set by @see setPercentContentSize
  86. *@param isUsed True to enable percent content size, false otherwise.
  87. */
  88. void setUsingPercentContentSize(bool isUsed);
  89. /**
  90. * Query whether use percent content size or not.
  91. *@return True if using percent content size, false otherwise.
  92. */
  93. bool getUsingPercentContentSize()const;
  94. /**
  95. * Set percent content size.
  96. * The value should be [0-1], 0 means the child's content size will be 0
  97. * and 1 means the child's content size is the same as its parents.
  98. *@param percent The percent (x,y) of the node in [0-1] scope.
  99. */
  100. void setPercentContentSize(const Vec2 &percent);
  101. /**
  102. * Query the percent content size value.
  103. *@return Percent (x,y) in Vec2.
  104. */
  105. Vec2 getPercentContentSize()const;
  106. /**
  107. * Query the anchor position.
  108. *@return Anchor position to it's parent
  109. */
  110. const Point& getAnchorPosition()const;
  111. /**
  112. * Change the anchor position to it's parent.
  113. *@param point A value in (x,y) format.
  114. */
  115. void setAnchorPosition(const Point& point);
  116. /**
  117. * Query the owner's position.
  118. *@return The owner's position.
  119. */
  120. const Point& getPosition()const;
  121. /**
  122. * Change the position of component owner.
  123. * @param position A position in (x,y)
  124. */
  125. void setPosition(const Point& position);
  126. /**
  127. * Whether position percentX is enabled or not.
  128. *@return True if position percentX is enable, false otherwise.
  129. */
  130. bool isPositionPercentXEnabled()const;
  131. /**
  132. * Toggle position percentX enabled.
  133. *@param isUsed True if enable position percentX, false otherwise.
  134. */
  135. void setPositionPercentXEnabled(bool isUsed);
  136. /**
  137. * Query the position percent X value.
  138. *@return Position percent X value in float.
  139. */
  140. float getPositionPercentX()const;
  141. /**
  142. * Change position percent X value.
  143. *@param percentMargin Margin in float.
  144. */
  145. void setPositionPercentX(float percentMargin);
  146. /**
  147. * Whether position percentY is enabled or not.
  148. *@see `setPositionPercentYEnabled`
  149. *@return True if position percentY is enabled, false otherwise.
  150. */
  151. bool isPositionPercentYEnabled()const;
  152. /**
  153. * Toggle position percentY enabled.
  154. *@param isUsed True if position percentY is enabled, false otherwise.
  155. */
  156. void setPositionPercentYEnabled(bool isUsed);
  157. /**
  158. * Query the position percentY Y value.
  159. *@return Position percent Y value in float.
  160. */
  161. float getPositionPercentY()const;
  162. /**
  163. * Change position percentY value.
  164. *@param percentMargin Margin in float.
  165. */
  166. void setPositionPercentY(float percentMargin);
  167. /**
  168. * Query element horizontal dock type.
  169. *@return Horizontal dock type.
  170. */
  171. HorizontalEdge getHorizontalEdge()const;
  172. /**
  173. * Change element's horizontal dock type.
  174. *@param hEage Horizontal dock type @see `HorizontalEdge`
  175. */
  176. void setHorizontalEdge(HorizontalEdge hEage);
  177. /**
  178. * Query element vertical dock type.
  179. *@return Vertical dock type.
  180. */
  181. VerticalEdge getVerticalEdge()const;
  182. /**
  183. * Change element's vertical dock type.
  184. *@param vEage Vertical dock type @see `VerticalEdge`.
  185. */
  186. void setVerticalEdge(VerticalEdge vEage);
  187. /**
  188. * Query left margin of owner relative to its parent.
  189. *@return Left margin in float.
  190. */
  191. float getLeftMargin()const;
  192. /**
  193. * Change left margin of owner relative to its parent.
  194. *@param margin Margin in float.
  195. */
  196. void setLeftMargin(float margin);
  197. /**
  198. * Query the right margin of owner relative to its parent.
  199. *@return Right margin in float.
  200. */
  201. float getRightMargin()const;
  202. /**
  203. * Change right margin of owner relative to its parent.
  204. *@param margin Margin in float.
  205. */
  206. void setRightMargin(float margin);
  207. /**
  208. * Query the top margin of owner relative to its parent.
  209. *@return Top margin in float.
  210. */
  211. float getTopMargin()const;
  212. /**
  213. * Change the top margin of owner relative to its parent.
  214. *@param margin Margin in float.
  215. */
  216. void setTopMargin(float margin);
  217. /**
  218. * Query the bottom margin of owner relative to its parent.
  219. *@return Bottom margin in float.
  220. */
  221. float getBottomMargin()const;
  222. /**
  223. * Change the bottom margin of owner relative to its parent.
  224. *@param margin in float.
  225. */
  226. void setBottomMargin(float margin);
  227. /**
  228. * Query owner's content size.
  229. *@return Owner's content size.
  230. */
  231. const Size& getSize()const;
  232. /**
  233. * Change the content size of owner.
  234. *@param size Content size in @see `Size`.
  235. */
  236. void setSize(const Size& size);
  237. /**
  238. * Query whether percent width is enabled or not.
  239. *@return True if percent width is enabled, false, otherwise.
  240. */
  241. bool isPercentWidthEnabled()const;
  242. /**
  243. * Toggle enable percent width.
  244. *@param isUsed True if percent width is enabled, false otherwise.
  245. */
  246. void setPercentWidthEnabled(bool isUsed);
  247. /**
  248. * Query content size width of owner.
  249. *@return Content size width in float.
  250. */
  251. float getSizeWidth()const;
  252. /**
  253. * Change content size width of owner.
  254. *@param width Content size width in float.
  255. */
  256. void setSizeWidth(float width);
  257. /**
  258. * Query percent width of owner.
  259. *@return percent width in float.
  260. */
  261. float getPercentWidth()const;
  262. /**
  263. * Change percent width of owner.
  264. *@param percentWidth Percent Width in float.
  265. */
  266. void setPercentWidth(float percentWidth);
  267. /**
  268. * Query whether percent height is enabled or not.
  269. *@return True if percent height is enabled, false otherwise.
  270. */
  271. bool isPercentHeightEnabled()const;
  272. /**
  273. * Toggle enable percent height.
  274. *@param isUsed True if percent height is enabled, false otherwise.
  275. */
  276. void setPercentHeightEnabled(bool isUsed);
  277. /**
  278. * Query size height of owner.
  279. *@return Size height in float.
  280. */
  281. float getSizeHeight()const;
  282. /**
  283. * Change size height of owner.
  284. *@param height Size height in float.
  285. */
  286. void setSizeHeight(float height);
  287. /**
  288. * Query percent height of owner.
  289. *@return Percent height in float.
  290. */
  291. float getPercentHeight()const;
  292. /**
  293. * Change percent height value of owner.
  294. *@param percentHeight Percent height in float.
  295. */
  296. void setPercentHeight(float percentHeight);
  297. /**
  298. * Query whether stretch width is enabled or not.
  299. *@return True if stretch width is enabled, false otherwise.
  300. */
  301. bool isStretchWidthEnabled()const;
  302. /**
  303. * Toggle enable stretch width.
  304. *@param isUsed True if enable stretch width, false otherwise.
  305. */
  306. void setStretchWidthEnabled(bool isUsed);
  307. /**
  308. * Query whether stretch height is enabled or not.
  309. *@return True if stretch height is enabled, false otherwise.
  310. */
  311. bool isStretchHeightEnabled()const;
  312. /**
  313. * Toggle enable stretch height.
  314. *@param isUsed True if stretch height is enabled, false otherwise.
  315. */
  316. void setStretchHeightEnabled(bool isUsed);
  317. /**
  318. * Toggle enable percent only.
  319. *@param enable True if percent only is enabled, false otherwise.
  320. */
  321. void setPercentOnlyEnabled(bool enable);
  322. /**
  323. * Toggle active enabled of LayoutComponent's owner.
  324. *@param enable True if active layout component, false otherwise.
  325. */
  326. void setActiveEnabled(bool enable);
  327. /**
  328. * Refresh layout of the owner.
  329. */
  330. void refreshLayout();
  331. protected:
  332. Node* getOwnerParent();
  333. void refreshHorizontalMargin();
  334. void refreshVerticalMargin();
  335. protected:
  336. HorizontalEdge _horizontalEdge;
  337. VerticalEdge _verticalEdge;
  338. float _leftMargin;
  339. float _rightMargin;
  340. float _bottomMargin;
  341. float _topMargin;
  342. bool _usingPositionPercentX;
  343. float _positionPercentX;
  344. bool _usingPositionPercentY;
  345. float _positionPercentY;
  346. bool _usingStretchWidth;
  347. bool _usingStretchHeight;
  348. float _percentWidth;
  349. bool _usingPercentWidth;
  350. float _percentHeight;
  351. bool _usingPercentHeight;
  352. bool _actived;
  353. bool _isPercentOnly;
  354. };
  355. }
  356. // end of ui group
  357. /// @}
  358. NS_CC_END
  359. #endif /* defined(__cocos2d_libs__LayoutComponent__) */