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
function yN()
local n=1
while true do
local x, y=term.getCursorPos()
term.clearLine()
if n==1 then write(">YES< NO") else write (" YES >NO<") end
term.setCursorPos(x, y)
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end
if n==1 then return true end
if n==2 then return false end
return false
end
what it does is:
Spoiler
function yN() --declare a function (note that I am not actually runningit anywhere)
local n=1 --declares the starting selected option (a 'yes')
while true do -- a loop, necessary for going from step4 to step 1
term.clearLine() --clears one line for writing
if n==1 then write("[YES] NO") else write (" YES [NO]") end --writes options with one highlighted
a, b=os.pullEvent("key") --waits for a key press
if b==203 and n==2 then n=1 end --if pressed arrow left and selected option is 'no' then select 'yes'
if b==205 and n==1 then n=2 end --if pressed arrow right and selected option is 'yes' then select 'no'
if b==28 then print("") break end --if pressed enter then break the loop and execute the code outside it
end --close the loop code
if n==1 then return true end --if chosen 'yes' then return true
if n==2 then return false end --if chosen 'no', return false
end --close the function
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.