#pragma once
#include "cocos2d.h"
#include "2d/CCClippingNode.h"
USING_NS_CC;
class PannelNews :public Node
{
protected:
Node* _holder;
Sprite* _newsBg;
Label* _news;
DrawNode* _shap;
ClippingNode* _cliper;
public:
PannelNews();
~PannelNews();
CREATE_FUNC(PannelNews);
virtual bool init() override;
void layout();
void scrollText(float);
bool UpdateText();
};
#include "PannelNews.h"
#include "DefineConfig.h"
#include "../../Tools/StringTools.h"
#define START_W 1058
#define END_W -10
#define FADE_LENGTH 300.f
PannelNews::PannelNews()
{
_holder = NULL;
_newsBg = NULL;
_news = NULL;
_shap = NULL;
_cliper = NULL;
}
PannelNews::~PannelNews()
{
}
bool PannelNews::init()
{
if (!Node::init())
return false;
_holder = Node::create();
addChild(_holder);
_newsBg = Sprite::createWithSpriteFrameName("public_msg_bg.png");
_holder->addChild(_newsBg);
_shap = DrawNode::create();
Point point[4] = { Point(0.f,0.f), Point(1048.f,0.f), Point(1048.f,50.f), Point(0.f,50.f) };
//标记显示的矩形区域
_shap->drawPolygon(point, 4, Color4F(255, 255, 255, 255), 2, Color4F(255, 255, 255, 255));
//剪切用于显示的矩形区域,参数对应:用于标识区域的Point(Vec2)数组指针,数组长度,填充颜色,边框宽度,边框颜色
_cliper = ClippingNode::create();
_cliper->setStencil(_shap);
_cliper->setAnchorPoint(Point(0.5, 0.5));
_newsBg->addChild(_cliper);
_news = Label::createWithTTF("", FONT_PATH_HKYT, 30);
_news->setColor(Color3B(255, 255, 255));
_news->enableOutline(Color4B(216, 129, 26, 255), 1);
_cliper->addChild(_news);
layout();
return true;
}
void PannelNews::layout()
{
_news->setAnchorPoint(Point::ZERO);
_news->setPosition(Point(END_W, 10));//设置公告文字内容的初始位置
schedule(schedule_selector(PannelNews::scrollText));//实现公告文字滚动
}
void PannelNews::scrollText(float)
{
_news->setPositionX(_news->getPositionX() - 2);
for (int i = 0; i < _news->getStringLength(); i++)
{
auto letter = _news->getLetter(i);
if (letter)
{
int letterX = _news->getPositionX() + letter->getPositionX();
if (letterX < FADE_LENGTH)
{
int opacity = 255 * letterX / FADE_LENGTH;
if (opacity < 0) opacity = 0;
if (opacity > 255) opacity = 255;
letter->setOpacity(opacity);
}
else if (letterX > START_W - FADE_LENGTH)
{
int opacity = 255 - 255 * (letterX - START_W + FADE_LENGTH) / FADE_LENGTH;
if (opacity < 0) opacity = 0;
if (opacity > 255) opacity = 255;
letter->setOpacity(opacity);
}
else
{
letter->setOpacity(255);
}
}
}
if (_news->getPositionX() < END_W - _news->getContentSize().width && UpdateText())
{
_news->setPositionX(START_W);
}
}
bool PannelNews::UpdateText()
{
static int a = 0;
a++;
static int b = 0;
printf("a=%d\n", a);
if (a > 100 || b % 2 == 0)
{
b++;
a = 0;
this->setVisible(true);
_news->setString("lsdhjahgkeajhgkjaegvajkgrjkagkjaergkjraergbjkaegjkaegrjkag");
return true;
}
this->setVisible(false);
return false;
}