UITabControl.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. #include "platform/CCFileUtils.h"
  22. #include "2d/CCSprite.h"
  23. #include "2d/CCLabel.h"
  24. #include "ui/UILayout.h"
  25. #include "ui/UITabControl.h"
  26. NS_CC_BEGIN
  27. namespace ui
  28. {
  29. TabControl::TabControl()
  30. : _selectedItem(nullptr)
  31. , _headerHeight(20)
  32. , _headerWidth(50)
  33. , _headerDockPlace(Dock::TOP)
  34. , _containerPosition(Vec2::ZERO)
  35. , _containerSize(Size::ZERO)
  36. , _currentHeaderZoom(0.1f)
  37. , _ignoreHeaderTextureSize(true)
  38. {
  39. this->_anchorPoint = Vec2(0.f, 0.f);
  40. setContentSize(Size(200, 200));
  41. }
  42. TabControl::~TabControl()
  43. {
  44. for (auto& item : _tabItems)
  45. {
  46. if (item)
  47. CC_SAFE_DELETE(item);
  48. }
  49. _tabItems.clear();
  50. }
  51. void TabControl::insertTab(int index, TabHeader* header, Layout* container)
  52. {
  53. int cellSize = (int)_tabItems.size();
  54. if (index > cellSize)
  55. {
  56. CCLOG("%s", "insert index error");
  57. return;
  58. }
  59. addProtectedChild(container, -3, -1);
  60. addProtectedChild(header, -2, -1);
  61. _tabItems.insert(_tabItems.begin() + index, new TabItem(header, container));
  62. header->_tabView = this;
  63. header->_tabSelectedEvent = CC_CALLBACK_2(TabControl::dispatchSelectedTabChanged, this); // binding tab selected event
  64. initAfterInsert(index);
  65. }
  66. void TabControl::initAfterInsert(int index)
  67. {
  68. auto cellSize = _tabItems.size();
  69. auto tabItem = _tabItems.at(index);
  70. auto headerCell = tabItem->header;
  71. auto container = tabItem->container;
  72. if (cellSize == 1)
  73. {
  74. setSelectTab(0);
  75. }
  76. else
  77. {
  78. deactiveTabItem(tabItem);
  79. }
  80. headerCell->setContentSize(Size(_headerWidth, _headerHeight));
  81. headerCell->setAnchorPoint(getHeaderAnchorWithDock());
  82. if (headerCell->isIgnoreContentAdaptWithSize() == _ignoreHeaderTextureSize)
  83. {
  84. headerCell->ignoreContentAdaptWithSize(!_ignoreHeaderTextureSize);
  85. if (_ignoreHeaderTextureSize)
  86. headerCell->setContentSize(Size(_headerWidth, _headerHeight));
  87. headerCell->backGroundDisabledTextureScaleChangedWithSize();
  88. headerCell->backGroundSelectedTextureScaleChangedWithSize();
  89. headerCell->backGroundDisabledTextureScaleChangedWithSize();
  90. headerCell->frontCrossTextureScaleChangedWithSize();
  91. headerCell->frontCrossDisabledTextureScaleChangedWithSize();
  92. }
  93. initTabHeadersPos(index);
  94. if (_containerSize.equals(Size::ZERO))
  95. initContainers();
  96. else
  97. {
  98. container->setPosition(_containerPosition);
  99. container->setContentSize(_containerSize);
  100. }
  101. }
  102. void TabControl::removeTab(int index)
  103. {
  104. int cellSize = (int)_tabItems.size();
  105. if (cellSize == 0 || index >= cellSize)
  106. {
  107. CCLOG("%s", "no tab or remove index error");
  108. return;
  109. }
  110. auto tabItem = _tabItems.at(index);
  111. if (tabItem == _selectedItem)
  112. _selectedItem = nullptr;
  113. auto header = tabItem->header;
  114. auto container = tabItem->container;
  115. if (tabItem)
  116. CC_SAFE_DELETE(tabItem);
  117. _tabItems.erase(_tabItems.begin() + index);
  118. if (header != nullptr)
  119. {
  120. header->_tabSelectedEvent = nullptr;
  121. header->_tabView = nullptr;
  122. removeProtectedChild(header);
  123. removeProtectedChild(container);
  124. }
  125. initTabHeadersPos(index);
  126. }
  127. size_t TabControl::getTabCount() const
  128. {
  129. return _tabItems.size();
  130. }
  131. void TabControl::setHeaderWidth(float headerWidth)
  132. {
  133. _headerWidth = headerWidth;
  134. if (_headerDockPlace == Dock::TOP || _headerDockPlace == Dock::BOTTOM)
  135. initTabHeadersPos(0);
  136. if (_headerDockPlace == Dock::LEFT || _headerDockPlace == Dock::RIGHT)
  137. initContainers();
  138. }
  139. void TabControl::setHeaderHeight(float headerHeight)
  140. {
  141. _headerHeight = headerHeight;
  142. if (_headerDockPlace == Dock::LEFT || _headerDockPlace == Dock::RIGHT)
  143. initTabHeadersPos(0);
  144. if (_headerDockPlace == Dock::TOP || _headerDockPlace == Dock::BOTTOM)
  145. initContainers();
  146. }
  147. void TabControl::setHeaderDockPlace(TabControl::Dock dockPlace)
  148. {
  149. if (_headerDockPlace != dockPlace)
  150. {
  151. _headerDockPlace = dockPlace;
  152. initTabHeadersPos(0);
  153. initContainers();
  154. auto anpoint = getHeaderAnchorWithDock();
  155. for (auto& item : _tabItems)
  156. {
  157. item->header->setAnchorPoint(anpoint);
  158. }
  159. }
  160. }
  161. cocos2d::Vec2 TabControl::getHeaderAnchorWithDock() const
  162. {
  163. Vec2 anpoint(.5f, .0f);
  164. switch (_headerDockPlace)
  165. {
  166. case Dock::TOP:
  167. break;
  168. case Dock::LEFT:
  169. anpoint.x = 1.f;
  170. anpoint.y = .5f;
  171. break;
  172. case Dock::BOTTOM:
  173. anpoint.x = .5f;
  174. anpoint.y = 1.f;
  175. break;
  176. case Dock::RIGHT:
  177. anpoint.x = 0.f;
  178. anpoint.y = .5f;
  179. break;
  180. default:
  181. break;
  182. }
  183. return anpoint;
  184. }
  185. void TabControl::onSizeChanged()
  186. {
  187. initTabHeadersPos(0);
  188. initContainers();
  189. }
  190. void TabControl::initTabHeadersPos(int startIndex)
  191. {
  192. int cellSize = (int)_tabItems.size();
  193. if (startIndex >= cellSize)
  194. return;
  195. float originX = _headerWidth * .5f;
  196. float originY = _contentSize.height - _headerHeight;
  197. Vec2 deltaPos(0.f, 0.f);
  198. switch (_headerDockPlace)
  199. {
  200. case Dock::TOP:
  201. deltaPos.x = _headerWidth;
  202. break;
  203. case Dock::LEFT:
  204. originX = _headerWidth;
  205. originY = _contentSize.height - _headerHeight * .5f;
  206. deltaPos.y = 0 - _headerHeight;
  207. break;
  208. case Dock::BOTTOM:
  209. originY = _headerHeight;
  210. deltaPos.x = _headerWidth;
  211. break;
  212. case Dock::RIGHT:
  213. originX = _contentSize.width - _headerWidth;
  214. originY = _contentSize.height - _headerHeight * .5f;
  215. deltaPos.y = 0 - _headerHeight;
  216. break;
  217. default:
  218. break;
  219. }
  220. for (int cellI = startIndex; cellI < cellSize; cellI++)
  221. {
  222. auto headerCell = _tabItems.at(cellI)->header;
  223. headerCell->setPosition(Vec2(originX + cellI* deltaPos.x, originY + cellI * deltaPos.y));
  224. }
  225. }
  226. void TabControl::initContainers()
  227. {
  228. switch (_headerDockPlace)
  229. {
  230. case Dock::TOP:
  231. _containerPosition = Vec2(0, 0);
  232. _containerSize = Size(_contentSize.width, _contentSize.height - _headerHeight);
  233. break;
  234. case Dock::LEFT:
  235. _containerPosition = Vec2(_headerWidth, 0);
  236. _containerSize = Size(_contentSize.width - _headerWidth, _contentSize.height);
  237. break;
  238. case Dock::BOTTOM:
  239. _containerPosition = Vec2(0, _headerHeight);
  240. _containerSize = Size(_contentSize.width, _contentSize.height - _headerHeight);
  241. break;
  242. case Dock::RIGHT:
  243. _containerPosition = Vec2(0, 0);
  244. _containerSize = Size(_contentSize.width - _headerWidth, _contentSize.height);
  245. break;
  246. default:
  247. break;
  248. }
  249. for (auto& tabItem : _tabItems)
  250. {
  251. Layout* container = tabItem->container;
  252. container->setPosition(_containerPosition);
  253. container->setContentSize(_containerSize);
  254. }
  255. }
  256. TabHeader* TabControl::getTabHeader(int index) const
  257. {
  258. if (index >= (int)getTabCount())
  259. return nullptr;
  260. return _tabItems.at(index)->header;
  261. }
  262. Layout* TabControl::getTabContainer(int index) const
  263. {
  264. if (index >= (int)getTabCount())
  265. return nullptr;
  266. return _tabItems.at(index)->container;
  267. }
  268. void TabControl::dispatchSelectedTabChanged(int tabIndex, TabHeader::EventType eventType)
  269. {
  270. if (eventType == TabHeader::EventType::SELECTED)
  271. {
  272. if (tabIndex <= -1 || tabIndex >= (int)_tabItems.size())
  273. {
  274. deactiveTabItem(_selectedItem);
  275. _selectedItem = nullptr;
  276. }
  277. else
  278. {
  279. deactiveTabItem(_selectedItem);
  280. auto tabItem = _tabItems.at(tabIndex);
  281. activeTabItem(tabItem);
  282. _selectedItem = tabItem;
  283. }
  284. }
  285. else if (eventType == TabHeader::EventType::UNSELECTED)
  286. {
  287. if (tabIndex >= 0 && tabIndex < (int)_tabItems.size())
  288. {
  289. auto tabItem = _tabItems.at(tabIndex);
  290. if (tabItem == _selectedItem)
  291. {
  292. deactiveTabItem(_selectedItem);
  293. _selectedItem = nullptr;
  294. }
  295. }
  296. }
  297. if (_tabChangedCallback != nullptr)
  298. {
  299. int currentIndex = getSelectedTabIndex();
  300. _tabChangedCallback(currentIndex, EventType::SELECT_CHANGED);
  301. }
  302. }
  303. void TabControl::setTabChangedEventListener(const ccTabControlCallback& callback)
  304. {
  305. _tabChangedCallback = callback;
  306. }
  307. int TabControl::indexOfTabHeader(const TabHeader* tabCell) const
  308. {
  309. int n = (int)_tabItems.size();
  310. for (auto i = 0; i < n; i++)
  311. {
  312. if (tabCell == _tabItems.at(i)->header)
  313. {
  314. return i;
  315. }
  316. }
  317. return -1;
  318. }
  319. TabControl* TabControl::create()
  320. {
  321. TabControl* tabview = new (std::nothrow) TabControl();
  322. if (tabview != nullptr && tabview->init())
  323. {
  324. tabview->autorelease();
  325. return tabview;
  326. }
  327. CC_SAFE_DELETE(tabview);
  328. return nullptr;
  329. }
  330. void TabControl::setSelectTab(int index)
  331. {
  332. dispatchSelectedTabChanged(index, TabHeader::EventType::SELECTED);
  333. }
  334. void TabControl::setSelectTab(TabHeader* tabHeader)
  335. {
  336. if (_selectedItem != nullptr && tabHeader == _selectedItem->header)
  337. return;
  338. setSelectTab(indexOfTabHeader(tabHeader));
  339. }
  340. void TabControl::setHeaderSelectedZoom(float zoom)
  341. {
  342. if (_currentHeaderZoom != zoom)
  343. {
  344. _currentHeaderZoom = zoom;
  345. if (_selectedItem != nullptr)
  346. {
  347. auto currentHeader = _selectedItem->header;
  348. currentHeader->setScale(1.0f + _currentHeaderZoom);
  349. }
  350. }
  351. }
  352. void TabControl::activeTabItem(TabItem* item)
  353. {
  354. if (item != nullptr)
  355. {
  356. item->header->setLocalZOrder(-1);
  357. item->header->setScale(1.0f + _currentHeaderZoom);
  358. item->header->setSelected(true);
  359. item->container->setVisible(true);
  360. _reorderProtectedChildDirty = true;
  361. }
  362. }
  363. void TabControl::deactiveTabItem(TabItem* item)
  364. {
  365. if (item != nullptr)
  366. {
  367. item->header->setLocalZOrder(-2);
  368. item->header->setScale(1.0f);
  369. item->header->setSelected(false);
  370. item->container->setVisible(false);
  371. _reorderProtectedChildDirty = true;
  372. }
  373. }
  374. void TabControl::copySpecialProperties(Widget* model)
  375. {
  376. auto srcTab = dynamic_cast<TabControl*>(model);
  377. if (srcTab != nullptr)
  378. {
  379. Widget::copySpecialProperties(srcTab);
  380. _headerWidth = srcTab->_headerWidth;
  381. _headerHeight = srcTab->_headerHeight;
  382. _headerDockPlace = srcTab->_headerDockPlace;
  383. _currentHeaderZoom = srcTab->_currentHeaderZoom;
  384. _tabChangedCallback = srcTab->_tabChangedCallback;
  385. }
  386. }
  387. void TabControl::ignoreHeadersTextureSize(bool ignore)
  388. {
  389. if (_ignoreHeaderTextureSize == ignore)
  390. return;
  391. _ignoreHeaderTextureSize = ignore;
  392. for (auto& item : _tabItems)
  393. {
  394. item->header->ignoreContentAdaptWithSize(!ignore);
  395. if (ignore)
  396. item->header->setContentSize(Size(_headerWidth, _headerHeight));
  397. item->header->backGroundDisabledTextureScaleChangedWithSize();
  398. item->header->backGroundSelectedTextureScaleChangedWithSize();
  399. item->header->backGroundDisabledTextureScaleChangedWithSize();
  400. item->header->frontCrossTextureScaleChangedWithSize();
  401. item->header->frontCrossDisabledTextureScaleChangedWithSize();
  402. }
  403. }
  404. int TabControl::getSelectedTabIndex() const
  405. {
  406. return _selectedItem == nullptr ? -1 : indexOfTabHeader(_selectedItem->header);
  407. }
  408. TabHeader::TabHeader()
  409. : _tabLabelRender(nullptr)
  410. , _tabLabelFontSize(12)
  411. , _tabView(nullptr)
  412. , _tabSelectedEvent(nullptr)
  413. , _fontType(FontType::SYSTEM)
  414. {
  415. }
  416. TabHeader::~TabHeader()
  417. {
  418. _tabLabelRender = nullptr;
  419. _tabView = nullptr;
  420. _tabSelectedEvent = nullptr;
  421. }
  422. TabHeader * TabHeader::create()
  423. {
  424. TabHeader* tabcell = new (std::nothrow) TabHeader();
  425. if (tabcell != nullptr && tabcell->init())
  426. {
  427. tabcell->_frontCrossRenderer->setVisible(false); // _isSelected == false
  428. tabcell->_anchorPoint = Vec2(.5f, 0);
  429. tabcell->autorelease();
  430. return tabcell;
  431. }
  432. CC_SAFE_DELETE(tabcell);
  433. return nullptr;
  434. }
  435. TabHeader * TabHeader::create(const std::string& titleStr,
  436. const std::string & backGround,
  437. const std::string & cross, TextureResType texType)
  438. {
  439. TabHeader *tabcell = new (std::nothrow) TabHeader;
  440. if (tabcell && tabcell->init(backGround,
  441. "",
  442. cross,
  443. "",
  444. "",
  445. texType))
  446. {
  447. tabcell->_frontCrossRenderer->setVisible(false);
  448. tabcell->_tabLabelRender->setString(titleStr);
  449. tabcell->_anchorPoint = Vec2(.5f, 0);
  450. tabcell->autorelease();
  451. return tabcell;
  452. }
  453. CC_SAFE_DELETE(tabcell);
  454. return nullptr;
  455. }
  456. TabHeader* TabHeader::create(const std::string& titleStr,
  457. const std::string& backGround,
  458. const std::string& backGroundSelected,
  459. const std::string& cross,
  460. const std::string& backGroundDisabled,
  461. const std::string& frontCrossDisabled,
  462. TextureResType texType /*= TextureResType::LOCAL*/)
  463. {
  464. TabHeader *tabcell = new (std::nothrow) TabHeader;
  465. if (tabcell && tabcell->init(backGround,
  466. backGroundSelected,
  467. cross,
  468. backGroundDisabled,
  469. frontCrossDisabled,
  470. texType))
  471. {
  472. tabcell->_frontCrossRenderer->setVisible(false);
  473. tabcell->_tabLabelRender->setString(titleStr);
  474. tabcell->_anchorPoint = Vec2(.5f, 0);
  475. tabcell->autorelease();
  476. return tabcell;
  477. }
  478. CC_SAFE_DELETE(tabcell);
  479. return nullptr;
  480. }
  481. void TabHeader::initRenderer()
  482. {
  483. _backGroundBoxRenderer = Sprite::create();
  484. _backGroundSelectedBoxRenderer = Sprite::create();
  485. _frontCrossRenderer = Sprite::create();
  486. _backGroundBoxDisabledRenderer = Sprite::create();
  487. _frontCrossDisabledRenderer = Sprite::create();
  488. _tabLabelRender = Label::create();
  489. addProtectedChild(_backGroundBoxRenderer, -2, -1);
  490. addProtectedChild(_backGroundSelectedBoxRenderer, -2, -1);
  491. addProtectedChild(_frontCrossRenderer, -2, -1);
  492. addProtectedChild(_backGroundBoxDisabledRenderer, -2, -1);
  493. addProtectedChild(_frontCrossDisabledRenderer, -2, -1);
  494. addProtectedChild(_tabLabelRender, -1, -1);
  495. }
  496. void TabHeader::setTitleText(const std::string& text)
  497. {
  498. if (text == getTitleText())
  499. {
  500. return;
  501. }
  502. _tabLabelRender->setString(text);
  503. updateContentSize();
  504. _tabLabelRender->setPosition(_contentSize * 0.5f);
  505. }
  506. std::string TabHeader::getTitleText() const
  507. {
  508. if (nullptr == _tabLabelRender)
  509. {
  510. return "";
  511. }
  512. return _tabLabelRender->getString();
  513. }
  514. void TabHeader::setTitleColor(const Color4B& color)
  515. {
  516. _tabLabelRender->setTextColor(color);
  517. }
  518. const Color4B& TabHeader::getTitleColor() const
  519. {
  520. if (nullptr == _tabLabelRender)
  521. {
  522. return Color4B::WHITE;
  523. }
  524. return _tabLabelRender->getTextColor();
  525. }
  526. void TabHeader::setTitleFontSize(float size)
  527. {
  528. _tabLabelFontSize = size;
  529. if (_fontType == FontType::SYSTEM)
  530. {
  531. _tabLabelRender->setSystemFontSize(_tabLabelFontSize);
  532. }
  533. else if (_fontType == FontType::TTF)
  534. {
  535. TTFConfig config = _tabLabelRender->getTTFConfig();
  536. config.fontSize = _tabLabelFontSize;
  537. _tabLabelRender->setTTFConfig(config);
  538. }
  539. //we can't change font size of BMFont.
  540. if (FontType::BMFONT != _fontType)
  541. {
  542. updateContentSize();
  543. }
  544. }
  545. float TabHeader::getTitleFontSize() const
  546. {
  547. return _tabLabelFontSize;
  548. }
  549. void TabHeader::updateContentSize()
  550. {
  551. ProtectedNode::setContentSize(_customSize);
  552. onSizeChanged();
  553. }
  554. void TabHeader::setTitleFontName(const std::string& fontName)
  555. {
  556. if (FileUtils::getInstance()->isFileExist(fontName))
  557. {
  558. std::string lowerCasedFontName = fontName;
  559. std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower);
  560. if (lowerCasedFontName.find(".fnt") != std::string::npos)
  561. {
  562. _tabLabelRender->setBMFontFilePath(fontName);
  563. _fontType = FontType::BMFONT;
  564. }
  565. else
  566. {
  567. TTFConfig config = _tabLabelRender->getTTFConfig();
  568. config.fontFilePath = fontName;
  569. config.fontSize = _tabLabelFontSize;
  570. _tabLabelRender->setTTFConfig(config);
  571. _fontType = FontType::TTF;
  572. }
  573. }
  574. else
  575. {
  576. _tabLabelRender->setSystemFontName(fontName);
  577. if (_fontType == FontType::TTF)
  578. {
  579. _tabLabelRender->requestSystemFontRefresh();
  580. }
  581. _tabLabelRender->setSystemFontSize(_tabLabelFontSize);
  582. _fontType = FontType::SYSTEM;
  583. }
  584. this->updateContentSize();
  585. }
  586. Label* TabHeader::getTitleRenderer()const
  587. {
  588. return _tabLabelRender;
  589. }
  590. std::string TabHeader::getTitleFontName() const
  591. {
  592. if (this->_fontType == FontType::SYSTEM)
  593. {
  594. return _tabLabelRender->getSystemFontName();
  595. }
  596. else if (this->_fontType == FontType::TTF)
  597. {
  598. return _tabLabelRender->getTTFConfig().fontFilePath;
  599. }
  600. else
  601. {
  602. return _tabLabelRender->getBMFontFilePath();
  603. }
  604. return "";
  605. }
  606. void TabHeader::onSizeChanged()
  607. {
  608. AbstractCheckButton::onSizeChanged();
  609. _tabLabelRender->setPosition(_contentSize * 0.5f);
  610. }
  611. void TabHeader::releaseUpEvent(Touch* touch)
  612. {
  613. Widget::releaseUpEvent(touch);
  614. if (!_isSelected)
  615. {
  616. setSelected(true);
  617. dispatchSelectChangedEvent(true);
  618. }
  619. }
  620. void TabHeader::dispatchSelectChangedEvent(bool select)
  621. {
  622. if (_tabView == nullptr)
  623. return;
  624. EventType eventType = (select ? EventType::SELECTED : EventType::UNSELECTED);
  625. if (_tabSelectedEvent != nullptr)
  626. {
  627. int index = _tabView->indexOfTabHeader(this);
  628. if (index != -1)
  629. _tabSelectedEvent(index, eventType);
  630. }
  631. if (_ccEventCallback != nullptr)
  632. {
  633. _ccEventCallback(this, static_cast<int>(eventType));
  634. }
  635. }
  636. int TabHeader::getIndexInTabControl() const
  637. {
  638. if (_tabView == nullptr)
  639. return -1;
  640. return _tabView->indexOfTabHeader(this);
  641. }
  642. void TabHeader::copySpecialProperties(Widget* model)
  643. {
  644. auto header = dynamic_cast<TabHeader*>(model);
  645. if (header != nullptr)
  646. {
  647. AbstractCheckButton::copySpecialProperties(model);
  648. _fontType = header->_fontType;
  649. _tabLabelFontSize = header->_tabLabelFontSize;
  650. _tabSelectedEvent = header->_tabSelectedEvent;
  651. }
  652. }
  653. }
  654. NS_CC_END