//Menu Tutorial from http://wiki.spheredev/org/Making_a_Menu function Menu() //Initializes the starting data used to create the menu { if (!this instanceof Menu) return new Menu(); this.windowstyle = LoadWindowStyle("main.rws"); this.font = LoadFont("myFont.rfn"); this.arrow = GetSystemArrow(); this.upArrow = GetSystemUpArrow(); this.downArrow = GetSystemDownArrow(); this.items = []; this.preRender = function() {} } Menu.prototype.addItem = function(name, description, callback, color) { if (color == undefined) color = CreateColor(255, 255, 255); var item = { name: name, desc: description, callback: callback, color: color }; this.items.push(item); } Menu.prototype.execute = function(x, y, w, h) { var font_height = font.getHeight(); //returns an error message "font is not defined" var max_shown = Math.floor(h / font_height); var offset = 0; var selection = 0; while(!IsKeyPressed(KEY_ESCAPE)) { this.preRender(); this.windowstyle.drawWindow(x, y, w, h) for (var i = 0; i < max_shown; ++i) { if (i >= items.length) continue; //limit what is drawn this.font.setColorMask(this.items[i+offset].color); this.font.drawText(x+16, y + i * font_height, this.items[i+offset]); } this.arrow.blit(x, y + (selection-offset) * font_height); if (offset > 0) this.upArrow.blit(x + w - this.upArrow.width, y); if (offset = max_shown < this.items.length) this.downArrow.blit(x + w - this.downArrow.width, y + font_height * max_shown - this.downArrow.height); FlipScreen(); while(AreKeysLeft()) { switch(GetKey()) { case KEY_UP: if (selection > 0) { selection--; if (selection < offset) offset--; } break; case KEY_DOWN: if (selection < this.items.length-1) { selection++; if (selection>= offset + max_shown) offset++; } break; case KEY_ENTER: this.items[selection].callback(); return; break; case KEY_ESCAPE: return; break; } } } }