You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SAE-2.01/MCTG/ConsoleApp/Menu/Menu.cs

103 lines
3.0 KiB

using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.Menu
{
internal abstract class Menu<T> : IMenu
where T : notnull
{
protected StringBuilder _screenDisplay;
protected readonly List<Selector<T>>? _selectList;
public string Title { get; private set; }
public StringBuilder InputStr { get; set; }
public int CurrentLine { get; private set; }
public T? CurrentSelected { get; private set; }
public bool WriteMode { get; set; }
private Menu(string title)
{
Title = title;
CurrentLine = 0;
WriteMode = false;
_screenDisplay = new StringBuilder();
InputStr = new StringBuilder();
}
public Menu(string title, params Selector<T>[] selections ) : this(title)
{
if (selections == null || selections.Length == 0)
throw new ArgumentException("Error: a menu must contain at least 1 selection");
_selectList = selections.ToList();
CurrentSelected = _selectList[0].Item;
}
public Menu(string title, Dictionary<T, string> lines) : this(title, CreateSelection(lines))
{ }
public static Selector<T>[] CreateSelection(Dictionary<T, string> lines)
{
if (lines is null)
{
throw new ArgumentNullException(nameof(lines));
}
Selector<T>[] selections = new Selector<T>[lines.Count];
foreach (KeyValuePair<T, string> line in lines)
{
selections.Append(new Selector<T>(line.Key, line.Value));
}
return selections;
}
public virtual void Display()
{
Console.WriteLine(_screenDisplay);
}
public virtual void Update()
{
_screenDisplay.AppendLine($"[ {Title} ]");
_screenDisplay.AppendLine("-------------------------------------------\n");
foreach (Selector<T> selector in _selectList)
{
if (selector.Equals(CurrentSelected))
_screenDisplay.Append($"> ");
else
_screenDisplay.Append($" ");
_screenDisplay.AppendLine($"{selector.Line}");
}
}
public void SelectNext()
{
CurrentSelected = _selectList[++CurrentLine].Item;
}
public void SelectPrevious()
{
CurrentSelected = _selectList[--CurrentLine].Item;
}
public void EnableWriteMode()
{
WriteMode = true;
}
public void DisableWriteMode()
{
WriteMode = false;
}
public abstract void Return();
}
}