Created
August 3, 2013 08:50
-
-
Save wtrebella/6145763 to your computer and use it in GitHub Desktop.
Word wrapping and coloring labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class WTLabel : FLabel { | |
protected Color[] quadColors_; | |
public WTLabel(string fontName, string text) : base(fontName, text) { | |
} | |
public void SetText(string text, Color[] letterColors) { | |
this.text = text; | |
CreateTextQuads(); | |
int quadCount = _letterQuadLines.Length - 1; | |
for (int i = 0; i < _letterQuadLines.Length; i++) { | |
quadCount += _letterQuadLines[i].quads.Length; | |
} | |
if (letterColors.Length != quadCount) throw new FutileException("color array has to have same amount of items as letters"); | |
quadColors_ = letterColors; | |
} | |
public void SetText(string text) { | |
this.text = text; | |
quadColors_ = null; | |
} | |
override public void PopulateRenderLayer() | |
{ | |
if(_isOnStage && _firstFacetIndex != -1) | |
{ | |
_isMeshDirty = false; | |
Vector3[] vertices = _renderLayer.vertices; | |
Vector2[] uvs = _renderLayer.uvs; | |
Color[] colors = _renderLayer.colors; | |
int vertexIndex0 = _firstFacetIndex*4; | |
int vertexIndex1 = vertexIndex0 + 1; | |
int vertexIndex2 = vertexIndex0 + 2; | |
int vertexIndex3 = vertexIndex0 + 3; | |
int lineCount = _letterQuadLines.Length; | |
for(int i = 0; i<lineCount; i++) | |
{ | |
FLetterQuad[] quads = _letterQuadLines[i].quads; | |
int quadNumBasis = 0; | |
for (int l = 0; l < i; l++) quadNumBasis += _letterQuadLines[l].quads.Length; | |
int quadCount = quads.Length; | |
for(int q = 0; q<quadCount; q++) | |
{ | |
FLetterQuad quad = quads[q]; | |
FCharInfo charInfo = quad.charInfo; | |
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex0], quad.topLeft,0); | |
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex1], quad.topRight,0); | |
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex2], quad.bottomRight,0); | |
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex3], quad.bottomLeft,0); | |
uvs[vertexIndex0] = charInfo.uvTopLeft; | |
uvs[vertexIndex1] = charInfo.uvTopRight; | |
uvs[vertexIndex2] = charInfo.uvBottomRight; | |
uvs[vertexIndex3] = charInfo.uvBottomLeft; | |
if (quadColors_ != null) { | |
colors[vertexIndex0] = quadColors_[quadNumBasis+q]; | |
colors[vertexIndex1] = quadColors_[quadNumBasis+q]; | |
colors[vertexIndex2] = quadColors_[quadNumBasis+q]; | |
colors[vertexIndex3] = quadColors_[quadNumBasis+q]; | |
} | |
else { | |
colors[vertexIndex0] = _alphaColor; | |
colors[vertexIndex1] = _alphaColor; | |
colors[vertexIndex2] = _alphaColor; | |
colors[vertexIndex3] = _alphaColor; | |
} | |
vertexIndex0 += 4; | |
vertexIndex1 += 4; | |
vertexIndex2 += 4; | |
vertexIndex3 += 4; | |
} | |
} | |
_renderLayer.HandleVertsChange(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string s = "This is the string that we're going to wrap after 20 chars and then we're going to color some words"; | |
s = WTUtils.WordWrap(s, 20); | |
Color[] colors = new Color[s.Length]; | |
for (int i = 0; i < s.Length; i++) { | |
if (i >= 12 && i <= 17) colors[i] = Color.red; | |
else if (i >= 50 && i <= 55) colors[i] = Color.blue; | |
else if (i >= 79 && i <= 83) colors[i] = Color.green; | |
else colors[i] = Color.black; | |
} | |
WTLabel label = new WTLabel("franchise", ""); | |
label.SetText(s, colors); | |
label.SetPosition(Futile.screen.halfWidth, Futile.screen.halfHeight); | |
AddChild(label); | |
// this is the result! http://i.imgur.com/uKrnmnr.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//word wrap method found at http://www.codeproject.com/Articles/51488/Implementing-Word-Wrap-in-C | |
public static string WordWrap(string text, int width) { | |
int pos, next; | |
StringBuilder sb = new StringBuilder(); | |
// Lucidity check | |
if (width < 1) | |
return text; | |
// Parse each line of text | |
for (pos = 0; pos < text.Length; pos = next) | |
{ | |
// Find end of line | |
int eol = text.IndexOf(Environment.NewLine, pos); | |
if (eol == -1) | |
next = eol = text.Length; | |
else | |
next = eol + Environment.NewLine.Length; | |
// Copy this line of text, breaking into smaller lines as needed | |
if (eol > pos) | |
{ | |
do | |
{ | |
int len = eol - pos; | |
if (len > width) | |
len = BreakLine(text, pos, width); | |
sb.Append(text, pos, len); | |
sb.Append(Environment.NewLine); | |
// Trim whitespace following break | |
pos += len; | |
while (pos < eol && Char.IsWhiteSpace(text[pos])) | |
pos++; | |
} while (eol > pos); | |
} | |
else sb.Append(Environment.NewLine); // Empty line | |
} | |
return sb.ToString(); | |
} | |
public static int NumLinesInString(string text) { | |
int num = 0; | |
// windows and mac add new lines differently | |
if (IsWindows()) foreach (char c in text) { | |
if ((int)c == 13) num++; | |
} | |
else foreach (char ch in text) { | |
//UnityEngine.Debug.Log((int)ch); | |
if ((int)ch == 10) num++; | |
} | |
return num; | |
} | |
private static int BreakLine(string text, int pos, int max) { | |
// Find last whitespace in line | |
int i = max; | |
while (i >= 0 && !Char.IsWhiteSpace(text[pos + i])) | |
i--; | |
// If no whitespace found, break at maximum length | |
if (i < 0) | |
return max; | |
// Find start of whitespace | |
while (i >= 0 && Char.IsWhiteSpace(text[pos + i])) | |
i--; | |
// Return length of text before whitespace | |
return i + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment