void Label::alignText() { if (_fontAtlas == nullptr) { return; } for (const auto& batchNode:_batchNodes) { batchNode->getTextureAtlas()->removeAllQuads(); } _fontAtlas->prepareLetterDefinitions(_currentUTF16String); auto textures = _fontAtlas->getTextures(); if (textures.size() > _batchNodes.size()) { for (auto index = _batchNodes.size(); index < textures.size(); ++index) { auto batchNode = SpriteBatchNode::createWithTexture(textures[index]); batchNode->setAnchorPoint(Point::ANCHOR_TOP_LEFT); batchNode->setPosition(Point::ZERO); Node::addChild(batchNode,0,Node::INVALID_TAG); _batchNodes.push_back(batchNode); } } LabelTextFormatter::createStringSprites(this); if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) ) LabelTextFormatter::createStringSprites(this); if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT)) LabelTextFormatter::alignText(this); int strLen = cc_wcslen(_currentUTF16String); Rect uvRect; Sprite* letterSprite; for(const auto &child : _children) { int tag = child->getTag(); if(tag >= strLen) { SpriteBatchNode::removeChild(child, true); } else if(tag >= 0) { letterSprite = dynamic_cast<Sprite*>(child); if (letterSprite) { uvRect.size.height = _lettersInfo[tag].def.height; uvRect.size.width = _lettersInfo[tag].def.width; uvRect.origin.x = _lettersInfo[tag].def.U; uvRect.origin.y = _lettersInfo[tag].def.V; letterSprite->setTexture(textures[_lettersInfo[tag].def.textureID]); letterSprite->setTextureRect(uvRect); } } } updateQuads(); updateColor(); }
void RenderableParticleEffectEntityItem::render(RenderArgs* args) { Q_ASSERT(getType() == EntityTypes::ParticleEffect); PerformanceTimer perfTimer("RenderableParticleEffectEntityItem::render"); if (_texturesChangedFlag) { if (_textures.isEmpty()) { _texture.clear(); } else { // for now use the textures string directly. // Eventually we'll want multiple textures in a map or array. _texture = DependencyManager::get<TextureCache>()->getTexture(_textures); } _texturesChangedFlag = false; } bool textured = _texture && _texture->isLoaded(); updateQuads(args, textured); Q_ASSERT(args->_batch); gpu::Batch& batch = *args->_batch; if (textured) { batch.setUniformTexture(0, _texture->getGPUTexture()); } batch.setModelTransform(getTransformToCenter()); DependencyManager::get<DeferredLightingEffect>()->bindSimpleProgram(batch, textured); DependencyManager::get<GeometryCache>()->renderVertices(batch, gpu::QUADS, _cacheID); };
void Label::alignText() { if (_fontAtlas == nullptr || _utf16Text.empty()) { setContentSize(Size::ZERO); return; } _fontAtlas->prepareLetterDefinitions(_utf16Text); auto& textures = _fontAtlas->getTextures(); if (textures.size() > _batchNodes.size()) { for (auto index = _batchNodes.size(); index < textures.size(); ++index) { auto batchNode = SpriteBatchNode::createWithTexture(textures.at(index)); if (batchNode) { _isOpacityModifyRGB = batchNode->getTexture()->hasPremultipliedAlpha(); _blendFunc = batchNode->getBlendFunc(); batchNode->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); batchNode->setPosition(Vec2::ZERO); _batchNodes.pushBack(batchNode); } } } if (_batchNodes.empty()) { return; } _reusedLetter->setBatchNode(_batchNodes.at(0)); _lengthOfString = 0; _textDesiredHeight = 0.f; _linesWidth.clear(); if (_maxLineWidth > 0.f && !_lineBreakWithoutSpaces) { multilineTextWrapByWord(); } else { multilineTextWrapByChar(); } computeAlignmentOffset(); updateQuads(); updateLabelLetters(); updateColor(); }
void DynamicSprite::onContentSizeDirty() { DynamicBatchNode::onContentSizeDirty(); if (!_texture) { _quads->clear(); return; } else if (_autofit == Autofit::None) { _textureOrigin = cocos2d::Vec2::ZERO; _textureSize = _contentSize; } else { auto size = _texture->getContentSize(); _textureOrigin = cocos2d::Vec2::ZERO; _textureSize = _contentSize; _textureRect = cocos2d::Rect(0, 0, size.width, size.height); float scale = 1.0f; if (_autofit == Autofit::Width) { scale = size.width / _contentSize.width; } else if (_autofit == Autofit::Height) { scale = size.height / _contentSize.height; } else if (_autofit == Autofit::Contain) { scale = MAX(size.width / _contentSize.width, size.height / _contentSize.height); } else if (_autofit == Autofit::Cover) { scale = MIN(size.width / _contentSize.width, size.height / _contentSize.height); } auto texSizeInView = cocos2d::Size(size.width / scale, size.height / scale); if (texSizeInView.width < _contentSize.width) { _textureSize.width -= (_contentSize.width - texSizeInView.width); _textureOrigin.x = (_contentSize.width - texSizeInView.width) * _autofitPos.x; } else if (texSizeInView.width > _contentSize.width) { _textureRect.origin.x = (_textureRect.size.width - _contentSize.width * scale) * _autofitPos.x; _textureRect.size.width = _contentSize.width * scale; } if (texSizeInView.height < _contentSize.height) { _textureSize.height -= (_contentSize.height - texSizeInView.height); _textureOrigin.y = (_contentSize.height - texSizeInView.height) * _autofitPos.y; } else if (texSizeInView.height > _contentSize.height) { _textureRect.origin.y = (_textureRect.size.height - _contentSize.height * scale) * _autofitPos.y; _textureRect.size.height = _contentSize.height * scale; } } updateQuads(); }
void display() { HandleOpcode(); if(DrawFlag) { // Clear framebuffer glClear(GL_COLOR_BUFFER_BIT); #ifdef DRAWWITHTEXTURE updateTexture(); #else updateQuads(); #endif // Swap buffers! glutSwapBuffers(); // Processed frame DrawFlag = 0; } }
void Label::alignText() { if (_fontAtlas == nullptr || _currentUTF16String.empty()) { setContentSize(Size::ZERO); return; } _fontAtlas->prepareLetterDefinitions(_currentUTF16String); auto& textures = _fontAtlas->getTextures(); if (textures.size() > _batchNodes.size()) { for (auto index = _batchNodes.size(); index < textures.size(); ++index) { auto batchNode = SpriteBatchNode::createWithTexture(textures.at(index)); batchNode->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); batchNode->setPosition(Vec2::ZERO); Node::addChild(batchNode,0,Node::INVALID_TAG); _batchNodes.push_back(batchNode); } } LabelTextFormatter::createStringSprites(this); if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) ) LabelTextFormatter::createStringSprites(this); if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT)) LabelTextFormatter::alignText(this); if (!_children.empty()) { int strLen = static_cast<int>(_currentUTF16String.length()); Rect uvRect; Sprite* letterSprite; for (auto index = 0; index < _children.size();) { auto child = _children.at(index); int tag = child->getTag(); if (tag >= strLen) { child->removeFromParentAndCleanup(true); } else if (tag >= 0) { letterSprite = dynamic_cast<Sprite*>(child); if (letterSprite) { auto& letterDef = _lettersInfo[tag].def; uvRect.size.height = letterDef.height; uvRect.size.width = letterDef.width; uvRect.origin.x = letterDef.U; uvRect.origin.y = letterDef.V; letterSprite->setBatchNode(_batchNodes[letterDef.textureID]); letterSprite->setTextureRect(uvRect, false, uvRect.size); letterSprite->setPosition(_lettersInfo[tag].position.x + letterDef.width/2, _lettersInfo[tag].position.y - letterDef.height/2); } ++index; } else { ++index; } } } for (const auto& batchNode : _batchNodes) { batchNode->getTextureAtlas()->removeAllQuads(); } updateQuads(); updateColor(); }
void Label::alignText() { if (_fontAtlas == nullptr || _currentUTF16String.empty()) { setContentSize(Size::ZERO); return; } _fontAtlas->prepareLetterDefinitions(_currentUTF16String); auto& textures = _fontAtlas->getTextures(); if (textures.size() > _batchNodes.size()) { for (auto index = _batchNodes.size(); index < textures.size(); ++index) { auto batchNode = SpriteBatchNode::createWithTexture(textures.at(index)); if (batchNode) { _blendFunc = batchNode->getBlendFunc(); batchNode->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); batchNode->setPosition(Vec2::ZERO); _batchNodes.pushBack(batchNode); } } } if (_batchNodes.empty()) { return; } _reusedLetter->setBatchNode(_batchNodes.at(0)); LabelTextFormatter::createStringSprites(this); if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) ) LabelTextFormatter::createStringSprites(this); if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT)) LabelTextFormatter::alignText(this); if (!_letters.empty()) { Rect uvRect; Sprite* letterSprite; int letterIndex; for (auto it = _letters.begin(); it != _letters.end();) { letterIndex = it->first; letterSprite = it->second; if (letterIndex >= _limitShowCount) { Node::removeChild(letterSprite, true); it = _letters.erase(it); } else { auto& letterDef = _lettersInfo[letterIndex].def; uvRect.size.height = letterDef.height; uvRect.size.width = letterDef.width; uvRect.origin.x = letterDef.U; uvRect.origin.y = letterDef.V; letterSprite->setBatchNode(_batchNodes.at(letterDef.textureID)); letterSprite->setTextureRect(uvRect, false, uvRect.size); letterSprite->setPosition(_lettersInfo[letterIndex].position.x + letterDef.width / 2, _lettersInfo[letterIndex].position.y - letterDef.height / 2); ++it; } } } for (const auto& batchNode : _batchNodes) { batchNode->getTextureAtlas()->removeAllQuads(); } updateQuads(); updateColor(); }