Esempio n. 1
0
        public void PrintRight(float x, float y, string str)
        {
            TextInfo ti = GetTextInfo(str, false);

            Print(x - ti.Width, y, str);
        }
Esempio n. 2
0
        public void Print(float x, float y, string str)
        {
            if (!Loaded)
            {
                Load(GameConfig.DefaultFont.ToPath());
            }
            if (!Loaded)
            {
                return;
            }

            float    cx = x, cy = y;
            int      i, sl, line = 0;
            char     symbol;
            CharInfo chr;
            TextInfo info = GetTextInfo(str);

            GL.Enable(EnableCap.Blend);
            GL.Enable(EnableCap.Texture2D);

            if (View.YAxisIsUp)
            {
                cy -= (BaseLine - LineHeight) * m_Scale.Y;
            }
            else
            {
                cy += BaseLine * m_Scale.Y;
            }
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);
            sl = str.Length;
            int           escape = 0, align = 0;
            StringBuilder sb = new StringBuilder();

            for (i = 0; i < sl; i++)
            {
                symbol = str[i];
                if (symbol == '\r' || symbol == '\t')
                {
                    continue;
                }
                if (escape > 0)
                {
                    if (escape == 1)
                    {
                        if (symbol == '<')
                        {
                            escape = 0;
                            cx     = x;
                            align  = 0;
                            continue;
                        }
                        else if (symbol == '|')
                        {
                            escape = 0;
                            cx     = x + (info.Width - info.LineWidth[line]) * 0.5f;
                            align  = 1;
                            continue;
                        }
                        else if (symbol == '>')
                        {
                            escape = 0;
                            cx     = x + (info.Width - info.LineWidth[line]);
                            align  = 2;
                            continue;
                        }
                        else if ((symbol >= '0' && symbol <= '9') || (symbol >= 'a' && symbol <= 'f') || (symbol >= 'A' && symbol <= 'F'))
                        {
                            escape++;
                            sb.Append(symbol);
                            continue;
                        }
                        else
                        {
                            escape = -1;
                        }
                    }
                    else if (escape <= 8)
                    {
                        if ((symbol >= '0' && symbol <= '9') || (symbol >= 'a' && symbol <= 'f') || (symbol >= 'A' && symbol <= 'F'))
                        {
                            escape++;
                            sb.Append(symbol);
                            continue;
                        }
                        else
                        {
                            escape = -1;                             // -1 means that there is something in the buffer
                        }
                    }
                    else
                    {
                        escape = -1;
                    }
                }

                if (escape < 0)
                {
                    byte r = 255, g = 255, b = 255, a = 255;
                    uint color;
                    if (sb.Length > 0)
                    {
                        switch (sb.Length)
                        {
                        case 3:
                            color  = Convert.ToUInt32(sb.ToString(), 16);
                            r      = (byte)((color >> 8) & 0xF);
                            g      = (byte)((color >> 4) & 0xF);
                            b      = (byte)((color) & 0xF);
                            r     += (byte)(r << 4);
                            g     += (byte)(g << 4);
                            b     += (byte)(b << 4);
                            escape = sb.Length - 3;
                            GL.Color4(r, g, b, (byte)255);
                            break;

                        case 4:
                        case 5:
                            color  = Convert.ToUInt32(sb.ToString(), 16);
                            r      = (byte)((color >> 12) & 0xF);
                            g      = (byte)((color >> 8) & 0xF);
                            b      = (byte)((color >> 4) & 0xF);
                            a      = (byte)((color) & 0xF);
                            r     += (byte)(r << 4);
                            g     += (byte)(g << 4);
                            b     += (byte)(b << 4);
                            a     += (byte)(a << 4);
                            escape = sb.Length - 4;
                            GL.Color4(r, g, b, a);
                            break;

                        case 6:
                        case 7:
                            color  = Convert.ToUInt32(sb.ToString(), 16);
                            r      = (byte)((color >> 16) & 0xFF);
                            g      = (byte)((color >> 8) & 0xFF);
                            b      = (byte)((color) & 0xFF);
                            escape = sb.Length - 6;
                            GL.Color4(r, g, b, (byte)255);
                            break;

                        case 8:
                            color  = Convert.ToUInt32(sb.ToString(), 16);
                            r      = (byte)((color >> 24) & 0xFF);
                            g      = (byte)((color >> 16) & 0xFF);
                            b      = (byte)((color >> 8) & 0xFF);
                            a      = (byte)((color) & 0xFF);
                            escape = sb.Length - 8;
                            GL.Color4(r, g, b, a);
                            break;

                        default:
                            escape = sb.Length;
                            break;
                        }
                    }

                    // positive "escape" means that we have to get back a little bit
                    if (escape > 0)
                    {
                        i     -= escape + 1;
                        escape = 0;
                        continue;
                    }
                    escape = 0;
                }
                else if (symbol == '⌂')
                {
                    if (escape == 0)
                    {
                        sb.Clear();
                        escape = 1;
                        continue;
                    }
                    else
                    {
                        escape = 0;
                    }
                }

                if (symbol == '\n')
                {
                    line++;
                    switch (align)
                    {
                    case 0: cx = x; break;

                    case 1: cx = x + (info.Width - info.LineWidth[line]) * 0.5f; break;

                    case 2: cx = x + (info.Width - info.LineWidth[line]); break;
                    }
                    if (View.YAxisIsUp)
                    {
                        cy -= LineHeight * m_Scale.Y;
                    }
                    else
                    {
                        cy += LineHeight * m_Scale.Y;
                    }
                    continue;
                }

                chr = this[symbol];
                chr.Render((int)cx, (int)cy, m_Scale.X, m_Scale.Y);
                cx += m_Scale.X * chr.Width;
            }
        }
