using System; using System.Drawing; using CC_Functions.Commandline.TUI; using CC_Functions.Core; namespace CC_Functions.Commandline { /// /// Provides differential drawing of a char[,] Do not use in combination with System.Console /// public static class DiffDraw { private static Pixel[,] _last = new Pixel[0, 0]; private static Pixel[,] Screen { get; set; } = new Pixel[0, 0]; /// /// The regions width /// public static int Width => Screen.GetLength(1); /// /// The regions height /// public static int Height => Screen.GetLength(0); /// /// Draws to the console /// /// Whether to use color /// Whether to redraw the entire screen (should be done from time to time to prevent corruption) public static void Draw(bool color, bool full = false) { Console.CursorTop = 0; Console.CursorLeft = 0; ConsoleColor fCol = Console.ForegroundColor; ConsoleColor bCol = Console.BackgroundColor; if (full) Console.Clear(); int width = Width; int height = Height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Pixel tmp1 = Screen[y, x]; if (!full && tmp1 == _last[y, x]) continue; if (!ReferenceEquals(tmp1, null) && color) { if (Console.ForegroundColor != tmp1.ForeColor) Console.ForegroundColor = tmp1.ForeColor; if (Console.BackgroundColor != tmp1.BackColor) Console.BackgroundColor = tmp1.BackColor; } Console.CursorLeft = x; Console.Write(tmp1 ?? Pixel.Empty); } Console.WriteLine(); Console.CursorLeft = 0; } Console.ForegroundColor = fCol; Console.BackgroundColor = bCol; _last = Screen; } /// /// Gets the char at a location /// /// The location /// The char public static char Get(Point p) => Get(p.X, p.Y); /// /// Gets the char at a location /// /// The locations X coordinate /// The locations Y coordinate /// The char public static char Get(int x, int y) => Screen[y, x].Content; /// /// Gets the foreground color at a location /// /// The location /// The color public static ConsoleColor GetForeColor(Point p) => GetForeColor(p.X, p.Y); /// /// Gets the foreground color at a location /// /// The locations X coordinate /// The locations Y coordinate /// The color public static ConsoleColor GetForeColor(int x, int y) => Screen[y, x].ForeColor; /// /// Gets the background color at a location /// /// The location /// The color public static ConsoleColor GetBackColor(Point p) => GetBackColor(p.X, p.Y); /// /// Gets the background color at a location /// /// The locations X coordinate /// The locations Y coordinate /// The color public static ConsoleColor GetBackColor(int x, int y) => Screen[y, x].BackColor; /// /// Sets a pixel at a point /// /// The point to place at /// The pixel to place public static void Set(Point p, Pixel c) => Set(p.X, p.Y, c); /// /// Sets a pixel at a location /// /// The locations X coordinate /// The locations Y coordinate /// The pixel to place public static void Set(int x, int y, Pixel c) => Screen[y, x] = c; /// /// Clears the screen /// public static void Clear() => Clear(Width, Height); /// /// Resizes and clears the screen /// /// The new width /// The new height public static void Clear(int width, int height) { Screen = new Pixel[height, width]; _last = _last.Resize(height, width); } /// /// Replaces the screen state /// /// The new state public static void Clear(Pixel[,] content) { Screen = content; _last = _last.Resize(Height, Width); } } }