ParseCommandBase CustomParseCommand(string command, string parameters, ColoredGlyph[] glyphString, ITextSurface surface, SurfaceEditor editor, ParseCommandStacks commandStacks) { switch (command) { case "t": return new ParseCommandRetext(parameters); default: return null; ; } }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, ITextSurface surface, SurfaceEditor editor, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { if (CommandType == CommandTypes.Background) glyphState.Background = GradientString[Length - Counter].Foreground; else glyphState.Foreground = GradientString[Length - Counter].Foreground; Counter--; if (Counter == 0) commandStack.RemoveSafe(this); }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, ITextSurface surface, SurfaceEditor editor, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { glyphState.Effect = BlinkEffect; if (Counter != -1) { Counter--; if (Counter == 0) commandStack.RemoveSafe(this); } }
public ParseCommandBlink(string parameters, ColoredGlyph[] glyphString, ParseCommandStacks commandStack, SurfaceEditor surfaceEditor) { string[] parametersArray = parameters.Split(':'); if (parametersArray.Length == 2) Speed = double.Parse(parametersArray[1]); if (parametersArray.Length >= 1 && parametersArray[0] != "") Counter = int.Parse(parametersArray[0]); else Counter = -1; // Try sync with surface editor if (surfaceEditor != null) { var effects = surfaceEditor.Effects.GetEffects(); if (effects != null) { var existingBlinks = new List<SadConsole.Effects.ICellEffect>(effects); foreach (var item in existingBlinks) { if (item is CustomBlinkEffect) { if (Speed == ((CustomBlinkEffect)item).BlinkSpeed) BlinkEffect = (CustomBlinkEffect)item; break; } } } } // Failed, look within this parse for existing if (BlinkEffect == null) { foreach (var item in glyphString) { if (item.Effect != null && item.Effect is CustomBlinkEffect) if (Speed == ((CustomBlinkEffect)item.Effect).BlinkSpeed) BlinkEffect = (CustomBlinkEffect)item.Effect; } } if (BlinkEffect == null) BlinkEffect = new CustomBlinkEffect() { BlinkSpeed = Speed }; commandStack.TurnOnEffects = true; // No exceptions, set the type CommandType = CommandTypes.Effect; }
public ParseCommandClearEffect(string parameters, ParseCommandStacks commandStack) { string[] parametersArray = parameters.Split(':'); if (parametersArray.Length == 1 && parametersArray[0] != "") { Counter = int.Parse(parametersArray[0], CultureInfo.InvariantCulture); } else { Counter = -1; } commandStack.TurnOnEffects = true; // No exceptions, set the type CommandType = CommandTypes.Effect; }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, ITextSurface surface, SurfaceEditor editor, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { Color newColor; if (Default) { if (CommandType == CommandTypes.Background) newColor = surface != null ? surface.DefaultBackground : Color.Transparent; else newColor = surface != null ? surface.DefaultForeground : Color.White; } else { if (CommandType == CommandTypes.Background) newColor = glyphState.Background; else newColor = glyphState.Foreground; if (!KeepRed) newColor.R = R; if (!KeepGreen) newColor.G = G; if (!KeepBlue) newColor.B = B; if (!KeepAlpha) newColor.A = A; } if (CommandType == CommandTypes.Background) glyphState.Background = newColor; else glyphState.Foreground = newColor; if (Counter != -1) { Counter--; if (Counter == 0) commandStack.RemoveSafe(this); } }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, CellSurface surface, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { if (RandomGlyph) { glyphState.GlyphCharacter = (char)SadConsole.Global.Random.Next(RandomGlyphMin, RandomGlyphMax); } else { glyphState.GlyphCharacter = Glyph; } if (Counter != -1) { Counter--; } if (Counter == 0) { commandStack.RemoveSafe(this); } }
/// <summary> /// Builds a glyph. /// </summary> /// <param name="glyphState">The current glyph being built.</param> /// <param name="glyphString">The current string of glyphs that has been processed until now.</param> /// <param name="surfaceIndex">Where on the surface this flyph will appear.</param> /// <param name="surface">The surface associated with the glyph.</param> /// <param name="stringIndex">Where in the original string this glyph is from.</param> /// <param name="processedString">The entire string being processed.</param> /// <param name="commandStack">The state of commands.</param> public abstract void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, Surfaces.SurfaceBase surface, ref int stringIndex, string processedString, ParseCommandStacks commandStack);
/// <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; }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, ISurface surface, SurfaceEditor editor, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { glyphState.Mirror = Mirror; if (Counter != -1) { Counter--; if (Counter == 0) { commandStack.RemoveSafe(this); } } }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, ITextSurface surface, SurfaceEditor editor, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { Color newColor; if (Default) { if (CommandType == CommandTypes.Background) { newColor = surface != null ? surface.DefaultBackground : Color.Transparent; } else { newColor = surface != null ? surface.DefaultForeground : Color.White; } } else { if (CommandType == CommandTypes.Background) { newColor = glyphState.Background; } else { newColor = glyphState.Foreground; } if (!KeepRed) { newColor.R = R; } if (!KeepGreen) { newColor.G = G; } if (!KeepBlue) { newColor.B = B; } if (!KeepAlpha) { newColor.A = A; } } if (CommandType == CommandTypes.Background) { glyphState.Background = newColor; } else { glyphState.Foreground = newColor; } if (Counter != -1) { Counter--; if (Counter == 0) { commandStack.RemoveSafe(this); } } }
/// <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)); } }
public ParseCommandBlink(string parameters, ColoredGlyph[] glyphString, ParseCommandStacks commandStack, CellSurface surfaceEditor) { string[] parametersArray = parameters.Split(':'); if (parametersArray.Length == 2) { Speed = double.Parse(parametersArray[1], CultureInfo.InvariantCulture); } if (parametersArray.Length >= 1 && parametersArray[0] != "") { Counter = int.Parse(parametersArray[0], CultureInfo.InvariantCulture); } else { Counter = -1; } // Try sync with surface editor if (surfaceEditor != null) { IEnumerable <Effects.ICellEffect> effects = surfaceEditor.Effects.GetEffects(); if (effects != null) { var existingBlinks = new List <Effects.ICellEffect>(effects); foreach (Effects.ICellEffect item in existingBlinks) { if (item is CustomBlinkEffect) { if (Speed == ((CustomBlinkEffect)item).BlinkSpeed) { BlinkEffect = (CustomBlinkEffect)item; } break; } } } } // Failed, look within this parse for existing if (BlinkEffect == null) { foreach (ColoredGlyph item in glyphString) { if (item.Effect != null && item.Effect is CustomBlinkEffect) { if (Speed == ((CustomBlinkEffect)item.Effect).BlinkSpeed) { BlinkEffect = (CustomBlinkEffect)item.Effect; } } } } if (BlinkEffect == null) { BlinkEffect = new CustomBlinkEffect() { BlinkSpeed = Speed }; } commandStack.TurnOnEffects = true; // No exceptions, set the type CommandType = CommandTypes.Effect; }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, CellSurface surface, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { }
/// <inheritdoc /> public override void Build(ref ColoredString.ColoredGlyphEffect glyphState, ColoredString.ColoredGlyphEffect[] glyphString, int surfaceIndex, ICellSurface surface, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { glyphState.Mirror = Mirror; if (_counter != -1) { _counter--; if (_counter == 0) { commandStack.RemoveSafe(this); } } }
/// <summary> /// Creates a colored string by parsing commands embedded in the string. /// </summary> /// <param name="value">The string to parse.</param> /// <param name="surfaceIndex">Index of where this string will be printed.</param> /// <param name="surface">The surface the string will be printed to.</param> /// <param name="editor">A surface editor associated with the text surface.</param> /// <param name="initialBehaviors">Any initial defaults.</param> /// <returns></returns> public static ColoredString Parse(string value, int surfaceIndex = -1, Consoles.ITextSurface surface = null, SurfaceEditor editor = null, ParseCommandStacks initialBehaviors = null) { var commandStacks = initialBehaviors != null ? initialBehaviors : new ParseCommandStacks(); List<ColoredGlyph> glyphs = new List<ColoredGlyph>(value.Length); for (int i = 0; i < value.Length; i++) { var existingGlyphs = glyphs.ToArray(); if (value[i] == '`' && i + 1 < value.Length && value[i + 1] == '[') continue; if (value[i] == '[' && (i == 0 || value[i - 1] != '`')) { try { if (i + 4 < value.Length && value[i + 1] == 'c' && value[i + 2] == ':' && value.IndexOf(']', i + 2) != -1) { int commandExitIndex = value.IndexOf(']', i + 2); string command = value.Substring(i + 3, commandExitIndex - (i + 3)); string commandParams = ""; if (command.Contains(" ")) { var commandSections = command.Split(new char[] { ' ' }, 2); command = commandSections[0].ToLower(); commandParams = commandSections[1]; } // Check for custom command ParseCommandBase commandObject = CustomProcessor != null ? CustomProcessor(command, commandParams, existingGlyphs, surface, editor, commandStacks) : null; // No custom command found, run build in ones if (commandObject == null) switch (command) { case "recolor": case "r": commandObject = new ParseCommandRecolor(commandParams); break; case "mirror": case "m": commandObject = new ParseCommandSpriteEffect(commandParams); break; case "undo": case "u": commandObject = new ParseCommandUndo(commandParams, commandStacks); break; case "grad": case "g": commandObject = new ParseCommandGradient(commandParams); break; case "blink": case "b": commandObject = new ParseCommandBlink(commandParams, existingGlyphs, commandStacks, editor); break; case "sglyph": case "sg": commandObject = new ParseCommandSetGlyph(commandParams); break; default: break; } if (commandObject != null && commandObject.CommandType != CommandTypes.Invalid) { commandStacks.AddSafe(commandObject); i = commandExitIndex; continue; } } } catch (System.Exception e1) { #if DEBUG throw e1; #endif } } int fixedSurfaceIndex; if (surfaceIndex == -1 || surface == null) fixedSurfaceIndex = -1; else fixedSurfaceIndex = i + surfaceIndex < surface.Cells.Length ? i + surfaceIndex : -1; ColoredGlyph newGlyph; if (fixedSurfaceIndex != -1) newGlyph = new ColoredGlyph(surface[i + surfaceIndex]) { Glyph = value[i] }; else newGlyph = new ColoredGlyph(new Cell()) { Glyph = value[i] }; // Foreground if (commandStacks.Foreground.Count != 0) commandStacks.Foreground.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks); // Background if (commandStacks.Background.Count != 0) commandStacks.Background.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks); if (commandStacks.Glyph.Count != 0) commandStacks.Glyph.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks); // SpriteEffect if (commandStacks.SpriteEffect.Count != 0) commandStacks.SpriteEffect.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks); // Effect if (commandStacks.Effect.Count != 0) commandStacks.Effect.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks); glyphs.Add(newGlyph); } return new ColoredString(glyphs.ToArray()) { IgnoreEffect = !commandStacks.TurnOnEffects }; }
/// <inheritdoc /> public override void Build(ref ColoredString.ColoredGlyphEffect glyphState, ColoredString.ColoredGlyphEffect[] glyphString, int surfaceIndex, ICellSurface surface, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { byte r = 0; byte g = 0; byte b = 0; byte a = 0; if (Default) { if (CommandType == CommandTypes.Background) { (surface != null ? surface.DefaultBackground : Color.Transparent).Deconstruct(out r, out g, out b, out a); } else { (surface != null ? surface.DefaultForeground : Color.White).Deconstruct(out r, out g, out b, out a); } } else { if (CommandType == CommandTypes.Background) { glyphState.Background.Deconstruct(out r, out g, out b, out a); } else { glyphState.Foreground.Deconstruct(out r, out g, out b, out a); } if (!KeepRed) { r = R; } if (!KeepGreen) { g = G; } if (!KeepBlue) { b = B; } if (!KeepAlpha) { a = A; } } if (CommandType == CommandTypes.Background) { glyphState.Background = new Color(r, g, b, a); } else { glyphState.Foreground = new Color(r, g, b, a); } if (_counter != -1) { _counter--; if (_counter == 0) { commandStack.RemoveSafe(this); } } }
public ParseCommandUndo(string parameters, ParseCommandStacks stacks) { var badCommandException = new ArgumentException("command is invalid for Undo: " + parameters); string[] parts = parameters.Split(new char[] { ':' }, 3); int times = 1; bool isSpecificStack = false; CommandTypes stackType = CommandTypes.Invalid; if (parts.Length > 1) { isSpecificStack = true; switch (parts[1]) { case "f": stackType = CommandTypes.Foreground; break; case "b": stackType = CommandTypes.Background; break; case "g": stackType = CommandTypes.Glyph; break; case "e": stackType = CommandTypes.Effect; break; case "m": stackType = CommandTypes.Mirror; break; case "a": isSpecificStack = false; break; default: throw badCommandException; } } if (parts.Length >= 1 && parts[0] != "") { times = int.Parse(parts[0], CultureInfo.InvariantCulture); } for (int i = 0; i < times; i++) { ParseCommandBase behavior = null; if (!isSpecificStack) { if (stacks.All.Count != 0) { behavior = stacks.All.Pop(); switch (behavior.CommandType) { case CommandTypes.Foreground: stacks.Foreground.Pop(); break; case CommandTypes.Background: stacks.Background.Pop(); break; case CommandTypes.Glyph: stacks.Glyph.Pop(); break; case CommandTypes.Mirror: stacks.Mirror.Pop(); break; case CommandTypes.Effect: stacks.Effect.Pop(); break; default: break; } } else { break; } } else { switch (stackType) { case CommandTypes.Foreground: if (stacks.Foreground.Count != 0) { behavior = stacks.Foreground.Pop(); } break; case CommandTypes.Background: if (stacks.Background.Count != 0) { behavior = stacks.Background.Pop(); } break; case CommandTypes.Glyph: if (stacks.Glyph.Count != 0) { behavior = stacks.Glyph.Pop(); } break; case CommandTypes.Mirror: if (stacks.Mirror.Count != 0) { behavior = stacks.Mirror.Pop(); } break; case CommandTypes.Effect: if (stacks.Effect.Count != 0) { behavior = stacks.Effect.Pop(); } break; default: break; } if (behavior != null) { List <ParseCommandBase> all = new List <ParseCommandBase>(stacks.All); all.Remove(behavior); stacks.All = new Stack <ParseCommandBase>(all); } } } CommandType = CommandTypes.PureCommand; }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, ITextSurface surface, SurfaceEditor editor, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, Surfaces.SurfaceBase surface, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { if (CommandType == CommandTypes.Background) { glyphState.Background = GradientString[Length - Counter].Foreground; } else { glyphState.Foreground = GradientString[Length - Counter].Foreground; } Counter--; if (Counter == 0) { commandStack.RemoveSafe(this); } }
public ParseCommandUndo(string parameters, ParseCommandStacks stacks) { var badCommandException = new ArgumentException("command is invalid for Undo: " + parameters); string[] parts = parameters.Split(new char[] { ':' }, 3); int times = 1; bool isSpecificStack = false; CommandTypes stackType = CommandTypes.Invalid; if (parts.Length > 1) { isSpecificStack = true; switch (parts[1]) { case "f": stackType = CommandTypes.Foreground; break; case "b": stackType = CommandTypes.Background; break; case "g": stackType = CommandTypes.Glyph; break; case "e": stackType = CommandTypes.Effect; break; case "m": stackType = CommandTypes.SpriteEffect; break; case "a": isSpecificStack = false; break; default: throw badCommandException; } } if (parts.Length >= 1 && parts[0] != "") times = int.Parse(parts[0]); for (int i = 0; i < times; i++) { ParseCommandBase behavior = null; if (!isSpecificStack) { if (stacks.All.Count != 0) { behavior = stacks.All.Pop(); switch (behavior.CommandType) { case CommandTypes.Foreground: stacks.Foreground.Pop(); break; case CommandTypes.Background: stacks.Background.Pop(); break; case CommandTypes.Glyph: stacks.Glyph.Pop(); break; case CommandTypes.SpriteEffect: stacks.SpriteEffect.Pop(); break; case CommandTypes.Effect: stacks.Effect.Pop(); break; default: break; } } else break; } else { switch (stackType) { case CommandTypes.Foreground: if (stacks.Foreground.Count != 0) behavior = stacks.Foreground.Pop(); break; case CommandTypes.Background: if (stacks.Background.Count != 0) behavior = stacks.Background.Pop(); break; case CommandTypes.Glyph: if (stacks.Glyph.Count != 0) behavior = stacks.Glyph.Pop(); break; case CommandTypes.SpriteEffect: if (stacks.SpriteEffect.Count != 0) behavior = stacks.SpriteEffect.Pop(); break; case CommandTypes.Effect: if (stacks.Effect.Count != 0) behavior = stacks.Effect.Pop(); break; default: break; } if (behavior != null) { List<ParseCommandBase> all = new List<ParseCommandBase>(stacks.All); all.Remove(behavior); stacks.All = new Stack<ParseCommandBase>(all); } } } CommandType = CommandTypes.PureCommand; }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, CellSurface surface, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { glyphState.Effect = BlinkEffect; if (Counter != -1) { Counter--; if (Counter == 0) { commandStack.RemoveSafe(this); } } }
/// <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)); } }