Esempio n. 3
0
        public void PrintCentered(float x, float y, string str)
        {
            TextInfo ti = GetTextInfo(str, false);

            Print(x - ti.Width * 0.5f, y, str);
        }
Esempio n. 4
0
        public TextInfo GetUnscaledTextInfo(string text)
        {
            TextInfo info = m_TextInfo[text];

            if (info == null)
            {
                info = new TextInfo();

                // Calculate info for the given text:
                //   width as a whole and for every line
                //   height for the whole text
                char         symbol;
                float        width;
                float        x = width = 0.0f, y = 0.0f;
                int          i, sl = text.Length, escape = 0, bl = 0;
                List <float> widths = new List <float>();

                for (i = 0; i < sl; i++)
                {
                    symbol = text[i];
                    if (symbol == '\r' || symbol == '\t')
                    {
                        continue;
                    }
                    if (escape > 0)
                    {
                        if (escape == 1)
                        {
                            if (symbol == '<')
                            {
                                escape = 0;
                                continue;
                            }
                            else if (symbol == '|')
                            {
                                escape = 0;
                                continue;
                            }
                            else if (symbol == '>')
                            {
                                escape = 0;
                                continue;
                            }
                            else if ((symbol >= '0' && symbol <= '9') || (symbol >= 'a' && symbol <= 'f') || (symbol >= 'A' && symbol <= 'F'))
                            {
                                escape++;
                                bl++;
                                continue;
                            }
                            else
                            {
                                escape = -1;
                            }
                        }
                        else if (escape <= 8)
                        {
                            if ((symbol >= '0' && symbol <= '9') || (symbol >= 'a' && symbol <= 'f') || (symbol >= 'A' && symbol <= 'F'))
                            {
                                escape++;
                                bl++;
                                continue;
                            }
                            else
                            {
                                escape = -1;
                            }
                        }
                        else
                        {
                            escape = -1;
                        }
                    }

                    if (escape < 0)
                    {
                        if (bl > 0)
                        {
                            switch (bl)
                            {
                            case 3:
                                escape = bl - 3;
                                break;

                            case 4:
                            case 5:
                                escape = bl - 4;
                                break;

                            case 6:
                            case 7:
                                escape = bl - 6;
                                break;

                            case 8:
                                escape = bl - 8;
                                break;

                            default:
                                escape = bl;
                                break;
                            }
                        }

                        if (escape > 0)
                        {
                            i     -= escape + 1;
                            escape = 0;
                            continue;
                        }
                        escape = 0;
                    }
                    else if (symbol == '⌂')
                    {
                        if (escape == 0)
                        {
                            bl     = 0;
                            escape = 1;
                            continue;
                        }
                        else
                        {
                            escape = 0;
                        }
                    }

                    if (symbol == '\n')
                    {
                        y += LineHeight;
                        if (width < x)
                        {
                            width = x;
                        }
                        widths.Add(x);
                        x = 0.0f;
                    }
                    else
                    {
                        x += this[text[i]].Width;
                    }
                }
                if (x > 0.0f)
                {
                    y += LineHeight;
                }

                if (width < x)
                {
                    width = x;
                }
                widths.Add(x);

                info.Width     = width;
                info.Height    = y;
                info.LineWidth = widths.ToArray();

                m_TextInfo[text] = info;
            }
            return(info);
        }