Class KeyMenu
A collapsible list of choices drawn with the debug text system, driven entirely by the keyboard.
public class KeyMenu
- Inheritance
-
KeyMenu
Examples
var shapes = new KeyMenu
{
Title = "Shape",
ToggleKey = Keys.C,
Items =
[
new(Keys.D1, "Cube", () => Rebuild(Primitive3D.Cube())),
new(Keys.D2, "Sphere", () => Rebuild(Primitive3D.Sphere())),
],
};
TextOverlay.GetOrCreate(game).AddSection("Shape", () => shapes.GetLines());
// in Update
shapes.Update(game.Input);
Remarks
Collapsed, it occupies a single line showing the toggle key, the title and the current selection. Pressing the toggle key expands it into one line per entry; pressing an entry's key selects it, runs its action and collapses the list again. This keeps a screen full of options down to one line until it is needed.
Nothing here reads the keyboard or draws on its own - call Update(InputManager) once per frame from your update loop, and hand GetLines() to a TextOverlay section, so the key menu shares one position and one toggle key with the camera help and everything else on screen.
Keep every title and label to printable ASCII. The debug text renderer replaces anything outside the range 32 to 126 with a space, so accented letters and arrow glyphs vanish silently rather than failing loudly.
Properties
CloseOnSelect
Gets or sets whether choosing an entry collapses the list. Defaults to true.
public bool CloseOnSelect { get; set; }
Property Value
Remarks
Set to false for a list you expect to use repeatedly - spawning shapes, say, where reopening it before every press would be tedious. The list then stays up until the toggle key or Escape closes it, and the highlight follows the last choice.
CollapsedMarker
Gets or sets the marker drawn before the toggle key while the list is collapsed.
public string CollapsedMarker { get; set; }
Property Value
Remarks
Printable ASCII only - see the remarks on KeyMenu. Arrow glyphs such as
▼ render as blanks.
ExpandedMarker
Gets or sets the marker drawn before the toggle key while the list is expanded.
public string ExpandedMarker { get; set; }
Property Value
Remarks
Printable ASCII only - see the remarks on KeyMenu. Arrow glyphs such as
▼ render as blanks.
IsOpen
Gets or sets a value indicating whether the list is currently expanded.
public bool IsOpen { get; set; }
Property Value
Remarks
Settable so that several key menus can coordinate - close the others when one opens, and their entry keys are then free to overlap.
Items
Gets the entries to choose from.
public required IReadOnlyList<KeyMenuItem> Items { get; init; }
Property Value
Selected
Gets the selected entry, or null when nothing has been selected yet.
public KeyMenuItem? Selected { get; }
Property Value
SelectedColor
Gets or sets the colour used for the currently selected entry while the list is expanded. Set to null to draw it like any other entry.
public Color? SelectedColor { get; set; }
Property Value
SelectedIndex
Gets or sets the index of the selected entry, or -1 when nothing has been selected yet.
public int SelectedIndex { get; set; }
Property Value
Remarks
Assigning this does not run the entry's action - it is for making the display agree with state you have already set up elsewhere. Out-of-range values read back as -1. The range is checked on read rather than on write so that an object initializer can set this before Items, which it will whenever the members are written in that order.
Title
Gets the name shown after the toggle key, for example Shape in [+] [C] Shape: Sphere.
public required string Title { get; init; }
Property Value
TitleColor
Gets or sets the colour of the collapsed line and of the title while expanded.
public Color? TitleColor { get; set; }
Property Value
ToggleKey
Gets the key that expands and collapses the list.
public required Keys ToggleKey { get; init; }
Property Value
Methods
GetLines()
Builds the lines the key menu currently occupies: one when collapsed, one per entry plus a title when expanded.
public IReadOnlyList<TextElement> GetLines()
Returns
- IReadOnlyList<TextElement>
The lines, in display order.
Remarks
The TextOverlay section the lines are handed to draws them; the key menu has no position of its own.
Update(InputManager)
Reads the keyboard and updates the key menu, running the selected entry's action if one is chosen.
public bool Update(InputManager input)
Parameters
inputInputManagerThe input manager to read from.
Returns
- bool
true when a key belonging to this key menu was pressed, so a caller with several key menus can stop looking once one of them has claimed the key.
Remarks
Call once per frame. While expanded, Escape collapses the list without selecting anything.