Difference between revisions of "Script:System/menu.js"
From Spherical
(created from http://web.archive.org/web/20111117214905/http://www.spheredev.org/wiki/Menu.js_(system_script)) |
(No difference)
|
Latest revision as of 20:22, 2 August 2013
The system script menu.js provides a simple menu object.
Usage
RequireSystemScript("menu.js");
- object constructor Menu()
- properties of objects of Menu type:
- font - a font object used by the menu. Defaults to the system font if not specified.
- window_style - a window style object used as a menu background. Defaults to the system window style if not specified.
- arrow - an image object representing the menu arrow image. Defaults to the system arrow if not specified.
- up_arrow - an image object to be used as an up arrow if the menu content exceeds the window. Defaults to the system up arrow if not specified.
- down_arrow - an image object to be used as a down arrow if the menu content exceeds the window. Defaults to the system down arrow if not specified.
- escape_function - a function to be called when the escape key is pressed.
- methods of objects of Menu type:
- addItem(name, callback [, color]) - adds an entry to the menu. The string 'name' specifies the menu text, 'callback' is a function to be called when the item is selected and 'color' is a color object defining the color of the item. If the color remains unspecified it is set to white.
- execute(x, y, width, height) - this displays and executes the menu. The arguments 'x', 'y', 'width' and 'height' define the size of the menu window. If the menu items don't fit into the window, the menu will be scrollable.
- properties of objects of Menu type:
Example
RequireSystemScript("menu.js");
var MainMenu = new Menu();
MainMenu.addItem("Items", ItemMenu);
MainMenu.addItem("Equip", EquipMenu);
MainMenu.addItem("Magic", MagicMenu);
MainMenu.addItem("Stats", StatsMenu);
MainMenu.addItem("Quit", Exit);
MainMenu.execute(20, 20, 50, 60);
The following functions are called if their respective menu item is selected. They have to be defined for the above to work. Exit(), which is called by the last menu item, doesn't need to be defined, because it is a function of the Sphere API.
function ItemMenu() {
//...
}
function EquipMenu() {
//...
}
function MagicMenu() {
//...
}
function StatsMenu() {
//...
}
Notes
- The menu arrow is moved using the up and down directional keys. A selection is always confirmed with enter. Joystick input is not supported.
- If necessary the up and down arrows will be displayed near the right border of the menu window.
- To specify a function as an escape_function or callback function you may not use brackets.
/* This won't work: */ MyMenu.escape_function = DoSomething(); MyMenu.addItem("New Game", NewGame());
/* This will work fine: */ MyMenu.escape_function = DoSomething; MyMenu.addItem("New Game", NewGame);
- Menu.js automatically includes the system script colors.js.
- A tutorial can be found in the docs/system_scripts/ directory of Sphere.
- The famous flik_menu.js is not a system script, but works similar to menu.js. It allows you to do much more though.