public Cursor(ITextSurface surface) { privateEditor = new SurfaceEditor(surface); _console = new WeakReference(privateEditor); Constructor(); }
public void Draw(SurfaceEditor surface) { if (BorderAppearance == null) BorderAppearance = new CellAppearance(Color.Blue, Color.Black, 4); Algorithms.Circle(Center.X, Center.Y, Radius, (x, y) => { if (surface.IsValidCell(x, y)) surface.SetCellAppearance(x, y, BorderAppearance); }); }
/// <summary> /// Creates a new instance of the class. /// </summary> /// <param name="pixelWidth">Width the source texture.</param> /// <param name="pixelHeight">Height of the source texture.</param> /// <param name="font">Font used for rendering.</param> public TextureToSurfaceReader(int pixelWidth, int pixelHeight, Font font) { this.width = pixelWidth; this.height = pixelHeight; pixels = new Color[pixelWidth * pixelHeight]; indexes = new int[pixelWidth * pixelHeight]; surface = new TextSurface(pixelWidth / font.Size.X, pixelHeight / font.Size.Y, font); fontPixels = font.Size.X * font.Size.Y; editor = new SurfaceEditor(surface); // build the indexes int currentIndex = 0; for (int h = 0; h < surface.Height; h++) { int startY = (h * surface.Font.Size.Y); //System.Threading.Tasks.Parallel.For(0, image.Width / surface.Font.Size.X, (w) => for (int w = 0; w < surface.Width; w++) { int startX = (w * surface.Font.Size.X); for (int y = 0; y < surface.Font.Size.Y; y++) { for (int x = 0; x < surface.Font.Size.X; x++) { int cY = y + startY; int cX = x + startX; indexes[currentIndex] = cY * pixelWidth + cX; currentIndex++; } } } } }
/// <summary> /// Sets the console this cursor is targetting. /// </summary> /// <param name="console">The console the cursor works with.</param> public void AttachConsole(SurfaceEditor console) { if (privateEditor != null) { privateEditor = null; } _console = new WeakReference(console); }
public FPSCounterComponent(Microsoft.Xna.Framework.Game game) : base(game) { console = new TextSurface(30, 1, Engine.DefaultFont); editor = new SurfaceEditor(console); console.DefaultBackground = Color.Black; editor.Clear(); consoleRender = new TextSurfaceRenderer(); }
public void Save(object surface, string file) { ITextSurface textSurface = (ITextSurface)surface; SurfaceEditor editor = new SurfaceEditor(textSurface); string[] lines = new string[textSurface.Height]; for (int y = 0; y < textSurface.Height; y++) lines[y] = editor.GetString(0, y, textSurface.Width); System.IO.File.WriteAllLines(file, lines); }
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 Draw(ITextSurface surface, Rectangle area) { string value = ((FileLoaders.IFileLoader)Item).FileTypeName; if (value.Length < area.Width) value += new string(' ', area.Width - value.Length); else if (value.Length > area.Width) value = value.Substring(0, area.Width); var editor = new SurfaceEditor(surface); editor.Print(area.Left, area.Top, value, _currentAppearance); _isDirty = false; }
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 override void Draw(ITextSurface surface, Rectangle area) { string value = ((Editors.EntityEditor.FrameWrapper)Item).CurrentIndex.ToString(); if (value.Length < area.Width) value += new string(' ', area.Width - value.Length); else if (value.Length > area.Width) value = value.Substring(0, area.Width); var editor = new SurfaceEditor(surface); editor.Print(area.X, area.Y, value, _currentAppearance); _isDirty = false; }
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; }
/// <summary> /// Creates a new instance of the cursor class that will work with the specified console. /// </summary> /// <param name="console">The console this cursor will print on.</param> public Cursor(SurfaceEditor console) { _console = new WeakReference(console); IsVisible = false; PrintAppearance = new CellAppearance(Color.White, Color.Black); AutomaticallyShiftRowsUp = true; CursorRenderCell = new Cell(); CursorRenderCell.GlyphIndex = _cursorCharacter; CursorRenderCell.Foreground = Color.White; CursorRenderCell.Background = Color.Transparent; ResetCursorEffect(); }
public AnsiWriter(Document ansiDocument, SurfaceEditor editor) { _ansiDoc = ansiDocument; _editor = editor; _cursor = new Cursor(editor); _cursor.UseStringParser = false; _cursor.DisableWordBreak = true; CharactersPerSecond = 800; _bytes = ansiDocument.AnsiBytes; _ansiState = new State(); _ansiCodeBuilder = new StringBuilder(5); _ansiStringBuilder = new StringBuilder(40); BlinkEffect = new Blink() { BlinkSpeed = 0.35f }; }
public void Draw(SurfaceEditor surface) { for (int x = Location.X; x < Location.X + Width; x++) { for (int y = Location.Y; y < Location.Y + Height; y++) { // Top row if (y == Location.Y) { if (x == Location.X) PlotCell(surface, x, y, TopLeftCharacter); else if (x == Location.X + Width - 1) PlotCell(surface, x, y, TopRightCharacter); else PlotCell(surface, x, y, TopSideCharacter); } // Bottom row else if (y == Location.Y + Height - 1) { if (x == Location.X) PlotCell(surface, x, y, BottomLeftCharacter); else if (x == Location.X + Width - 1) PlotCell(surface, x, y, BottomRightCharacter); else PlotCell(surface, x, y, BottomSideCharacter); } // Other rows else { if (x == Location.X) PlotCell(surface, x, y, LeftSideCharacter); else if (x == Location.X + Width - 1) PlotCell(surface, x, y, RightSideCharacter); else if (Fill) PlotCell(surface, x, y, 0, Fill); } } } }
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 RandomScrollingConsole() : base(80, 25) { messageData = new SurfaceEditor(new TextSurface(10, 1, Engine.DefaultFont)); IsVisible = false; KeyboardHandler = (cons, info) => { if (info.IsKeyDown(Keys.Left)) cons.TextSurface.RenderArea = new Rectangle(cons.TextSurface.RenderArea.Left - 1, cons.TextSurface.RenderArea.Top, 80, 25); if (info.IsKeyDown(Keys.Right)) cons.TextSurface.RenderArea = new Rectangle(cons.TextSurface.RenderArea.Left + 1, cons.TextSurface.RenderArea.Top, 80, 25); if (info.IsKeyDown(Keys.Up)) cons.TextSurface.RenderArea = new Rectangle(cons.TextSurface.RenderArea.Left, cons.TextSurface.RenderArea.Top - 1, 80, 25); if (info.IsKeyDown(Keys.Down)) cons.TextSurface.RenderArea = new Rectangle(cons.TextSurface.RenderArea.Left, cons.TextSurface.RenderArea.Top + 1, 80, 25); return true; }; }
public override void Draw(ITextSurface surface, Microsoft.Xna.Framework.Rectangle area) { string value = ((LayerMetadata)((LayeredTextSurface.Layer)Item).Metadata).Name; if (value.Length < area.Width) value += new string(' ', area.Width - value.Length); else if (value.Length > area.Width) value = value.Substring(0, area.Width); var editor = new SurfaceEditor(surface); editor.Print(area.X, area.Y, value, _currentAppearance); _isDirty = false; }
/// <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 }; }
/// <summary> /// Creates a new instance of the cursor class that will work with the specified console. /// </summary> /// <param name="console">The console this cursor will print on.</param> public Cursor(SurfaceEditor console) { _console = new WeakReference(console); Constructor(); }
public static unsafe void ToSurface(this Texture2D image, TextSurface surface, Color[] cachedColorArray, bool blockMode = false) { int imageWidth = image.Width; int imageHeight = image.Height; image.GetData<Color>(cachedColorArray); SurfaceEditor editor = new SurfaceEditor(surface); editor.Clear(); global::System.Threading.Tasks.Parallel.For(0, imageHeight / surface.Font.Size.Y, (h) => //for (int h = 0; h < imageHeight / surface.Font.Size.Y; h++) { int startY = (h * surface.Font.Size.Y); //System.Threading.Tasks.Parallel.For(0, imageWidth / surface.Font.Size.X, (w) => for (int w = 0; w < imageWidth / surface.Font.Size.X; w++) { int startX = (w * surface.Font.Size.X); float allR = 0; float allG = 0; float allB = 0; for (int y = 0; y < surface.Font.Size.Y; y++) { for (int x = 0; x < surface.Font.Size.X; x++) { int cY = y + startY; int cX = x + startX; Color color = cachedColorArray[cY * imageWidth + cX]; allR += color.R; allG += color.G; allB += color.B; } } byte sr = (byte)(allR / (surface.Font.Size.X * surface.Font.Size.Y)); byte sg = (byte)(allG / (surface.Font.Size.X * surface.Font.Size.Y)); byte sb = (byte)(allB / (surface.Font.Size.X * surface.Font.Size.Y)); var newColor = new Color(sr, sg, sb); float sbri = newColor.GetBrightness() * 255; if (blockMode) { if (sbri > 204) editor.SetGlyph(w, h, 219, newColor); //█ else if (sbri > 152) editor.SetGlyph(w, h, 178, newColor); //▓ else if (sbri > 100) editor.SetGlyph(w, h, 177, newColor); //▒ else if (sbri > 48) editor.SetGlyph(w, h, 176, newColor); //░ } else { if (sbri > 230) editor.SetGlyph(w, h, (int)'#', newColor); else if (sbri > 207) editor.SetGlyph(w, h, (int)'&', newColor); else if (sbri > 184) editor.SetGlyph(w, h, (int)'$', newColor); else if (sbri > 161) editor.SetGlyph(w, h, (int)'X', newColor); else if (sbri > 138) editor.SetGlyph(w, h, (int)'x', newColor); else if (sbri > 115) editor.SetGlyph(w, h, (int)'=', newColor); else if (sbri > 92) editor.SetGlyph(w, h, (int)'+', newColor); else if (sbri > 69) editor.SetGlyph(w, h, (int)';', newColor); else if (sbri > 46) editor.SetGlyph(w, h, (int)':', newColor); else if (sbri > 23) editor.SetGlyph(w, h, (int)'.', newColor); } } } ); }
//public ICellAppearance FillAppearance; //public bool Fill; public void Draw(SurfaceEditor surface) { Algorithms.Ellipse(StartingPoint.X, StartingPoint.Y, EndingPoint.X, EndingPoint.Y, (x, y) => { if (surface.IsValidCell(x, y)) surface.SetCellAppearance(x, y, BorderAppearance); }); }
public override void Build(ref ColoredGlyph glyphState, ColoredGlyph[] glyphString, int surfaceIndex, ITextSurface surface, SurfaceEditor editor, ref int stringIndex, string processedString, ParseCommandStacks commandStack) { }
private void PlotCell(SurfaceEditor surface, int x, int y, int glyph, bool fillMe = false) { if (surface.IsValidCell(x,y)) { var cell = surface[x, y]; if (fillMe) { cell.Background = FillColor; cell.Foreground = Foreground; cell.GlyphIndex = glyph; return; } if (Foreground != Color.Transparent || DrawTransparency) cell.Foreground = Foreground; if (BorderBackground != Color.Transparent || DrawTransparency) cell.Background = BorderBackground; cell.GlyphIndex = glyph; } }
/// <summary> /// Sets the console this cursor is targetting. /// </summary> /// <param name="console">The console the cursor works with.</param> public void AttachConsole(SurfaceEditor console) { if (privateEditor != null) privateEditor = null; _console = new WeakReference(console); }
public override void Draw(ITextSurface surface, Microsoft.Xna.Framework.Rectangle area) { string value = ((ResizableObject)Item).Name; if (string.IsNullOrEmpty(value)) value = "<no name>"; if (value.Length < area.Width) value += new string(' ', area.Width - value.Length); else if (value.Length > area.Width) value = value.Substring(0, area.Width); var editor = new SurfaceEditor(surface); editor.Print(area.X, area.Y, value, _currentAppearance); _isDirty = false; }
/// <summary> /// Draws the line shape. /// </summary> /// <param name="surface">The cell surface to draw on.</param> public void Draw(SurfaceEditor surface) { List<Cell> cells = new List<Cell>(); Algorithms.Line(StartingLocation.X, StartingLocation.Y, EndingLocation.X, EndingLocation.Y, (x, y) => { cells.Add(surface[x, y]); return true; }); if (cells.Count > 1) { if (UseStartingCell) { StartingCellAppearance.Copy(cells[0]); cells[0].Effect = StartingCellAppearance.Effect; } else { CellAppearance.Copy(cells[0]); cells[0].Effect = StartingCellAppearance.Effect; } if (UseEndingCell) { EndingCellAppearance.Copy(cells[cells.Count - 1]); cells[cells.Count - 1].Effect = EndingCellAppearance.Effect; } else { CellAppearance.Copy(cells[cells.Count - 1]); cells[cells.Count - 1].Effect = CellAppearance.Effect; } for (int i = 1; i < cells.Count - 1; i++) { CellAppearance.Copy(cells[i]); cells[i].Effect = CellAppearance.Effect; } } else if (cells.Count == 1) { CellAppearance.Copy(cells[0]); cells[0].Effect = CellAppearance.Effect; } }
/// <summary> /// Sets the console this cursor is targetting. /// </summary> /// <param name="console">The console the cursor works with.</param> public void AttachConsole(SurfaceEditor console) { _console = new WeakReference(console); }
/// <summary> /// /// </summary> /// <param name="surface"></param> /// <param name="area"></param> public override void Draw(ITextSurface surface, Microsoft.Xna.Framework.Rectangle area) { var hotSpot = ((Hotspot)Item); ColoredString value = ((char)hotSpot.DebugAppearance.GlyphIndex).ToString().CreateColored(hotSpot.DebugAppearance.Foreground, hotSpot.DebugAppearance.Background, hotSpot.DebugAppearance.SpriteEffect) + " ".CreateColored(_currentAppearance.Foreground, _currentAppearance.Background) + hotSpot.Title.CreateColored(_currentAppearance.Foreground, _currentAppearance.Background); if (value.Count < area.Width) value += new string(' ', area.Width - value.Count).CreateColored(_currentAppearance.Foreground, _currentAppearance.Background); else if (value.Count > area.Width) value = new ColoredString(value.Take(area.Width).ToArray()); var editor = new SurfaceEditor(surface); editor.Print(area.X, area.Y, value); _isDirty = false; }