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.
136 lines
3.3 KiB
136 lines
3.3 KiB
using Mindmagma.Curses;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace SCI.CursesWrapper.UiElements;
|
|
|
|
public class TopMenu : Window {
|
|
private List<MenuItem> _menuItems = new List<MenuItem> ();
|
|
public ReadOnlyCollection<MenuItem> MenuItems {
|
|
get {
|
|
return _menuItems.AsReadOnly ();
|
|
}
|
|
}
|
|
|
|
private MenuItem? activeMenuItem;
|
|
|
|
/// <summary>
|
|
/// Class constructor
|
|
/// </summary>
|
|
/// <param name="screen"></param>
|
|
/// <param name="targetInputHandler"></param>
|
|
/// <param name="menuItems"></param>
|
|
public TopMenu (nint screen, InputHandler targetInputHandler, List<MenuItem> menuItems) :
|
|
base (0, 0, CursesWrapper.GetWidth (screen), 1, targetInputHandler)
|
|
{
|
|
var colorSchemeNormal = ColorSchemes.TopMenuNormal ();
|
|
BackgroundColorId = colorSchemeNormal;
|
|
NCurses.WindowAttributeOn (WindowId, colorSchemeNormal);
|
|
|
|
_menuItems = menuItems;
|
|
foreach (var item in menuItems) {
|
|
item.AssignParentTopMenuWindow (this);
|
|
}
|
|
|
|
RegisterItemEventHandlers ();
|
|
DrawMenu ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finalizer, clean up event handlers
|
|
/// </summary>
|
|
~TopMenu () {
|
|
UnregisterItemEventHandlers ();
|
|
Destroy ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register key event handlers for each menu item
|
|
/// </summary>
|
|
private void RegisterItemEventHandlers () {
|
|
if (TargetInputHandler is null) return;
|
|
|
|
foreach (var item in MenuItems) {
|
|
item.OnItemActivated += ItemActivatedHandler;
|
|
TargetInputHandler.OnKeyPressPrivileged += item.KeypressHandler;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unregister key event handlers for each menu item
|
|
/// </summary>
|
|
private void UnregisterItemEventHandlers () {
|
|
if (TargetInputHandler is null) return;
|
|
|
|
foreach (var item in MenuItems) {
|
|
item.OnItemActivated -= ItemActivatedHandler;
|
|
OnKeyPress -= item.KeypressHandler;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event handler to update menu interface when item reports itself as activated
|
|
/// </summary>
|
|
/// <param name="sender">Sender Menu Item firing the event</param>
|
|
/// <param name="e">Event arguments</param>
|
|
private void ItemActivatedHandler (object? sender, EventArgs e) {
|
|
if (sender is null) return;
|
|
|
|
activeMenuItem = (MenuItem) sender;
|
|
DrawMenu ();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
public void ActivateItem (MenuItem item) {
|
|
foreach (var myItem in MenuItems) {
|
|
if (myItem == item) {
|
|
ItemActivatedHandler (myItem, EventArgs.Empty);
|
|
myItem.ActivateItem ();
|
|
return;
|
|
}
|
|
}
|
|
|
|
throw new Exception ("This item does not exist");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draw the top menu with currently configured menu items
|
|
/// </summary>
|
|
private void DrawMenu () {
|
|
NCurses.ClearWindow (WindowId);
|
|
|
|
var normalColor = ColorSchemes.TopMenuNormal ();
|
|
var activeColor = ColorSchemes.TopMenuActive ();
|
|
|
|
foreach (var item in MenuItems) {
|
|
var itemIsActive = item == activeMenuItem;
|
|
var itemString = " ";
|
|
|
|
if (itemIsActive) {
|
|
itemString = "[";
|
|
NCurses.WindowAttributeOff (WindowId, normalColor);
|
|
NCurses.WindowAttributeOn (WindowId, activeColor);
|
|
}
|
|
|
|
itemString += $"{item.Label}";
|
|
|
|
if (item.Key is not null) {
|
|
itemString += $" ({item.Key})";
|
|
}
|
|
|
|
if (itemIsActive) {
|
|
NCurses.WindowAttributeOff (WindowId, activeColor);
|
|
NCurses.WindowAttributeOn (WindowId, normalColor);
|
|
itemString += "] ";
|
|
} else {
|
|
itemString += " ";
|
|
}
|
|
|
|
NCurses.WindowAddString (WindowId, itemString);
|
|
}
|
|
|
|
Draw ();
|
|
}
|
|
} |