-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMenuBase.cs
55 lines (50 loc) · 1.57 KB
/
MenuBase.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
using System;
using System.Collections.Generic;
namespace Roguelike
{
public class MenuBase
{
protected List<MenuItem> MenuItems;
protected int CurIndex = 0;
protected ConsoleColor FgColor = ConsoleColor.Green;
protected virtual void Print(int offsetX, int offsetY)
{
for (int i = 0; i < MenuItems.Count; i++)
{
Console.SetCursorPosition(offsetX, offsetY + i);
if (i == CurIndex)
Console.ForegroundColor = FgColor;
Console.WriteLine(MenuItems[i].Name);
Console.ResetColor();
}
}
protected bool ProcessKey(ConsoleKey key)
{
switch (key)
{
case ConsoleKey.DownArrow:
if (CurIndex == MenuItems.Count - 1) CurIndex = 0;
else CurIndex++;
break;
case ConsoleKey.UpArrow:
if (CurIndex == 0) CurIndex = MenuItems.Count - 1;
else CurIndex--;
break;
case ConsoleKey.Enter:
return true;
}
return false;
}
public void Process(int offsetX = 0, int offsetY = 0)
{
while (true)
{
Print(offsetX, offsetY);
var key = Console.ReadKey().Key;
int prevIndex = CurIndex;
if (ProcessKey(key) == true)
MenuItems[CurIndex].OnClick();
}
}
}
}