Skip to content

Widgets & Menus

Jacob-Mc-kenzie edited this page Jul 13, 2020 · 1 revision

An important part of this library is the ability to make GUIs easy on the console. This page will cover how to make your own Widget based menu and an intro to widgets.

What are widgets?

Widgets, which inherit from the Widget class are UI element templates. for example the Frame widget, this allows for the easy creation of windows within windows in a GUI, the benefit of this over just drawing a rectangle is that it can be interacted with in the same context of other widgets.

Usage

To use a widget it must be instantiated, then the appropriate Draw function must be called before pushing each frame.

static void Main(string[] args)
{
    Graphics graphics = new Graphics();
	//Declare the widget
    Widget frame = new Frame('#',new Rect(0, 5, 0, 5));
    //Continually draw frames
    while (true)
    {
        //update the frame
        frame.Draw(graphics);
        //now that all drawing is done, push the frame to the buffer.
        graphics.pushFrame();
    }
}

The Menu class

While manually specifying the order and how to update each widget in a window in every given frame, creating a menu inheriting from the Menu class can do a lot of the work for you.

Making a Menu

Note that Mouse support must currently still be done manually as the Menu class uses the old input method

To make a menu is really simple, just:

  1. create a class inheriting from Menu, and add widgets to the onPage list.
  2. In your main loop call Menu.StepFrame()
class ExampleMenu : Menu
{
    public ExampleMenu(Graphics graphics) : base(graphics)
    {
        onPage.Add(new Frame('#', new Rect(0, 5, 0, 5)));
        onPage.Add(new Textbox("This is some text", new Rect(1, 4, 1, 4)));
    }
}
static void Main(string[] args)
{
    Graphics graphics = new Graphics();
    ExampleMenu menu = new ExampleMenu(graphics);
    //Continually draw frames
    while (true)
    {
        //update the UI
        menu.StepFrame();
        //now that all drawing is done, push the frame to the buffer.
        graphics.pushFrame();
    }
}
Clone this wiki locally