issue #5057, refactor LoadingBar

This commit is contained in:
andyque 2014-05-12 10:12:22 +08:00
parent 43eb75fb1a
commit 7f15a14d55
6 changed files with 56 additions and 40 deletions

View File

@ -63,7 +63,7 @@ namespace cocostudio
}
/**/
loadingBar->setDirection(LoadingBarType(DICTOOL->getIntValue_json(options, "direction")));
loadingBar->setBarDirection(LoadingBar::Direction(DICTOOL->getIntValue_json(options, "direction")));
loadingBar->setPercent(DICTOOL->getIntValue_json(options, "percent"));

View File

@ -121,6 +121,9 @@ CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_CENTER_HORIZONT
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_TOP = ListView::Gravity::TOP;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_BOTTOM = ListView::Gravity::BOTTOM;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_CENTER_VERTICAL = ListView::Gravity::CENTER_VERTICAL;
CC_DEPRECATED_ATTRIBUTE const LoadingBar::Direction LoadingBarTypeLeft = LoadingBar::Direction::LEFT;
CC_DEPRECATED_ATTRIBUTE const LoadingBar::Direction LoadingBarTypeRight = LoadingBar::Direction::RIGHT;
CC_DEPRECATED_ATTRIBUTE typedef Widget::TextureResType TextureResType;
CC_DEPRECATED_ATTRIBUTE typedef Widget::PositionType PositionType;
@ -134,6 +137,7 @@ CC_DEPRECATED_ATTRIBUTE typedef LayoutParameter::Type LayoutParameterType;
CC_DEPRECATED_ATTRIBUTE typedef LinearLayoutParameter::LinearGravity LinearGravity;
CC_DEPRECATED_ATTRIBUTE typedef RelativeLayoutParameter::RelativeAlign RelativeAlign;
CC_DEPRECATED_ATTRIBUTE typedef ListView::Gravity ListViewGravity;
CC_DEPRECATED_ATTRIBUTE typedef LoadingBar::Direction LoadingBarType;
}

View File

@ -34,7 +34,7 @@ static const int BAR_RENDERER_Z = (-1);
IMPLEMENT_CLASS_GUI_INFO(LoadingBar)
LoadingBar::LoadingBar():
_barType(LoadingBarTypeLeft),
_direction(Direction::LEFT),
_percent(100),
_totalLength(0),
_barRenderer(nullptr),
@ -85,17 +85,22 @@ void LoadingBar::initRenderer()
_barRenderer->setAnchorPoint(Vector2(0.0,0.5));
}
void LoadingBar::setDirection(LoadingBarType dir)
void LoadingBar::setDirection(Direction direction)
{
if (_barType == dir)
this->setBarDirection(direction);
}
void LoadingBar::setBarDirection(cocos2d::ui::LoadingBar::Direction direction)
{
if (_direction == direction)
{
return;
}
_barType = dir;
switch (_barType)
_direction = direction;
switch (_direction)
{
case LoadingBarTypeLeft:
case Direction::LEFT:
_barRenderer->setAnchorPoint(Vector2(0.0f,0.5f));
_barRenderer->setPosition(Vector2(-_totalLength*0.5f,0.0f));
if (!_scale9Enabled)
@ -103,7 +108,7 @@ void LoadingBar::setDirection(LoadingBarType dir)
static_cast<Sprite*>(_barRenderer)->setFlippedX(false);
}
break;
case LoadingBarTypeRight:
case Direction::RIGHT:
_barRenderer->setAnchorPoint(Vector2(1.0f,0.5f));
_barRenderer->setPosition(Vector2(_totalLength*0.5f,0.0f));
if (!_scale9Enabled)
@ -112,11 +117,17 @@ void LoadingBar::setDirection(LoadingBarType dir)
}
break;
}
}
LoadingBar::Direction LoadingBar::getBarDirection()
{
return _direction;
}
int LoadingBar::getDirection()
{
return _barType;
return static_cast<int>(_direction);
}
void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
@ -159,22 +170,22 @@ int LoadingBar::getDirection()
updateRGBAToRenderer(_barRenderer);
_barRendererTextureSize = _barRenderer->getContentSize();
switch (_barType)
switch (_direction)
{
case LoadingBarTypeLeft:
_barRenderer->setAnchorPoint(Vector2(0.0f,0.5f));
if (!_scale9Enabled)
{
static_cast<Sprite*>(_barRenderer)->setFlippedX(false);
}
break;
case LoadingBarTypeRight:
_barRenderer->setAnchorPoint(Vector2(1.0f,0.5f));
if (!_scale9Enabled)
{
static_cast<Sprite*>(_barRenderer)->setFlippedX(true);
}
break;
case Direction::LEFT:
_barRenderer->setAnchorPoint(Vector2(0.0f,0.5f));
if (!_scale9Enabled)
{
static_cast<Sprite*>(_barRenderer)->setFlippedX(false);
}
break;
case Direction::RIGHT:
_barRenderer->setAnchorPoint(Vector2(1.0f,0.5f));
if (!_scale9Enabled)
{
static_cast<Sprite*>(_barRenderer)->setFlippedX(true);
}
break;
}
// barRendererScaleChangedWithSize();
updateContentSizeWithTextureSize(_barRendererTextureSize);
@ -331,12 +342,12 @@ void LoadingBar::barRendererScaleChangedWithSize()
_barRenderer->setScaleY(scaleY);
}
}
switch (_barType)
switch (_direction)
{
case LoadingBarTypeLeft:
case Direction::LEFT:
_barRenderer->setPosition(Vector2(0.0f, _contentSize.height / 2.0f));
break;
case LoadingBarTypeRight:
case Direction::RIGHT:
_barRenderer->setPosition(Vector2(_totalLength, _contentSize.height / 2.0f));
break;
default:
@ -385,7 +396,7 @@ void LoadingBar::copySpecialProperties(Widget *widget)
loadTexture(loadingBar->_textureFile, loadingBar->_renderBarTexType);
setCapInsets(loadingBar->_capInsets);
setPercent(loadingBar->_percent);
setDirection(loadingBar->_barType);
setDirection(loadingBar->_direction);
}
}

