class ColoredMenu {
private ConsoleColor defaultBackColor, defaultForeColor;
private ConsoleColor BackColor;
private ConsoleColor ForeColor;
private string title;
private List<string> options;
private int selectedIndex;
private int Top, Left;
private bool first_time = true;
public int SelectedIndex {
get => selectedIndex;
set => selectedIndex = value;
}
public delegate void Event(int id);
private delegate void Printer();
public List<Event> SelectedEventHandlers;
public ColoredMenu(string title, List<string> options, List<Event> SelectedEventHandlers, ConsoleColor higlighted_back = ConsoleColor.Blue, ConsoleColor highlighted_fore = ConsoleColor.White) {
defaultBackColor = Console.BackgroundColor;
defaultForeColor = Console.ForegroundColor;
BackColor = higlighted_back;
ForeColor = highlighted_fore;
this.title = title;
this.options = options;
selectedIndex = 0;
this.SelectedEventHandlers = SelectedEventHandlers;
Top = 0;
Left = 0;
}
public ColoredMenu(string title, List<string> options, ConsoleColor higlighted_back = ConsoleColor.Blue, ConsoleColor highlighted_fore = ConsoleColor.White) {
defaultBackColor = Console.BackgroundColor;
defaultForeColor = Console.ForegroundColor;
BackColor = higlighted_back;
ForeColor = highlighted_fore;
this.title = title;
this.options = options;
selectedIndex = 0;
SelectedEventHandlers = [];
Top = 0;
Left = 0;
}
~ColoredMenu() {
Console.CursorVisible = true;
}
private void Highlight() {
Console.BackgroundColor = BackColor;
Console.ForegroundColor = ForeColor;
}
private void DefaultColor() {
Console.BackgroundColor = defaultBackColor;
Console.ForegroundColor = defaultForeColor;
}
private void PrintlineHighlighted(string option) {
Highlight();
Console.WriteLine(" " + option);
DefaultColor();
}
private void PrintHighlighted(string option) {
Highlight();
Console.Write(option);
DefaultColor();
}
public void PrintMenuSingle() {
if (options == null) { return; }
Console.WriteLine(title);
for(int i = 0; i < options.Count; i++) {
if(i == selectedIndex) {
PrintlineHighlighted(options[i]);
continue;
}
Console.WriteLine(" " + options[i]);
}
}
private int GetMax() {
int max = title.Length;
foreach(string option in options) {
max = Math.Max(max, option.Length);
}
return max;
}
public void PrintMenuAlt() {
if (options == null) { return; }
if(first_time) {
(Left, Top) = Console.GetCursorPosition();
}
Console.Write(title);
Top += 1;
Left += 1;
for(int i = 0; i < options.Count; i++) {
Console.SetCursorPosition(Left, Top);
if(i == selectedIndex) {
PrintHighlighted(options[i]);
Top += 1;
continue;
}
Console.Write(options[i]);
Top += 1;
}
Left -= 1;
Console.SetCursorPosition(Left, Top);
}
private void DecrementSelected() {
if (selectedIndex - 1 < 0) { return; }
selectedIndex -= 1;
}
private void IncrementSelected() {
if (selectedIndex + 1 >= options.Count) { return; }
SelectedIndex += 1;
}
private int PrintMenuLoop(Printer Print) {
while (true) {
Console.CursorVisible = false;
Print.Invoke();
var key = Console.ReadKey(false).Key;
if(key == ConsoleKey.Enter) {
Console.CursorVisible = true;
return selectedIndex;
}
if(key == ConsoleKey.UpArrow) {
DecrementSelected();
}
if(key == ConsoleKey.DownArrow) {
IncrementSelected();
}
Clear();
Console.CursorVisible = true;
}
}
public void PrintMenu() {
if(SelectedEventHandlers.Count < options.Count) {
throw new ArgumentOutOfRangeException("You cannot call this function without supplying enough event handlers. \"Try PrintMenuNoHandler()\" instead.");
}
int result = PrintMenuLoop(PrintMenuSingle);
SelectedEventHandlers[result].Invoke(result);
}
public void PrintMenuAlternative() {
if(SelectedEventHandlers.Count < options.Count) {
throw new ArgumentOutOfRangeException("You cannot call this function without supplying enough event handlers. \"Try PrintMenuNoHandler()\" instead.");
}
int result = PrintMenuLoop(PrintMenuAlt);
SelectedEventHandlers[result].Invoke(result);
}
public int PrintMenuNoHandler() {
return PrintMenuLoop(PrintMenuAlt);
}
private void ClearNLines(int n) {
var lastline = Console.GetCursorPosition();
lastline.Top -= 1;
Console.SetCursorPosition(0, lastline.Top);
int max = GetMax();
for(int i = 0; i < n; i++) {
for(int j = 0; j < max; j++) {
Console.Write(" ");
}
Console.SetCursorPosition(0, lastline.Top);
lastline.Top -= 1;
}
}
private void ClearAll(int n) {
var lastline = Console.GetCursorPosition();
lastline.Top -= 1;
Console.SetCursorPosition(0, lastline.Top);
int max = GetMax();
for(int i = 0; i < n; i++) {
for(int j = 0; j < Console.WindowWidth; j++) {
Console.Write(" ");
}
Console.SetCursorPosition(0, lastline.Top);
lastline.Top -= 1;
}
}
public void Clear() {
ClearNLines(options.Count + 1);
}
public void ClearAll() {
ClearAll(options.Count + 1);
}
}
class ConsoleList {
public static void Print(int left, int top, string title, List<String> list) {
var initialPos = Console.GetCursorPosition();
Console.SetCursorPosition(left, top);
var current = Console.GetCursorPosition();
Console.Write(title);
Console.SetCursorPosition(left += 1, current.Top + 1);
current = Console.GetCursorPosition();
foreach(var item in list) {
Console.Write(item);
Console.SetCursorPosition(left, current.Top + 1);
current = Console.GetCursorPosition();
}
Console.SetCursorPosition(initialPos.Left, top - 1);
}
}