GetTextSize() public method

public GetTextSize ( string text ) : Vector2
text string
return Vector2
Ejemplo n.º 1
0
    public LabelContent(GUIContent c, float x, float y, Color t, Color s, GUIFont font)
    {
        this.content = c;
        this.textColor = t;
        this.shadowColor = s;

        Vector2 size;
        if(this.content.image != null)
        {
            size = new Vector2(this.content.image.width, this.content.image.height);
        }
        else
        {
            size = font.GetTextSize(this.content.text);
        }
        this.bounds = new Rect(x, y, size.x, size.y);
    }
Ejemplo n.º 2
0
    public Texture2D AddTexture(Texture2D hudTexture, Character c, Vector2 pos, GUIFont font, int hudID)
    {
        if(HUDElementType.TEXT.Equals(this.type))
        {
            hudTexture = font.AddTextTexture(hudTexture, this.text[GameHandler.GetLanguage()],
                    this.GetAnchoredRect(pos, font.GetTextSize(this.text[GameHandler.GetLanguage()]), this.bounds),
                    DataHolder.Color(this.textColor), DataHolder.Color(this.shadowColor), 0,
                    Vector2.zero, this.showShadow, this.shadowOffset, false);
        }
        else if(HUDElementType.IMAGE.Equals(this.type))
        {
            this.GetImage();
            if(this.texture != null)
            {
                Vector2 scaledSize = TextureDrawer.GetScaledSize(this.texture, this.bounds, this.scaleMode);
                hudTexture = TextureDrawer.AddTexture(hudTexture,
                        new Rect(this.bounds.x+pos.x, pos.y-this.bounds.y-scaledSize.y, scaledSize.x, scaledSize.y),
                        TextureDrawer.GetScaledPixels(this.texture, scaledSize, this.bounds, this.scaleMode));
            }
        }
        else if(HUDElementType.NAME.Equals(this.type))
        {
            GUIContent gc = null;
            if(HUDContentType.TEXT.Equals(this.contentType))
            {
                if(HUDNameType.CHARACTER.Equals(this.nameType)) gc = new GUIContent(c.GetName());
                else if(HUDNameType.CLASS.Equals(this.nameType)) gc = new GUIContent(DataHolder.Classes().GetName(c.currentClass));
                else if(HUDNameType.STATUS.Equals(this.nameType)) gc = new GUIContent(DataHolder.StatusValues().GetName(this.statusID));
            }
            else if(HUDContentType.ICON.Equals(this.contentType))
            {
                if(HUDNameType.CHARACTER.Equals(this.nameType)) gc = new GUIContent(c.GetIcon());
                else if(HUDNameType.CLASS.Equals(this.nameType)) gc = new GUIContent(DataHolder.Classes().GetIcon(c.currentClass));
                else if(HUDNameType.STATUS.Equals(this.nameType)) gc = new GUIContent(DataHolder.StatusValues().GetIcon(this.statusID));
            }
            else if(HUDContentType.BOTH.Equals(this.contentType))
            {
                if(HUDNameType.CHARACTER.Equals(this.nameType)) gc = c.GetContent();
                else if(HUDNameType.CLASS.Equals(this.nameType)) gc = DataHolder.Classes().GetContent(c.currentClass);
                else if(HUDNameType.STATUS.Equals(this.nameType)) gc = DataHolder.StatusValues().GetContent(this.statusID);
            }
            if(gc != null)
            {
                Vector2 size = Vector2.zero;
                float gap = 0;
                if("" != gc.text)
                {
                    size = font.GetTextSize(gc.text);
                    if(gc.image != null)
                    {
                        gap = font.GetTextSize(" ").x;
                        size.x += gap;
                    }
                }
                if(gc.image != null)
                {
                    size.x += gc.image.width;
                    if(size.y < gc.image.height) size.y = gc.image.height;
                }

                Rect rect = this.GetAnchoredRect(pos, size, this.bounds);
                if(gc.image != null)
                {
                    Texture2D tex = gc.image as Texture2D;
                    hudTexture = TextureDrawer.AddTexture(hudTexture,
                            new Rect(rect.x, rect.y, gc.image.width, gc.image.height),
                            tex.GetPixels());
                    rect.x += gc.image.width+gap;
                    rect.width -= gc.image.width+gap;
                }
                if("" != gc.text)
                {
                    hudTexture = font.AddTextTexture(hudTexture, gc.text, rect,
                        DataHolder.Color(this.textColor), DataHolder.Color(this.shadowColor), 0,
                        Vector2.zero, this.showShadow, this.shadowOffset, false);
                }
            }
        }
        else if(HUDElementType.STATUS.Equals(this.type))
        {
            c.status[this.statusID].lastValueHUD[hudID] = c.status[this.statusID].GetValue();
            if(HUDDisplayType.TEXT.Equals(this.displayType))
            {
                string txt = c.status[this.statusID].GetValue().ToString();
                if(c.status[this.statusID].IsConsumable() && this.showMax)
                {
                    txt += this.divider+c.status[c.status[this.statusID].maxStatus].GetValue().ToString();
                }
                hudTexture = font.AddTextTexture(hudTexture, txt,
                        this.GetAnchoredRect(pos, font.GetTextSize(txt), this.bounds),
                        DataHolder.Color(this.textColor), DataHolder.Color(this.shadowColor), 0,
                        Vector2.zero, this.showShadow, this.shadowOffset, false);
            }
            else if(HUDDisplayType.BAR.Equals(this.displayType) && c.status[this.statusID].IsConsumable())
            {
                float v1 = c.status[this.statusID].GetValue();
                float v2 = c.status[c.status[this.statusID].maxStatus].GetValue();
                v2 /= 100;
                v1 /= v2;

                this.GetImage();
                if(this.texture != null)
                {
                    Rect b = new Rect(this.bounds.x, this.bounds.y, this.bounds.width*v1/100, this.bounds.height);
                    Vector2 scaledSize = TextureDrawer.GetScaledSize(this.texture, b, this.scaleMode);
                    hudTexture = TextureDrawer.AddTexture(hudTexture,
                            new Rect(b.x+pos.x, pos.y-b.y-scaledSize.y, scaledSize.x, scaledSize.y),
                            TextureDrawer.GetScaledPixels(this.texture, scaledSize, b, this.scaleMode));
                }
            }
        }
        else if(HUDElementType.TIMEBAR.Equals(this.type) ||
            HUDElementType.USED_TIMEBAR.Equals(this.type) ||
            HUDElementType.CASTTIME.Equals(this.type))
        {
            float v1 = 0;
            float v2 = 0;
            if(HUDElementType.TIMEBAR.Equals(this.type))
            {
                v1 = c.timeBar;
                v2 = DataHolder.BattleSystem().maxTimebar;
                c.timeBarHUD[hudID] = v1;
            }
            else if(HUDElementType.USED_TIMEBAR.Equals(this.type))
            {
                v1 = c.usedTimeBar;
                v2 = DataHolder.BattleSystem().maxTimebar;
                c.usedTimeBarHUD[hudID] = v1;
            }
            else if(HUDElementType.CASTTIME.Equals(this.type))
            {
                v1 = c.GetSkillCastTime();
                v2 = c.GetSkillCastTimeMax();
                c.castTimeHUD[hudID] = v1;
            }
            if(HUDDisplayType.TEXT.Equals(this.displayType))
            {
                string txt = ((int)v1).ToString();
                hudTexture = font.AddTextTexture(hudTexture, txt,
                        this.GetAnchoredRect(pos, font.GetTextSize(txt), this.bounds),
                        DataHolder.Color(this.textColor), DataHolder.Color(this.shadowColor), 0,
                        Vector2.zero, this.showShadow, this.shadowOffset, false);
            }
            else if(HUDDisplayType.BAR.Equals(this.displayType) && v2 > 0)
            {
                v2 /= 100;
                v1 /= v2;

                this.GetImage();
                if(this.texture != null)
                {
                    Rect b = new Rect(this.bounds.x, this.bounds.y, this.bounds.width*v1/100, this.bounds.height);
                    Vector2 scaledSize = TextureDrawer.GetScaledSize(this.texture, b, this.scaleMode);
                    hudTexture = TextureDrawer.AddTexture(hudTexture,
                            new Rect(b.x+pos.x, pos.y-b.y-scaledSize.y, scaledSize.x, scaledSize.y),
                            TextureDrawer.GetScaledPixels(this.texture, scaledSize, b, this.scaleMode));
                }
            }
        }
        else if(HUDElementType.EFFECT.Equals(this.type))
        {
            c.effectHUD = false;
            int maxView = this.rows*this.columns;
            float cellWidth = this.bounds.width;
            float cellHeight = this.bounds.height;
            cellWidth -= this.spacing*(this.columns-1);
            cellHeight -= this.spacing*(this.rows-1);
            cellWidth /= this.columns;
            cellHeight /= this.rows;
            int currentColumn = 0;
            int currentRow = 0;
            for(int i=0; i<c.effect.Length; i++)
            {
                if(i >= maxView)
                {
                    break;
                }
                else
                {
                    GUIContent gc = null;
                    if(HUDContentType.TEXT.Equals(this.contentType))
                    {
                        gc = new GUIContent(DataHolder.Effects().GetName(c.effect[i].realID));
                    }
                    else if(HUDContentType.ICON.Equals(this.contentType))
                    {
                        gc = new GUIContent(DataHolder.Effects().GetIcon(c.effect[i].realID));
                    }
                    else if(HUDContentType.BOTH.Equals(this.contentType))
                    {
                        gc = DataHolder.Effects().GetContent(c.effect[i].realID);
                    }
                    if(gc != null)
                    {
                        Rect rect = new Rect(this.bounds.x+cellWidth*currentColumn+this.spacing*currentColumn,
                                this.bounds.y+cellHeight*currentRow+this.spacing*currentRow, cellWidth, cellHeight);

                        Vector2 size = Vector2.zero;
                        float gap = 0;
                        if("" != gc.text)
                        {
                            size = font.GetTextSize(gc.text);
                            if(gc.image != null)
                            {
                                gap = font.GetTextSize(" ").x;
                                size.x += gap;
                            }
                        }
                        if(gc.image != null)
                        {
                            size.x += gc.image.width;
                            if(size.y < gc.image.height) size.y = gc.image.height;
                        }

                        rect = this.GetAnchoredRect(pos, size, rect);
                        if(gc.image != null)
                        {
                            Texture2D tex = gc.image as Texture2D;
                            hudTexture = TextureDrawer.AddTexture(hudTexture,
                                    new Rect(rect.x, rect.y, gc.image.width, gc.image.height),
                                    tex.GetPixels());
                            rect.x += gc.image.width+gap;
                            rect.width -= gc.image.width+gap;
                        }
                        if("" != gc.text)
                        {
                            hudTexture = font.AddTextTexture(hudTexture, gc.text, rect,
                                DataHolder.Color(this.textColor), DataHolder.Color(this.shadowColor), 0,
                                Vector2.zero, this.showShadow, this.shadowOffset, false);
                        }
                        currentColumn++;
                    }
                    if(currentColumn >= this.columns)
                    {
                        currentColumn = 0;
                        currentRow++;
                    }
                }
            }
        }
        else if(HUDElementType.VARIABLE.Equals(this.type))
        {
            string txt = this.GetVariableText();
            if(txt != null && txt != "")
            {
                hudTexture = font.AddTextTexture(hudTexture, txt,
                        this.GetAnchoredRect(pos, font.GetTextSize(txt), this.bounds),
                        DataHolder.Color(this.textColor), DataHolder.Color(this.shadowColor), 0,
                        Vector2.zero, this.showShadow, this.shadowOffset, false);
            }
            this.lastVarText = txt;
        }
        return hudTexture;
    }
