Example #1
0
 public BattleTextSentance(BattleTextFont font, string sentance)
 {
     Font       = font;
     Sentance   = sentance;
     glyphs     = font.GetGlyphs(sentance);
     GlyphCount = glyphs.Length;
 }
Example #2
0
    public static BattleTextFont Load(TextAsset asset)
    {
        if (asset == null)
        {
            Debug.LogError("[NamePlates] Text asset was null");
            return(null);
        }

        BattleTextFont font;

        if (!fonts.TryGetValue(asset.name, out font))
        {
            font = new BattleTextFont();
            StringReader reader = new StringReader(asset.text);
            XmlReader    xml    = XmlReader.Create(reader);

            while (xml.Read())
            {
                if (xml.NodeType == XmlNodeType.Element)
                {
                    switch (xml.Name)
                    {
                    case "info":
                        font.Name      = xml.GetAttribute(0);
                        font.GlyphSize = Int32.Parse(xml.GetAttribute(1));
                        break;

                    case "common":
                        font.TextureSize = Single.Parse(xml.GetAttribute(2));
                        break;

                    case "char":
                        int   id     = Int32.Parse(xml.GetAttribute(0));
                        float x      = Single.Parse(xml.GetAttribute(1));
                        float y      = Single.Parse(xml.GetAttribute(2));
                        float width  = Single.Parse(xml.GetAttribute(3));
                        float height = Single.Parse(xml.GetAttribute(4));
                        float offset = Single.Parse(xml.GetAttribute(6));

                        font.Glyphs[id] = new BattleTextGlyph(id, x, y, width, height, offset);

                        break;
                    }
                }
            }

            fonts.Add(asset.name, font);
        }

        return(font);
    }
Example #3
0
    public static BattleTextFont Load(TextAsset asset)
    {
        if (asset == null)
        {
            Debug.LogError("[NamePlates] Text asset was null");
            return null;
        }

        BattleTextFont font;

        if (!fonts.TryGetValue(asset.name, out font))
        {
            font = new BattleTextFont();
            StringReader reader = new StringReader(asset.text);
            XmlReader xml = XmlReader.Create(reader);

            while (xml.Read())
            {
                if (xml.NodeType == XmlNodeType.Element)
                {
                    switch (xml.Name)
                    {
                        case "info":
                            font.Name = xml.GetAttribute(0);
                            font.GlyphSize = Int32.Parse(xml.GetAttribute(1));
                            break;

                        case "common":
                            font.TextureSize = Single.Parse(xml.GetAttribute(2));
                            break;

                        case "char":
                            int id = Int32.Parse(xml.GetAttribute(0));
                            float x = Single.Parse(xml.GetAttribute(1));
                            float y = Single.Parse(xml.GetAttribute(2));
                            float width = Single.Parse(xml.GetAttribute(3));
                            float height = Single.Parse(xml.GetAttribute(4));
                            float offset = Single.Parse(xml.GetAttribute(6));

                            font.Glyphs[id] = new BattleTextGlyph(id, x, y, width, height, offset);

                            break;
                    }
                }
            }

            fonts.Add(asset.name, font);
        }

        return font;
    }
