axmol/cocos/gui/UILoadingBar.cpp

360 lines
9.7 KiB
C++
Raw Normal View History

2013-09-20 22:23:13 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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 "gui/UILoadingBar.h"
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
2013-09-20 22:23:13 +08:00
namespace gui {
2013-09-20 22:23:13 +08:00
2013-11-06 16:04:06 +08:00
2013-09-20 22:23:13 +08:00
#define DYNAMIC_CAST_CCSPRITE dynamic_cast<cocos2d::Sprite*>(_barRenderer)
UILoadingBar::UILoadingBar():
_barType(LoadingBarTypeLeft),
_percent(100),
_totalLength(0),
2013-11-14 11:37:46 +08:00
_barRenderer(nullptr),
2013-09-20 22:23:13 +08:00
_renderBarTexType(UI_TEX_TYPE_LOCAL),
2013-11-06 16:04:06 +08:00
_barRendererTextureSize(cocos2d::Size::ZERO),
2013-09-20 22:23:13 +08:00
_scale9Enabled(false),
_prevIgnoreSize(true),
2013-11-06 16:04:06 +08:00
_capInsets(cocos2d::Rect::ZERO),
2013-09-20 22:23:13 +08:00
_textureFile("")
{
}
UILoadingBar::~UILoadingBar()
{
}
UILoadingBar* UILoadingBar::create()
{
UILoadingBar* widget = new UILoadingBar();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-20 22:23:13 +08:00
}
void UILoadingBar::initRenderer()
{
UIWidget::initRenderer();
2013-11-06 16:04:06 +08:00
_barRenderer = cocos2d::Sprite::create();
2013-09-20 22:23:13 +08:00
_renderer->addChild(_barRenderer);
2013-11-06 16:04:06 +08:00
_barRenderer->setAnchorPoint(cocos2d::Point(0.0,0.5));
2013-09-20 22:23:13 +08:00
}
void UILoadingBar::setDirection(LoadingBarType dir)
{
if (_barType == dir)
{
return;
}
_barType = dir;
switch (_barType)
{
case LoadingBarTypeLeft:
2013-11-06 16:04:06 +08:00
_barRenderer->setAnchorPoint(cocos2d::Point(0.0f,0.5f));
_barRenderer->setPosition(cocos2d::Point(-_totalLength*0.5f,0.0f));
2013-09-20 22:23:13 +08:00
if (!_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setFlippedX(false);
2013-09-20 22:23:13 +08:00
}
break;
case LoadingBarTypeRight:
2013-11-06 16:04:06 +08:00
_barRenderer->setAnchorPoint(cocos2d::Point(1.0f,0.5f));
_barRenderer->setPosition(cocos2d::Point(_totalLength*0.5f,0.0f));
2013-09-20 22:23:13 +08:00
if (!_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setFlippedX(true);
2013-09-20 22:23:13 +08:00
}
break;
}
}
int UILoadingBar::getDirection()
{
return _barType;
}
void UILoadingBar::loadTexture(const char* texture,TextureResType texType)
{
if (!texture || strcmp(texture, "") == 0)
{
return;
}
_renderBarTexType = texType;
_textureFile = texture;
switch (_renderBarTexType)
{
case UI_TEX_TYPE_LOCAL:
if (_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->initWithFile(texture);
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setCapInsets(_capInsets);
2013-09-20 22:23:13 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->initWithFile(texture);
2013-09-20 22:23:13 +08:00
}
break;
case UI_TEX_TYPE_PLIST:
if (_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->initWithSpriteFrameName(texture);
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setCapInsets(_capInsets);
2013-09-20 22:23:13 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->initWithSpriteFrameName(texture);
2013-09-20 22:23:13 +08:00
}
break;
default:
break;
}
if (_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setColor(getColor());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setOpacity(getOpacity());
2013-09-20 22:23:13 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setColor(getColor());
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setOpacity(getOpacity());
2013-09-20 22:23:13 +08:00
}
2013-11-06 16:04:06 +08:00
_barRendererTextureSize = _barRenderer->getContentSize();
2013-09-20 22:23:13 +08:00
switch (_barType)
{
case LoadingBarTypeLeft:
2013-11-06 16:04:06 +08:00
_barRenderer->setAnchorPoint(cocos2d::Point(0.0f,0.5f));
2013-09-20 22:23:13 +08:00
if (!_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setFlippedX(false);
2013-09-20 22:23:13 +08:00
}
break;
case LoadingBarTypeRight:
2013-11-06 16:04:06 +08:00
_barRenderer->setAnchorPoint(cocos2d::Point(1.0f,0.5f));
2013-09-20 22:23:13 +08:00
if (!_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setFlippedX(true);
2013-09-20 22:23:13 +08:00
}
break;
}
barRendererScaleChangedWithSize();
}
void UILoadingBar::setScale9Enabled(bool enabled)
{
if (_scale9Enabled == enabled)
{
return;
}
_scale9Enabled = enabled;
_renderer->removeChild(_barRenderer, true);
2013-11-14 11:37:46 +08:00
_barRenderer = nullptr;
2013-09-20 22:23:13 +08:00
if (_scale9Enabled)
{
2013-11-06 16:04:06 +08:00
_barRenderer = cocos2d::extension::Scale9Sprite::create();
2013-09-20 22:23:13 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
_barRenderer = cocos2d::Sprite::create();
2013-09-20 22:23:13 +08:00
}
loadTexture(_textureFile.c_str(),_renderBarTexType);
_renderer->addChild(_barRenderer);
if (_scale9Enabled)
{
bool ignoreBefore = _ignoreSize;
ignoreContentAdaptWithSize(false);
_prevIgnoreSize = ignoreBefore;
}
else
{
ignoreContentAdaptWithSize(_prevIgnoreSize);
}
setCapInsets(_capInsets);
}
2013-11-06 16:04:06 +08:00
void UILoadingBar::setCapInsets(const cocos2d::Rect &capInsets)
2013-09-20 22:23:13 +08:00
{
_capInsets = capInsets;
if (!_scale9Enabled)
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setCapInsets(capInsets);
2013-09-20 22:23:13 +08:00
}
void UILoadingBar::setPercent(int percent)
{
if ( percent < 0 || percent > 100)
{
return;
}
if (_totalLength <= 0)
{
return;
}
_percent = percent;
float res = _percent/100.0;
int x = 0, y = 0;
switch (_renderBarTexType)
{
case UI_TEX_TYPE_PLIST:
{
2013-11-06 16:04:06 +08:00
cocos2d::Sprite* barNode = DYNAMIC_CAST_CCSPRITE;
2013-09-20 22:23:13 +08:00
if (barNode)
{
2013-11-06 16:04:06 +08:00
cocos2d::Point to = barNode->getTextureRect().origin;
2013-09-20 22:23:13 +08:00
x = to.x;
y = to.y;
}
break;
}
default:
break;
}
if (_scale9Enabled)
{
setScale9Scale();
}
else
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_barRenderer)->setTextureRect(cocos2d::Rect(x, y, _barRendererTextureSize.width * res, _barRendererTextureSize.height));
2013-09-20 22:23:13 +08:00
}
}
int UILoadingBar::getPercent()
{
return _percent;
}
void UILoadingBar::onSizeChanged()
{
barRendererScaleChangedWithSize();
}
void UILoadingBar::ignoreContentAdaptWithSize(bool ignore)
{
if (!_scale9Enabled || (_scale9Enabled && !ignore))
{
UIWidget::ignoreContentAdaptWithSize(ignore);
_prevIgnoreSize = ignore;
}
}
2013-11-06 16:04:06 +08:00
const cocos2d::Size& UILoadingBar::getContentSize() const
2013-09-20 22:23:13 +08:00
{
return _barRendererTextureSize;
}
2013-11-06 16:04:06 +08:00
cocos2d::Node* UILoadingBar::getVirtualRenderer()
2013-09-20 22:23:13 +08:00
{
return _barRenderer;
}
void UILoadingBar::barRendererScaleChangedWithSize()
{
if (_ignoreSize)
{
if (!_scale9Enabled)
{
_totalLength = _barRendererTextureSize.width;
_barRenderer->setScale(1.0f);
_size = _barRendererTextureSize;
}
}
else
{
_totalLength = _size.width;
if (_scale9Enabled)
{
setScale9Scale();
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _barRendererTextureSize;
2013-09-20 22:23:13 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
_barRenderer->setScale(1.0f);
return;
}
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
_barRenderer->setScaleX(scaleX);
_barRenderer->setScaleY(scaleY);
}
}
switch (_barType)
{
case LoadingBarTypeLeft:
2013-11-06 16:04:06 +08:00
_barRenderer->setPosition(cocos2d::Point(-_totalLength * 0.5f, 0.0f));
2013-09-20 22:23:13 +08:00
break;
case LoadingBarTypeRight:
2013-11-06 16:04:06 +08:00
_barRenderer->setPosition(cocos2d::Point(_totalLength * 0.5f, 0.0f));
2013-09-20 22:23:13 +08:00
break;
default:
break;
}
}
void UILoadingBar::setScale9Scale()
{
float width = (float)(_percent) / 100 * _totalLength;
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_barRenderer)->setPreferredSize(cocos2d::Size(width, _size.height));
2013-09-20 22:23:13 +08:00
}
const char* UILoadingBar::getDescription() const
{
return "LoadingBar";
}
2013-11-06 16:04:06 +08:00
UIWidget* UILoadingBar::createCloneInstance()
{
return UILoadingBar::create();
}
void UILoadingBar::copySpecialProperties(UIWidget *widget)
{
UILoadingBar* loadingBar = dynamic_cast<UILoadingBar*>(widget);
if (loadingBar)
{
_prevIgnoreSize = loadingBar->_prevIgnoreSize;
setScale9Enabled(loadingBar->_scale9Enabled);
loadTexture(loadingBar->_textureFile.c_str(), loadingBar->_renderBarTexType);
setCapInsets(loadingBar->_capInsets);
setPercent(loadingBar->_percent);
}
}
}