Log In  

Cart [#51325#] | Code | 2018-04-05 | License: CC4-BY-NC-SA | Embed

(Note: I already pushed it a few days ago, but I deleted it by accident^^)

Hello,
Just programmed a simple "Menu Manager" and I thought, it can be useful for others. :)

It presents a way to write your own Menu, moving a cursor with Arrow-Keys (Up/Down), select an Entry (X) and call a Function.

The Sourcecode is well documented.

EXAMPLE

menu={}

function menu.update() --Copy from Cartridge
function menu.draw() --Copy from Cartridge

function _update()
 menu.update()
end

function _draw()
 menu.draw()
end

function entry_one()
 cls()
 print("entry one selected!")
end

function entry_two()
 cls()
 print("entry two selected!")
end

function entry_x()
 cls()
 print("entry x selected!")
end

cls()
menu.entries = {
 "text entry one", entry_one,
 "text entry two", entry_two,
 "text entry x", entry_x}
cursor(26,40)
menu.start()

Again, the Sourcecode is well documented :)

HOW TO USE IT

  1. Make sure you place
    menu={}

    on your Sourcecode. Not in a function, not even in _init(), it must be placed on the "highest level".

  2. Copy every function, that starts with "menu.", from that Cartridge to your Sourcecode. Thats
    function menu.start()
    function menu.update()
    function menu.draw()

  3. Make sure to call the "update"- and "draw"-function in _update() and _draw(). Don't worry, these Functions won't do anything, they only react after calling another Function ("menu.start()"):
    function _update()
    menu.update()
    end

    function _draw()
    menu.draw()
    end

  4. Set your Screen-Position using "cursor" (it must be done at least once).
    cursor(0,0)

  5. Fill up the "menu.entries"-Array, representing your Menu.
    That Array contains at least two Values: Value "One" is the shown Text, Value "two" is a function that will be called, if player selects the Text. This goes on for Value "Three" (Text), Value "Four" (function) and so on...
    menu.entries = {
    "start game", start_game,
    "options", show_options,
    "credits", show_credits,
    }

6. Make sure these functions at least exist, otherwise the Menu wouldn't shown properly:
function start_game()
[...more code...]
end
[...same for "show_options" and "show_credits"...]

  1. Finally, call that Menu:
    menu.start()

SOME MORE OPTIONS
You can change some Variables before calling "menu.start()":

menu.cursym = ">"

Change the Symbol used as Cursor.

menu.curclr = 10

The same for the used Cursor-Color.

menu.backclr = 0

Color to remove the Cursor-Symbol after moving it.

menu.cancel = [function]

A function that will be called, if the Player press Button "4". If this is set to "nil", nothing will happened.

menu.pos

Use it after calling "menu.start()". You can set the preselected Entry with this Variable. 0 means the first Entry, 1 the Second Entry and so on...


All this code is free to take, use, change, adapt, or whatever.

As said, I thought it can be useful for others^^.

P#51326 2018-04-05 03:46 ( Edited 2018-04-05 07:54)


[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-10-20 03:25 | 0.013s | 2097k | Q:17