namespace DX86.Modules; /* * Copyright 2024 SnapixLP */ public class ItemSelector { private MessageSender ms; public ItemSelector(MessageSender ms) { this.ms = ms; ms.Log("[ItemSelector] ItemSelector initialized"); } private string listTitle = ""; public void SetTitle(string title) { listTitle = title; } public string SelectItemFromList(string[] items) { bool noerror = true; while (noerror) { try { Console.Clear(); ms.SaveOnly(true); int cursorPosition = 1; int listPosition = 1; bool running = true; while (running) { GenerateList(listTitle, items, cursorPosition, listPosition); ConsoleKeyInfo keyInfo = Console.ReadKey(true); switch (keyInfo.Key) { case ConsoleKey.UpArrow: cursorPosition -= 1; break; case ConsoleKey.DownArrow: cursorPosition += 1; break; case ConsoleKey.W: listPosition -= 1; break; case ConsoleKey.S: listPosition += 1; break; case ConsoleKey.Enter: running = false; break; case ConsoleKey.F1: MessageBox mbox = new MessageBox(ms); mbox.ShowAsync("Help", [ "Use the Arrow keys to select the items from the list.", "Press enter to select the items from the list.", "Press F1 to see this message.", ]); break; } if (cursorPosition > items.Length) { cursorPosition = 1; listPosition = 1; } if (cursorPosition < 1) { cursorPosition = items.Length; listPosition = items.Length - 4; } if (listPosition > 1 && cursorPosition - listPosition == 0) { listPosition -= 1; } if (listPosition < items.Length - 4 && cursorPosition - listPosition == 4) { listPosition += 1; } Console.Clear(); } ms.SaveOnly(false); Console.Clear(); ms.SendHistory(); noerror = false; return items[cursorPosition - 1]; } catch (Exception e) { MessageBox messageBox = new MessageBox(ms); ms.Error("[ItemSelector] [SelectItemFromList()] An Error Occured"); ms.Error(e.Message); messageBox.ShowAsync("An error Occured | ItemSelector.cs at SelectItemFromList()", [e.Message, "", "Please try again..."]); } } return ""; } public static void GenerateList(string title, string[] items, int cursorPostion, int listPosition) { //Console.WriteLine($"{cursorPostion}, {listPosition}, {items.Length}, {cursorPostion - listPosition}, {listPosition < items.Length - 4}");; string outMessage = ""; string headBar = ""; for (int i = 1; i <= title.Length; i++) headBar += "="; string titleSpace = ""; if (headBar.Length - (items[cursorPostion - 1].Length + 14) < 0) { // make int from negative to positive int adjustedLength = Math.Abs(headBar.Length - (items[cursorPostion - 1].Length + 14)); for (int i = 1; i <= adjustedLength; i++) { headBar += "="; titleSpace += " "; } } string spaces = ""; for (int i = 1; i <= headBar.Length - (items[cursorPostion - 1].Length + 14); i++) spaces += " "; outMessage += $"[ {headBar} ]\n"; outMessage += ($"[ {title}{titleSpace} ]\n"); outMessage += ($"[ Current Item: {items[cursorPostion-1]} [{cursorPostion}/{items.Length}]{spaces} ]\n"); outMessage += ($"[ Press \"F1\" for help ]\n"); outMessage += "\n"; Console.WriteLine(outMessage); int b = listPosition - 2; for (int i = listPosition - 1; i < items.Length && i <= b + 5; i++) { if(i == cursorPostion - 1) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("[" + listPosition + "] " + items[i]); Console.ResetColor(); listPosition++; } else { Console.WriteLine("[" + listPosition + "] " + items[i]); listPosition++; } } } }