UILayoutParameter.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 __LAYOUTPARMETER_H__
  22. #define __LAYOUTPARMETER_H__
  23. #include <string>
  24. #include "base/CCRef.h"
  25. #include "ui/GUIExport.h"
  26. /**
  27. * @addtogroup ui
  28. * @{
  29. */
  30. NS_CC_BEGIN
  31. namespace ui {
  32. /**
  33. *@brief Margin of widget's in point. Margin value should be positive.
  34. *@lua NA
  35. */
  36. class CC_GUI_DLL Margin
  37. {
  38. public:
  39. /**
  40. * Left margin.
  41. */
  42. float left;
  43. /**
  44. * Top margin.
  45. */
  46. float top;
  47. /**
  48. * Right margin.
  49. */
  50. float right;
  51. /**
  52. * Bottom margin.
  53. */
  54. float bottom;
  55. public:
  56. /**
  57. * Default constructor.
  58. */
  59. Margin();
  60. /**
  61. * Construct a Margin instance with left, top, right and bottom margins.
  62. *@param l Left margin in float.
  63. *@param t Top margin in float.
  64. *@param r Right margin in float.
  65. *@param b Bottom margin in float.
  66. */
  67. Margin(float l, float t, float r, float b);
  68. /**
  69. * Copy constructor.
  70. */
  71. Margin(const Margin& other);
  72. /**
  73. * Copy assignment operator.
  74. */
  75. Margin& operator= (const Margin& other);
  76. /**
  77. * Change margin with left, top, right and bottom margin.
  78. *@param l Left margin in float.
  79. *@param t Top margin in float.
  80. *@param r Right margin in float.
  81. *@param b Bottom margin in float.
  82. */
  83. void setMargin(float l, float t, float r, float b);
  84. /**
  85. * Test equality of two margins.
  86. *@param target A Margin instance.
  87. *@return True if two margins are equal, false otherwise.
  88. */
  89. bool equals(const Margin& target) const;
  90. /**
  91. * A margin constant with all margins equal zero.
  92. */
  93. static const Margin ZERO;
  94. };
  95. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  96. #ifdef RELATIVE
  97. #undef RELATIVE
  98. #endif
  99. #endif
  100. /**
  101. *@brief Base class for various LayoutParameter.
  102. */
  103. class CC_GUI_DLL LayoutParameter : public Ref
  104. {
  105. public:
  106. /**
  107. *Layout parameter type.
  108. * There are mainly two types:
  109. * - Linear: Elements will be arranged by margin.
  110. * - Relative: Elements will be arranged by margin and relative widget name.
  111. */
  112. enum class Type
  113. {
  114. NONE = 0,
  115. LINEAR,
  116. RELATIVE
  117. };
  118. /**
  119. * Default constructor.
  120. *
  121. * @lua new
  122. */
  123. LayoutParameter() : _margin(Margin())
  124. {
  125. _layoutParameterType = Type::NONE;
  126. }
  127. /**
  128. * Default destructor.
  129. * @lua NA
  130. */
  131. virtual ~LayoutParameter(){};
  132. /**
  133. * Create a empty LayoutParameter.
  134. * @return A autorelease LayoutParameter instance.
  135. */
  136. static LayoutParameter* create();
  137. /**
  138. * Set margin parameter for LayoutParameter.
  139. *
  140. * @see Margin
  141. * @param margin
  142. */
  143. void setMargin(const Margin& margin);
  144. /**
  145. * Gets margin parameter of LayoutParameter.
  146. *
  147. * @see Margin
  148. * @return Margin of layout parameter.
  149. */
  150. const Margin& getMargin() const;
  151. /**
  152. * Gets LayoutParameterType of LayoutParameter.
  153. *
  154. * @see LayoutParameterType.
  155. * @return LayoutParameterType
  156. */
  157. Type getLayoutType() const;
  158. /**
  159. * Create a copy of original LayoutParameter.
  160. *@return A LayoutParameter pointer.
  161. */
  162. LayoutParameter* clone();
  163. /**
  164. * Create a cloned instance of LayoutParameter.
  165. *@return A LayoutParameter pointer.
  166. */
  167. virtual LayoutParameter* createCloneInstance();
  168. /**
  169. * Copy all the member field from argument LayoutParameter to self.
  170. *@param model A LayoutParameter instance.
  171. */
  172. virtual void copyProperties(LayoutParameter* model);
  173. protected:
  174. Margin _margin;
  175. Type _layoutParameterType;
  176. };
  177. /**
  178. * Protocol for getting a LayoutParameter.
  179. * Every element want to have layout parameter should inherit from this class.
  180. */
  181. class CC_GUI_DLL LayoutParameterProtocol
  182. {
  183. public:
  184. /**
  185. * Default destructor.
  186. */
  187. virtual ~LayoutParameterProtocol(){}
  188. /**
  189. *
  190. *@return A LayoutParameter and its descendant pointer.
  191. */
  192. virtual LayoutParameter* getLayoutParameter() const= 0;
  193. };
  194. /**
  195. * @brief Linear layout parameter.
  196. * It is used by linear layout manager for arranging elements linearly.
  197. */
  198. class CC_GUI_DLL LinearLayoutParameter : public LayoutParameter
  199. {
  200. public:
  201. /**
  202. * Linear gravity.
  203. */
  204. enum class LinearGravity
  205. {
  206. NONE,
  207. LEFT,
  208. TOP,
  209. RIGHT,
  210. BOTTOM,
  211. CENTER_VERTICAL,
  212. CENTER_HORIZONTAL
  213. };
  214. /**
  215. * Default constructor.
  216. *
  217. * @lua new
  218. */
  219. LinearLayoutParameter()
  220. : _linearGravity(LinearGravity::NONE)
  221. {
  222. _layoutParameterType = Type::LINEAR;
  223. }
  224. /**
  225. * Default destructor.
  226. *
  227. * @lua NA
  228. */
  229. virtual ~LinearLayoutParameter(){};
  230. /**
  231. * Create a empty LinearLayoutParameter instance.
  232. * @return A initialized LayoutParameter which is marked as "autorelease".
  233. */
  234. static LinearLayoutParameter* create();
  235. /**
  236. * Sets LinearGravity parameter for LayoutParameter.
  237. *
  238. * @see LinearGravity
  239. * @param gravity Gravity in LinearGravity.
  240. */
  241. void setGravity(LinearGravity gravity);
  242. /**
  243. * Gets LinearGravity parameter for LayoutParameter.
  244. *
  245. * @see LinearGravity
  246. * @return LinearGravity
  247. */
  248. LinearGravity getGravity() const;
  249. //override functions.
  250. virtual LayoutParameter* createCloneInstance() override;
  251. virtual void copyProperties(LayoutParameter* model) override;
  252. protected:
  253. LinearGravity _linearGravity;
  254. int i;
  255. };
  256. /**
  257. * @brief Relative layout parameter.
  258. * It is mainly used by `RelativeLayoutManager`.
  259. */
  260. class CC_GUI_DLL RelativeLayoutParameter : public LayoutParameter
  261. {
  262. public:
  263. /**
  264. * Relative Alignment type
  265. */
  266. enum class RelativeAlign
  267. {
  268. NONE,
  269. PARENT_TOP_LEFT,
  270. PARENT_TOP_CENTER_HORIZONTAL,
  271. PARENT_TOP_RIGHT,
  272. PARENT_LEFT_CENTER_VERTICAL,
  273. CENTER_IN_PARENT,
  274. PARENT_RIGHT_CENTER_VERTICAL,
  275. PARENT_LEFT_BOTTOM,
  276. PARENT_BOTTOM_CENTER_HORIZONTAL,
  277. PARENT_RIGHT_BOTTOM,
  278. LOCATION_ABOVE_LEFTALIGN,
  279. LOCATION_ABOVE_CENTER,
  280. LOCATION_ABOVE_RIGHTALIGN,
  281. LOCATION_LEFT_OF_TOPALIGN,
  282. LOCATION_LEFT_OF_CENTER,
  283. LOCATION_LEFT_OF_BOTTOMALIGN,
  284. LOCATION_RIGHT_OF_TOPALIGN,
  285. LOCATION_RIGHT_OF_CENTER,
  286. LOCATION_RIGHT_OF_BOTTOMALIGN,
  287. LOCATION_BELOW_LEFTALIGN,
  288. LOCATION_BELOW_CENTER,
  289. LOCATION_BELOW_RIGHTALIGN
  290. };
  291. /**
  292. * Default constructor
  293. *
  294. * @lua new
  295. */
  296. RelativeLayoutParameter()
  297. : _relativeAlign(RelativeAlign::NONE),
  298. _relativeWidgetName(""),
  299. _relativeLayoutName(""),
  300. _put(false)
  301. {
  302. _layoutParameterType = Type::RELATIVE;
  303. }
  304. /**
  305. * Default destructor
  306. *
  307. * @lua NA
  308. */
  309. virtual ~RelativeLayoutParameter(){};
  310. /**
  311. * Create a RelativeLayoutParameter instance.
  312. * @return A initialized LayoutParameter which is marked as "autorelease".
  313. */
  314. static RelativeLayoutParameter* create();
  315. /**
  316. * Sets RelativeAlign parameter for LayoutParameter.
  317. *
  318. * @see RelativeAlign
  319. * @param align Relative align in `RelativeAlign`.
  320. */
  321. void setAlign(RelativeAlign align);
  322. /**
  323. * Get RelativeAlign parameter for LayoutParameter.
  324. *
  325. * @see RelativeAlign
  326. * @return A RelativeAlign variable.
  327. */
  328. RelativeAlign getAlign() const;
  329. /**
  330. * Set widget name your widget want to relative to.
  331. *
  332. * @param name Relative widget name.
  333. */
  334. void setRelativeToWidgetName(const std::string& name);
  335. /**
  336. * Get the relative widget name.
  337. * @return name A relative widget name in string.
  338. */
  339. const std::string& getRelativeToWidgetName() const;
  340. /**
  341. * Set a name for LayoutParameter in Relative Layout.
  342. *
  343. * @param name A string name.
  344. */
  345. void setRelativeName(const std::string& name);
  346. /**
  347. * Get a name of LayoutParameter in Relative Layout.
  348. *
  349. * @return name Relative name in string.
  350. */
  351. const std::string& getRelativeName() const;
  352. //override functions.
  353. virtual LayoutParameter* createCloneInstance() override;
  354. virtual void copyProperties(LayoutParameter* model) override;
  355. protected:
  356. RelativeAlign _relativeAlign;
  357. std::string _relativeWidgetName;
  358. std::string _relativeLayoutName;
  359. bool _put;
  360. friend class RelativeLayoutManager;
  361. };
  362. }
  363. NS_CC_END
  364. // end of ui group
  365. /// @}
  366. #endif /* defined(__LayoutParameter__) */