Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SelectionMenu
- {
- public string[] MenuItems { get; set; }
- public int Columns { get; }
- public int Rows { get; }
- public int CursorPosition { get; private set; }
- public int CellLength { get; }
- public string Title { get; }
- public SelectionMenu(string title, IEnumerable<string> menuItems, int columns, int cellLength)
- {
- this.Title = title;
- this.MenuItems = menuItems.ToArray();
- this.Columns = columns;
- this.Rows = this.MenuItems.Length / columns +
- (this.MenuItems.Length % columns == 0 ? 0 : 1);
- this.CellLength = cellLength;
- }
- private void DrawTable(int offsetY, int offsetX)
- {
- var fullRow = "-".Multiply(this.CellLength);
- var partialRow = " ".Multiply(this.CellLength);
- fullRow += "----";
- fullRow = fullRow.Multiply(this.Columns);
- partialRow += " |";
- partialRow = partialRow.Multiply(this.Columns);
- partialRow = "|" + partialRow.Substring(1, partialRow.Length - 1);
- partialRow += "\r\n";
- var table = $"{fullRow}\r\n{partialRow}";
- table = table.Multiply(this.Rows);
- table += fullRow;
- Console.SetCursorPosition(offsetX, offsetY);
- Console.Write(table);
- }
- private void DrawItems(int offsetY, int offsetX)
- {
- var cursorX = Console.CursorLeft;
- var cursorY = Console.CursorTop;
- for (int i = 0; i < this.MenuItems.Length; i++)
- {
- var x = offsetX - 1 + (i % this.Columns) * (this.CellLength + 2) + (i % this.Columns + 1) * 2;
- var y = offsetY + 1 + (i / this.Columns) * 2;
- if (i % this.Columns == 0)
- {
- x++;
- }
- Console.SetCursorPosition(x, y);
- var menuItemcontent = this.MenuItems[i];
- if (menuItemcontent.Length > this.CellLength)
- {
- menuItemcontent = menuItemcontent.Substring(0, this.CellLength);
- }
- Console.Write(menuItemcontent);
- }
- Console.SetCursorPosition(cursorX, cursorY);
- }
- private void DrawCursorItem(int offsetY, int offsetX)
- {
- var curX = Console.CursorLeft;
- var curY = Console.CursorTop;
- var x = offsetX + 2 + this.CellLength * (this.CursorPosition % this.Columns) + (this.CursorPosition % this.Columns) * 4;
- var y = offsetY + 1 + 2 * (this.CursorPosition / this.Columns);
- if (this.CursorPosition % this.Columns > 0)
- {
- x--;
- }
- var content = this.MenuItems[CursorPosition];
- if (content.Length > 8)
- {
- content = content.Substring(0, this.CellLength - 2);
- }
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("> " + content);
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.SetCursorPosition(curX, curY);
- }
- public void DrawMenu(int offsetY = 0, int offsetX = 0)
- {
- var curX = Console.CursorLeft;
- var curY = Console.CursorTop;
- Console.SetCursorPosition(offsetY, offsetX);
- Console.WriteLine($"<{this.Title}>");
- this.DrawTable (offsetY + 1, offsetX);
- this.DrawItems (offsetY + 1, offsetX);
- this.DrawCursorItem(offsetY + 1, offsetX);
- Console.SetCursorPosition(curX, curY);
- }
- public void MoveCursor(Direction dir)
- {
- switch (dir)
- {
- case Direction.Up:
- if (this.CursorPosition >= this.Columns)
- {
- this.CursorPosition -= this.Columns;
- }
- break;
- case Direction.Down:
- if (this.CursorPosition / this.Columns < this.Rows - 1
- && this.CursorPosition + this.Columns < this.MenuItems.Length)
- {
- this.CursorPosition += this.Columns;
- }
- break;
- case Direction.Right:
- if (this.CursorPosition % this.Columns < this.Columns - 1
- && this.CursorPosition < this.MenuItems.Length - 1)
- {
- this.CursorPosition++;
- }
- break;
- case Direction.Left:
- if (this.CursorPosition % this.Columns > 0)
- {
- this.CursorPosition--;
- }
- break;
- }
- }
- public void MoveCursor(ConsoleKey key)
- {
- switch (key)
- {
- case ConsoleKey.UpArrow:
- this.MoveCursor(Direction.Up);
- break;
- case ConsoleKey.DownArrow:
- this.MoveCursor(Direction.Down);
- break;
- case ConsoleKey.LeftArrow:
- this.MoveCursor(Direction.Left);
- break;
- case ConsoleKey.RightArrow:
- this.MoveCursor(Direction.Right);
- break;
- }
- }
- public void Clear(int offsetY, int offsetX)
- {
- var clearString = " ";
- clearString = clearString.Multiply(Console.WindowWidth - offsetX);
- }
- public int? GetMenuResult(int offsetY = 0, int offsetX = 0)
- {
- do
- {
- this.DrawMenu(offsetY, offsetX);
- var key = Console.ReadKey().Key;
- switch (key)
- {
- case ConsoleKey.C:
- return null;
- case ConsoleKey.Enter:
- return this.CursorPosition;
- }
- this.MoveCursor(key);
- } while (true);
- }
- }
- public static class StringExtension
- {
- public static string Multiply(this string str, int amount)
- {
- string result = str;
- for (int i = 1; i < amount; i++)
- {
- result += str;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement