Skip to content

Instantly share code, notes, and snippets.

@Jakz
Created September 25, 2014 16:37
Show Gist options
  • Select an option

  • Save Jakz/35cd746caf878712e5d4 to your computer and use it in GitHub Desktop.

Select an option

Save Jakz/35cd746caf878712e5d4 to your computer and use it in GitHub Desktop.
Cocos2d-x Multicolored Label
class LabelBMColoredFont : public cocos2d::Label
{
private:
struct ColorIndex
{
const cocos2d::Color3B& color;
size_t index;
};
LabelBMColoredFont(cocos2d::FontAtlas *atlas = nullptr, cocos2d::TextHAlignment hAlignment = cocos2d::TextHAlignment::LEFT,
cocos2d::TextVAlignment vAlignment = cocos2d::TextVAlignment::TOP,bool useDistanceField = false,bool useA8Shader = false);
std::vector<ColorIndex> colors;
public:
static LabelBMColoredFont * create(const std::string& str, const std::string& fntFile, float width = 0, cocos2d::TextHAlignment alignment = cocos2d::TextHAlignment::LEFT,const cocos2d::Vec2& imageOffset = cocos2d::Vec2::ZERO);
void setString(const std::string& string) override;
void updateColor() override;
};
LabelBMColoredFont::LabelBMColoredFont(cocos2d::FontAtlas *atlas, cocos2d::TextHAlignment hAlignment, cocos2d::TextVAlignment vAlignment,bool useDistanceField,bool useA8Shader) : Label(atlas,hAlignment,vAlignment,useDistanceField,useA8Shader) { }
LabelBMColoredFont *LabelBMColoredFont::create(const std::string& str, const std::string& fntFile, float width /* = 0 */, TextHAlignment alignment /* = TextHAlignment::LEFT */,const Vec2& imageOffset /* = Vec2::ZERO */)
{
auto ret = new LabelBMColoredFont(nullptr,alignment);
if (ret && ret->setBMFontFilePath(fntFile,imageOffset))
{
ret->setMaxLineWidth(width);
ret->setString(str);
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
static const Color3B scolors[] = {
Color3B(255,255,255), // 0
Color3B(0,0,0), // 1
Color3B(208,25,27), // 2
Color3B(43,167,68), // 3
Color3B(165,24,217), // v (purple)
Color3B(233,181,31), // y (yellow)
Color3B(28,134,222), // b
Color3B(245,19,147), // m
Color3B(255,128,0), // o (orange)
};
void LabelBMColoredFont::setString(const string &text)
{
colors.clear();
u16string text16;
StringUtils::UTF8ToUTF16(text, text16);
_currentUTF16String.clear();
const size_t length = text16.length();
text16.reserve(length);
Color3B baseColor = getColor();
size_t tindex = 0;
for (size_t i = 0; i < length; ++i)
{
if (text16[i] == '^')
{
char mapping = text16[i+1];
s8 colorIndex = -1;
switch (mapping) {
case '0': colorIndex = 0; break;
case '1': colorIndex = 1; break;
case '2': colorIndex = 2; break;
case '3': colorIndex = 3; break;
case 'v': colorIndex = 4; break;
case 'y': colorIndex = 5; break;
case 'b': colorIndex = 6; break;
case 'm': colorIndex = 7; break;
case 'o': colorIndex = 8; break;
case 's': colors.push_back({baseColor,tindex}); break;
}
if (colorIndex != -1)
colors.push_back({scolors[colorIndex], tindex});
++i;
continue;
}
else if (text[i] == '\n')
--tindex;
else if (i == 0)
colors.push_back({baseColor,0});
++tindex;
_currentUTF16String.push_back(text16[i]);
}
_originalUTF8String.clear();
StringUtils::UTF16ToUTF8(_currentUTF16String, _originalUTF8String);
_contentDirty = true;
}
void LabelBMColoredFont::updateColor()
{
if (nullptr == _textureAtlas)
{
return;
}
cocos2d::TextureAtlas* textureAtlas;
V3F_C4B_T2F_Quad *quads;
for (const auto& batchNode:_batchNodes)
{
textureAtlas = batchNode->getTextureAtlas();
quads = textureAtlas->getQuads();
auto count = textureAtlas->getTotalQuads();
size_t cindex = 0;
for (size_t i = 0; i < count; ++i)
{
if (cindex < colors.size() - 1 && i == colors[cindex+1].index)
++cindex;
Color4B color(colors[cindex].color);
color.a = _displayedOpacity;
if (_isOpacityModifyRGB)
{
color.r *= _displayedOpacity/255.0f;
color.g *= _displayedOpacity/255.0f;
color.b *= _displayedOpacity/255.0f;
}
else
color.a = _displayedOpacity;
quads[i].bl.colors = color;
quads[i].br.colors = color;
quads[i].tl.colors = color;
quads[i].tr.colors = color;
textureAtlas->updateQuad(&quads[i], i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment