Jump to content




A quick guide through menu making


  • This topic is locked This topic is locked
42 replies to this topic

#1 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 18 March 2012 - 12:22 PM

Okay, so you have come here to look for a quick way to set up a menu? Good. Before we start, please note that I will first show an algorithm, then the code and finally an explanation. Now we can begin.

1. Yes/No Menu

First and easiest type of menu is a yes/no menu. We need it to act like:
1. Print "Yes No" with one option highlighted, like ">Yes< No"
2. Wait for key pressed.
3. If pressed enter return yes (true) or no (false) and end program
4. If pressed arrow left/right highlight the correct option and go to Step 1.


okay, here is the code:
Spoiler

what it does is:

Spoiler

2. Customizable Menu


Okay, it's now time for the more advanced stuff. Say, you don't want all your menus to only have yes/no options, right? Now I will show you how to make a better version.
1. Get the list of options, preferably a table.
2. Print those options.
3. Select the first one.
4. If pressed arrow up go one option up and go to step#2.
5. If pressed arrow down do the reverse and go to step#2.
6. If pressed enter end and return selected option.


Code:
function CUI(m)
n=1
l=#m
while true do
term.clear()
term.setCursorPos(1,2)
for i=1, l, 1 do
if i==n then print(i, " ["..m[i].."]") else print(i, " ", m[i]) end
end
print("Select a number[arrow up/arrow down]")
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
And explanation:
function CUI(m) --declare function
n=1 --declare selected option
while true do --start a loop for the 'go to step#2' part
term.clear() term.setCursorPos(1,2) --clear the sceen and position the cursor
for i=1, #m, 1 do --traverse the table of options
if i==n then print(i, " ["..m[i].."]") else print(i, " ", m[i]) end --print them
end
a, b= os.pullEvent("key") --wait for keypress
if b==200 and n>1 then n=n-1 end --arrow up pressed, one option up
if b==208 and n<=l then n=n+1 end --arrow down pressed, one option down
if b==28 then break end --enter pressed, break the loop
end
term.clear() term.setCursorPos(1,1) --clear screen
return n --return the value
end

Please note that the above function requires to be called with a table, like this:
local options={
"option1",
"option2",
"option3"
}
local n=CUI(options)
print(n)

and returns the number of the option chosen.

Do you want me to make some other types of menus as well? If so, post them below.

#2 jtdavis99

  • New Members
  • 39 posts

Posted 18 March 2012 - 11:41 PM

How did you know what codes were equivalent to the keys?

#3 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 19 March 2012 - 12:00 AM

View Postjtdavis99, on 18 March 2012 - 11:41 PM, said:

How did you know what codes were equivalent to the keys?
http://www.minecraft.../wiki/Key_Codes

#4 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 19 March 2012 - 01:12 PM

Posted another version of menu. Hope this will be enough.

And for key codes I actually used trial and error, but espen solution is much more elegant :D/>

#5 Wesnc

  • New Members
  • 2 posts

Posted 19 March 2012 - 04:14 PM

I was messing around with your code a bit here, and its only showing option #2, I'll post snippets of the code.


for i=1, l, 1 do
if i==selection then print("["..options[i].."]" .. " " .. options[i]) else print(options[i] .. " ["..options[i].."]") end
end


local optionsTable = {
"OPEN",
"CLOSE"
}

Those are pretty much the only edits, besides keys. Issue is it only shows option 2 ex:

Quote

CLOSE [CLOSE]
What have I did wrong here?


EDIT: I fixed it, I think. kinda defeats the purpose of the whole thing though, just a quick fix, not a proper one.
if i==selection then print("["..options[1].."]" .. " " .. options[2]) else print(options[1] .. " ["..options[2].."]") end

It was throwing the selection numbers off too, so I fixed that for now
 return selection-1 
EDIT: a stupid mistake, a mis-arrangement of print, fixed.

Help still needed, if this is NOT the correct way of doing it(obviously)

I'm also looking into having multiple selections, by that I mean something like this:

Quote

Door1 - OPEN CLOSE
Door2 - OPEN CLOSE
Door3 - OPEN CLOSE
and even making use of nested tables for a selection menu such as that.
Not sure how I would get into the nested table and the table itself. Was thinking for the table to look like this:
optionsTable = {
["Door1"] = {
"OPEN"
"CLOSE"
},
["Door2"] = {
"OPEN"
"CLOSE"
}
}

Mainly, I need to get the string within ["Door1"] and the nested table, unsure how to do this in LUA.

and I know there are easier ways to do this, this is just the way I would prefer. less code clutter

#6 Ian-Moone

    The Germ

  • New Members
  • 124 posts
  • LocationLiverpool (no I am not a Scouser Im form Holland)

Posted 21 March 2012 - 12:26 PM

