Skip to content

menu manager and menus system

yoel123 edited this page Oct 6, 2020 · 15 revisions

the games view side is the menu manager and menus system. the big concept is that most console programs are just text menus that wait for input, not one menu, menus.

so for a single menu, we have a class called gconsole_menu , what it does is display a numbered menu and wait for user input. once it gets that input it saves it on an attribute called "next" (which holds the next input that the user just sent).

by default the gconsole_menu class menu_user_input method does nothing so gconsole_menu will display the menu again and wait for input, that's the menus loop.

to do something with the input stored on next we need to overite the menu_user_input method (see an example for that in almost any child class of gconsole_menu in screens).

gmenu_manger is the menu manager class. it holds all the menus on an HashMap, only one menu runs at a time. the menu manager also has a method to switch menus by their name (its easier to do this with a name rather than a position number in an array).

when you add a menu to the menu manager you also set its name like this:

gm.add_menu("start", tst);//(this is a line of code from ymain.java in the main method.)

the first argument is the menu name "start", the second is the instance of the menu (usually a child class of gconsole_menu).

to set start as the current menu we use this line:

gm.change_menu("start");

this will make the start menu run first.

inside each menu class there's also a handle to the game manager class on an attribute called "gm" that way we can change a menu from within a menu class instance like this:

//go to main game screen

gm.change_menu("game_main");

to start the program in main we simply call the menu manager run method:

gm.run();

in a single menu class (a child class of gconsole_menu ), the menu items are stored on a string array attribute called menu_items

it's usually initialized on the constructor of the menu class like this:

'

menu_items = new String[]{	"buy","sell","back"	}; 

'

how to get user input:

@Override public void menu_user_input() { super.menu_user_input(); //user input was 1 if(next.equals("1")) { //do somthing } }//end menu_user_input 

basically, you override menu_user_input method inside a gconsole_menu child class, the super menu_user_input puts the user input on an attribute called next, and to respond to it just check if next equals to the input your expecting, let's say 1 is buy, then call the buy method from market (check do_buy() in the market screen to see how it's done).

**if you want a message to be displayed after you get an input(after the menu updates and clears the screen) **you can put it on the message attribute:

messge = "bla"; 

otherwise, the screen gets cleared before the next menu update

you can also wait for empty input (like press enter to continue).

that's basically how this system works.

how not to output text using cio (and how you shod):

so the user chose your menu item or inside your event (etc etc), you got his input and want to output a response you write:

cio.print("bla bla");

but it doesn't appear and all you see is the current menu items.

that's because the screen gets cleared every update! and then it prints the current menu items.

how to avoid it? ask for another string input(after your output) "press enter to continue" if you have a gyinput instance on a variable called ui (for example):

ui.get_string("press enter to continue");

this will stop execution (and stop update from clearing the screen) until the user inputs anything.

if you're inside a menu object and want your text to appear above the menu use messge attribute:

messge = "bla bla"; 

it will be cleared the next update too (the menu waits for user input so it will happen after the user inputs something).

Clone this wiki locally