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

97 lines
2.7 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 List<Selector<T>> _selectList = new List<Selector<T>>();
public string Title { get; private set; }
public StringBuilder InputStr { get; set; }
private int _currentLine;
public int CurrentLine {
get => _currentLine;
private set
{
_currentLine = value;
if (_currentLine < 0) _currentLine = 0;
if (_currentLine >= _selectList.Count) _currentLine = _selectList.Count-1;
}
}
public T? CurrentSelected { get; protected set; }
public bool WriteMode { get; set; }
protected Menu(string title)
{
Title = title;
CurrentLine = 0;
WriteMode = false;
_screenDisplay = new StringBuilder();
InputStr = new StringBuilder();
}
protected 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 virtual void Display()
{
Console.Clear();
Console.WriteLine(_screenDisplay);
}
public virtual void Update()
{
_screenDisplay.AppendLine($"[ {Title} ]");
_screenDisplay.AppendLine("-------------------------------------------\n");
for (int i = 0; i < _selectList.Count; i++)
{
if (CurrentLine == i)
_screenDisplay.Append($"> ");
else
_screenDisplay.Append($" ");
_screenDisplay.AppendLine($"{_selectList[i].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();
}
}