Ejemplo n.º 3
0
    public MultiContent(string text, DialoguePosition dp)
    {
        this.font = DataHolder.Fonts().GetFont(dp.skin.font);

        GUIStyle shadowStyle = new GUIStyle(dp.skin.label);
        shadowStyle.normal.textColor = DataHolder.Color(1);
        GUIStyle textStyle = new GUIStyle(dp.skin.label);
        textStyle.wordWrap = false;
        TextPosition textPosition = new TextPosition(dp.boxBounds, dp.boxPadding, dp.lineSpacing);
        textPosition.bounds.width -= (dp.boxPadding.x + dp.boxPadding.z);
        textPosition.bounds.height -= (dp.boxPadding.y + dp.boxPadding.w);

        this.originalText = text;
        this.label = new LabelContent[0];

        this.currentColor = 0;
        this.shadowColor = -1;
        int oldColor = 0;
        int oldShadowColor = -1;

        this.textPos = 0;
        int i = 0;
        int nl = 0;
        int fs = 0;
        int skip = 0;
        bool lineBreak = false;
        this.xPos = 0;
        this.yPos = 0;

        bool setNextX = false;
        bool setNextY = false;
        float nextX = 0;
        float nextY = 0;

        string addstring = "";
        bool addSpace = false;
        bool finished = false;

        Vector2 v = Vector2.zero;
        Color tCol;
        Color sCol;

        Texture2D icon = null;
        GUIContent part = null;
        LabelContent nextLabel;

        while(!finished)
        {
            skip = 0;
            lineBreak = false;
            oldColor = this.currentColor;
            oldShadowColor = this.shadowColor;
            icon = null;
            part = null;

            tCol = DataHolder.Color(this.currentColor);
            if(this.shadowColor == -1 || this.shadowColor >= DataHolder.Colors().GetDataCount())
            {
                sCol = DataHolder.Color(1);
            }
            else
            {
                sCol = DataHolder.Color(this.shadowColor);
            }

            fs = text.IndexOf("#", this.textPos);
            nl = text.IndexOf("\n", this.textPos);

            if(fs != -1 && (nl == -1 || fs < nl))
            {
                i = fs;
            }
            else
            {
                i = nl;
            }

            if(i == -1)
            {
                i = text.Length - 1;
                finished = true;
            }

            if(setNextX)
            {
                this.xPos = nextX;
                setNextX = false;
            }
            if(setNextY)
            {
                this.yPos = nextY;
                setNextY = false;
            }

            if(!finished)
            {
                if (text[i].ToString() == "#")
                {
                    if((text.Length - i) >= 1)
                    {
                        string nextChar = text[i+1].ToString();

                        if(nextChar == "c" || nextChar == "s")
                        {
                            // set text color for the next part
                            int nS = text.IndexOf("#", i+1);
                            if(nS != -1)
                            {
                                string sub = text.Substring(i, nS-i+1);
                                skip = sub.Length+1;

                                try
                                {
                                    if(nextChar == "c")
                                    {
                                        this.currentColor = int.Parse(sub.Substring(2, sub.Length-3));
                                    }
                                    else if(nextChar == "s")
                                    {
                                        this.shadowColor = int.Parse(sub.Substring(2, sub.Length-3));
                                    }
                                }
                                catch(System.Exception e)
                                {
                                    Debug.Log(e.Message);
                                    if(nextChar == "c")
                                    {
                                        this.currentColor = 0;
                                    }
                                    else if(nextChar == "s")
                                    {
                                        this.shadowColor = -1;
                                    }
                                }
                            }
                        }
                        else if(nextChar == "!")
                        {
                            this.currentColor = 0;
                            this.shadowColor = -1;
                            skip = 3;
                            if((text.Length - (i+skip-1)) >= 1 && (text.Substring(i+skip-1, 1) == "\n" || text.Substring(i+skip-1, 1) == "\r"))
                            {
                                lineBreak = true;
                                skip += 1;
                            }
                        }
                        else if(nextChar == "#")
                        {
                            i++;
                            skip = 2;
                        }
                        else if(nextChar == "p")
                        {
                            nextChar = text[i+2].ToString();

                            // set x or y position for the next text part
                            int nS = text.IndexOf("#", i+2);
                            if(nS != -1)
                            {
                                string sub = text.Substring(i, nS-i+1);
                                skip = sub.Length+1;

                                try
                                {
                                    if(nextChar == "x")
                                    {
                                        nextX = float.Parse(sub.Substring(3, sub.Length-4));
                                        setNextX = true;
                                    }
                                    else if(nextChar == "y")
                                    {
                                        nextY = float.Parse(sub.Substring(3, sub.Length-4));
                                        setNextY = true;
                                    }
                                }
                                catch(System.Exception e)
                                {
                                    Debug.Log(e.Message);
                                }
                            }
                        }
                        else if(nextChar == "i" && text[i+2].ToString() == "q") // icons
                        {
                            int nS = text.IndexOf("#", i+4);
                            if(nS != -1)
                            {
                                string sub = text.Substring(i, nS-i+1);
                                skip = sub.Length+1;
                                sub = text.Substring(i+5, nS-(i+5));
                                int index = int.Parse(sub);
                                sub = text.Substring(i+3, 2);
                                if(sub == "sv") icon = DataHolder.StatusValues().GetIcon(index);
                                else if(sub == "se") icon = DataHolder.Effects().GetIcon(index);
                                else if(sub == "el") icon = DataHolder.Elements().GetIcon(index);
                                else if(sub == "st") icon = DataHolder.SkillTypes().GetIcon(index);
                                else if(sub == "sk") icon = DataHolder.Skills().GetIcon(index);
                                else if(sub == "it") icon = DataHolder.ItemTypes().GetIcon(index);
                                else if(sub == "im") icon = DataHolder.Items().GetIcon(index);
                                else if(sub == "ir") icon = DataHolder.ItemRecipes().GetIcon(index);
                                else if(sub == "ep") icon = DataHolder.EquipmentParts().GetIcon(index);
                                else if(sub == "wp") icon = DataHolder.Weapons().GetIcon(index);
                                else if(sub == "am") icon = DataHolder.Armors().GetIcon(index);
                                else if(sub == "cl") icon = DataHolder.Classes().GetIcon(index);
                                else if(sub == "ch") icon = DataHolder.Characters().GetIcon(index);
                                else if(sub == "en") icon = DataHolder.Enemies().GetIcon(index);
                                else icon = null;
                            }
                        }
                        else
                        {
                            skip = 2;
                        }
                    }
                    else
                    {
                        skip = 1;
                    }
                }
                else if((text.Length - i) >= 1 && (text.Substring(i, 1) == "\n" || text.Substring(i, 1) == "\r"))
                {
                    lineBreak = true;
                    skip = 2;
                }
                i--;
            }

            if((i-this.textPos+1) > 0)
            {
                if(addSpace) addstring = " "; else addstring = "";
                part = new GUIContent(addstring + text.Substring(this.textPos, i - this.textPos + 1));

                nextLabel = new LabelContent(part, this.xPos, this.yPos, tCol, sCol, font);
                v = new Vector2(nextLabel.bounds.width, nextLabel.bounds.height);

                if ((this.xPos+v.x) > textPosition.bounds.width)
                {
                    i = this.textPos+font.GetFittingTextLength(part.text, textPosition.bounds.width-this.xPos);
                    lineBreak = true;
                    icon = null;
                    skip = 0;
                    int tmp = i;

                    part = new GUIContent(addstring + text.Substring(this.textPos, i - this.textPos + 1));
                    v = font.GetTextSize(part.text);

                    while (this.xPos+v.x > textPosition.bounds.width)
                    {
                        i--;
                        if((i-this.textPos+1) > 0)
                        {
                            part = new GUIContent(addstring + text.Substring(this.textPos, i - this.textPos + 1));
                        }
                        else
                        {
                            part = new GUIContent("");
                        }
                        v = font.GetTextSize(part.text);
                        tmp = i+1;
                    }

                    while(" " != text[i].ToString() && i>(this.textPos+1))
                    {
                        i--;
                    }
                    if((i-this.textPos+1) > 0 && " " == text[i].ToString())
                    {
                        part = new GUIContent(addstring + text.Substring(this.textPos, i - this.textPos + 1));
                    }
                    else
                    {
                        i = tmp;
                    }
                    nextLabel = new LabelContent(part, this.xPos, this.yPos, tCol, sCol, font);
                    v = new Vector2(nextLabel.bounds.width, nextLabel.bounds.height);
                    this.currentColor = oldColor;
                    this.shadowColor = oldShadowColor;
                    finished = false;
                }
                this.label = ArrayHelper.Add(nextLabel, this.label);
                this.xPos += v.x;

                if(!lineBreak)
                {
                    addSpace = part.text[part.text.Length-1].ToString() == " ";
                }
            }
            if(icon != null)
            {
                part = new GUIContent(icon);
                nextLabel = new LabelContent(part, this.xPos, this.yPos, tCol, sCol, font);
                v = new Vector2(nextLabel.bounds.width, nextLabel.bounds.height);
                //this.xPos += font.GetTextSize(".").x;
                if(this.xPos+v.x <= textPosition.bounds.width)
                {
                    this.label = ArrayHelper.Add(nextLabel, this.label);
                    this.xPos += v.x;
                    addSpace = true;
                    icon = null;
                }
                else
                {
                    lineBreak = true;
                }
            }
            if(lineBreak)
            {
                this.xPos = 0;
                this.yPos += v.y + textPosition.lineSpacing;
                addSpace = false;

                if(i+skip < text.Length && " " == text[i+skip].ToString())
                {
                    i++;
                }
                if((this.yPos+v.y) > (textPosition.bounds.height) && !dp.scrollable)
                {
                    finished = true;
                }
                else if(icon != null)
                {
                    part = new GUIContent(icon);
                    nextLabel = new LabelContent(part, this.xPos, this.yPos, tCol, sCol, font);
                    v = new Vector2(nextLabel.bounds.width, nextLabel.bounds.height);
                    //this.xPos += font.GetTextSize(".").x;
                    this.label = ArrayHelper.Add(nextLabel, this.label);
                    this.xPos += v.x;
                    addSpace = true;
                    icon = null;
                }
            }
            else if(finished)
            {
                this.xPos = 0;
                this.yPos += v.y + textPosition.lineSpacing;
            }

            this.textPos = i + skip;
        }
    }