UITabControl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /****************************************************************************
  2. Copyright (c) 2015-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 __UITABVIEW_H__
  22. #define __UITABVIEW_H__
  23. #include "ui/UIAbstractCheckButton.h"
  24. #include "ui/UIWidget.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. class Label;
  31. namespace ui {
  32. class Layout;
  33. class TabControl;
  34. /**
  35. * the header button in TabControl
  36. */
  37. class CC_GUI_DLL TabHeader : public AbstractCheckButton
  38. {
  39. friend class TabControl;
  40. public:
  41. enum class EventType
  42. {
  43. SELECTED,
  44. UNSELECTED
  45. };
  46. /**
  47. * Create and return a empty TabHeader instance pointer.
  48. */
  49. static TabHeader* create();
  50. /**
  51. * factory method to create a TabHeader instance.
  52. * This method uses less resource to create a TabHeader.
  53. * @param titleStr The text on the TabHeader
  54. * @param backGround The background image name in `std::string`.
  55. * @param cross The cross image name in `std::string`.
  56. * @param texType The texture's resource type in `Widget::TextureResType`.
  57. * @return A TabHeader instance pointer
  58. */
  59. static TabHeader* create(const std::string& titleStr,
  60. const std::string& backGround,
  61. const std::string& cross,
  62. TextureResType texType = TextureResType::LOCAL);
  63. /**
  64. * Create a TabHeader with various images.
  65. * @param titleStr The text on the TabHeader
  66. * @param backGround backGround texture.
  67. * @param backGroundSelected backGround selected state texture.
  68. * @param cross cross texture.
  69. * @param backGroundDisabled backGround disabled state texture.
  70. * @param frontCrossDisabled cross dark state texture.
  71. * @param texType @see `Widget::TextureResType`
  72. *
  73. * @return A TabHeader instance pointer.
  74. */
  75. static TabHeader* create(const std::string& titleStr,
  76. const std::string& backGround,
  77. const std::string& backGroundSelected,
  78. const std::string& cross,
  79. const std::string& backGroundDisabled,
  80. const std::string& frontCrossDisabled,
  81. TextureResType texType = TextureResType::LOCAL);
  82. /**
  83. * Return the inner Label renderer of TabHeader.
  84. * @return The TabHeader Label.
  85. */
  86. Label* getTitleRenderer()const;
  87. /**
  88. * Change the content of Header's text.
  89. *@param text The Header's text.
  90. */
  91. void setTitleText(const std::string& text);
  92. /**
  93. * get the TabHeader text
  94. *@return he TabHeader text
  95. */
  96. std::string getTitleText() const;
  97. /**
  98. * Change the color of he TabHeader text
  99. *@param color The he TabHeader text's color in Color4B.
  100. */
  101. void setTitleColor(const Color4B& color);
  102. /**
  103. * get the TabHeader text color.
  104. *@return Color4B of TabHeader text.
  105. */
  106. const Color4B& getTitleColor() const;
  107. /**
  108. * Change the font size of TabHeader text
  109. *@param size TabHeader text's font size in float.
  110. */
  111. void setTitleFontSize(float size);
  112. /**
  113. * get the font size of TabHeader text
  114. *@return TabHeader text's font size in float.
  115. */
  116. float getTitleFontSize() const;
  117. /**
  118. * Change the font name of TabHeader text
  119. *@param fontName a font name string.
  120. */
  121. void setTitleFontName(const std::string& fontName);
  122. /**
  123. * get the font name of TabHeader text
  124. *@return font name in std::string
  125. */
  126. std::string getTitleFontName() const;
  127. /**
  128. * get the index this header in the TabControl
  129. * @return -1 means not in any TabControl
  130. */
  131. int getIndexInTabControl() const;
  132. protected:
  133. TabHeader();
  134. ~TabHeader();
  135. virtual void initRenderer() override;
  136. virtual void onSizeChanged() override;
  137. void updateContentSize();
  138. virtual void releaseUpEvent(Touch* touch=nullptr) override;
  139. void dispatchSelectChangedEvent(bool select) override;
  140. virtual void copySpecialProperties(Widget* model) override;
  141. private:
  142. Label* _tabLabelRender;
  143. float _tabLabelFontSize;
  144. TabControl* _tabView;
  145. typedef std::function<void(int tabindex, TabHeader::EventType)> ccTabCallback;
  146. ccTabCallback _tabSelectedEvent;
  147. enum class FontType
  148. {
  149. SYSTEM,
  150. TTF,
  151. BMFONT
  152. };
  153. FontType _fontType;
  154. };
  155. /**
  156. * TabControl, use header button switch container
  157. */
  158. class CC_GUI_DLL TabControl : public Widget
  159. {
  160. public:
  161. enum class Dock
  162. {
  163. TOP,
  164. LEFT,
  165. BOTTOM,
  166. RIGHT
  167. };
  168. enum class EventType
  169. {
  170. SELECT_CHANGED,
  171. };
  172. typedef std::function<void(int tabIndex, EventType)> ccTabControlCallback;
  173. static TabControl* create();
  174. /// @{
  175. /// @name behaviours
  176. /**
  177. * remove the tab from this TabControl
  178. * @param index The index of tab
  179. */
  180. void removeTab(int index);
  181. /**
  182. * set tab selected, switch the current selected tab and visible container
  183. * @param index The index of tab
  184. */
  185. void setSelectTab(int index);
  186. /**
  187. * set tab selected, switch the current selected tab and visible container
  188. * @param tabHeader The tab instance
  189. */
  190. void setSelectTab(TabHeader* tabHeader);
  191. /**
  192. * get TabHeader
  193. * @param index The index of tab
  194. */
  195. TabHeader* getTabHeader(int index) const;
  196. /**
  197. * get Container
  198. * @param index The index of tab
  199. */
  200. Layout* getTabContainer(int index) const;
  201. /**
  202. * insert tab, and init the position of header and container
  203. * @param index The index tab should be
  204. * @param header The header Button, will be a protected child in TabControl
  205. * @param container The container, will be a protected child in TabControl
  206. */
  207. void insertTab(int index, TabHeader* header, Layout* container);
  208. /**
  209. * get the count of tabs in this TabControl
  210. * @return the count of tabs
  211. */
  212. size_t getTabCount() const;
  213. /**
  214. * get current selected tab's index
  215. * @return the current selected tab index
  216. */
  217. int getSelectedTabIndex() const;
  218. /**
  219. * get the index of tabCell in TabView, return -1 if not exists in.
  220. * @return the index of tabCell in TabView, `-1` means not exists in.
  221. */
  222. int indexOfTabHeader(const TabHeader* tabCell) const;
  223. /**
  224. * Add a callback function which would be called when selected tab changed
  225. *@param callback A std::function with type @see `ccTabControlCallback`
  226. */
  227. void setTabChangedEventListener(const ccTabControlCallback& callback);
  228. /// @}
  229. /// @{
  230. /// @ properties
  231. /**
  232. * set header width, affect all tab
  233. * @param headerWidth each tab header's width
  234. */
  235. void setHeaderWidth(float headerWidth);
  236. /**
  237. * get tab header's width
  238. * @return header's width
  239. */
  240. float getHeaderWidth() const { return (float)_headerWidth; }
  241. /**
  242. * set header height, affect all tab
  243. * @param headerHeight each tab header's height
  244. */
  245. void setHeaderHeight(float headerHeight);
  246. /**
  247. * get tab header's height
  248. * @return header's height
  249. */
  250. int getHeaderHeight() const { return _headerHeight; }
  251. /**
  252. * ignore the textures' size in header, scale them with _headerWidth and _headerHeight
  253. * @param ignore is `true`, the header's texture scale with _headerWidth and _headerHeight
  254. * ignore is `false`, use the texture's size, do not scale them
  255. */
  256. void ignoreHeadersTextureSize(bool ignore);
  257. /**
  258. * get whether ignore the textures' size in header, scale them with _headerWidth and _headerHeight
  259. * @return whether ignore the textures' size in header
  260. */
  261. bool isIgnoreHeadersTextureSize() const { return _ignoreHeaderTextureSize; };
  262. /**
  263. * set the delta zoom of selected tab
  264. * @param zoom The delta zoom
  265. */
  266. void setHeaderSelectedZoom(float zoom);
  267. /**
  268. * get the delta zoom of selected tab
  269. * @return zoom, the delta zoom
  270. */
  271. float getHeaderSelectedZoom() const { return _currentHeaderZoom; }
  272. /**
  273. * the header dock place of header in TabControl
  274. * @param dockPlace The strip place
  275. */
  276. void setHeaderDockPlace(TabControl::Dock dockPlace);
  277. TabControl::Dock getHeaderDockPlace() const { return _headerDockPlace; }
  278. /// @}
  279. protected:
  280. TabControl();
  281. ~TabControl();
  282. void onSizeChanged() override;
  283. void initTabHeadersPos(int startIndex);
  284. void initContainers();
  285. virtual void copySpecialProperties(Widget* model) override;
  286. ccTabControlCallback _tabChangedCallback;
  287. // dispatch selected changed
  288. void dispatchSelectedTabChanged(int tabIndex, TabHeader::EventType eventType);
  289. private:
  290. typedef struct CellContainer
  291. {
  292. TabHeader* header;
  293. Layout* container;
  294. CellContainer(TabHeader* headerCell, Layout* layout)
  295. {
  296. header = headerCell;
  297. container = layout;
  298. }
  299. } TabItem;
  300. // format tab header and container after insert
  301. void initAfterInsert(int index);
  302. void activeTabItem(TabItem* item);
  303. void deactiveTabItem(TabItem* item);
  304. Vec2 getHeaderAnchorWithDock() const;
  305. TabItem* _selectedItem;
  306. int _headerHeight;
  307. int _headerWidth;
  308. Dock _headerDockPlace;
  309. Vec2 _containerPosition;
  310. Size _containerSize;
  311. float _currentHeaderZoom;
  312. bool _ignoreHeaderTextureSize;
  313. // for index the cells and containers
  314. std::vector<TabItem*> _tabItems;
  315. };
  316. }
  317. // end group
  318. /// @}
  319. NS_CC_END
  320. #endif // __UITABVIEW_H__