http://wiki.spheredev.org/index.php?title=Script:System/menu.js&feed=atom&action=historyScript:System/menu.js - Revision history2024-03-29T16:00:17ZRevision history for this page on the wikiMediaWiki 1.29.0http://wiki.spheredev.org/index.php?title=Script:System/menu.js&diff=1001&oldid=prevApollolux: created from http://web.archive.org/web/20111117214905/http://www.spheredev.org/wiki/Menu.js_(system_script)2013-08-02T20:22:39Z<p>created from http://web.archive.org/web/20111117214905/http://www.spheredev.org/wiki/Menu.js_(system_script)</p>
<p><b>New page</b></p><div>The [[Script:system|system script]] [[Script:system/menu.js|menu.js]] provides a simple menu object.<br />
<br />
__TOC__<br />
<br />
==Usage==<br />
{{usage|func=[[API:RequireSystemScript|RequireSystemScript]]|params="menu.js"}}<br />
<br />
==Variables and functions of menu.js==<br />
* object constructor '''Menu()'''<br />
** properties of objects of Menu type:<br />
*** '''font''' - a [[API:Font|font object]] used by the menu. Defaults to the [[API:GetSystemFont|system font]] if not specified.<br />
*** '''window_style''' - a [[API:WindowStyle|window style object]] used as a menu background. Defaults to the [[API:GetSystemWindowStyle|system window style]] if not specified.<br />
*** '''arrow''' - an [[API:Image|image object]] representing the menu arrow image. Defaults to the [[API:GetSystemArrow|system arrow]] if not specified.<br />
*** '''up_arrow''' - an [[API:Image|image object]] to be used as an up arrow if the menu content exceeds the window. Defaults to the [[API:GetSystemUpArrow|system up arrow]] if not specified.<br />
*** '''down_arrow''' - an [[API:Image|image object]] to be used as a down arrow if the menu content exceeds the window. Defaults to the [[API:GetSystemDownArrow|system down arrow]] if not specified.<br />
*** '''escape_function''' - a function to be called when the escape key is pressed.<br />
** methods of objects of Menu type:<br />
*** '''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 [[API:Color|color object]] defining the color of the item. If the color remains unspecified it is set to white.<br />
*** '''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.<br />
<br />
==Example==<br />
<syntaxhighlight><br />
RequireSystemScript("menu.js");<br />
<br />
var MainMenu = new Menu();<br />
MainMenu.addItem("Items", ItemMenu);<br />
MainMenu.addItem("Equip", EquipMenu);<br />
MainMenu.addItem("Magic", MagicMenu);<br />
MainMenu.addItem("Stats", StatsMenu);<br />
MainMenu.addItem("Quit", Exit);<br />
<br />
MainMenu.execute(20, 20, 50, 60);<br />
</syntaxhighlight><br />
<br />
The following functions are called if their respective menu item is selected. They have to be defined for the above to work. [[API:Exit|Exit]](), which is called by the last menu item, doesn't need to be defined, because it is a function of the [[API:Functions|Sphere API]].<br />
<syntaxhighlight><br />
function ItemMenu() {<br />
//...<br />
}<br />
<br />
function EquipMenu() {<br />
//...<br />
}<br />
<br />
function MagicMenu() {<br />
//...<br />
}<br />
<br />
function StatsMenu() {<br />
//...<br />
}<br />
</syntaxhighlight><br />
<br />
==Notes==<br />
* The menu arrow is moved using the up and down directional keys. A selection is always confirmed with enter. Joystick input is not supported.<br />
* If necessary the up and down arrows will be displayed near the right border of the menu window.<br />
* To specify a function as an <var>escape_function</var> or <var>callback</var> function you may ''not'' use brackets.<br/><pre>/* This won't work: */&#10;MyMenu.escape_function = DoSomething();&#10;MyMenu.addItem("New Game", NewGame());</pre><pre>/* This will work fine: */&#10;MyMenu.escape_function = DoSomething;&#10;MyMenu.addItem("New Game", NewGame);</pre><br />
* Menu.js automatically includes the [[Script:System/colors.js|system script colors.js]].<br />
* A tutorial can be found in the {{dir|docs/system_scripts/}} directory of Sphere.<br />
* The famous [[Script:flik_menu.js|flik_menu.js]] is not a system script, but works similar to menu.js. It allows you to do much more though.<br />
<br />
==External links==<br />
* [http://sphere.sourceforge.net/flik/docs/sphere-menu.html Flik's Sphere Central &rarr; Docs : How to use Sphere's default menu object].<br />
<br />
[[Category:Scripts]]</div>Apollolux