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