public static EmojiSpriteLabel create(NGUILabel pLabel) { EmojiSpriteLabel instance = new EmojiSpriteLabel(); instance.label = pLabel; instance.sprites = new List <GameObject>(); return(instance); }
/// <summary> /// Draw the label. /// </summary> public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) { if (mFont == null) return; MakePositionPerfect(); Pivot p = pivot; int offset = verts.size; Color col = color; col.a *= mPanel.alpha; if (font.premultipliedAlpha) col = NGUITools.ApplyPMA(col); //add by chiuan if(isUsedEmojiSprite && mEmojiSpriteLabel ==null) mEmojiSpriteLabel = EmojiSpriteLabel.create(this); if(mEmojiSpriteLabel != null && mFont.isDynamic && mFont.dynamicSymbolsFont != null) { mEmojiSpriteLabel.destroy(); mFont.dynamicSymbolLabel = mEmojiSpriteLabel; } // Print the text into the buffers if (p == Pivot.Left || p == Pivot.TopLeft || p == Pivot.BottomLeft) { mFont.Print(processedText, col, verts, uvs, cols, mEncoding, mSymbols, NGUIFont.Alignment.Left, 0, mPremultiply); } else if (p == Pivot.Right || p == Pivot.TopRight || p == Pivot.BottomRight) { mFont.Print(processedText, col, verts, uvs, cols, mEncoding, mSymbols, NGUIFont.Alignment.Right, Mathf.RoundToInt(relativeSize.x * mFont.size), mPremultiply); } else { mFont.Print(processedText, col, verts, uvs, cols, mEncoding, mSymbols, NGUIFont.Alignment.Center, Mathf.RoundToInt(relativeSize.x * mFont.size), mPremultiply); } // Apply an effect if one was requested if (effectStyle != Effect.None) { Vector3 scale = cachedTransform.localScale; if (scale.x == 0f || scale.y == 0f) return; int end = verts.size; float pixel = 1f / mFont.size; float fx = pixel * mEffectDistance.x; float fy = pixel * mEffectDistance.y; ApplyShadow(verts, uvs, cols, offset, end, fx, -fy); if (effectStyle == Effect.Outline) { offset = end; end = verts.size; ApplyShadow(verts, uvs, cols, offset, end, -fx, fy); offset = end; end = verts.size; ApplyShadow(verts, uvs, cols, offset, end, fx, fy); offset = end; end = verts.size; ApplyShadow(verts, uvs, cols, offset, end, -fx, -fy); } } //maybe create all symblos if(mEmojiSpriteLabel != null) { mEmojiSpriteLabel.createSymblos(); } MakePixelPerfect(); }
/// <summary> /// Print the specified text into the buffers. /// Note: 'lineWidth' parameter should be in pixels. /// </summary> public void Print(string text, Color32 color, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols, bool encoding, SymbolStyle symbolStyle, Alignment alignment, int lineWidth, bool premultiply) { if (mReplacement != null) { mReplacement.Print(text, color, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth, premultiply); } else if (text != null) { if (!isValid) { Debug.LogError("Attempting to print using an invalid font!"); return; } // Make sure the characters are present in the dynamic font before printing them bool dynamic = isDynamic; #if !UNITY_3_5 if (dynamic) { mDynamicFont.textureRebuildCallback = OnFontChanged; mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle); mDynamicFont.textureRebuildCallback = null; } #endif mColors.Clear(); mColors.Add(color); int fs = size; Vector2 invSize = fs > 0 ? new Vector2(1f / fs, 1f / fs) : Vector2.one; int indexOffset = verts.size; int maxX = 0; int x = 0; int y = 0; int prev = 0; int lineHeight = (fs + mSpacingY); Vector3 v0 = Vector3.zero, v1 = Vector3.zero; Vector2 u0 = Vector2.zero, u1 = Vector2.zero; float invX = uvRect.width / mFont.texWidth; float invY = mUVRect.height / mFont.texHeight; int textLength = text.Length; bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols && (dynamicSymbolsFont == null? sprite != null : dynamicSymbolsFont.sprite != null) ; List<int> dynamicSymblosIndexs = new List<int>(); //need to record all dynamic symblo verts index. List<Vector2> dynamicSymblosPosBase = new List<Vector2>(); List<string> dynamicSymblosKeyName = new List<string>(); for (int i = 0; i < textLength; ++i) { char c = text[i]; if (c == '\n') { if (x > maxX) maxX = x; if (alignment != Alignment.Left) { Align(verts, indexOffset, alignment, x, lineWidth); indexOffset = verts.size; } x = 0; y += lineHeight; prev = 0; continue; } if (c < ' ') { prev = 0; continue; } if (encoding && c == '[') { int retVal = NGUITools.ParseSymbol(text, i, mColors, premultiply); if (retVal > 0) { color = mColors[mColors.Count - 1]; i += retVal - 1; continue; } } if (!dynamic) { // See if there is a symbol matching this text BMSymbol symbol = useSymbols ? MatchSymbol(text, i, textLength) : null; if (symbol == null) { BMGlyph glyph = mFont.GetGlyph(c); if (glyph == null) continue; if (prev != 0) x += glyph.GetKerning(prev); if (c == ' ') { x += mSpacingX + glyph.advance; prev = c; continue; } v0.x = invSize.x * (x + glyph.offsetX); v0.y = -invSize.y * (y + glyph.offsetY); v1.x = v0.x + invSize.x * glyph.width; v1.y = v0.y - invSize.y * glyph.height; u0.x = mUVRect.xMin + invX * glyph.x; u0.y = mUVRect.yMax - invY * glyph.y; u1.x = u0.x + invX * glyph.width; u1.y = u0.y - invY * glyph.height; x += mSpacingX + glyph.advance; prev = c; if (glyph.channel == 0 || glyph.channel == 15) { for (int b = 0; b < 4; ++b) cols.Add(color); } else { // Packed fonts come as alpha masks in each of the RGBA channels. // In order to use it we need to use a special shader. // // Limitations: // - Effects (drop shadow, outline) will not work. // - Should not be a part of the atlas (eastern fonts rarely are anyway). // - Lower color precision Color col = color; col *= 0.49f; switch (glyph.channel) { case 1: col.b += 0.51f; break; case 2: col.g += 0.51f; break; case 4: col.r += 0.51f; break; case 8: col.a += 0.51f; break; } for (int b = 0; b < 4; ++b) cols.Add(col); } } else { v0.x = invSize.x * (x + symbol.offsetX); v0.y = -invSize.y * (y + symbol.offsetY); v1.x = v0.x + invSize.x * symbol.width; v1.y = v0.y - invSize.y * symbol.height; Rect uv = symbol.uvRect; u0.x = uv.xMin; u0.y = uv.yMax; u1.x = uv.xMax; u1.y = uv.yMin; x += mSpacingX + symbol.advance; i += symbol.length - 1; prev = 0; if (symbolStyle == SymbolStyle.Colored) { for (int b = 0; b < 4; ++b) cols.Add(color); } else { Color32 col = Color.white; col.a = color.a; for (int b = 0; b < 4; ++b) cols.Add(col); } } verts.Add(new Vector3(v1.x, v0.y)); verts.Add(new Vector3(v1.x, v1.y)); verts.Add(new Vector3(v0.x, v1.y)); verts.Add(new Vector3(v0.x, v0.y)); uvs.Add(new Vector2(u1.x, u0.y)); uvs.Add(new Vector2(u1.x, u1.y)); uvs.Add(new Vector2(u0.x, u1.y)); uvs.Add(new Vector2(u0.x, u0.y)); } #if !UNITY_3_5 else { // See if there is a symbol matching this text BMSymbol symbol = useSymbols ? MatchSymbol(text, i, textLength) : null; if(symbol != null) { v0.x = invSize.x * (x + symbol.offsetX); v0.y = -invSize.y * (y + symbol.offsetY); v1.x = v0.x + invSize.x * symbol.width; v1.y = v0.y - invSize.y * symbol.height; Rect uv = symbol.uvRect; u0.x = uv.xMin; u0.y = uv.yMax; u1.x = uv.xMax; u1.y = uv.yMin; //x += mSpacingX + symbol.advance; x += mSpacingX + mDynamicFontSize; i += symbol.length - 1; prev = 0; if (symbolStyle == SymbolStyle.Colored) { Color32 col = color; col.a = 0; for (int b = 0; b < 4; ++b) cols.Add(col); } else { Color32 col = Color.white; col.a = 0; for (int b = 0; b < 4; ++b) cols.Add(col); } uvs.Add(new Vector2(0, 0)); uvs.Add(new Vector2(0, 0)); uvs.Add(new Vector2(0, 0)); uvs.Add(new Vector2(0, 0)); verts.Add(new Vector3(v1.x, v0.y)); verts.Add(new Vector3(v0.x, v0.y)); //maybe need to add into dynamic symblos verts dynamicSymblosIndexs.Add(verts.size - 1); //record this index position.. dynamicSymblosPosBase.Add(new Vector3(v0.x, v0.y)); dynamicSymblosKeyName.Add(symbol.spriteName); verts.Add(new Vector3(v0.x, v1.y)); verts.Add(new Vector3(v1.x, v1.y)); } else // add dynamic font's info { if (!mDynamicFont.GetCharacterInfo(c, out mChar, mDynamicFontSize, mDynamicFontStyle)) continue; v0.x = invSize.x * (x + mChar.vert.xMin); v0.y = -invSize.y * (y - mChar.vert.yMax + mDynamicFontOffset); v1.x = v0.x + invSize.x * mChar.vert.width; v1.y = v0.y - invSize.y * mChar.vert.height; u0.x = mChar.uv.xMin; u0.y = mChar.uv.yMin; u1.x = mChar.uv.xMax; u1.y = mChar.uv.yMax; x += mSpacingX + (int)mChar.width; for (int b = 0; b < 4; ++b) cols.Add(color); if (mChar.flipped) { uvs.Add(new Vector2(u0.x, u1.y)); uvs.Add(new Vector2(u0.x, u0.y)); uvs.Add(new Vector2(u1.x, u0.y)); uvs.Add(new Vector2(u1.x, u1.y)); } else { uvs.Add(new Vector2(u1.x, u0.y)); uvs.Add(new Vector2(u0.x, u0.y)); uvs.Add(new Vector2(u0.x, u1.y)); uvs.Add(new Vector2(u1.x, u1.y)); } verts.Add(new Vector3(v1.x, v0.y)); verts.Add(new Vector3(v0.x, v0.y)); verts.Add(new Vector3(v0.x, v1.y)); verts.Add(new Vector3(v1.x, v1.y)); } } #endif } if (alignment != Alignment.Left && indexOffset < verts.size) { Align(verts, indexOffset, alignment, x, lineWidth); indexOffset = verts.size; } //get all after align dynamic symblos verts if(dynamicSymblosIndexs.Count > 0 && mDynamicSymbolLabel != null) { for(int i =0; i< dynamicSymblosIndexs.Count; i++) { int tIndex = dynamicSymblosIndexs[i]; if(tIndex <= verts.size - 1) { if(alignment == Alignment.Left) mDynamicSymbolLabel.addSymbloVert(dynamicSymblosPosBase[i],dynamicSymblosKeyName[i]); else mDynamicSymbolLabel.addSymbloVert(verts.buffer[tIndex],dynamicSymblosKeyName[i]); } } } //reset mDynamicSymbolLabel = null; } }
/// <summary> /// Draw the label. /// </summary> public override void OnFill(BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols) { if (mFont == null) { return; } MakePositionPerfect(); Pivot p = pivot; int offset = verts.size; Color col = color; col.a *= mPanel.alpha; if (font.premultipliedAlpha) { col = NGUITools.ApplyPMA(col); } //add by chiuan if (isUsedEmojiSprite && mEmojiSpriteLabel == null) { mEmojiSpriteLabel = EmojiSpriteLabel.create(this); } if (mEmojiSpriteLabel != null && mFont.isDynamic && mFont.dynamicSymbolsFont != null) { mEmojiSpriteLabel.destroy(); mFont.dynamicSymbolLabel = mEmojiSpriteLabel; } // Print the text into the buffers if (p == Pivot.Left || p == Pivot.TopLeft || p == Pivot.BottomLeft) { mFont.Print(processedText, col, verts, uvs, cols, mEncoding, mSymbols, NGUIFont.Alignment.Left, 0, mPremultiply); } else if (p == Pivot.Right || p == Pivot.TopRight || p == Pivot.BottomRight) { mFont.Print(processedText, col, verts, uvs, cols, mEncoding, mSymbols, NGUIFont.Alignment.Right, Mathf.RoundToInt(relativeSize.x * mFont.size), mPremultiply); } else { mFont.Print(processedText, col, verts, uvs, cols, mEncoding, mSymbols, NGUIFont.Alignment.Center, Mathf.RoundToInt(relativeSize.x * mFont.size), mPremultiply); } // Apply an effect if one was requested if (effectStyle != Effect.None) { Vector3 scale = cachedTransform.localScale; if (scale.x == 0f || scale.y == 0f) { return; } int end = verts.size; float pixel = 1f / mFont.size; float fx = pixel * mEffectDistance.x; float fy = pixel * mEffectDistance.y; ApplyShadow(verts, uvs, cols, offset, end, fx, -fy); if (effectStyle == Effect.Outline) { offset = end; end = verts.size; ApplyShadow(verts, uvs, cols, offset, end, -fx, fy); offset = end; end = verts.size; ApplyShadow(verts, uvs, cols, offset, end, fx, fy); offset = end; end = verts.size; ApplyShadow(verts, uvs, cols, offset, end, -fx, -fy); } } //maybe create all symblos if (mEmojiSpriteLabel != null) { mEmojiSpriteLabel.createSymblos(); } MakePixelPerfect(); }