using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SRMultiplayer { public class ConsoleInput { public string inputString = ""; public event Action OnInputText; /// /// Handle updates the console /// This is to catch key presses and process them /// public void Update() { if (Console.KeyAvailable) { ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(); switch (consoleKeyInfo.Key) { case ConsoleKey.Enter: this.OnEnter(); break; case ConsoleKey.Backspace: this.OnBackspace(); break; case ConsoleKey.Escape: this.OnEscape(); break; case ConsoleKey.UpArrow: this.GetCommand(1); break; case ConsoleKey.DownArrow: this.GetCommand(-1); break; default: //check if pressed key is a character bool flag = consoleKeyInfo.KeyChar > '\0'; if (flag) { //if so at it tothe input line this.inputString += consoleKeyInfo.KeyChar.ToString(); this.RedrawInputLine(); } break; } } } #region Console Processors /// /// Clears out the current console line /// public void ClearLine() { Console.CursorLeft = 0; Console.Write(new string(' ', Console.BufferWidth)); Console.CursorTop--; Console.CursorLeft = 0; } /// /// Used to redraw the input line incase we had to hold for console write lines /// public void RedrawInputLine() { bool flag = Console.CursorLeft > 0; if (flag) { this.ClearLine(); } Console.ForegroundColor = ConsoleColor.Green; Console.Write("> "); Console.Write(this.inputString); } #endregion region #region Key Press Events /// /// Process Backspace pressed /// internal void OnBackspace() { bool flag = this.inputString.Length <= 0; if (!flag) { this.inputString = this.inputString.Substring(0, this.inputString.Length - 1); this.RedrawInputLine(); } } /// /// Process Escape pressed /// internal void OnEscape() { this.ClearLine(); this.inputString = ""; this.RedrawInputLine(); } /// /// Process Enter pressed /// internal void OnEnter() { this.ClearLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("> " + this.inputString); string obj = this.inputString; this.inputString = ""; bool flag = this.OnInputText != null; if (flag) { //on text inputed reset the search loc and cycle the search tree searchLoc = -1; //searchLoc set to -1 to always go to place 0 on first arrow AddToCommandTree(obj); this.OnInputText(obj); } } #endregion #region Command History Retrieval List cmdTree = new List(); int maxCommands = 10; /// /// Manages the Command tree for the up and down arrows /// /// internal void AddToCommandTree(string cmdText) { cmdTree.Insert(0, cmdText); //if tree gets larger than 10 remove the 11th item if (cmdTree.Count > maxCommands) { cmdTree.RemoveAt(10); }; } //handle internal cycling of last 10 commands int searchLoc = -1; /// /// Gets the command at the designaed slot from the current possition /// /// Possition from the current Command internal void GetCommand(int diff) { if (cmdTree.Count > 0) { searchLoc = searchLoc + diff; //prevent below 0 or over max position if (searchLoc > (cmdTree.Count - 1)) searchLoc = (cmdTree.Count - 1); if (searchLoc < 0) searchLoc = 0; //if a new location is found enter the search text in the input and redraw it. this.inputString = cmdTree[searchLoc]; this.RedrawInputLine(); } else { Console.WriteLine("cmdTree is empty"); } } #endregion } }