View File

@ -31,11 +31,6 @@ NS_CC_BEGIN
namespace ui {
typedef enum
{
LoadingBarTypeLeft,
LoadingBarTypeRight
}LoadingBarType;
/**
* @js NA
* @lua NA
@ -46,6 +41,11 @@ class LoadingBar : public Widget
DECLARE_CLASS_GUI_INFO
public:
enum class Direction
{
LEFT,
RIGHT
};
/**
* Default constructor
*/
@ -73,7 +73,8 @@ public:
*
* @param LoadingBarType
*/
void setDirection(LoadingBarType dir);
CC_DEPRECATED_ATTRIBUTE void setDirection(Direction direction);
void setBarDirection(Direction direction);
/**
* Gets the progress direction of loadingbar.
@ -82,7 +83,8 @@ public:
*
* @param LoadingBarType
*/
int getDirection();
CC_DEPRECATED_ATTRIBUTE int getDirection();
Direction getBarDirection();
/**
* Load texture for loadingbar.
@ -150,7 +152,7 @@ protected:
virtual void copySpecialProperties(Widget* model) override;
virtual void adaptRenderers() override;
protected:
LoadingBarType _barType;
Direction _direction;
int _percent;
float _totalLength;
Node* _barRenderer;

View File

@ -314,7 +314,6 @@ bool UIListViewTest_Horizontal::init()
ssize_t index = listView->getIndex(item);
button->setTitleText(static_cast<__String*>(_array->getObjectAtIndex(index))->getCString());
}
// remove last item
listView->removeLastItem();

View File

@ -111,7 +111,7 @@ bool UILoadingBarTest_Right::init()
// Create the loading bar
LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
loadingBar->setTag(0);
loadingBar->setDirection(LoadingBarTypeRight);
loadingBar->setBarDirection(LoadingBar::Direction::RIGHT);
loadingBar->setPosition(Vector2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f + loadingBar->getSize().height / 4.0f));
@ -276,7 +276,7 @@ bool UILoadingBarTest_Right_Scale9::init()
loadingBar->setScale9Enabled(true);
loadingBar->setCapInsets(Rect(0, 0, 0, 0));
loadingBar->setSize(Size(300, 13));
loadingBar->setDirection(LoadingBarTypeRight);
loadingBar->setBarDirection(LoadingBar::Direction::RIGHT);
loadingBar->setPosition(Vector2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f + loadingBar->getSize().height / 4.0f));