Example #4
0
    void SetText(TextAsset fontDefinition, Material fontMaterial, string text)
    {
        if (fontDefinition == null)
        {
            Debug.LogError("[BattleTextRenderer] Parameter 'fontDefinition' was null");
            return;
        }

        if (fontMaterial == null)
        {
            Debug.LogError("[BattleTextRenderer] Parameter 'fontMaterial' was null");
            return;
        }

        text     = text ?? "";
        font     = BattleTextFont.Load(fontDefinition);
        sentance = font.MakeSentance(text);

        MeshFilter   filter   = GetComponent <MeshFilter>();
        MeshRenderer renderer = GetComponent <MeshRenderer>();

        renderer.material = fontMaterial;

        mesh = filter.mesh;
        mesh.Clear();

        Vector3[] vs   = new Vector3[sentance.GlyphCount * 4];
        Vector2[] uvs  = new Vector2[vs.Length];
        Color[]   clr  = new Color[vs.Length];
        int[]     tris = new int[sentance.GlyphCount * 2 * 3];

        int   v  = 0;
        int   t  = 0;
        int   u  = 0;
        int   gc = sentance.GlyphCount;
        float ts = font.TextureSize;

        float r         = Color.r;
        float g         = Color.g;
        float b         = Color.b;
        float time      = Time.time + FadeDelay;
        float offset    = 0; // -(sentance.CalculateWidth(GlyphSpacing) / 2f);
        float maxHeight = float.MinValue;

        for (int gn = 0; gn < gc; ++gn)
        {
            if (gn > 0)
            {
                offset += GlyphSpacing;
            }

            BattleTextGlyph glyph = sentance[gn];

            // Vertices

            float width       = glyph.CalculateWidth(font);
            float height      = glyph.CalculateHeight(font);
            float glyphOffset = glyph.CalculateOffset(font);

            maxHeight = Mathf.Max(maxHeight, height);

            if (MonoSpace != 0f)
            {
                float moffset = (MonoSpace * gn) - width;

                vs[v + 0] = new Vector3(moffset, -glyphOffset);
                vs[v + 1] = new Vector3(moffset + width, -glyphOffset);
                vs[v + 2] = new Vector3(moffset, -height - glyphOffset);
                vs[v + 3] = new Vector3(moffset + width, -height - glyphOffset);
            }
            else
            {
                vs[v + 0] = new Vector3(offset, -glyphOffset);
                vs[v + 1] = new Vector3(offset + width, -glyphOffset);
                vs[v + 2] = new Vector3(offset, -height - glyphOffset);
                vs[v + 3] = new Vector3(offset + width, -height - glyphOffset);
            }

            // Triangles

            tris[t + 0] = v + 0;
            tris[t + 1] = v + 1;
            tris[t + 2] = v + 2;

            tris[t + 5] = v + 2;
            tris[t + 4] = v + 3;
            tris[t + 3] = v + 1;

            // UVs

            float x = glyph.X;
            float y = glyph.Y;
            float w = glyph.Width;
            float h = glyph.Height;

            float top   = ts - y;
            float right = x + w;

            if (IsAnimated)
            {
                // Magic :)
                uvs[v + 0] = new Vector2(x / ts, time);
                uvs[v + 1] = new Vector2(right / ts, time);
                uvs[v + 2] = new Vector2(x / ts, time);
                uvs[v + 3] = new Vector2(right / ts, time);

                clr[v + 0] = new Color(r, g, b, top / ts);
                clr[v + 1] = new Color(r, g, b, top / ts);
                clr[v + 2] = new Color(r, g, b, (top - h) / ts);
                clr[v + 3] = new Color(r, g, b, (top - h) / ts);
            }
            else
            {
                uvs[v + 0] = new Vector2(x / ts, top / ts);
                uvs[v + 1] = new Vector2(right / ts, top / ts);
                uvs[v + 2] = new Vector2(x / ts, (top - h) / ts);
                uvs[v + 3] = new Vector2(right / ts, (top - h) / ts);

                clr[v + 0] = Color;
                clr[v + 1] = Color;
                clr[v + 2] = Color;
                clr[v + 3] = Color;
            }

            // Increment for next

            t += 6;
            v += 4;
            u += 4;

            offset += MonoSpace != 0f ? MonoSpace : width;
        }

        if (MonoSpace != 0 && gc > 0)
        {
            FpsHudAnchor anchor = GetComponent <FpsHudAnchor>();
            anchor.Height = (int)(maxHeight * GlyphSize);
            anchor.Width  = (int)(GlyphSize * MonoSpace) * gc;
        }

        mesh.vertices  = vs;
        mesh.uv        = uvs;
        mesh.colors    = clr;
        mesh.triangles = tris;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
    }
Example #5
0
 public float CalculateOffset(BattleTextFont font)
 {
     return(Offset / font.GlyphSize);
 }
Example #6
0
 public float CalculateHeight(BattleTextFont font)
 {
     return(Height / font.GlyphSize);
 }
