Skip to content

Instantly share code, notes, and snippets.

@hww
Last active December 31, 2015 21:59
Show Gist options
  • Save hww/8050796 to your computer and use it in GitHub Desktop.
Save hww/8050796 to your computer and use it in GitHub Desktop.
Futils extension
using UnityEngine;
using System.Collections;
/*
*
* By: hww
*
* Use:
*
* animatedLabel = new FLabelAnimated("Tondu");
* Futile.stage.AddChild(animatedLabel);
*
* // First set text. It should init animation
*
* animatedLabel.text = "Happy New Year";
*
* // Set "target values" for all characters
*
* animatedLabel.effect.PositionTo(new Vector2(0,800), 800);
* Color [] colors = new Color[] {Color.red, Color.blue};
* animatedLabel.effect.ColorTo(colors,2);
* animatedLabel.effect.ScaleTo(new Vector2(2,5f), new Vector2(5f,2));
* animatedLabel.effect.RotationTo(2,5);
*
* // Instead of run effect just targets as current values
*
* animatedLabel.effect.Appply();
*
* // Now set new "target values" for characters
*
* animatedLabel.effect.PositionTo(new Vector2(0,0), 0, 1);
* animatedLabel.effect.ColorTo(Color.white, 1);
* animatedLabel.effect.ScaleTo(new Vector2(1,1), new Vector2(1,1),1);
* animatedLabel.effect.RotationTo(0,0,1);
*
* // Make delay between chars
*
* animatedLabel.effectPhase = 0.5f;
*
* // Now set effect time. do it per frame tu update text
*
* animatedLabel.effectTime = Time.time;
*
*/
public class FLabelAnimated : FLabel {
FTextEffect effect_; // https://gist.github.com/hww/8050800
float effectTime_;
float effectPhase_;
string text_;
public FLabelAnimated(string fontName):this(fontName, new FTextParams())
{
}
public FLabelAnimated(string fontName, FTextParams textParams):base(fontName, "", textParams)
{
effect_ = new FTextEffect();
effectTime_ = -1;
}
public FTextEffect effect {
get { return effect_; }
}
public float effectPhase {
get { return effectPhase_; }
set { effectPhase_ = value; }
}
public float effectTime {
get { return effectTime_; }
set {
if (effectTime_!=value) {
effectTime_ = value;
effect_.Update(effectTime_, effectPhase_);
_isAlphaDirty=true;
}
}
}
public override string text {
get { return text_; }
set {
if (text_!=value) {
text_ = value;
effect_.Init(text_);
base.text = value;
_isAlphaDirty=true;
}
}
}
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 charIdx=0;
int lineCount = _letterQuadLines.Length;
for(int i = 0; i<lineCount; i++)
{
FLetterQuad[] quads = _letterQuadLines[i].quads;
int quadCount = quads.Length;
for(int q = 0; q<quadCount; q++)
{
FLetterQuad quad = quads[q];
FCharInfo charInfo = quad.charInfo;
Color c = effect_.GetColor (charIdx);
c.a *= _alphaColor.a;
Vector2 position = effect_.GetPosition (charIdx);
Vector2 scale = effect_.GetScale (charIdx);
float rotation = effect_.GetRotation (charIdx);
float sin = (float)Mathf.Sin(rotation);
float cos = (float)Mathf.Cos(rotation);
Rect rect = quad.rect;
Vector2 center = rect.center;
float cx = center.x;
float cy = center.y;
float ix, iy;
ix = rect.xMin - cx;
iy = rect.yMax - cy;
float tlX = ix * cos - iy * sin;
float tlY = ix * sin + iy * cos;
ix = rect.xMax - cx;
iy = rect.yMax - cy;
float trX = ix * cos - iy * sin;
float trY = ix * sin + iy * cos;
ix = rect.xMax - cx;
iy = rect.yMin - cy;
float brX = ix * cos - iy * sin;
float brY = ix * sin + iy * cos;
ix = rect.xMin - cx;
iy = rect.yMin - cy;
float blX = ix * cos - iy * sin;
float blY = ix * sin + iy * cos;
cx += position.x - textRect.width * anchorX;
cy += position.y - textRect.height * anchorY;
tlX = tlX * scale.x + cx; tlY = tlY * scale.y + cy;
trX = trX * scale.x + cx; trY = trY * scale.y + cy;
brX = brX * scale.x + cx; brY = brY * scale.y + cy;
blX = blX * scale.x + cx; blY = blY * scale.y + cy;
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex0], new Vector2(tlX,tlY), 0);
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex1], new Vector2(trX,trY), 0);
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex2], new Vector2(brX,brY), 0);
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex3], new Vector2(blX,blY), 0);
uvs[vertexIndex0] = charInfo.uvTopLeft;
uvs[vertexIndex1] = charInfo.uvTopRight;
uvs[vertexIndex2] = charInfo.uvBottomRight;
uvs[vertexIndex3] = charInfo.uvBottomLeft;
colors[vertexIndex0] = c;
colors[vertexIndex1] = c;
colors[vertexIndex2] = c;
colors[vertexIndex3] = c;
vertexIndex0 += 4;
vertexIndex1 += 4;
vertexIndex2 += 4;
vertexIndex3 += 4;
charIdx++;
}
}
_renderLayer.HandleVertsChange();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment