#include "ScrollText.h"
USING_NS_CC;
ScrollText::ScrollText()
{
m_pLable = NULL;
m_autoScroll = false;
m_moveFlag = false;
m_autoMidAlignment = false;
}
ScrollText::~ScrollText()
{
}
void ScrollText::update(float delta)
{
if (!m_pLable)
{
return;
}
if (!m_moveFlag)
{
return;
}
if (!m_autoScroll)
{
return;
}
float currentX = m_pLable->getPositionX();
float contentX = m_width;
float lableX = m_pLable->getContentSize().width*(-1.0f);
{
if (currentX >= (lableX - contentX / 2))
m_pLable->setPositionX(currentX - 15.0f*delta);
else
{
m_pLable->setPositionX(contentX / 2);
}
}
}
void ScrollText::setAutoScroll(bool moveFlag)
{
m_moveFlag = moveFlag;
if (m_moveFlag)
{
m_autoScroll = m_pLable->getContentSize().width > m_width ? true : false;
}
else
{
m_autoScroll = false;
}
if (!m_autoScroll && m_autoMidAlignment)
{
m_pLable->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
m_pLable->setPositionX(0);
}
else
{
m_pLable->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
m_pLable->setPositionX(m_width *-0.5f);
}
}
cocos2d::Label* ScrollText::GetLabel()
{
return m_pLable;
}
void ScrollText::setString(const char* text)
{
m_pLable->setString(text);
if (m_moveFlag)
{
m_autoScroll = m_pLable->getContentSize().width > m_width ? true : false;
}
if (!m_autoScroll && m_autoMidAlignment)
{
m_pLable->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
m_pLable->setPositionX(0);
}
else
{
m_pLable->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
m_pLable->setPositionX(m_width *-0.5f);
}
}
ScrollText* ScrollText::create(int width, int height, cocos2d::Label* pMoveChild, bool autoMidAlignment)
{
auto *sTxt = new ScrollText();
if (sTxt->initWithDatas(width, height, pMoveChild, autoMidAlignment))
{
sTxt->autorelease();
return sTxt;
}
{
delete sTxt;
sTxt = NULL;
return NULL;
}
}
bool ScrollText::initWithDatas(int width, int height, cocos2d::Label* pMoveChild, bool autoMidAlignment)
{
m_pLable = pMoveChild;
m_autoMidAlignment = autoMidAlignment;
if (height < pMoveChild->getHeight())
{
height = pMoveChild->getHeight();
}
m_width = width;
m_height = height;
setAutoScroll(false);
this->addChild(pMoveChild);
this->setClippingRegion(Rect(-0.5*width, -0.5*height, width, height));
this->setClippingEnabled(true);
return true;
}