|
|
|
using SCI.CursesWrapper;
|
|
|
|
using Mindmagma.Curses;
|
|
|
|
|
|
|
|
namespace ANSI_Fahrplan.UiElements;
|
|
|
|
|
|
|
|
public class MenuItem {
|
|
|
|
private List<MenuItem> _childItems;
|
|
|
|
public List<MenuItem> ChildItems { get { return _childItems; }}
|
|
|
|
|
|
|
|
private string _label;
|
|
|
|
public string Label { get { return _label; }}
|
|
|
|
|
|
|
|
private string _key;
|
|
|
|
public string Key { get { return _key; }}
|
|
|
|
|
|
|
|
public delegate void ItemActivated (object sender, EventArgs e);
|
|
|
|
|
|
|
|
// Event fired when menu item is activated
|
|
|
|
public event EventHandler? OnItemActivated;
|
|
|
|
|
|
|
|
private InputHandler _inputHandler;
|
|
|
|
public InputHandler InputHandler { get { return _inputHandler; }}
|
|
|
|
|
|
|
|
public MenuItem (string label, string key, InputHandler inputHandler, List<MenuItem>? childItems = null) {
|
|
|
|
_childItems = childItems ?? new List<MenuItem> ();
|
|
|
|
_key = key;
|
|
|
|
_inputHandler = inputHandler;
|
|
|
|
_label = label;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void KeyHandler (object sender, NCursesKeyPressEventArgs e) {
|
|
|
|
if (OnItemActivated is null) return;
|
|
|
|
|
|
|
|
if (Key.Length > 1 && Key.StartsWith ("F")) { // Handle F keys
|
|
|
|
if (e.KeyCode == CursesKey.KEY_F (int.Parse (Key.Substring (1)))) {
|
|
|
|
OnItemActivated (this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
} else if (Key.Length == 1) { // Handle letters and numbers
|
|
|
|
if (e.KeyCode == Key [0]) {
|
|
|
|
OnItemActivated (this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
} else throw new NotImplementedException ("Currently only F-keys and letters work for Top Menu actions");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RegisterKeyHandler () {
|
|
|
|
InputHandler.OnKeyPress += KeyHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnregisterKeyHandler () {
|
|
|
|
InputHandler.OnKeyPress -= KeyHandler;
|
|
|
|
}
|
|
|
|
}
|