void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = -1 */) { CCASSERT(_currentLabelType == LabelType::STRING_TEXTURE || _currentLabelType == LabelType::TTF, "Only supported system font and TTF!"); _effectColor = outlineColor; _effectColorF.r = _effectColor.r / 255.0f; _effectColorF.g = _effectColor.g / 255.0f; _effectColorF.b = _effectColor.b / 255.0f; _effectColorF.a = _effectColor.a / 255.0f; if (outlineSize > 0) { _outlineSize = outlineSize; if (_currentLabelType == LabelType::TTF) { if (_fontConfig.outlineSize != outlineSize) { auto config = _fontConfig; config.outlineSize = outlineSize; setTTFConfig(config); updateShaderProgram(); } } _currLabelEffect = LabelEffect::OUTLINE; _contentDirty = true; } }
void Label::setFontSize(int fontSize) { if (_currentLabelType == LabelType::TTF) { if (_fontConfig.fontSize == fontSize) { return; } if (_fontConfig.distanceFieldEnabled) { _fontConfig.fontSize = fontSize; this->setFontScale(1.0f * fontSize / DefultFontSize); } else { auto fontConfig = _fontConfig; fontConfig.fontSize = fontSize; setTTFConfig(fontConfig); } } else if(_fontDefinition._fontSize != fontSize) { _fontDefinition._fontSize = fontSize; _fontConfig.fontSize = fontSize; _contentDirty = true; } }
Label* Label::create(const std::string& text, const std::string& fontName, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */) { auto ret = new Label(nullptr,hAlignment,vAlignment); if (ret) { do { if (FileUtils::getInstance()->isFileExist(fontName)) { TTFConfig ttfConfig(fontName.c_str(),fontSize,GlyphCollection::DYNAMIC); if (ret->setTTFConfig(ttfConfig)) { break; } } FontDefinition fontDef; fontDef._fontName = fontName; fontDef._fontSize = fontSize; fontDef._dimensions = dimensions; fontDef._alignment = hAlignment; fontDef._vertAlignment = vAlignment; ret->setFontDefinition(fontDef); } while (0); ret->setDimensions(dimensions.width,dimensions.height); ret->setString(text); ret->autorelease(); } return ret; }
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = -1 */) { CCASSERT(_currentLabelType == LabelType::STRING_TEXTURE || _currentLabelType == LabelType::TTF, "Only supported system font and TTF!"); if (outlineSize > 0 || _currLabelEffect == LabelEffect::OUTLINE) { if (_currentLabelType == LabelType::TTF) { _effectColorF.r = outlineColor.r / 255.0f; _effectColorF.g = outlineColor.g / 255.0f; _effectColorF.b = outlineColor.b / 255.0f; _effectColorF.a = outlineColor.a / 255.0f; if (outlineSize > 0 && _fontConfig.outlineSize != outlineSize) { _fontConfig.outlineSize = outlineSize; setTTFConfig(_fontConfig); } } else if (_effectColor != outlineColor || _outlineSize != outlineSize) { _effectColor = outlineColor; _outlineSize = outlineSize; _currLabelEffect = LabelEffect::OUTLINE; _contentDirty = true; } } }
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = -1 */) { _effectColor = outlineColor; _effectColorF.r = _effectColor.r / 255.0f; _effectColorF.g = _effectColor.g / 255.0f; _effectColorF.b = _effectColor.b / 255.0f; _effectColorF.a = _effectColor.a / 255.0f; if (outlineSize > 0) { if (_currentLabelType == LabelType::TTF) { if (_fontConfig.outlineSize != outlineSize) { auto config = _fontConfig; config.outlineSize = outlineSize; setTTFConfig(config); updateShaderProgram(); } } _fontDefinition._stroke._strokeEnabled = true; _fontDefinition._stroke._strokeSize = outlineSize; _fontDefinition._stroke._strokeColor = Color3B(outlineColor.r,outlineColor.g,outlineColor.b); _currLabelEffect = LabelEffect::OUTLINE; _contentDirty = true; } }
bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize) { _placeHolder = placeholder; do { // If fontName is ttf file and it corrected, use TTFConfig if (FileUtils::getInstance()->isFileExist(fontName)) { TTFConfig ttfConfig(fontName, fontSize, GlyphCollection::DYNAMIC); if (setTTFConfig(ttfConfig)) { break; } } setSystemFontName(fontName); setSystemFontSize(fontSize); } while (false); Label::setTextColor(_colorSpaceHolder); Label::setString(_placeHolder); #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) // On desktop default enable cursor if (_currentLabelType == LabelType::TTF) { setCursorEnabled(true); } #endif return true; }
Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment /* = TextHAlignment::CENTER */, int maxLineWidth /* = 0 */) { auto ret = new Label(nullptr,alignment); if (!ret) { return nullptr; } do { if( FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) && ret->setTTFConfig(ttfConfig)) { break; } FontDefinition fontDef; fontDef._fontName = ttfConfig.fontFilePath; fontDef._fontSize = ttfConfig.fontSize; fontDef._dimensions = Size::ZERO; fontDef._alignment = alignment; fontDef._vertAlignment = TextVAlignment::TOP; ret->setFontDefinition(fontDef); } while (0); ret->setMaxLineWidth(maxLineWidth); ret->setString(text); ret->autorelease(); return ret; }
void LabelTTF::setFontFile(const std::string& fontFile) { TTFConfig ttfConfig = getTTFConfig(); if ( FileUtils::getInstance()->isFileExist(fontFile) ) { TTFConfig newConfig(fontFile.c_str(),ttfConfig.fontSize,GlyphCollection::DYNAMIC); setTTFConfig(newConfig); enableOutline(Color4B::BLACK,2); } }
void LabelTTF::setFontSize(float fontSize) { TTFConfig ttfConfig = getTTFConfig(); if ( FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) ) { TTFConfig newConfig(ttfConfig.fontFilePath.c_str(),fontSize,GlyphCollection::DYNAMIC); setTTFConfig(newConfig); enableOutline(Color4B::BLACK,2); } }
void Label::disableEffect() { if (_currLabelEffect == LabelEffect::OUTLINE) { _fontConfig.outlineSize = 0; setTTFConfig(_fontConfig); } _currLabelEffect = LabelEffect::NORMAL; initProgram(); _contentDirty = true; }
void Label::updateFont() { if (FileUtils::getInstance()->isFileExist(_fontName)) { _fontConfig.fontFilePath = _fontName; _fontConfig.fontSize = _fontSize; setTTFConfig(_fontConfig); } _fontDefinition._fontName = _fontName; _fontDefinition._fontSize = _fontSize; _contentDirty = true; _fontDirty = false; }
void Label::disableEffect(LabelEffect effect) { switch (effect) { case cocos2d::LabelEffect::NORMAL: break; case cocos2d::LabelEffect::OUTLINE: if (_currLabelEffect == LabelEffect::OUTLINE) { if (_currentLabelType == LabelType::TTF) { _fontConfig.outlineSize = 0; setTTFConfig(_fontConfig); } _currLabelEffect = LabelEffect::NORMAL; _contentDirty = true; } break; case cocos2d::LabelEffect::SHADOW: if (_shadowEnabled) { _shadowEnabled = false; if (_shadowNode) { Node::removeChild(_shadowNode, true); _shadowNode = nullptr; } } break; case cocos2d::LabelEffect::GLOW: if (_currLabelEffect == LabelEffect::GLOW) { _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); } break; case LabelEffect::ALL: { disableEffect(LabelEffect::SHADOW); disableEffect(LabelEffect::GLOW); disableEffect(LabelEffect::OUTLINE); } break; default: break; } }
LabelRPG* LabelRPG::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment /* = TextHAlignment::CENTER */, int maxLineWidth /* = 0 */) { auto ret = new LabelRPG(nullptr,alignment); if (ret && FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) && ret->setTTFConfig(ttfConfig)) { ret->setMaxLineWidth(maxLineWidth); ret->setString(text); ret->autorelease(); return ret; } delete ret; return nullptr; }
void Label::disableEffect() { if (_currLabelEffect == LabelEffect::OUTLINE) { _fontConfig.outlineSize = 0; setTTFConfig(_fontConfig); } _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); _contentDirty = true; _shadowEnabled = false; if (_shadowNode) { Node::removeChild(_shadowNode,true); _shadowNode = nullptr; } }
void Label::setFontName(const std::string& fontName) { if (fontName.find('.') != fontName.npos) { auto config = _fontConfig; config.fontFilePath = fontName; if (setTTFConfig(config)) { return; } } if (_fontDefinition._fontName != fontName) { _fontDefinition._fontName = fontName; _contentDirty = true; } }
bool LabelTTF::initWithString(const std::string& string, const std::string& fontFile, float fontSize, const cocos2d::Size &dimensions, TextHAlignment hAlignment, TextVAlignment vAlignment) { if ( FileUtils::getInstance()->isFileExist(fontFile) ) { TTFConfig ttfConfig(fontFile.c_str(),fontSize,GlyphCollection::DYNAMIC); if (setTTFConfig(ttfConfig)) { setDimensions(dimensions.width,dimensions.height); setString(string); setAlignment(hAlignment,vAlignment); enableOutline(Color4B::BLACK,2); return true; } } return false; }
void Label::enableGlow(const Color4B& glowColor) { if (_currentLabelType == LabelType::TTF) { if (_fontConfig.distanceFieldEnabled == false) { auto config = _fontConfig; config.outlineSize = 0; config.distanceFieldEnabled = true; setTTFConfig(config); _contentDirty = true; } _currLabelEffect = LabelEffect::GLOW; _effectColorF.r = glowColor.r / 255.0f; _effectColorF.g = glowColor.g / 255.0f; _effectColorF.b = glowColor.b / 255.0f; _effectColorF.a = glowColor.a / 255.0f; updateShaderProgram(); } }
Label* Label::createWithTTF(const std::string& text, const std::string& fontFile, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */) { auto ret = new (std::nothrow) Label(nullptr,hAlignment,vAlignment); if (ret && FileUtils::getInstance()->isFileExist(fontFile)) { TTFConfig ttfConfig(fontFile.c_str(),fontSize,GlyphCollection::DYNAMIC); if (ret->setTTFConfig(ttfConfig)) { ret->setDimensions(dimensions.width,dimensions.height); ret->setString(text); ret->autorelease(); return ret; } } delete ret; return nullptr; }
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = 1 */) { _outlineColor = outlineColor; if (outlineSize > 0) { if (_currentLabelType == LabelType::TTF) { if (_fontConfig.outlineSize != outlineSize) { auto config = _fontConfig; config.outlineSize = outlineSize; setTTFConfig(config); initProgram(); } } _fontDefinition._stroke._strokeEnabled = true; _fontDefinition._stroke._strokeSize = outlineSize; _fontDefinition._stroke._strokeColor = Color3B(outlineColor.r,outlineColor.g,outlineColor.b); _currLabelEffect = LabelEffect::OUTLINE; _contentDirty = true; } }