示例#1
0
        public void TextIsSetFromIconName()
        {
            var span = new IconSpan {
                GlyphName = "test-foo"
            };

            Assert.Single(span.Text);
            Assert.Equal(MockFontAMapping.Foo, span.Text);
        }
示例#2
0
        public void TextIsSetFromIconName()
        {
            IconFontRegistry.Reset();
            IconFontRegistry.Register(new IconFont("TestFontFamily", "test", typeof(MockFontA)));

            var span = new IconSpan {
                GlyphName = "test-foo"
            };

            Assert.Single(span.Text);
            Assert.Equal(MockFontA.Foo, span.Text);
        }
示例#3
0
        internal void SetColors(TextSpan[] colors)
        {
            if (colors != null && colors.Length < m_sprites.Length)
            {
                throw new ArgumentException("Too few colors in array!");
            }

            int ialpha = MathHelper.Clamp((int)(m_alpha * 255), 0, 255);

            for (int i = 0; i < m_sprites.Length; i++)
            {
                ISprite sprite = m_sprites[i];

                if (colors != null && colors[i] != null)
                {
                    if (colors[i] is IconSpan)
                    {
                        IconSpan icon       = (IconSpan)colors[i];
                        ISprite  iconSprite = WindowController.Instance.CreateSprite(icon.Icon, Vector2.Zero);

                        if (iconSprite != null) // consume the error cause there is no place for log or message
                        {
                            iconSprite.Position = new Vector2(sprite.Position.X, sprite.Position.Y + m_font.Baseline - icon.Height / 2);

                            iconSprite.Transform.Scale = new Vector3(icon.Width / iconSprite.Size.X, icon.Height / iconSprite.Size.Y, 1.0f);

                            m_sprites[i] = iconSprite;
                            sprite       = m_sprites[i];
                        }
                    }

                    sprite.Color = colors[i].Color;
                }
                else
                {
                    sprite.Color = m_color;
                }

                sprite.Alpha            = (byte)ialpha;
                sprite.Transform.Parent = Transform;
            }
        }
示例#4
0
        public static string ParseRichText(string text, int defaultColor, out TextSpan[] colors, int spaceWidth)
        {
            colors = new TextSpan[text.Length];
            char[] chars = new char[text.Length];

            int      length       = 0;
            TextSpan currentColor = new TextSpan(defaultColor);

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '|' && i < text.Length - 1)
                {
                    char code = text[i + 1];

                    switch (code)
                    {
                    case 'c':     // color
                        if (i < text.Length - 9)
                        {
                            string colorString = text.Substring(i + 2, 6);

                            int color;
                            if (int.TryParse(colorString, System.Globalization.NumberStyles.HexNumber, null, out color))
                            {
                                currentColor = new TextSpan(color);
                                i           += 7;
                                continue;
                            }
                        }
                        break;

                    case 'r':     // restore color
                        currentColor = new TextSpan(defaultColor);
                        i++;
                        continue;

                    case '|':     // symbol |
                        i++;
                        chars[length]  = '|';
                        colors[length] = currentColor;
                        length++;
                        continue;

                    case 'n':     // new line
                        i++;
                        chars[length]  = '\n';
                        colors[length] = currentColor;
                        length++;
                        continue;

                    case 't':     // image
                        if (i < text.Length - 4)
                        {
                            // we wait for string alike
                            // |ticon_money:32:32:ffbbee|t

                            int nextT = text.IndexOf("|t", i + 1);
                            if (nextT != -1)
                            {
                                string   iconText = text.Substring(i + 2, nextT - i - 2);
                                string[] iconData = iconText.Split(':');
                                string   icon     = iconData[0];
                                int      width;
                                int      height;
                                int      color;

                                // ignore the errors if any

                                if (iconData.Length > 2 && int.TryParse(iconData[1], out width) && int.TryParse(iconData[2], out height))
                                {
                                    if (iconData.Length > 3 && int.TryParse(iconData[3], System.Globalization.NumberStyles.HexNumber, null, out color))
                                    {
                                    }
                                    else
                                    {
                                        color = 0xffffff;     // defaults to white color
                                    }
                                    // use spaces to preserve space for image
                                    TextSpan span = new IconSpan(color, icon, width, height);

                                    int spaces = (int)Math.Ceiling(width / (float)spaceWidth);

                                    colors[length] = span;

                                    for (int k = 0; k < spaces; k++)
                                    {
                                        chars[length] = '\u00a0';     // &nbsp; symbol will default to space but avoid line breaks
                                        length++;
                                    }

                                    i = nextT + 1;

                                    continue;
                                }
                            }
                        }

                        break;
                    }
                }

                chars[length]  = text[i];
                colors[length] = currentColor;
                length++;
            }

            return(new string(chars, 0, length));
        }