/// <summary> /// Draws the string on the console at the specified location with the specified settings. /// </summary> /// <param name="x">X location of the text.</param> /// <param name="y">Y location of the text.</param> /// <param name="text">The string to display.</param> /// <param name="foreground">Sets the foreground of all characters in the text.</param> /// <param name="background">Sets the background of all characters in the text.</param> /// <param name="spriteEffect">The sprite effect to set on the cell.</param> public void Print(int x, int y, string text, Color? foreground = null, Color? background = null, SpriteEffects? spriteEffect = null) { if (String.IsNullOrEmpty(text)) return; if (x >= textSurface.Width || x < 0 || y >= textSurface.Height || y < 0) throw new Exception("X,Y is out of range for Print"); int index = y * textSurface.Width + x; if (!UsePrintProcessor) { int total = index + text.Length > textSurface.Cells.Length ? textSurface.Cells.Length - index : index + text.Length; int charIndex = 0; for (; index < total; index++) { textSurface.Cells[index].GlyphIndex = text[charIndex]; if (background.HasValue) textSurface.Cells[index].Background = background.Value; if (foreground.HasValue) textSurface.Cells[index].Foreground = foreground.Value; if (spriteEffect.HasValue) textSurface.Cells[index].SpriteEffect = spriteEffect.Value; charIndex++; } } else { 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 (spriteEffect.HasValue) stacks.AddSafe(new ParseCommandSpriteEffect() { Effect = spriteEffect.Value, CommandType = CommandTypes.SpriteEffect }); PrintNoCheck(index, ColoredString.Parse(text, index, textSurface, this, stacks)); } }
/// <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="spriteEffect">The background color. If null, <see cref="ColoredString.IgnoreEffect"/> 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, SpriteEffects? spriteEffect = 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 (spriteEffect.HasValue) stacks.AddSafe(new ParseCommandSpriteEffect() { Effect = spriteEffect.Value, CommandType = CommandTypes.SpriteEffect }); ColoredString newString = ColoredString.Parse(value, initialBehaviors: stacks); if (!foreground.HasValue) newString.IgnoreForeground = true; if (!background.HasValue) newString.IgnoreBackground = true; if (!spriteEffect.HasValue) newString.IgnoreSpriteEffect = true; return newString; }
/// <summary> /// Draws the string on the console at the specified location with the specified foreground and background color, wrapping if needed. /// </summary> /// <param name="x">X location of the text.</param> /// <param name="y">Y location of the text.</param> /// <param name="text">The string to display.</param> /// <param name="foreground">Sets the foreground of all characters in the text.</param> /// <param name="background">Sets the background of all characters in the text.</param> public void Print(int x, int y, string text, Color foreground, Color background) { if (String.IsNullOrEmpty(text)) return; if (x >= textSurface.Width || x < 0 || y >= textSurface.Height || y < 0) throw new Exception("X,Y is out of range for Print"); int index = y * textSurface.Width + x; if (!UsePrintProcessor) { int total = index + text.Length > textSurface.Cells.Length ? textSurface.Cells.Length - index : index + text.Length; int charIndex = 0; for (; index < total; index++) { textSurface.Cells[index].GlyphIndex = text[charIndex]; textSurface.Cells[index].Background = background; textSurface.Cells[index].Foreground = foreground; charIndex++; } } else { var behaviorFore = new ParseCommandRecolor() { R = foreground.R, G = foreground.G, B = foreground.B, A = foreground.A, CommandType = CommandTypes.Foreground }; var behaviorBack = new ParseCommandRecolor() { R = background.R, G = background.G, B = background.B, A = background.A, CommandType = CommandTypes.Background }; var stacks = new ParseCommandStacks(); stacks.AddSafe(behaviorFore); stacks.AddSafe(behaviorBack); PrintNoCheck(index, ColoredString.Parse(text, index, textSurface, this, stacks)); } }