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

Menu with Sub menus problems (pulling my hair out here :( )

Started by Diakatarnis, 09 September 2012 - 10:35 PM
Diakatarnis #1
Posted 10 September 2012 - 12:35 AM
Hi Guys,

I want to make a menu with sub menus but im having problmes displaying the second menu and getting it to return the selected item correctly.

I've been fiddling and although i can get it to go to the second menu now, it doesnt seem to pay any attention to the selection process :D/>/>


term.clear()
function reactor1()
while true do
term.setCursorPos(18,4)
	 local sel = 1
	 local list = {"Shutdown R1", "Shutdown R2"}
	 local offX,offY = term.getCursorPos()
	 local curX,curY = term.getCursorPos()
	 while true do
		 if sel > #list then sel = 1 end
		 if sel < 1 then sel = #list end
		 for i = 1, #list do
			  term.setCursorPos(offX,offY+i-1)
			  if sel == i then
				 print("["..list[i].."]") --customise for menu border
			  else
				 print(" "..list[i].." ") --same again
			  end
		 end
		 while true do
			  local e,e1,e2,e3,e4,e5 = os.pullEvent()
			  if e == "key" then
				 if e1 == 200 then --up key
				    sel = sel-1
				    break
				 end
				 if e1 == 208 then --down key
				    sel = sel+1
				    break
				 end
				 if e1 == 28 then --enter key
				    term.setCursorPos(curX,curY)
				    return list[sel],sel
				 end
			  end
		   end
	    end
	 end
term.setCursorPos(14,1)
print("Please select Option")
local selection = reactor1()
if selection == "Shutdown R1" then
   term.clear()
   print("Sad Panda")
   sleep(5)
   reactor1()
elseif selection == "Shutdown R2" then
   menu()
end
end
while true do
function menu()
term.setCursorPos(18,4)
	 local sel = 1
	 local list = {"Reactor 1 Control", "Hardware","Exit"}
	 local offX,offY = term.getCursorPos()
	 local curX,curY = term.getCursorPos()
	 while true do
		 if sel > #list then sel = 1 end
		 if sel < 1 then sel = #list end
		 for i = 1, #list do
			  term.setCursorPos(offX,offY+i-1)
			  if sel == i then
				 print("["..list[i].."]") --customise for menu border
			  else
				 print(" "..list[i].." ") --same again
			  end
		 end
		 while true do
			  local e,e1,e2,e3,e4,e5 = os.pullEvent()
			  if e == "key" then
				 if e1 == 200 then --up key
				    sel = sel-1
				    break
				 end
				 if e1 == 208 then --down key
				    sel = sel+1
				    break
				 end
				 if e1 == 28 then --enter key
				    term.setCursorPos(curX,curY)
				    return list[sel],sel
				 end
			  end
		   end
	    end
	 end
term.setCursorPos(14,1)
print("Please select Option")
local selection = menu()
if selection == "Reactor 1 Control" then
   term.clear()
   reactor1()
elseif selection == "Exit" then
   term.clear()
   term.setCursorPos(1,1)
   break
end
end

I've used some code kindly placed on the forums, but it only seems to deal with a single menu, as soon as i try to do sub menus it all goes a little pear shaped, any suggestions would be greatly appreaciated.

Diak
KaoS #2
Posted 11 September 2012 - 06:40 AM
take a look at the function I made for it http://pastebin.com/X9cC2MS3

hope it helps :D/>/>