Advertisement
joernneumeyer

SelectionMenu

Jan 8th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. public class SelectionMenu
  2.   {
  3.     public string[] MenuItems { get; set; }
  4.     public int Columns { get; }
  5.     public int Rows { get;  }
  6.     public int CursorPosition { get; private set; }
  7.     public int CellLength { get; }
  8.     public string Title { get; }
  9.  
  10.     public SelectionMenu(string title, IEnumerable<string> menuItems, int columns, int cellLength)
  11.     {
  12.       this.Title = title;
  13.       this.MenuItems = menuItems.ToArray();
  14.       this.Columns = columns;
  15.       this.Rows = this.MenuItems.Length / columns +
  16.         (this.MenuItems.Length % columns == 0 ? 0 : 1);
  17.       this.CellLength = cellLength;
  18.     }
  19.  
  20.     private void DrawTable(int offsetY, int offsetX)
  21.     {
  22.       var fullRow = "-".Multiply(this.CellLength);
  23.       var partialRow = " ".Multiply(this.CellLength);
  24.       fullRow += "----";
  25.       fullRow = fullRow.Multiply(this.Columns);
  26.       partialRow += "   |";
  27.       partialRow = partialRow.Multiply(this.Columns);
  28.       partialRow = "|" + partialRow.Substring(1, partialRow.Length - 1);
  29.       partialRow += "\r\n";
  30.       var table = $"{fullRow}\r\n{partialRow}";
  31.       table = table.Multiply(this.Rows);
  32.       table += fullRow;
  33.       Console.SetCursorPosition(offsetX, offsetY);
  34.       Console.Write(table);
  35.     }
  36.  
  37.     private void DrawItems(int offsetY, int offsetX)
  38.     {
  39.       var cursorX = Console.CursorLeft;
  40.       var cursorY = Console.CursorTop;
  41.       for (int i = 0; i < this.MenuItems.Length; i++)
  42.       {
  43.         var x = offsetX - 1 + (i % this.Columns) * (this.CellLength + 2) + (i % this.Columns + 1) * 2;
  44.         var y = offsetY + 1 + (i / this.Columns) * 2;
  45.         if (i % this.Columns == 0)
  46.         {
  47.           x++;
  48.         }
  49.         Console.SetCursorPosition(x, y);
  50.         var menuItemcontent = this.MenuItems[i];
  51.         if (menuItemcontent.Length > this.CellLength)
  52.         {
  53.           menuItemcontent = menuItemcontent.Substring(0, this.CellLength);
  54.         }
  55.         Console.Write(menuItemcontent);
  56.       }
  57.       Console.SetCursorPosition(cursorX, cursorY);
  58.     }
  59.  
  60.     private void DrawCursorItem(int offsetY, int offsetX)
  61.     {
  62.       var curX = Console.CursorLeft;
  63.       var curY = Console.CursorTop;
  64.       var x = offsetX + 2 + this.CellLength * (this.CursorPosition % this.Columns) + (this.CursorPosition % this.Columns) * 4;
  65.       var y = offsetY + 1 + 2 * (this.CursorPosition / this.Columns);
  66.       if (this.CursorPosition % this.Columns > 0)
  67.       {
  68.         x--;
  69.       }
  70.  
  71.       var content = this.MenuItems[CursorPosition];
  72.       if (content.Length > 8)
  73.       {
  74.         content = content.Substring(0, this.CellLength - 2);
  75.       }
  76.       Console.SetCursorPosition(x, y);
  77.       Console.ForegroundColor = ConsoleColor.White;
  78.       Console.Write("> " + content);
  79.       Console.ForegroundColor = ConsoleColor.Gray;
  80.       Console.SetCursorPosition(curX, curY);
  81.     }
  82.  
  83.     public void DrawMenu(int offsetY = 0, int offsetX = 0)
  84.     {
  85.       var curX = Console.CursorLeft;
  86.       var curY = Console.CursorTop;
  87.       Console.SetCursorPosition(offsetY, offsetX);
  88.       Console.WriteLine($"<{this.Title}>");
  89.       this.DrawTable     (offsetY + 1, offsetX);
  90.       this.DrawItems     (offsetY + 1, offsetX);
  91.       this.DrawCursorItem(offsetY + 1, offsetX);
  92.       Console.SetCursorPosition(curX, curY);
  93.     }
  94.  
  95.     public void MoveCursor(Direction dir)
  96.     {
  97.       switch (dir)
  98.       {
  99.         case Direction.Up:
  100.           if (this.CursorPosition >= this.Columns)
  101.           {
  102.             this.CursorPosition -= this.Columns;
  103.           }
  104.           break;
  105.  
  106.         case Direction.Down:
  107.           if (this.CursorPosition / this.Columns < this.Rows - 1
  108.               && this.CursorPosition + this.Columns < this.MenuItems.Length)
  109.           {
  110.             this.CursorPosition += this.Columns;
  111.           }
  112.           break;
  113.  
  114.         case Direction.Right:
  115.           if (this.CursorPosition % this.Columns < this.Columns - 1
  116.               && this.CursorPosition < this.MenuItems.Length - 1)
  117.           {
  118.             this.CursorPosition++;
  119.           }
  120.           break;
  121.  
  122.         case Direction.Left:
  123.           if (this.CursorPosition % this.Columns > 0)
  124.           {
  125.             this.CursorPosition--;
  126.           }
  127.           break;
  128.       }
  129.     }
  130.  
  131.     public void MoveCursor(ConsoleKey key)
  132.     {
  133.       switch (key)
  134.       {
  135.         case ConsoleKey.UpArrow:
  136.           this.MoveCursor(Direction.Up);
  137.           break;
  138.         case ConsoleKey.DownArrow:
  139.           this.MoveCursor(Direction.Down);
  140.           break;
  141.         case ConsoleKey.LeftArrow:
  142.           this.MoveCursor(Direction.Left);
  143.           break;
  144.         case ConsoleKey.RightArrow:
  145.           this.MoveCursor(Direction.Right);
  146.           break;
  147.       }
  148.     }
  149.  
  150.     public void Clear(int offsetY, int offsetX)
  151.     {
  152.       var clearString = " ";
  153.       clearString = clearString.Multiply(Console.WindowWidth - offsetX);
  154.      
  155.  
  156.     }
  157.  
  158.     public int? GetMenuResult(int offsetY = 0, int offsetX = 0)
  159.     {
  160.       do
  161.       {
  162.         this.DrawMenu(offsetY, offsetX);
  163.         var key = Console.ReadKey().Key;
  164.         switch (key)
  165.         {
  166.           case ConsoleKey.C:
  167.             return null;
  168.           case ConsoleKey.Enter:
  169.             return this.CursorPosition;
  170.         }
  171.         this.MoveCursor(key);
  172.       } while (true);
  173.     }
  174.   }
  175.  
  176.   public static class StringExtension
  177.   {
  178.     public static string Multiply(this string str, int amount)
  179.     {
  180.       string result = str;
  181.       for (int i = 1; i < amount; i++)
  182.       {
  183.         result += str;
  184.       }
  185.       return result;
  186.     }
  187.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement