Parse() public static method

Creates a colored string by parsing commands embedded in the string.
public static Parse ( string value, int surfaceIndex = -1, Consoles surface = null, SurfaceEditor editor = null, ParseCommandStacks initialBehaviors = null ) : ColoredString
value string The string to parse.
surfaceIndex int Index of where this string will be printed.
surface Consoles The surface the string will be printed to.
editor SadConsole.Consoles.SurfaceEditor A surface editor associated with the text surface.
initialBehaviors SadConsole.StringParser.ParseCommandStacks Any initial defaults.
return ColoredString
        /// <summary>
        /// Creates a new instance of the ColoredString class with the specified string value, foreground and background colors, and a cell effect.
        /// </summary>
        /// <param name="value">The backing string.</param>
        /// <param name="foreground">The foreground color for each character.</param>
        /// <param name="background">The background color for each character.</param>
        /// <param name="spriteEffect">The sprite effects for each character.</param>
        public ColoredString(string value, Color foreground, Color background)
        {
            var stacks = new ParseCommandStacks();

            stacks.AddSafe(new ParseCommandRecolor()
            {
                R = foreground.R, G = foreground.G, B = foreground.B, A = foreground.A, CommandType = CommandTypes.Foreground
            });
            stacks.AddSafe(new ParseCommandRecolor()
            {
                R = background.R, G = background.G, B = background.B, A = background.A, CommandType = CommandTypes.Background
            });
            _characters = ColoredString.Parse(value, initialBehaviors: stacks)._characters;
        }
Beispiel #2
0
        /// <summary>
        /// Prints text on the console.
        /// </summary>
        /// <param name="text">The text to print.</param>
        /// <param name="template">The way the text will look when it is printed.</param>
        /// <param name="templateEffect">Effect to apply to the text as its printed.</param>
        /// <returns>Returns this cursor object.</returns>
        public Cursor Print(string text, Cell template, Effects.ICellEffect templateEffect)
        {
            ColoredString coloredString;

            if (UseStringParser)
            {
                coloredString = ColoredString.Parse(text, position.Y * editor.TextSurface.Width + position.X, editor.TextSurface, editor, new StringParser.ParseCommandStacks());
            }
            else
            {
                coloredString = text.CreateColored(template.Foreground, template.Background, template.Mirror);
                coloredString.SetEffect(templateEffect);
            }

            return(Print(coloredString));
        }
        /// <summary>
        /// Creates a <see cref="ColoredString"/> object from an existing string with the specified foreground and background, setting the ignore properties if needed.
        /// </summary>
        /// <param name="value">The current string.</param>
        /// <param name="foreground">The foreground color. If null, <see cref="ColoredString.IgnoreForeground"/> will be set.</param>
        /// <param name="background">The background color. If null, <see cref="ColoredString.IgnoreBackground"/> will be set.</param>
        /// <param name="mirror">The mirror setting. If null, <see cref="ColoredString.IgnoreMirror"/> will be set.</param>
        /// <returns>A <see cref="ColoredString"/> object instace.</returns>
        public static ColoredString CreateColored(this string value, Color?foreground = null, Color?background = null, Mirror?mirror = null)
        {
            var stacks = new ParseCommandStacks();

            if (foreground.HasValue)
            {
                stacks.AddSafe(new ParseCommandRecolor()
                {
                    R = foreground.Value.R, G = foreground.Value.G, B = foreground.Value.B, A = foreground.Value.A, CommandType = CommandTypes.Foreground
                });
            }

            if (background.HasValue)
            {
                stacks.AddSafe(new ParseCommandRecolor()
                {
                    R = background.Value.R, G = background.Value.G, B = background.Value.B, A = background.Value.A, CommandType = CommandTypes.Background
                });
            }

            if (mirror.HasValue)
            {
                stacks.AddSafe(new ParseCommandMirror()
                {
                    Mirror = mirror.Value, CommandType = CommandTypes.Mirror
                });
            }

            ColoredString newString = ColoredString.Parse(value, initialBehaviors: stacks);

            if (!foreground.HasValue)
            {
                newString.IgnoreForeground = true;
            }

            if (!background.HasValue)
            {
                newString.IgnoreBackground = true;
            }

            if (!mirror.HasValue)
            {
                newString.IgnoreMirror = true;
            }

            return(newString);
        }