#include "SystemNotice.h" #include "cocostudio/CocoStudio.h" USING_NS_CC; using namespace cocostudio; using namespace ui; #define Offset_Width 10 SystemNotice::SystemNotice() : _text(nullptr) , _runing(false) { } SystemNotice::~SystemNotice() { } SystemNotice* SystemNotice::createSystemNotice(const char* stencil_Path) { SystemNotice* notice = new SystemNotice(); if (notice->init(stencil_Path)) { notice->autorelease(); return notice; } else { CC_SAFE_DELETE(notice); return nullptr; } } bool SystemNotice::init(const char* stencil_Path) { if (!Layer::init()) { return false; } auto stencil = Sprite::create(stencil_Path); _showSize = stencil->getContentSize(); stencil->setAnchorPoint(Vec2(0.5f, 0.5f)); stencil->setPosition(_showSize.width / 2, _showSize.height / 2); _text = createLabel("", 26, Color3B(255, 255, 255)); _text->setAnchorPoint(Vec2(0.5f, 0.5f)); _text->setPositionY(_showSize.height / 2); _wordSize = _text->getContentSize(); _text->setPositionX(_showSize.width + _wordSize.width / 2); ClippingNode* clipNode = ClippingNode::create(); clipNode->setStencil(stencil); clipNode->setAnchorPoint(Vec2::ZERO); clipNode->setPosition(Vec2::ZERO); this->addChild(clipNode); clipNode->addChild(_text); clipNode->setCascadeOpacityEnabled(true); this->ignoreAnchorPointForPosition(false); this->setContentSize(_showSize); return true; } Label* SystemNotice::createLabel(const std::string& text, float fontSize, Color3B color) { Label* label = Label::createWithSystemFont(text, "", fontSize); label->setColor(color); return label; } void SystemNotice::postMessage(const std::string& message, int repeat /*= 0*/, bool bForever/* = false*/) { _notifyMsgs.push(NotifyItem(message, repeat, bForever)); if (!_notifyMsgs.empty()) { start(); } } void SystemNotice::moveWord(float delta) { if (nullptr == _text) return; if (_text->getString().empty()) { updateNextMessage(); } else { float posX = _text->getPositionX() - 2; float overPosX = 0; overPosX = -_wordSize.width / 2; if(posX <= overPosX) { updateNextMessage(); } else { _text->setPositionX(posX); } } } void SystemNotice::start() { if (!_runing) { _runing = true; schedule(schedule_selector(SystemNotice::moveWord), 0.02f); } } void SystemNotice::stop() { if (_runing) { unschedule(schedule_selector(SystemNotice::moveWord)); _runing = false; if (nullptr != _sure) { _sure(); } } } void SystemNotice::updateNextMessage() { if (!_notifyMsgs.empty()) { NotifyItem msg = _notifyMsgs.front(); _text->setString(msg.message); _wordSize = _text->getContentSize(); _text->setPositionX(_showSize.width + _wordSize.width / 2); if (msg.isForever) { _notifyMsgs.pop(); _notifyMsgs.push(msg); return; } // Ñ­»·´ÎÊý if (msg.repeat-- > 0) { _notifyMsgs.pop(); _notifyMsgs.push(msg); } else { _notifyMsgs.pop(); } } else { stop(); } } void SystemNotice::setCallBack(std::function sure) { _sure = sure; } void SystemNotice::setNoticTextColor(Color4B color) { _text->setTextColor(color); } void SystemNotice::setTextFontSize(int fontSize) { _text->setSystemFontSize(fontSize); }