Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

How to add the AIC menu

Arawn Davies edited this page Oct 13, 2017 · 2 revisions

It is very easy to add a menu to your operating system.
As the AIC Framework is written in C#, along with the majority of Cosmos operating systems, we will be using C# in this tutorial.

First things first, you need to include the AIC_Framework assembly in your operating system.
If you haven't yet done this, look at the how to page on the right.

Once that's done, you can proceed. In a class file of your operating system, add the following code to your using statements located at the top.

using AIC_Framework;
// Use "menu" as the static AIC_Framework.Core.Menu class
using menu = AIC_Framework.AConsole.Menu;

This will allow you to use AIC methods in your operating system.

Next, in the method which your menu will run, (in this case the main Run method in Kernel.cs), call the menu.Reset() method, which initializes the menu by resetting variables such as the colours used for the menus, the categories and buttons used in the menu.

protected override void Run()
{
    // Initialize the menu
    menu.Reset();
}

Once the menu has been reset, you can begin to add categories to the menu to be displayed.
In each category, you can have multiple entries, "buttons" which can be selected and entered by the user.
Each entry can then execute a command, by calling another method.

    // Create a new category: catTest
    menu.Category catTest = new menu.Category("Test");
    catTest.AddEntry(new TestCommand());

    // Create a new category: catHello
    menu.Category catHello = new menu.Category("Hello");
    catHello.AddEntry(new HelloWorld());

Once the categories have been created and the entries defined, you need to define categories and add the categories to the menu.
Remember to define the categories and the entries before adding them to the menu.

Just before the current class, but in the same file, add the following code:

    #region Entries
    // A test entry

    // Creates a new class which inherits the methods from menu.Entry
    public class TestCommand : menu.Entry
    {
        // Defines what text this entry should display
        public TestCommand() { this.text = "Test Command"; }

        // Defines what the entry is supposed to do when selected
        public override void Execute()
        {
            Console.Clear();
            Console.WriteLine("This is an entry which is executed after being selected");
            // Place the code that should be executed here
        }
    }

    // A hello world entry - same as above, just repeated differently for demonstration purposes
    public class HelloWorld : menu.Entry
    {
        public HelloWorld() { this.text = "Hello World!"; }
        public override void Execute()
        {
            Console.Clear();
            Console.WriteLine("See? Different to the TestCommand entry but it inherits common items");
            Console.WriteLine("Make it say whatever you want!");
            // Place the code that should be executed here
        }
    }

    #endregion

Now, later in the code where you created the categories, in the same method add the following code.
This adds the created categories to the menu, along with their entries:

    // Add the categories to the menu
    menu.AddCategory(catTest);
    menu.AddCategory(catHello);

Last of all, but most importantly, you need to show the menu!
Otherwise, the user would just be presented by a black screen with nothing to do.

    // Show the menu
    menu.Show();

Once all of this is done, your menu file should look something similar to this:

using System;
using System.Collections.Generic;
using sys = Cosmos.System;
using AIC_Framework;

// Use "menu" as the static AIC_Framework,AConsole.Menu class
using menu = AIC_Framework.AConsole.Menu;

namespace YourOS
{
    public class Kernel : sys.Kernel
    {
        protected override void BeforeRun()
        {
            Console.WriteLine("Press any key to start");
            Console.ReadKey();
        }

        protected override void Run()
        {
            // Initialize the menu
            menu.Reset();

            // Create a new category: catTest
            menu.Category catTest = new menu.Category("Test");
            catTest.AddEntry(new TestCommand());

            // Create a new category: catHello
            menu.Category catHello = new menu.Category("Hello");
            catHello.AddEntry(new HelloWorld());

            // Add the categories to the menu
            menu.AddCategory(catTest);
            menu.AddCategory(catHello);

            // Show the menu
            menu.Show();
        }
    }

    #region Entries

    // A test entry
    public class TestCommand : menu.Entry
    {
        public TestCommand() { this.text = "Test Command"; }
        public override void Execute()
        {
            // Place the code that should be executed here
        }
    }

    // A hello world entry
    public class HelloWorld : menu.Entry
    {
        public HelloWorld() { this.text = "Hello World!"; }
        public override void Execute()
        {
            // Place the code that should be executed here
        }
    }

    #endregion
}

Well done! You can now create menus for your operating system and add categories to sort out commands and assign specific methods to be called by each command. Happy creating!

Clone this wiki locally