axmol/cocos/ui/UITabControl.cpp

736 lines
22 KiB
C++
Raw Normal View History

Squashed commit of the following: commit 87b7c3e404a9b6cce6d4f6bc8565e7feef429ee0 Author: geron-cn <c.ever.fallen@gmail.com> Date: Thu Mar 3 13:43:04 2016 +0800 change enum to enum class use tabitem as pointer to replace _currentTabItemIndex commit c0830cc581b8a07d6eed63268bf5be6a828c113a Merge: 3663156 166931a Author: geron-cn <c.ever.fallen@gmail.com> Date: Thu Mar 3 11:48:44 2016 +0800 Merge remote-tracking branch 'gero/tabview' into tabcontrol commit 366315624143ff01c3e2b22b415ab79050e381e4 Author: geron-cn <c.ever.fallen@gmail.com> Date: Thu Mar 3 11:48:15 2016 +0800 Squashed commit of the following: commit 166931a28c8736ab58b7c4319c1139b5e4865a9e Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 21:35:06 2016 +0800 fix commit e0eb069846a8ceb4b726c3c945801c944404b102 Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 21:30:44 2016 +0800 fix active deactive in tabcontrol commit aeb1c857cab23299e44a86ac1f627c7bd5fa5a6d Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 20:16:22 2016 +0800 fix active -1 commit e57796cbbae851e8cff52656b3265c80c2e8f6f7 Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 16:00:51 2016 +0800 fix merge file commit cdd44f1c577ad190bc175896107a48fbb36b65bb Merge: 1658ad6 6bf55ed Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 15:57:48 2016 +0800 Merge branch 'v3' of github.com:cocos2d/cocos2d-x into tabview # Conflicts: # cocos/ui/CMakeLists.txt commit 1658ad6f4a2bd0889d6cc020072d7fb9192cafc3 Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 15:27:31 2016 +0800 configure for win8.1 commit ec236bff711b72952d48ec4d8b9137b4812b7a72 Author: geron-cn <c.ever.fallen@gmail.com> Date: Tue Mar 1 10:20:23 2016 +0800 configure for iOS mac twos android linux commit 9d6678a4388461a791abe968fa637df2fc6839d3 Author: geron-cn <c.ever.fallen@gmail.com> Date: Tue Mar 1 10:02:17 2016 +0800 add lack files commit 1f2b5a3e989b05f67a705c81a20c75d3e0ed88a1 Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 22:05:22 2016 +0800 add test case for TabControl commit 97b8d8d87a37881b33ebe90478392150ef341b3e Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 15:43:29 2016 +0800 add const to getter setter commit f49a0483049d2aa7e21bcbe10e553c207adc4bdf Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 14:50:04 2016 +0800 format the comments commit a1989a8f89d33ef8d9eefff6c8117b45977308ad Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:51:06 2016 +0800 remove reference commit 1a573e68d30cc56bed9b2338cbbd311e2541fdfd Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:40:04 2016 +0800 configure for win8.1 commit c015ac54028534202f62ff85f9a9d4b62b12801e Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:27:54 2016 +0800 fix error commit 91c4ac9ae73d4e7efcee389d7af5ec7178c94836 Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:25:51 2016 +0800 add interface commit eaf3e237cdcb9c49cd4161691471a8761f8dbdae Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 03:21:22 2016 +0800 fix compile error for tw-os commit 3bfaa2976751e844a591ae5524bfd81249927c7c Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 03:04:23 2016 +0800 fix linux compile error commit fafe3ad660e644924fd02665b77e1df4076a4802 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:54:34 2016 +0800 re-indent code with X-Code commit f8dfe5b551b0bb0a1a1d7b15556db700a35651a5 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:37:51 2016 +0800 add reader configure for android commit 07033f7b6074edf8a333e40c2382a09a2b2325e4 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:22:00 2016 +0800 format code, remove some warning commit 22ed160628f1946d0ccbdf6fc066ec80e56ae611 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:02:27 2016 +0800 configure for mac project commit de21cea12c0a898ea95f9951022be85bdddeefaf Author: geron-cn <c.ever.fallen@gmail.com> Date: Sat Feb 27 18:28:10 2016 +0800 add comment for apis commit b9cebf881a5cd4ecbb740b98cff7dc61183b1db2 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sat Feb 27 15:32:46 2016 +0800 fix parse objects in container commit 53bcce68b053a5b918a29735fed393c210d9cc2c Author: geron-cn <c.ever.fallen@gmail.com> Date: Fri Feb 26 22:10:54 2016 +0800 fix flatbuffer parse commit 9cbdbdc5d046751ae1398dd622ccab0e2797d055 Author: geron-cn <c.ever.fallen@gmail.com> Date: Fri Feb 26 18:26:07 2016 +0800 Init ui::TabControl commit 166931a28c8736ab58b7c4319c1139b5e4865a9e Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 21:35:06 2016 +0800 fix commit e0eb069846a8ceb4b726c3c945801c944404b102 Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 21:30:44 2016 +0800 fix active deactive in tabcontrol commit aeb1c857cab23299e44a86ac1f627c7bd5fa5a6d Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 20:16:22 2016 +0800 fix active -1 commit e57796cbbae851e8cff52656b3265c80c2e8f6f7 Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 16:00:51 2016 +0800 fix merge file commit cdd44f1c577ad190bc175896107a48fbb36b65bb Merge: 1658ad6 6bf55ed Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 15:57:48 2016 +0800 Merge branch 'v3' of github.com:cocos2d/cocos2d-x into tabview # Conflicts: # cocos/ui/CMakeLists.txt commit 1658ad6f4a2bd0889d6cc020072d7fb9192cafc3 Author: geron-cn <c.ever.fallen@gmail.com> Date: Wed Mar 2 15:27:31 2016 +0800 configure for win8.1 commit ec236bff711b72952d48ec4d8b9137b4812b7a72 Author: geron-cn <c.ever.fallen@gmail.com> Date: Tue Mar 1 10:20:23 2016 +0800 configure for iOS mac twos android linux commit 9d6678a4388461a791abe968fa637df2fc6839d3 Author: geron-cn <c.ever.fallen@gmail.com> Date: Tue Mar 1 10:02:17 2016 +0800 add lack files commit 1f2b5a3e989b05f67a705c81a20c75d3e0ed88a1 Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 22:05:22 2016 +0800 add test case for TabControl commit 97b8d8d87a37881b33ebe90478392150ef341b3e Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 15:43:29 2016 +0800 add const to getter setter commit f49a0483049d2aa7e21bcbe10e553c207adc4bdf Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 14:50:04 2016 +0800 format the comments commit a1989a8f89d33ef8d9eefff6c8117b45977308ad Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:51:06 2016 +0800 remove reference commit 1a573e68d30cc56bed9b2338cbbd311e2541fdfd Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:40:04 2016 +0800 configure for win8.1 commit c015ac54028534202f62ff85f9a9d4b62b12801e Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:27:54 2016 +0800 fix error commit 91c4ac9ae73d4e7efcee389d7af5ec7178c94836 Author: geron-cn <c.ever.fallen@gmail.com> Date: Mon Feb 29 11:25:51 2016 +0800 add interface commit eaf3e237cdcb9c49cd4161691471a8761f8dbdae Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 03:21:22 2016 +0800 fix compile error for tw-os commit 3bfaa2976751e844a591ae5524bfd81249927c7c Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 03:04:23 2016 +0800 fix linux compile error commit fafe3ad660e644924fd02665b77e1df4076a4802 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:54:34 2016 +0800 re-indent code with X-Code commit f8dfe5b551b0bb0a1a1d7b15556db700a35651a5 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:37:51 2016 +0800 add reader configure for android commit 07033f7b6074edf8a333e40c2382a09a2b2325e4 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:22:00 2016 +0800 format code, remove some warning commit 22ed160628f1946d0ccbdf6fc066ec80e56ae611 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sun Feb 28 02:02:27 2016 +0800 configure for mac project commit de21cea12c0a898ea95f9951022be85bdddeefaf Author: geron-cn <c.ever.fallen@gmail.com> Date: Sat Feb 27 18:28:10 2016 +0800 add comment for apis commit b9cebf881a5cd4ecbb740b98cff7dc61183b1db2 Author: geron-cn <c.ever.fallen@gmail.com> Date: Sat Feb 27 15:32:46 2016 +0800 fix parse objects in container commit 53bcce68b053a5b918a29735fed393c210d9cc2c Author: geron-cn <c.ever.fallen@gmail.com> Date: Fri Feb 26 22:10:54 2016 +0800 fix flatbuffer parse commit 9cbdbdc5d046751ae1398dd622ccab0e2797d055 Author: geron-cn <c.ever.fallen@gmail.com> Date: Fri Feb 26 18:26:07 2016 +0800 Init ui::TabControl
2016-03-03 14:17:48 +08:00
/****************************************************************************
Copyright (c) 2015 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "platform/CCFileUtils.h"
#include "2d/CCSprite.h"
#include "2d/CCLabel.h"
#include "ui/UILayout.h"
#include "UITabControl.h"
NS_CC_BEGIN
namespace ui
{
TabControl::TabControl()
: _selectedItem(nullptr)
, _headerHeight(20)
, _headerWidth(50)
, _headerDockPlace(Dock::TOP)
, _containerPosition(Vec2::ZERO)
, _containerSize(Size::ZERO)
, _currentHeaderZoom(0.1f)
, _ignoreHeaderTextureSize(true)
{
this->_anchorPoint = Vec2(0.f, 0.f);
setContentSize(Size(200, 200));
}
TabControl::~TabControl()
{
for (auto& item : _tabItems)
{
if (item)
CC_SAFE_DELETE(item);
}
_tabItems.clear();
}
void TabControl::insertTab(int index, TabHeader* header, Layout* container)
{
int cellSize = (int)_tabItems.size();
if (index > cellSize)
{
CCLOG("%s", "insert index error");
return;
}
addProtectedChild(container, -3, -1);
addProtectedChild(header, -2, -1);
_tabItems.insert(_tabItems.begin() + index, new TabItem(header, container));
header->_tabView = this;
header->_tabSelectedEvent = CC_CALLBACK_2(TabControl::dispatchSelectedTabChanged, this); // binding tab selected event
initAfterInsert(index);
}
void TabControl::initAfterInsert(int index)
{
auto cellSize = _tabItems.size();
auto tabItem = _tabItems.at(index);
auto headerCell = tabItem->header;
auto container = tabItem->container;
if (cellSize == 1)
{
setSelectTab(0);
}
else
{
headerCell->setSelected(false);
container->setVisible(false);
}
headerCell->setContentSize(Size(_headerWidth, _headerHeight));
headerCell->setAnchorPoint(getHeaderAnchorWithDock());
if (headerCell->isIgnoreContentAdaptWithSize() == _ignoreHeaderTextureSize)
{
headerCell->ignoreContentAdaptWithSize(!_ignoreHeaderTextureSize);
if (_ignoreHeaderTextureSize)
headerCell->setContentSize(Size(_headerWidth, _headerHeight));
headerCell->backGroundDisabledTextureScaleChangedWithSize();
headerCell->backGroundSelectedTextureScaleChangedWithSize();
headerCell->backGroundDisabledTextureScaleChangedWithSize();
headerCell->frontCrossTextureScaleChangedWithSize();
headerCell->frontCrossDisabledTextureScaleChangedWithSize();
}
initTabHeadersPos(index);
if (_containerSize.equals(Size::ZERO))
initContainers();
else
{
container->setPosition(_containerPosition);
container->setContentSize(_containerSize);
}
}
void TabControl::removeTab(int index)
{
int cellSize = (int)_tabItems.size();
if (cellSize == 0 || index >= cellSize)
{
CCLOG("%s", "no tab or remove index error");
return;
}
auto tabItem = _tabItems.at(index);
if (tabItem == _selectedItem)
_selectedItem = nullptr;
auto header = tabItem->header;
auto container = tabItem->container;
if (tabItem)
CC_SAFE_DELETE(tabItem);
_tabItems.erase(_tabItems.begin() + index);
if (header != nullptr)
{
header->_tabSelectedEvent = nullptr;
header->_tabView = nullptr;
removeProtectedChild(header);
removeProtectedChild(container);
}
initTabHeadersPos(index);
}
size_t TabControl::getTabCount() const
{
return _tabItems.size();
}
void TabControl::setHeaderWidth(float headerWith)
{
_headerWidth = headerWith;
if (_headerDockPlace == Dock::TOP || _headerDockPlace == Dock::BOTTOM)
initTabHeadersPos(0);
if (_headerDockPlace == Dock::LEFT || _headerDockPlace == Dock::RIGHT)
initContainers();
}
void TabControl::setHeaderHeight(float headerHeigt)
{
_headerHeight = headerHeigt;
if (_headerDockPlace == Dock::LEFT || _headerDockPlace == Dock::RIGHT)
initTabHeadersPos(0);
if (_headerDockPlace == Dock::TOP || _headerDockPlace == Dock::BOTTOM)
initContainers();
}
void TabControl::setHeaderDockPlace(TabControl::Dock dockPlace)
{
if (_headerDockPlace != dockPlace)
{
_headerDockPlace = dockPlace;
initTabHeadersPos(0);
initContainers();
auto anpoint = getHeaderAnchorWithDock();
for (auto& item : _tabItems)
{
item->header->setAnchorPoint(anpoint);
}
}
}
cocos2d::Vec2 TabControl::getHeaderAnchorWithDock() const
{
Vec2 anpoint(.5f, .0f);
switch (_headerDockPlace)
{
case Dock::TOP:
break;
case Dock::LEFT:
anpoint.x = 1.f;
anpoint.y = .5f;
break;
case Dock::BOTTOM:
anpoint.x = .5f;
anpoint.y = 1.f;
break;
case Dock::RIGHT:
anpoint.x = 0.f;
anpoint.y = .5f;
break;
default:
break;
}
return anpoint;
}
void TabControl::onSizeChanged()
{
initTabHeadersPos(0);
initContainers();
}
void TabControl::initTabHeadersPos(int startIndex)
{
int cellSize = (int)_tabItems.size();
if (startIndex >= cellSize)
return;
float originX = _headerWidth * .5f;
float originY = _contentSize.height - _headerHeight;
Vec2 deltaPos(0.f, 0.f);
switch (_headerDockPlace)
{
case Dock::TOP:
deltaPos.x = _headerWidth;
break;
case Dock::LEFT:
originX = _headerWidth;
originY = _contentSize.height - _headerHeight * .5f;
deltaPos.y = 0 - _headerHeight;
break;
case Dock::BOTTOM:
originY = _headerHeight;
deltaPos.x = _headerWidth;
break;
case Dock::RIGHT:
originX = _contentSize.width - _headerWidth;
originY = _contentSize.height - _headerHeight * .5f;
deltaPos.y = 0 - _headerHeight;
break;
default:
break;
}
for (int cellI = startIndex; cellI < cellSize; cellI++)
{
auto headerCell = _tabItems.at(cellI)->header;
headerCell->setPosition(Vec2(originX + cellI* deltaPos.x, originY + cellI * deltaPos.y));
}
}
void TabControl::initContainers()
{
switch (_headerDockPlace)
{
case Dock::TOP:
_containerPosition = Vec2(0, 0);
_containerSize = Size(_contentSize.width, _contentSize.height - _headerHeight);
break;
case Dock::LEFT:
_containerPosition = Vec2(_headerWidth, 0);
_containerSize = Size(_contentSize.width - _headerWidth, _contentSize.height);
break;
case Dock::BOTTOM:
_containerPosition = Vec2(0, _headerHeight);
_containerSize = Size(_contentSize.width, _contentSize.height - _headerHeight);
break;
case Dock::RIGHT:
_containerPosition = Vec2(0, 0);
_containerSize = Size(_contentSize.width - _headerWidth, _contentSize.height);
break;
default:
break;
}
for (auto& tabItem : _tabItems)
{
Layout* container = tabItem->container;
container->setPosition(_containerPosition);
container->setContentSize(_containerSize);
}
}
TabHeader* TabControl::getTabHeader(int index) const
{
if (index >= (int)getTabCount())
return nullptr;
return _tabItems.at(index)->header;
}
Layout* TabControl::getTabContainer(int index) const
{
if (index >= (int)getTabCount())
return nullptr;
return _tabItems.at(index)->container;
}
void TabControl::dispatchSelectedTabChanged(int tabIndex, TabHeader::EventType eventType)
{
if (eventType == TabHeader::EventType::SELECTED)
{
if (tabIndex <= -1 || tabIndex >= (int)_tabItems.size())
{
deactiveHeader(_selectedItem);
_selectedItem = nullptr;
}
else
{
deactiveHeader(_selectedItem);
auto tabItem = _tabItems.at(tabIndex);
activeHeader(tabItem);
_selectedItem = tabItem;
}
}
else if (eventType == TabHeader::EventType::UNSELECTED)
{
if (tabIndex >= 0 && tabIndex < (int)_tabItems.size())
{
auto tabItem = _tabItems.at(tabIndex);
if (tabItem == _selectedItem)
{
deactiveHeader(_selectedItem);
_selectedItem = nullptr;
}
}
}
if (_tabChangedCallback != nullptr)
{
int currentIndex = getSelectedTabIndex();
_tabChangedCallback(currentIndex, EventType::SELECT_CHANGED);
}
}
void TabControl::setTabChangedEventListener(const ccTabControlCallback& callBack)
{
_tabChangedCallback = callBack;
}
int TabControl::indexOfTabHeader(const TabHeader* tabCell) const
{
int n = (int)_tabItems.size();
for (auto i = 0; i < n; i++)
{
if (tabCell == _tabItems.at(i)->header)
{
return i;
}
}
return -1;
}
TabControl* TabControl::create()
{
TabControl* tabview = new (std::nothrow) TabControl();
if (tabview != nullptr && tabview->init())
{
tabview->autorelease();
return tabview;
}
CC_SAFE_DELETE(tabview);
return nullptr;
}
void TabControl::setSelectTab(int index)
{
dispatchSelectedTabChanged(index, TabHeader::EventType::SELECTED);
}
void TabControl::setSelectTab(TabHeader* tabHeader)
{
if (_selectedItem != nullptr && tabHeader == _selectedItem->header)
return;
setSelectTab(indexOfTabHeader(tabHeader));
}
void TabControl::setHeaderSelectedZoom(float zoom)
{
if (_currentHeaderZoom != zoom)
{
_currentHeaderZoom = zoom;
if (_selectedItem != nullptr)
{
auto currentHeader = _selectedItem->header;
currentHeader->setScale(1.0f + _currentHeaderZoom);
}
}
}
void TabControl::activeHeader(TabItem* item)
{
if (item != nullptr)
{
item->header->setLocalZOrder(-1);
item->header->setScale(1.0f + _currentHeaderZoom);
item->header->setSelected(true);
item->container->setVisible(true);
_reorderProtectedChildDirty = true;
}
}
void TabControl::deactiveHeader(TabItem* item)
{
if (item != nullptr)
{
item->header->setLocalZOrder(-2);
item->header->setScale(1.0f);
item->header->setSelected(false);
item->container->setVisible(false);
_reorderProtectedChildDirty = true;
}
}
void TabControl::copySpecialProperties(Widget* model)
{
auto srcTab = dynamic_cast<TabControl*>(model);
if (srcTab != nullptr)
{
Widget::copySpecialProperties(srcTab);
_headerWidth = srcTab->_headerWidth;
_headerHeight = srcTab->_headerHeight;
_headerDockPlace = srcTab->_headerDockPlace;
_currentHeaderZoom = srcTab->_currentHeaderZoom;
_tabChangedCallback = srcTab->_tabChangedCallback;
}
}
void TabControl::ignoreHeadersTextureSize(bool ignore)
{
if (_ignoreHeaderTextureSize == ignore)
return;
_ignoreHeaderTextureSize = ignore;
for (auto& item : _tabItems)
{
item->header->ignoreContentAdaptWithSize(!ignore);
if (ignore)
item->header->setContentSize(Size(_headerWidth, _headerHeight));
item->header->backGroundDisabledTextureScaleChangedWithSize();
item->header->backGroundSelectedTextureScaleChangedWithSize();
item->header->backGroundDisabledTextureScaleChangedWithSize();
item->header->frontCrossTextureScaleChangedWithSize();
item->header->frontCrossDisabledTextureScaleChangedWithSize();
}
}
int TabControl::getSelectedTabIndex() const
{
return _selectedItem == nullptr ? -1 : indexOfTabHeader(_selectedItem->header);
}
TabHeader::TabHeader()
: _tabLabelRender(nullptr)
, _tabLabelFontSize(12)
, _tabView(nullptr)
, _tabSelectedEvent(nullptr)
, _fontType(FontType::SYSTEM)
{
}
TabHeader::~TabHeader()
{
_tabLabelRender = nullptr;
_tabView = nullptr;
_tabSelectedEvent = nullptr;
}
TabHeader * TabHeader::create()
{
TabHeader* tabcell = new (std::nothrow) TabHeader();
if (tabcell != nullptr && tabcell->init())
{
tabcell->_frontCrossRenderer->setVisible(false); // _isSelected == fales
tabcell->_anchorPoint = Vec2(.5f, 0);
tabcell->autorelease();
return tabcell;
}
CC_SAFE_DELETE(tabcell);
return nullptr;
}
TabHeader * TabHeader::create(const std::string& titleStr,
const std::string & backGround,
const std::string & cross, TextureResType texType)
{
TabHeader *tabcell = new (std::nothrow) TabHeader;
if (tabcell && tabcell->init(backGround,
"",
cross,
"",
"",
texType))
{
tabcell->_frontCrossRenderer->setVisible(false);
tabcell->_tabLabelRender->setString(titleStr);
tabcell->_anchorPoint = Vec2(.5f, 0);
tabcell->autorelease();
return tabcell;
}
CC_SAFE_DELETE(tabcell);
return nullptr;
}
TabHeader* TabHeader::create(const std::string& titleStr,
const std::string& backGround,
const std::string& backGroundSelected,
const std::string& cross,
const std::string& backGroundDisabled,
const std::string& frontCrossDisabled,
TextureResType texType /*= TextureResType::LOCAL*/)
{
TabHeader *tabcell = new (std::nothrow) TabHeader;
if (tabcell && tabcell->init(backGround,
backGroundSelected,
cross,
backGroundDisabled,
frontCrossDisabled,
texType))
{
tabcell->_frontCrossRenderer->setVisible(false);
tabcell->_tabLabelRender->setString(titleStr);
tabcell->_anchorPoint = Vec2(.5f, 0);
tabcell->autorelease();
return tabcell;
}
CC_SAFE_DELETE(tabcell);
return nullptr;
}
void TabHeader::initRenderer()
{
_backGroundBoxRenderer = Sprite::create();
_backGroundSelectedBoxRenderer = Sprite::create();
_frontCrossRenderer = Sprite::create();
_backGroundBoxDisabledRenderer = Sprite::create();
_frontCrossDisabledRenderer = Sprite::create();
_tabLabelRender = Label::create();
addProtectedChild(_backGroundBoxRenderer, -2, -1);
addProtectedChild(_backGroundSelectedBoxRenderer, -2, -1);
addProtectedChild(_frontCrossRenderer, -2, -1);
addProtectedChild(_backGroundBoxDisabledRenderer, -2, -1);
addProtectedChild(_frontCrossDisabledRenderer, -2, -1);
addProtectedChild(_tabLabelRender, -1, -1);
}
void TabHeader::setTitleText(const std::string& text)
{
if (text == getTitleText())
{
return;
}
_tabLabelRender->setString(text);
updateContentSize();
_tabLabelRender->setPosition(_contentSize * 0.5f);
}
const std::string TabHeader::getTitleText() const
{
if (nullptr == _tabLabelRender)
{
return "";
}
return _tabLabelRender->getString();
}
void TabHeader::setTitleColor(const Color4B& color)
{
_tabLabelRender->setTextColor(color);
}
const Color4B& TabHeader::getTitleColor() const
{
if (nullptr == _tabLabelRender)
{
return Color4B::WHITE;
}
return _tabLabelRender->getTextColor();
}
void TabHeader::setTitleFontSize(float size)
{
_tabLabelFontSize = size;
if (_fontType == FontType::SYSTEM)
{
_tabLabelRender->setSystemFontSize(_tabLabelFontSize);
}
else if (_fontType == FontType::TTF)
{
TTFConfig config = _tabLabelRender->getTTFConfig();
config.fontSize = _tabLabelFontSize;
_tabLabelRender->setTTFConfig(config);
}
//we can't change font size of BMFont.
if (FontType::BMFONT != _fontType)
{
updateContentSize();
}
}
float TabHeader::getTitleFontSize() const
{
return _tabLabelFontSize;
}
void TabHeader::updateContentSize()
{
ProtectedNode::setContentSize(_customSize);
onSizeChanged();
}
void TabHeader::setTitleFontName(const std::string& fontName)
{
if (FileUtils::getInstance()->isFileExist(fontName))
{
std::string lowerCasedFontName = fontName;
std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower);
if (lowerCasedFontName.find(".fnt") != std::string::npos)
{
_tabLabelRender->setBMFontFilePath(fontName);
_fontType = FontType::BMFONT;
}
else
{
TTFConfig config = _tabLabelRender->getTTFConfig();
config.fontFilePath = fontName;
config.fontSize = _tabLabelFontSize;
_tabLabelRender->setTTFConfig(config);
_fontType = FontType::TTF;
}
}
else
{
_tabLabelRender->setSystemFontName(fontName);
if (_fontType == FontType::TTF)
{
_tabLabelRender->requestSystemFontRefresh();
}
_tabLabelRender->setSystemFontSize(_tabLabelFontSize);
_fontType = FontType::SYSTEM;
}
this->updateContentSize();
}
Label* TabHeader::getTitleRenderer()const
{
return _tabLabelRender;
}
const std::string TabHeader::getTitleFontName() const
{
if (this->_fontType == FontType::SYSTEM)
{
return _tabLabelRender->getSystemFontName();
}
else if (this->_fontType == FontType::TTF)
{
return _tabLabelRender->getTTFConfig().fontFilePath;
}
else
{
return _tabLabelRender->getBMFontFilePath();
}
return "";
}
void TabHeader::onSizeChanged()
{
AbstractCheckButton::onSizeChanged();
_tabLabelRender->setPosition(_contentSize * 0.5f);
}
void TabHeader::releaseUpEvent()
{
Widget::releaseUpEvent();
if (!_isSelected)
{
setSelected(true);
dispatchSelectChangedEvent(true);
}
}
void TabHeader::dispatchSelectChangedEvent(bool select)
{
if (_tabView == nullptr)
return;
EventType eventType = (select ? EventType::SELECTED : EventType::UNSELECTED);
if (_tabSelectedEvent != nullptr)
{
int index = _tabView->indexOfTabHeader(this);
if (index != -1)
_tabSelectedEvent(index, eventType);
}
if (_ccEventCallback != nullptr)
{
_ccEventCallback(this, static_cast<int>(eventType));
}
}
int TabHeader::getIndexInTabControl() const
{
if (_tabView == nullptr)
return -1;
return _tabView->indexOfTabHeader(this);
}
void TabHeader::copySpecialProperties(Widget* model)
{
auto header = dynamic_cast<TabHeader*>(model);
if (header != nullptr)
{
AbstractCheckButton::copySpecialProperties(model);
_fontType = header->_fontType;
_tabLabelFontSize = header->_tabLabelFontSize;
_tabSelectedEvent = header->_tabSelectedEvent;
}
}
}
NS_CC_END