1
0

UITabControl.h 12 KB

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