-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrowMenu.cs
247 lines (200 loc) · 5.35 KB
/
ArrowMenu.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
class ArrowMenu
{
private MenuSettings settings;
private int width;
public ArrowMenu(MenuSettings settings)
{
// Add the settings to the menu
this.settings = settings;
}
// Arrow menu without a title
public int Menu(string[] menuItems)
{
return CreateMenu(menuItems);
}
// Arrow menu with a title
public int Menu(string title, string[] menuItems)
{
return CreateMenu(menuItems, title);
}
private int CreateMenu(string[] menuItems, string title = "")
{
int index = 0;
int top = Console.CursorTop + 1;
int padding = settings.padding;
Line line = settings.lineStyle;
// Hide the cursor while the menu is active
Console.CursorVisible = false;
// Get the longest item in the menu
int longest = title.Length;
for (int i = 0; i < menuItems.Length; i++)
{
if (menuItems[i].Length > longest) longest = menuItems[i].Length;
}
width = (longest + (padding * 2));
// If width is odd, make it even
if (width % 2 != 0) width++;
// Check for if there is a title supplied
if (title != "")
{
// Add the top of the title area
Console.WriteLine(line.topLeft + new string(line.horizontal, width) + line.topRight);
// Add the title area
string whitespace = new string(' ', (width - title.Length) / 2);
Console.WriteLine(line.vertical + whitespace + title + whitespace + line.vertical);
// Add the divider area
Console.WriteLine(line.verticalLeftDivider + new string(line.horizontalDivider, width) + line.verticalRightDivider);
// Update the top
top += 2;
}
else Console.WriteLine(line.topLeft + new string(line.horizontal, width) + line.topRight);
// Add the initial menu items
Console.CursorTop = top + menuItems.Length;
Console.WriteLine(line.bottomLeft + new string(line.horizontal, width) + line.bottomRight);
Console.CursorTop = top;
for (int i = 0; i < menuItems.Length; i++)
{
Console.SetCursorPosition(0, (top + i));
DrawItem(menuItems[i], i == index);
}
// Menu loop
while (true)
{
// Highlight the selected index
for (int i = 0; i < menuItems.Length; i++)
{
// Draw the current menu item
Console.SetCursorPosition(0, top + i);
DrawItem(menuItems[i], i == index);
}
// Get the user input
ConsoleKeyInfo input = Console.ReadKey(true);
if (input.Key == ConsoleKey.UpArrow)
{
// Go up
index--;
}
else if (input.Key == ConsoleKey.DownArrow || input.Key == ConsoleKey.Tab)
{
// Go down
index++;
}
else if (input.Key == ConsoleKey.Enter || input.Key == ConsoleKey.Spacebar)
{
// Reset the cursor
Console.SetCursorPosition(0, (top + menuItems.Length + 1));
Console.CursorVisible = true;
// Submit the current index as the answer
return index;
}
// Check for if the new index is out of bounds
if (index == -1) index = (menuItems.Length - 1);
if (index == menuItems.Length) index = 0;
}
}
private void DrawItem(string item, bool selected)
{
Line line = settings.lineStyle;
// Draw the left border
Console.Write(line.vertical);
// Check for if the current item is selected
if (selected == true)
{
// Flip the console colors
ConsoleColor background = Console.BackgroundColor;
Console.BackgroundColor = Console.ForegroundColor;
Console.ForegroundColor = background;
// Add a arrow thing
Console.Write("> ");
}
else Console.Write(" ");
// Add the text and whitespace, then revert the colors
Console.Write(item);
Console.Write(new string(' ', width - item.Length - 2));
Console.ResetColor();
// Draw the the right border
Console.Write(line.vertical);
}
}
class MenuSettings
{
public Line lineStyle { get; set; }
public int padding { get; set; }
public MenuSettings(LineStyle lineStyle, int padding)
{
this.lineStyle = new Line(lineStyle);
this.padding = padding;
}
}
// Line stuff
public enum LineStyle
{
THIN,
THICK,
CLASSIC,
BLOCKY
}
class Line
{
public char topLeft { get; }
public char topRight { get; }
public char bottomLeft { get; }
public char bottomRight { get; }
public char horizontal { get; }
public char vertical { get; }
public char verticalLeftDivider { get; set; }
public char verticalRightDivider { get; set; }
public char horizontalDivider { get; set; }
public Line(LineStyle lineStyle)
{
//TODO: If another style is added, convert to switch statement
if (lineStyle == LineStyle.THIN)
{
topLeft = '┌';
topRight = '┐';
bottomLeft = '└';
bottomRight = '┘';
horizontal = '─';
vertical = '│';
verticalLeftDivider = '├';
verticalRightDivider = '┤';
horizontalDivider = '─';
}
else if (lineStyle == LineStyle.THICK)
{
topLeft = '╔';
topRight = '╗';
bottomLeft = '╚';
bottomRight = '╝';
horizontal = '═';
vertical = '║';
verticalLeftDivider = '╟';
verticalRightDivider = '╢';
horizontalDivider = '─';
}
else if (lineStyle == LineStyle.CLASSIC)
{
topLeft = '+';
topRight = '+';
bottomLeft = '+';
bottomRight = '+';
horizontal = '-';
vertical = '|';
verticalLeftDivider = '+';
verticalRightDivider = '+';
horizontalDivider = '-';
}
else if (lineStyle == LineStyle.BLOCKY)
{
topLeft = '▒';
topRight = '▒';
bottomLeft = '▒';
bottomRight = '▒';
horizontal = '▒';
vertical = '▒';
verticalLeftDivider = '▒';
verticalRightDivider = '▒';
horizontalDivider = '░';
}
}
}