This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Paullie's profile picture

Basic Computer Coding - Menu

Started by Paullie, 19 February 2015 - 08:24 PM
Paullie #1
Posted 19 February 2015 - 09:24 PM
Hi, thanks for reading this to begin with, I am here today to give you a bit of code which should help if you want to create a programme which has multiple menus and multiple selections

here is the code and thank you for taking the time to read it


-- Menu Program
-- Coded and Produced by Paullie
-- www.youtube.com/user/AllGamesWide
--Menu Selection
local tSel = {
"Test 1",
"Test 2",
"Test 3"
}
-- Starting Co-Ords for the Table
local startX = 2
local startY = 2

-- Menu to create/print
-- and everything else
local function startMenu(tMenu, sTitle)
local function printMenu(tMenu, sTitle, nSelected)
  for index, text in pairs(tMenu) do
   term.setCursorPos(startX, startY + index - 1)
   write( (nSelected == index and '[' or ' ') .. text .. (nSelected == index and ']' or ' ')  )
  end --For Index
end --Function printMenu

--Setting Default Selection for Menu
local selection = 1
--Creating the loop till enter is entered
while true do
  printMenu(tMenu, sTitle, selection)
   event, button = os.pullEvent("key")
	if button == key.up then
	 --Up button pressed
	 selection = selection - 1
	elseif button == key.down then
	 --Down button pressed
	 selection = selection + 1
	elseif button == key.enter then
	 --Enter button pressed
	 --To return text and number of option entered
	 return tMenu[selection], selection
	end --If function
  
	 --this adds the automated increase for the table
	 selection = selection < 1 and #tMenu or selection > #tMenu and 1 or selection
   end --printMenu function
end --while function
--Clearing the terminal
term.clear()
--Starting the whole process
sOptions, nNumb = startMenu(tSel)
term.clear()
term.setCursorPos(1,1)
--Part of the code which controls what happens upon enter and which option
if nNumb == 1 then
--shell.run("programme name")
  elseif nNumb == 2 then
	elseif nNumb == 3 then
  end --nNumb 3
end --nNumb 2
end --nNumb 1
Edited on 19 February 2015 - 08:25 PM
Lyqyd #2
Posted 19 February 2015 - 10:03 PM
Moved to Programs.
Zareks #3
Posted 22 February 2015 - 05:32 PM
I plan on using this for an airship controller program i am developing. I appreciate the tutorial!
Creator #4
Posted 23 February 2015 - 08:13 PM
Nice. I use this:


function dialogBox(tableInput,clearBg)
if clearBg ~= false then
clear(colors.cyan)
end
local nTable = {}
dialogBoxWindow = window.create(
tableInput[1],
tableInput[2],
tableInput[3],
tableInput[4],
tableInput[5])
dialogBoxWindow.setBackgroundColor(tableInput[9])
dialogBoxWindow.setCursorPos(2,1)
dialogBoxWindow.clearLine()
dialogBoxWindow.setTextColor(tableInput[8])
dialogBoxWindow.write(tableInput[7])
dialogBoxWindow.setCursorPos(1,2)
dialogBoxWindow.setBackgroundColor(tableInput[12])
for i = 2 , tableInput[5] do
dialogBoxWindow.setCursorPos(1,i)
dialogBoxWindow.clearLine()
end
dialogBoxWindow.setCursorPos(1,2)
dialogBoxWindow.setTextColor(tableInput[11])
for token in string.gmatch(tableInput[10],"[^%%]+") do
nTable[#nTable+1] = token
end
for i,v in pairs(nTable) do
dialogBoxWindow.setCursorPos(2,1+i)
dialogBoxWindow.write(v)
end
local totalLenght = 0
globalButtonList = {}
for i,v in pairs(tableInput[13]) do
dialogBoxWindow.setCursorPos(2+totalLenght,tableInput[5]-1)
dialogBoxWindow.setTextColor(v[2])
dialogBoxWindow.setBackgroundColor(v[3])
local toWrite = " "..v[1].." "
dialogBoxWindow.write(toWrite)
if globalButtonList == nil then
globalButtonList = {{tableInput[2]+1+totalLenght,tableInput[3] + tableInput[5]-2,tableInput[2]+totalLenght + #toWrite,tableInput[3] + tableInput[5]-1,v[4]}}
else
globalButtonList[#globalButtonList+1] = {tableInput[2]+1+totalLenght,tableInput[3] + tableInput[5]-2,tableInput[2]+totalLenght + #toWrite,tableInput[3] + tableInput[5]-1,v[4]}
end
totalLenght = #toWrite + totalLenght + 2
end
local repeatIt = true
while repeatIt == true do
local unparsedResult = detectButtonHit(globalButtonList)
result = unparsedResult[1]
if result ~= "pong" then
repeatIt = false
end
end
return result
end

With this as argument:


local folderMenu = {
term.current(),
2,
math.floor((h-6)/2),
48,
6,
true,
"Folder Options",
colors.white,
colors.blue,
"What would you%like to do?",
colors.black,
colors.lightGray,
{
  {
   "Open",
   colors.black,
   colors.blue,
   "open",
  },
  {
   "Delete",
   colors.black,
   colors.blue,
   "delete",
  },
  {
   "Rename",
   colors.black,
   colors.blue,
   "rename",
  },
  {
   "Move",
   colors.black,
   colors.blue,
   "move",
  },
  {
   "Cancel",
   colors.black,
   colors.blue,
   "cancel",
  },
},
}

With this to handlle the events:


function detectButtonHit(buttonsToTest)
local event, button, x, y
repeat
  event, button, x, y = os.pullEvent()
until event == "mouse_click" or event == "key"
if event == "mouse_click" then
  for i,v in pairs(buttonsToTest) do
   if v[1] <= x and x <= v[3] and v[2] <= y and y <= v[4] then
    return {v[5], button}
   end
  end
elseif event == "key" then
  return {"key:"..tostring(button)}
end
return {"pong"}
end

With this in the beggining:


local globalButtonList = {}