public void ApplyChanges(IConsoleBox box) { foreach (var kvp in changes) { var last = buffer[kvp.Key.X, kvp.Key.Y]; var changed = last.Char == kvp.Value.Char && last.Background == kvp.Value.Background && last.Foreground == kvp.Value.Foreground && last.Attributes == kvp.Value.Attributes; if (!changed) { box.Set( kvp.Key.X, kvp.Key.Y, kvp.Value.Char, kvp.Value.Foreground, kvp.Value.Background, kvp.Value.Attributes); buffer[kvp.Key.X, kvp.Key.Y] = kvp.Value; } } changes.Clear(); }
public WidgetApplication(IConsoleBox console, Func <IWidget> viewFactory) { Console = console; Context = new BuildContext(); factory = viewFactory; InputManager = new UserInputManager(); UI = new UIManager(console); // note: Order on wiring up events matters! // we want the UI to process the events prior to // firing off renders console.KeyEvent += (s, e) => { InputManager.OnKey(e); Render(); }; console.MouseMove += (s, e) => Render(); console.MouseButtonUp += (s, e) => Render(); console.MouseClick += (s, e) => Render(); console.MouseWheel += (s, e) => Render(); console.MouseButtonDown += (s, e) => Render(); console.ResizeEvent += (s, e) => { console.Clear(); Render(true); }; Context.State.Set(UserInputManager.STATE_KEY, InputManager); }
static void Start(IConsoleBox box) { box.Initialize(); var app = new WidgetApplication(box, () => new DemoApp()); //await app.Start(); app.StartSync(); //var test = new TestingApp(box); box.PollEvents(); box.ShutDown(); }
public UIManager(IConsoleBox box) { this.box = box; context = new UIContext(box, buffer); Clear(); this.box = box; box.MouseMove += (s, m) => { var exits = context .EventHandlers .Where(h => h.Area.Contains(m.PreviousStatus.Position)) .Where(h => !h.Area.Contains(m.Status.Position)); foreach (var e in exits) { e.Handler.OnMouseHoverChange?.Invoke(m, false); } var enters = context .EventHandlers .Where(h => h.Area.Contains(m.Status.Position)) .Where(h => !h.Area.Contains(m.PreviousStatus.Position)); foreach (var e in enters) { e.Handler.OnMouseHoverChange?.Invoke(m, true); } }; box.MouseClick += (s, m) => { var clicks = context .EventHandlers .Where(h => h.Area.Contains(m.Status.Position)); foreach (var e in clicks) { e.Handler.OnMouseClick?.Invoke(m); } }; box.ResizeEvent += (s, m) => { // new size, need a new buffer // anything in the render queue or cache is garbage now anyway Clear(); }; }
public TestingApp(IConsoleBox box) { var ui = new UIManager(box); ui.Render(Create()); }
/// <summary> /// The print at console. /// </summary> /// <param name="output"> /// The output. /// </param> /// <param name="box"> /// The console box. /// </param> public static void Print(IConsoleWrapper<ConsoleColor, ConsoleKeyInfo> output, IConsoleBox<ConsoleColor> box) { output.SetWindowSize(80, 50); output.BackgroundColor = box.ColorBackground; output.ForegroundColor = box.ColorText; output.SetCursorPosition(5, 2); output.Write(@"__ __ ___ _ _ _____ ______ _______ _____ ____ _____ ____ "); output.SetCursorPosition(5, 3); output.Write(@"| \/ |_ _| \ | | ____/ ___\ \ / / ____| ____| _ \| ____| _ \ "); output.SetCursorPosition(5, 4); output.Write(@"| |\/| || || \| | _| \___ \\ \ /\ / /| _| | _| | |_) | _| | |_) |"); output.SetCursorPosition(5, 5); output.Write(@"| | | || || |\ | |___ ___) |\ V V / | |___| |___| __/| |___| _ < "); output.SetCursorPosition(5, 6); output.Write(@"|_| |_|___|_| \_|_____|____/ \_/\_/ |_____|_____|_| |_____|_| \_\"); for (var i = box.StartX; i <= box.StartX + box.SizeX; i++) { for (var j = box.StartY; j <= box.StartY + box.SizeY; j++) { output.SetCursorPosition(i, j); output.Write(" "); } } output.SetCursorPosition(box.StartX, box.StartY); output.Write("╔"); output.SetCursorPosition(box.StartX, box.StartY + box.SizeY); output.Write("╚"); output.SetCursorPosition(box.StartX + box.SizeX, box.StartY); output.Write("╗"); output.SetCursorPosition(box.StartX + box.SizeX, box.StartY + box.SizeY); output.Write("╝"); for (var i = box.StartX + 1; i < box.StartX + box.SizeX; i++) { output.SetCursorPosition(i, box.StartY); output.Write("═"); output.SetCursorPosition(i, box.StartY + box.SizeY); output.Write("═"); } for (var i = box.StartY + 1; i < box.StartY + box.SizeY; i++) { output.SetCursorPosition(box.StartX, i); output.Write("║"); output.SetCursorPosition(box.StartX + box.SizeX, i); output.Write("║"); } if (box.Text != string.Empty) { var rows = box.Text.SplitBy(box.SizeX - 1); var cnt = 0; foreach (var row in rows) { output.SetCursorPosition(box.StartX + 1, box.StartY + 1 + cnt); output.Write(row); cnt++; } } output.ResetColor(); }
/// <summary> /// The print grid. /// </summary> /// <param name="output"> /// The output. /// </param> /// <param name="box"> /// The console box. /// </param> /// <param name="openCellEvent"> /// The open cell event. /// </param> /// <param name="protectCellEvent"> /// The protect cell event. /// </param> public static void PrintGrid( IConsoleWrapper<ConsoleColor, ConsoleKeyInfo> output, IConsoleBox<ConsoleColor> box, EventHandler openCellEvent, EventHandler protectCellEvent) { Print(output, box); var x = box.StartX + 1; var y = box.StartY + 1; output.CursorVisible = true; output.CursorSize = 100; output.SetCursorPosition(x, y); ConsoleKeyInfo key; do { key = output.ReadKey(true); if (key.Key == ConsoleKey.RightArrow) { if (x < box.StartX + box.SizeX - 1) { x += 1; } } if (key.Key == ConsoleKey.DownArrow) { if (y < box.StartY + box.SizeY - 1) { y += 1; } } if (key.Key == ConsoleKey.LeftArrow) { if (x > box.StartX + 1) { x -= 1; } } if (key.Key == ConsoleKey.UpArrow) { if (y > box.StartY + 1) { y -= 1; } } if (key.Key == ConsoleKey.Spacebar) { var args = new MinesweeperCellClickEventArgs { Row = y - box.StartY - 1, Col = x - box.StartX - 1 }; openCellEvent.Invoke(null, args); } if (key.Key == ConsoleKey.F) { var args = new MinesweeperCellClickEventArgs { Row = y - box.StartY - 1, Col = x - box.StartX - 1 }; protectCellEvent.Invoke(null, args); } output.SetCursorPosition(x, y); } while (key.Key != ConsoleKey.End); }
public UIContext(IConsoleBox console, ConsoleBuffer buffer) { Buffer = buffer; Console = console; EventHandlers = new List <AreaEventHandlerRegistration>(); }