really helpful thanks :(/>
this will speed up development of my programs so much
ps.ill give you a mention in the credits when and where i use it

#7 fuj1n

  • New Members
  • 4 posts

Posted 25 March 2012 - 03:42 AM

View Postjtdavis99, on 18 March 2012 - 11:41 PM, said:

How did you know what codes were equivalent to the keys?

View PostEspen, on 19 March 2012 - 12:00 AM, said:

View Postjtdavis99, on 18 March 2012 - 11:41 PM, said:

How did you know what codes were equivalent to the keys?
http://www.minecraft.../wiki/Key_Codes
What I Do Is Simply Create A Computer Program Where It Does The Following:
Spoiler
Simple And Easy

#8 Alex_

  • Members
  • 63 posts
  • LocationPontefract, West Yorkshire, England

Posted 25 March 2012 - 07:41 PM

Thank You :(/>

#9 Aerik

  • New Members
  • 15 posts

Posted 06 April 2012 - 06:43 PM

Loved it, thanks a lot!

Is it possible to create a menu where it asks you to enter several variables, for things such as, say, a set of coodinates, or the depth, width and heigth of a room to dig/build?

#10 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 06 April 2012 - 07:27 PM

View PostAerik, on 06 April 2012 - 06:43 PM, said:

Loved it, thanks a lot!

Is it possible to create a menu where it asks you to enter several variables, for things such as, say, a set of coodinates, or the depth, width and heigth of a room to dig/build?
Sure that's possible! Just use read() :)/>

#11 Aerik

  • New Members
  • 15 posts

Posted 06 April 2012 - 07:40 PM

Lol, yeah, I'd just figured that out myself :)/>
Thanks anyway!

#12 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 06 April 2012 - 07:57 PM

I may do a menu for this later on as well.

#13 libraryaddict

  • New Members
  • 195 posts

Posted 14 May 2012 - 10:50 AM

Created a menu API featuring unlimited menu selections here : http://www.computerc...enu-scrollable/

Its scrollable :
However it deletes everything else on the screen atm >.>

#14 giangigzu

  • New Members
  • 1 posts

Posted 13 July 2012 - 02:48 AM

Hello, i was hoping you could help me with a little menu that i've been trying to learn how to do. its suppossed to have three options: Pump-Tank, Tank-Gen., Gen-MFE.
when one of them is selected then it should open a sub-menu that actualy asks me if i want to turn the feature on. i only need this base and i will later add the bundled cable outputs myself. Please send me a message with the finished code, if i figure it out first ill tell u. THANK YOU!

#15 Graypup

  • Members
  • 90 posts

Posted 29 July 2012 - 06:05 PM

My only use for a menu is an installer, but this is well explained enough for me to bother with a menu at all.

#16 Brodur

  • Members
  • 21 posts
  • LocationCanada, eh?

Posted 29 July 2012 - 06:32 PM

How do you add an action to the options? I tried
local options={
"Launch VaulTec",
"Continue to CraftOS 1.3"
}
local n=CUI(options)
print(n)
if n=1 then
sleep(0.1)
shell.run "vault"
else
shell.run "shell"
end

and this doesn't work, so how do I add an action to the option? Please help!

#17 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 02 August 2012 - 03:52 PM

Another REALLY simple menu is just this:
function newMenu()
term.setCursorPos(1,1)
print("Please make your selection now")
term.setCursorPos(1,9)
write("Selection: ")
end
function selection()
print("1. Selection1")
print("2. Selection2")
--you can add more selections if you want
term.setCursorPos(11,9) -- Resets the cursor back to the end of the selection line. Can be changed to any cursor position.
end
-- Now, the actual code
term.clear()
newMenu()
selection()
local input = read() --can add (*) if using passwords
if input == "1" then --can also be anything else to designate the selection
-- Do stuff here
elseif input == "2" then
--Do other stuff here
else
  print("INVALID SELECTION. PLEASE TRY AGAIN")
  sleep(1)
  os.reboot() --only necessary if using this as a startup code.
end
Nothing fancy, but easily modified to add as manyy selections as you can fit onto a screen.

#18 BigSHinyToys

  • Members
  • 1,001 posts

Posted 02 August 2012 - 04:16 PM

well seeing as I have released programs with this menu Open Source I am throwing down. You may recognize it from some of my progs (probably not)
Spoiler
This menu will display on the screen from where ever the cursor is last at.

#19 Xhisor

  • New Members
  • 37 posts
  • LocationSweden

Posted 06 August 2012 - 04:04 PM

View PostBigSHinyToys, on 02 August 2012 - 04:16 PM, said:

well seeing as I have released programs with this menu Open Source I am throwing down. You may recognize it from some of my progs (probably not)
Spoiler
This menu will display on the screen from where ever the cursor is last at.
Oh i've been looking for a good way to make a nice menu, can i use (and modify) your menu in my turtle program? You will get credited of course!

#20 BigSHinyToys

  • Members
  • 1,001 posts

Posted 06 August 2012 - 04:58 PM

View PostXhisor, on 06 August 2012 - 04:04 PM, said:

-- snip --
Oh i've been looking for a good way to make a nice menu, can i use (and modify) your menu in my turtle program? You will get credited of course!
You can use,modify,set fire too,detonate or anything else you can think of to that program. While i would appreciate crediting a simple note in the code will suffice but is not required.

I take it a compliment that out of all the above menu systems you chose mine so Thank you.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users