// Helper bool CCLabelTTF::updateTexture() { CCTexture2D *tex; // let system compute label's width or height when its value is 0 // refer to cocos2d-x issue #1430 tex = new CCTexture2D(); if (!tex) { return false; } tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); this->setTexture(tex); tex->release(); CCRect rect = CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); return true; }
SpriteFrame* Sprite::getSpriteFrame() const { return SpriteFrame::createWithTexture(_texture, CC_RECT_POINTS_TO_PIXELS(_rect), _rectRotated, CC_POINT_POINTS_TO_PIXELS(_unflippedOffsetPositionFromCenter), CC_SIZE_POINTS_TO_PIXELS(_contentSize)); }
SpriteFrame* MGRBlurSprite::getSpriteFrame() const { if(nullptr != this->_spriteFrame) { return this->_spriteFrame; } return SpriteFrame::createWithTexture(_texture, CC_RECT_POINTS_TO_PIXELS(_rect), _rectRotated, CC_POINT_POINTS_TO_PIXELS(_unflippedOffsetPositionFromCenter), CC_SIZE_POINTS_TO_PIXELS(_contentSize)); }
// Helper void CCLabelTTF::updateTexture() { CCTexture2D *tex; if (m_tDimensions.width == 0 || m_tDimensions.height == 0) { tex = new CCTexture2D(); tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ; } else { tex = new CCTexture2D(); tex->initWithString(m_string.c_str(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment, m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()); } // iPad ? //if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) { if (CCApplication::sharedApplication().isIpad()) { if (CC_CONTENT_SCALE_FACTOR() == 2) { tex->setResolutionType(kCCResolutioniPadRetinaDisplay); } else { tex->setResolutionType(kCCResolutioniPad); } } // iPhone ? else { if (CC_CONTENT_SCALE_FACTOR() == 2) { tex->setResolutionType(kCCResolutioniPhoneRetinaDisplay); } else { tex->setResolutionType(kCCResolutioniPhone); } } this->setTexture(tex); tex->release(); CCRect rect = CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); }
void Scale9Sprite::createSlicedSprites() { //todo create sliced sprite if (_scale9Enabled) { Texture2D *tex = _scale9Image ? _scale9Image->getTexture() : nullptr; if (tex == nullptr) { return; } if (_renderingType == RenderingType::SIMPLE) { this->configureSimpleModeRendering(); } auto capInsets = CC_RECT_POINTS_TO_PIXELS(_capInsetsInternal); auto textureRect = CC_RECT_POINTS_TO_PIXELS(_spriteRect); auto spriteRectSize = _spriteRect.size; auto originalSize = CC_SIZE_POINTS_TO_PIXELS(_originalSize); auto offset = CC_POINT_POINTS_TO_PIXELS(_offset); Vec4 offsets; offsets.x = offset.x + (originalSize.width - textureRect.size.width) / 2; offsets.w = offset.y + (originalSize.height - textureRect.size.height) / 2; offsets.z = originalSize.width - textureRect.size.width - offsets.x; offsets.y = originalSize.height - textureRect.size.height - offsets.w; //handle .9.png if (_isPatch9) { originalSize = Size(originalSize.width - 2, originalSize.height-2); } if(capInsets.equals(Rect::ZERO)) { capInsets = Rect(originalSize.width/3, originalSize.height/3, originalSize.width/3, originalSize.height/3); } auto uv = this->calculateUV(tex, capInsets, originalSize, offsets); auto vertices = this->calculateVertices(capInsets, originalSize, offsets); auto triangles = this->calculateTriangles(uv, vertices); _scale9Image->getPolygonInfo().setTriangles(triangles); } }
// Helper bool CCRichLabelTTF::updateTexture() { CCTexture2D_richlabel *tex; tex = new CCTexture2D_richlabel(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccFontDefinition texDef = _prepareTextDefinition(true); tex->initWithRichString( m_string.c_str(), &texDef ); #else tex->initWithRichString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); //ok return true; }
caFontDefinition CALabel::setFontDefiniton(bool flag) { caFontDefinition texDef; if (flag) texDef.m_fontSize = m_nfontSize * CC_CONTENT_SCALE_FACTOR(); else texDef.m_fontSize = m_nfontSize; texDef.m_fontName = m_nfontName; texDef.m_alignment = m_nTextAlignment; texDef.m_vertAlignment = m_nVerticalTextAlignmet; if (flag) texDef.m_dimensions = CC_SIZE_POINTS_TO_PIXELS(m_nDimensions); else texDef.m_dimensions = m_nDimensions; // text tint texDef.m_fontFillColor = m_nTextcolor; return texDef; }
ccFontDefinition CCRichLabelTTF::_prepareTextDefinition(bool adjustForResolution) { ccFontDefinition texDef; if (adjustForResolution) texDef.m_fontSize = m_fFontSize * CC_CONTENT_SCALE_FACTOR(); else texDef.m_fontSize = m_fFontSize; texDef.m_fontName = *m_pFontName; texDef.m_alignment = m_hAlignment; texDef.m_vertAlignment = m_vAlignment; if (adjustForResolution) texDef.m_dimensions = CC_SIZE_POINTS_TO_PIXELS(m_tDimensions); else texDef.m_dimensions = m_tDimensions; // stroke if ( m_strokeEnabled ) { texDef.m_stroke.m_strokeEnabled = true; texDef.m_stroke.m_strokeColor = m_strokeColor; if (adjustForResolution) texDef.m_stroke.m_strokeSize = m_strokeSize * CC_CONTENT_SCALE_FACTOR(); else texDef.m_stroke.m_strokeSize = m_strokeSize; } else { texDef.m_stroke.m_strokeEnabled = false; } // shadow if ( m_shadowEnabled ) { texDef.m_shadow.m_shadowEnabled = true; texDef.m_shadow.m_shadowBlur = m_shadowBlur; texDef.m_shadow.m_shadowOpacity = m_shadowOpacity; if (adjustForResolution) texDef.m_shadow.m_shadowOffset = CC_SIZE_POINTS_TO_PIXELS(m_shadowOffset); else texDef.m_shadow.m_shadowOffset = m_shadowOffset; } else { texDef.m_shadow.m_shadowEnabled = false; } // text tint texDef.m_fontFillColor = m_textFillColor; return texDef; }
// Helper bool CCLabelTTF::updateTexture() { //if(this->isFlipY()) // this->setFlipY(false); CCTexture2D *tex; tex = new CCTexture2D(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccFontDefinition texDef = _prepareTextDefinition(true); tex->initWithString( m_string.c_str(), &texDef ); #else tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); /***************************给文字描黑边begin*********************************/ CCPoint prePos = this->getPosition(); ccColor3B color1 = this->getColor(); float strokeValue = 2; CCSize textureSize = this->getContentSize(); textureSize.width += 2 * strokeValue; textureSize.height += 2 * strokeValue; /* 监测OpenGl的错误状态 */ glGetError(); /* 创建一张纹理画布 */ CCRenderTexture *rt = CCRenderTexture::create(textureSize.width, textureSize.height); if(!rt) { CCLog("create render texture failed !!!!"); //addChild(label); return 0; } /* 设置描边的颜色 */ this->setColor(ccBLACK); /* *拿到源文字的混色机制,存储以备恢复,并设置新的目标混色机制 *混色机制设为:源颜色透明度(影响亮度)和目标颜色(影响颜色) */ ccBlendFunc originalBlend = this->getBlendFunc(); ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE}; this->setBlendFunc(func); CCPoint anchPoint = this->getAnchorPoint(); /* 这是自定义的一些调整,倾斜了一点 */ this->setAnchorPoint(CCPoint(0.5, 0.5)); //label->setRotationX(15); /* 张开画布,开始绘画 */ rt->begin(); this->setFlipY(true); for(int i = 0; i < 360; i += 5)//每变化5度绘制一张 { float r = CC_DEGREES_TO_RADIANS(i); //度数格式的转换 this->setPosition(CCPoint(textureSize.width * 0.5f + sin(r) * strokeValue,textureSize.height * 0.5f + cos(r) * strokeValue)); /* CCRenderTexture的用法,在begin和end之间visit的纹理,都会画在CCRenderTexture里面 */ this->visit();//画了一次该label } /* 恢复原始的label并绘制在最上层 */ this->setColor(color1); this->setBlendFunc(originalBlend); this->setPosition(CCPoint(textureSize.width * 0.5f, textureSize.height * 0.5f)); this->visit(); this->setFlipY(false); /* 在画布上绘制结束,此时会生成一张纹理 */ rt->end(); /* 取出生成的纹理,添加抗锯齿打磨,并返回 */ CCTexture2D *texture = rt->getSprite()->getTexture(); texture->setAntiAliasTexParameters();// setAliasTexParameters(); this->setTexture(texture); // release it rt->release(); //set the size in the sprite rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); //this->setFlipY(true); //this->setFlipX(true); this->setTextureRect(rect); //if(!this->isFlipY()) // this->setFlipY(true); this->setAnchorPoint(anchPoint); this->setColor(color1); this->setPosition(prePos); /***************************给文字描黑边end*********************************/ //ok return true; }
// Helper bool CCRichLabelTTF::updateTexture() { CCTexture2D_richlabel *tex; tex = new CCTexture2D_richlabel(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccRichFontDefinition texDef = _prepareTextDefinition(true); tex->initWithRichString( m_string.c_str(), &texDef ); #else tex->initWithRichString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = _texture->getContentSize(); this->setTextureRect(rect); // create link menu CCMenu* menu = (CCMenu*)getChildByTag(TAG_MENU); const LinkMetaList& linkMetas = tex->getLinkMetas(); if(!linkMetas.empty() && m_textChanging) { // delete old menu if has if(menu) { menu->removeFromParent(); menu = NULL; } // now create items for every link //CCArray* items = CCArray::create(); Vector<MenuItem*> items; for(LinkMetaList::const_iterator iter = linkMetas.begin(); iter != linkMetas.end(); iter++) { const LinkMeta& meta = *iter; CCMenuItemColor* item = CCMenuItemColor::create(ccc4FromInt(meta.normalBgColor), ccc4FromInt(meta.selectedBgColor), this, menu_selector(CCRichLabelTTF::onLinkMenuItemClicked)); item->setTag(START_TAG_LINK_ITEM + meta.tag); item->setPosition(ccp(meta.x + meta.width / 2, meta.y + meta.height / 2)); item->setContentSize(CCSizeMake(meta.width, meta.height)); item->setStateListener(m_stateListener); items.pushBack(item); } // add menu in -1 z order so that it won't override label UI CCMenu* newMenu = CCMenu::createWithArray(items); newMenu->setPosition(CCPointZero); addChild(newMenu, -1, TAG_MENU); // clear flag m_textChanging = false; } // sync shadow stroke padding to link menu // the padding must divide scale factor. Because at this time, texture already scaled for label menu = (CCMenu*)getChildByTag(TAG_MENU); if(menu) { const CCPoint& p = tex->getShadowStrokePadding(); menu->setPosition(ccp(p.x / CC_CONTENT_SCALE_FACTOR(), p.y / CC_CONTENT_SCALE_FACTOR())); } //ok return true; }