Example #7
0
 public float CalculateWidth(BattleTextFont font)
 {
     return(Width / font.GlyphSize);
 }
    void SetText(TextAsset fontDefinition, Material fontMaterial, string text)
    {
        if (fontDefinition == null)
        {
            Debug.LogError("[BattleTextRenderer] Parameter 'fontDefinition' was null");
            return;
        }

        if (fontMaterial == null)
        {
            Debug.LogError("[BattleTextRenderer] Parameter 'fontMaterial' was null");
            return;
        }

        text = text ?? "";
        font = BattleTextFont.Load(fontDefinition);
        sentance = font.MakeSentance(text);

        MeshFilter filter = GetComponent<MeshFilter>();
        MeshRenderer renderer = GetComponent<MeshRenderer>();

        renderer.material = fontMaterial;

        mesh = filter.mesh;
        mesh.Clear();

        Vector3[] vs = new Vector3[sentance.GlyphCount * 4];
        Vector2[] uvs = new Vector2[vs.Length];
        Color[] clr = new Color[vs.Length];
        int[] tris = new int[sentance.GlyphCount * 2 * 3];

        int v = 0;
        int t = 0;
        int u = 0;
        int gc = sentance.GlyphCount;
        float ts = font.TextureSize;

        float r = Color.r;
        float g = Color.g;
        float b = Color.b;
        float time = Time.time + FadeDelay;

        float offset = 0; // -(sentance.CalculateWidth(GlyphSpacing) / 2f);

        for (int gn = 0; gn < gc; ++gn)
        {
            if (gn > 0)
            {
                offset += GlyphSpacing;
            }

            BattleTextGlyph glyph = sentance[gn];

            // Vertices

            float width = glyph.CalculateWidth(font);
            float height = glyph.CalculateHeight(font);
            float glyphOffset = glyph.CalculateOffset(font);

            vs[v + 0] = new Vector3(offset, -glyphOffset);
            vs[v + 1] = new Vector3(offset + width, -glyphOffset);
            vs[v + 2] = new Vector3(offset, -height - glyphOffset);
            vs[v + 3] = new Vector3(offset + width, -height - glyphOffset);

            // Triangles

            tris[t + 0] = v + 0;
            tris[t + 1] = v + 1;
            tris[t + 2] = v + 2;

            tris[t + 5] = v + 2;
            tris[t + 4] = v + 3;
            tris[t + 3] = v + 1;

            // UVs

            float x = glyph.X;
            float y = glyph.Y;
            float w = glyph.Width;
            float h = glyph.Height;

            float top = ts - y;
            float right = x + w;

            if (IsAnimated)
            {
                // Magic :)
                uvs[v + 0] = new Vector2(x / ts, time);
                uvs[v + 1] = new Vector2(right / ts, time);
                uvs[v + 2] = new Vector2(x / ts, time);
                uvs[v + 3] = new Vector2(right / ts, time);

                clr[v + 0] = new Color(r, g, b, top / ts);
                clr[v + 1] = new Color(r, g, b, top / ts);
                clr[v + 2] = new Color(r, g, b, (top - h) / ts);
                clr[v + 3] = new Color(r, g, b, (top - h) / ts);
            }
            else
            {
                uvs[v + 0] = new Vector2(x / ts, top / ts);
                uvs[v + 1] = new Vector2(right / ts, top / ts);
                uvs[v + 2] = new Vector2(x / ts, (top - h) / ts);
                uvs[v + 3] = new Vector2(right / ts, (top - h) / ts);

                clr[v + 0] = Color;
                clr[v + 1] = Color;
                clr[v + 2] = Color;
                clr[v + 3] = Color;
            }

            // Increment for next

            t += 6;
            v += 4;
            u += 4;

            offset += width;
        }

        mesh.vertices = vs;
        mesh.uv = uvs;
        mesh.colors = clr;
        mesh.triangles = tris;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
    }
Example #9
0
 public BattleTextSentance(BattleTextFont font, string sentance)
 {
     Font = font;
     Sentance = sentance;
     glyphs = font.GetGlyphs(sentance);
     GlyphCount = glyphs.Length;
 }
Example #10
0
 public float CalculateOffset(BattleTextFont font)
 {
     return Offset / font.GlyphSize;
 }
Example #11
0
 public float CalculateHeight(BattleTextFont font)
 {
     return Height / font.GlyphSize;
 }
Example #12
0
 public float CalculateWidth(BattleTextFont font)
 {
     return Width / font.GlyphSize;
 }