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

Help turning a program into a menu?

Started by Tassyr, 01 April 2013 - 10:21 PM
Tassyr #1
Posted 02 April 2013 - 12:21 AM
So here's the situation; I have a reactor control program that I've been tinkering with and I really like. but I'd like to make it an actual 'menu' with scroll up/down options instead of just 'input the number to get the effect.' Here's what I have so far: (And yes, this is a 'fallout' based world. XD)
Spoiler

c = colors.combine(colors.red,colors.black,colors.white,colors.green)
white = false
black = false
green = false
red   = false
rs.setBundledOutput("back", c)

while true do
  term.clear()
  term.setTextColor(colors.lime)
  term.setCursorPos(1,1)
  print("**************************************************")
  print("***** VAULT-TEC REACTOR MK 4 CONTROL STATION *****")
  print("*****	   AUTHORIZED PERSONEL  ONLY!	   *****")
  print("**************************************************")
  i=1
  while i <= 13 do
  term.setCursorPos(1,i+4)
  print("*****										*****")
  i=i+1
  end
  print("**************************************************")
  term.setCursorPos(7,15)
  print("**************************************************")
  term.setTextColor(colors.blue)
  term.setCursorPos(7,5)
  print(" [1] ACTIVATE	REACTORS 1 - 3")
  term.setCursorPos(7,6)
  print(" [2] DEACTIVATE  REACTORS 1 - 3")
  term.setCursorPos(7,7)
  print(" [3] ACTIVATE	REACTORS 4 - 6")
  term.setCursorPos(7,8)
  print(" [4] DEACTIVATE  REACTORS 4 - 6")
  term.setCursorPos(7,9)
  print(" [5] ACTIVATE	BREEDER CORE 1")
  term.setCursorPos(7,10)
  print(" [6] DEACTIVATE  BREEDER CORE 1")
  term.setCursorPos(7,11)
  print(" [7] ACTIVATE	BREEDER CORE 2")
  term.setCursorPos(7,12)
  print(" [8] DEACTIVATE  BREEDER CORE 2")
  term.setCursorPos(7,13)
  term.setTextColor(colors.red)
  print(" [0] -EMERGENCY-  CORE SHUTDOWN")
  term.setTextColor(colors.blue)	
  term.setCursorPos(7,17)
  print(" ENTER CHOICE: [ ]")
  term.setCursorPos(23,17)
  input = read()
  
  if input == "1" then
  c = colors.subtract(c, colors.white)
  rs.setBundledOutput("back",c)
  white = not white
  end
  if input == "2" then
  c = colors.combine(c, colors.white)
  rs.setBundledOutput("back",c)
  white = not white
  end
  if input == "3" then
  c = colors.subtract(c, colors.black)
  rs.setBundledOutput("back",c)
  black = not black
  end
  if input == "4" then
  c = colors.combine(c, colors.black)
  rs.setBundledOutput("back",c)
  black = not black
  end
  if input == "5" then
  c = colors.subtract(c, colors.red)
  rs.setBundledOutput("back",c)
  red = not red
  end
  if input == "6" then
  c = colors.combine(c, colors.red)
  rs.setBundledOutput("back",c)
  red = not red
  end
  if input == "7" then
  c = colors.subtract(c, colors.green)
  rs.setBundledOutput("back",c)
  green = not green
  end
  if input == "8" then
  c = colors.combine(c, colors.green)
  rs.setBundledOutput("back", c)
  green = not green
  end
  if input == "0" then
  c = colors.combine(colors.green,colors.red,colors.white,colors.black)
  green = false
  red = false
  black = false
  white = false
  rs.setBundledOutput("back", c)
  end
end
http://pastebin.com/YKYnazTS

So, how do I make this a menu where you scroll up and down over the options, instead of having to type something in manually? the examples I find online don't seem to be much help.
Engineer #2
Posted 02 April 2013 - 12:54 AM
Why dont you make it simple?
Just make them press the button:

local _, char = os.pullEvent("char")
if char == "1" then
  -- code
elseif char == "2" then
  -- code
end

Really to modify your code, you have to remove the input = io.read() and replace with code above. Only you must change char into input, otherwise it will mess up you if-statements.
Tassyr #3
Posted 02 April 2013 - 01:05 AM
Well, that's one way- but I've seen menus where you could scroll up and down along the program, and it looked AWESOME- it's just that I can't figure out how to make one of those interfaces. I'll hold this in reserve, though! And experiment with i
LBPHacker #4
Posted 02 April 2013 - 01:08 AM
You want to run that menu on an advanced computer, right? (Cuz if so, I throw together a "menu API" or something like that - probably gonna make a tutorial about menus… Hmm…)
JokerRH #5
Posted 02 April 2013 - 02:42 AM


And to the Menue thingy:


local options = {
  {"Option1", func1},
  {"Option2", func2},
  {"OptionX", func3}
}
local selected = 1

function draw()
  --Set the position for the first option. Change the second number if you want a headline or so...
  term.setCursorPos(1, 1)

  --Iterate over any option and print then on the screen
  for ind, param in ipairs(options) do
	--Clear line
	term.clearLine()

	--Print with or without marker
	if selected == ind then
	  print(">"..param[1])
	else
	  print(" "..param[1])
	end
  end
end

function run()
  while true do
	--Pull the event
	local _, key = os.pullEvent("key")

	if key == keys.up and selected > 1 then
	  --Decrement selected.
	  selected = selected - 1
	elseif key == keys.down and selected < #options then
	  --Increment selected.
	  selected = selected + 1
	elseif key == keys.enter then
	  --Run the function saved in the second slot of the currently selected option.
	  options[selected][2]()
	  break
	end

	--Redraw the screen
	draw()
  end
end

draw()
run()

That's what I posted in the

Press any key to continue &amp; Scroll-Enter

thread
TheOddByte #6
Posted 02 April 2013 - 02:49 AM
Here's a little code for you..
I edited yours a little and added menu functions so you have to press up and down arrow to use it..
And enter to select an options ofcourse..
Here you go!
Spoiler


--Screen/Drawing functions
-----------------------------
w,h = term.getSize()
hW = w/2
hH = h/2  

function centerPrint(y,text)
  term.setCursorPos(w/2 - #text/2, y)
    term.write(text)
end

function drawAt(x,y,text)
 term.setCursorPos(x,y)
write(text)
end

function cleanScreen()
term.clear()
  term.setCursorPos(1,1)
 end


input = 1


function drawArt() 

c = colors.combine(colors.red,colors.black,colors.white,colors.green)
white = false
black = false
green = false
red   = false
rs.setBundledOutput("back", c)

  cleanScreen()
  term.setTextColor(colors.yellow)
  term.setBackgroundColor(colors.lime)
  print[[

      VAULT-TEC REACTOR MK 4 CONTROL STATION             
          AUTHORIZED PERSONEL  ONLY!                            

]]


 for i = 5,19 do
 term.setBackgroundColor(colors.lime)
  drawAt(1,i,"                                                   ")
   term.setBackgroundColor(colors.black)
  end

  if input == nil then
  input = 1
  end

  if input == 1 then
  selected =  " [1] ACTIVATE  REACTORS 1 - 3 "

  elseif input == 2 then
  selected = " [2] DEACTIVATE  REACTORS 1 - 3 "

  elseif input == 3 then
  selected = " [3] ACTIVATE  REACTORS 4 - 6 "

  elseif input == 4 then
  selected = " [4] DEACTIVATE  REACTORS 4 - 6 "

  elseif input == 5 then
  selected = " [5] ACTIVATE  BREEDER CORE 1 "

  elseif input == 6 then
  selected = " [6] DEACTIVATE  BREEDER CORE 1 "

  elseif input == 7 then
  selected = " [7] ACTIVATE  BREEDER CORE 2 "

  elseif input == 8 then
  selected = " [8] DEACTIVATE  BREEDER CORE 2 "

  elseif input == 9 then
  selected = " [0] -EMERGENCY-  CORE SHUTDOWN "
  end

 term.setBackgroundColor(colors.lime)    
  drawAt(7,13,"                                                   ")

  term.setBackgroundColor(colors.gray)
  term.setTextColor(colors.yellow)
   centerPrint(16,selected)
    term.setBackgroundColor(colors.black)




  options = {
"                                ",
" [1] ACTIVATE  REACTORS 1 - 3   ",
" [2] DEACTIVATE  REACTORS 1 - 3 ",
" [3] ACTIVATE  REACTORS 4 - 6   ",
" [4] DEACTIVATE  REACTORS 4 - 6 ",
" [5] ACTIVATE  BREEDER CORE 1   ",
" [6] DEACTIVATE  BREEDER CORE 1 ",
" [7] ACTIVATE  BREEDER CORE 2   ",
" [8] DEACTIVATE  BREEDER CORE 2 ",
" [0] -EMERGENCY-  CORE SHUTDOWN ",
"                                ",
}


for i = 1, #options do
term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.blue)
  drawAt(9,i + 4,options[i])
 end

 term.setTextColor(colors.red)
 drawAt(9,14," [0] -EMERGENCY-  CORE SHUTDOWN")
end




   function events()
   evt, but = os.pullEvent()
     if evt == "key" then

 if but == 200 then --Up arrow
 input = input + 1
 if input > 9 then
 input = 9
 end

 elseif but == 208 then --Down arrow
 input = input - 1
 if input < 1 then
 input = 1
 end

 elseif but == 28 then --Enter
 if input == 1 then
  c = colors.subtract(c, colors.white)
  rs.setBundledOutput("back",c)
  white = not white


  elseif input == 2 then
  c = colors.combine(c, colors.white)
  rs.setBundledOutput("back",c)
  white = not white


  elseif input == 3 then
  c = colors.subtract(c, colors.black)
  rs.setBundledOutput("back",c)
  black = not black


  elseif input == 4 then
  c = colors.combine(c, colors.black)
  rs.setBundledOutput("back",c)
  black = not black


  elseif input == 5 then
  c = colors.subtract(c, colors.red)
  rs.setBundledOutput("back",c)
  red = not red


  elseif input == 6 then
  c = colors.combine(c, colors.red)
  rs.setBundledOutput("back",c)
  red = not red


  elseif input == 7 then
  c = colors.subtract(c, colors.green)
  rs.setBundledOutput("back",c)
  green = not green


  elseif input == 8 then
  c = colors.combine(c, colors.green)
  rs.setBundledOutput("back", c)
  green = not green


  elseif input == 0 then
  c = colors.combine(colors.green,colors.red,colors.white,colors.black)
  green = false
  red = false
  black = false
  white = false
  rs.setBundledOutput("back", c)
  end

end 
   end
     end

 function main()
    while true do
      cleanScreen()
       drawArt()
     events() --Handles the menu stuff
 end
    end


main()

Hope it helped! :)/>
Tassyr #7
Posted 02 April 2013 - 08:38 AM
JokerRH - yeah, I saw that on your post, but for some reason it just didn't sink in. x.x I can't say why.
Hellkid-MUCH closer to what I was after! I have a few tweaks to make- I was hoping for one of those 'bracketed' selection menus, but you've given me enough of a template to figure out,

I broke something. x.x Could someone please take a look at this http://pastebin.com/fdzPg810 and tell me why for some reason the program won't change the redstone outputs now?


Spoiler



--Screen/Drawing functions
-----------------------------
w,h = term.getSize()
hW = w/2
hH = h/2  

function centerPrint(y,text)
  term.setCursorPos(w/2 - #text/2, y)
	term.write(text)
end

function drawAt(x,y,text)
term.setCursorPos(x,y)
write(text)
end

function cleanScreen()
term.clear()
  term.setCursorPos(1,1)
end


input = 1


function drawArt()

c = colors.combine(colors.red,colors.black,colors.white,colors.green)
white = false
black = false
green = false
red   = false
rs.setBundledOutput("back", c)

  cleanScreen()
  term.setTextColor(colors.lime)
  term.setBackgroundColor(colors.black) --title and title background color
  print[[

	  VAULT-TEC REACTOR MK 4 CONTROL STATION			
		  AUTHORIZED PERSONEL  ONLY!							

]]


for i = 5,19 do
term.setBackgroundColor(colors.black) -- MAIN BACKGROUND SPAM COLOR
  drawAt(1,i,"												   ")
   term.setBackgroundColor(colors.black)
  end

  if input == nil then
  input = 1
  end

  term.setBackgroundColor(colors.black) -- menu section background
  term.setTextColor(colors.red) -- menu section brackets
   term.setCursorPos(11,input + 5)
   print("[")
   term.setCursorPos(42,input + 5)
   print("]")
	term.setBackgroundColor(colors.black)




  options = {
"							  ",
"  ACTIVATE	REACTORS 1 - 3  ",
"  DEACTIVATE  REACTORS 1 - 3  ",
"  ACTIVATE	REACTORS 4 - 6  ",
"  DEACTIVATE  REACTORS 4 - 6  ",
"  ACTIVATE	BREEDER CORE 1  ",
"  DEACTIVATE  BREEDER CORE 1  ",
"  ACTIVATE	BREEDER CORE 2  ",
"  DEACTIVATE  BREEDER CORE 2  ",
"  -EMERGENCY-  CORE SHUTDOWN  ",
"							  ",
}


for i = 1, #options do
term.setBackgroundColor(colors.black) -- options background color
term.setTextColor(colors.blue) -- options text color
  drawAt(12,i + 4,options[i])
end

term.setTextColor(colors.red) -- option text color optional
drawAt(12,14,"  -EMERGENCY-  CORE SHUTDOWN  ")
end




   function events()
   evt, but = os.pullEvent()
	 if evt == "key" then

if but == 208 then --down arrow
input = input + 1
if input > 9 then
input = 9
end

elseif but == 200 then --up arrow
input = input - 1
if input < 1 then
input = 1
end

elseif but == 28 then --Enter
if input == 1 then
  c = colors.subtract(c, colors.white)
  rs.setBundledOutput("back",c)
  white = not white


  elseif input == 2 then
  c = colors.combine(c, colors.white)
  rs.setBundledOutput("back",c)
  white = not white


  elseif input == 3 then
  c = colors.subtract(c, colors.black)
  rs.setBundledOutput("back",c)
  black = not black


  elseif input == 4 then
  c = colors.combine(c, colors.black)
  rs.setBundledOutput("back",c)
  black = not black


  elseif input == 5 then
  c = colors.subtract(c, colors.red)
  rs.setBundledOutput("back",c)
  red = not red


  elseif input == 6 then
  c = colors.combine(c, colors.red)
  rs.setBundledOutput("back",c)
  red = not red


  elseif input == 7 then
  c = colors.subtract(c, colors.green)
  rs.setBundledOutput("back",c)
  green = not green


  elseif input == 8 then
  c = colors.combine(c, colors.green)
  rs.setBundledOutput("back", c)
  green = not green


  elseif input == 0 then
  c = colors.combine(colors.green,colors.red,colors.white,colors.black)
  green = false
  red = false
  black = false
  white = false
  rs.setBundledOutput("back", c)
  end

end
   end
	 end

function main()
	while true do
	  cleanScreen()
	   drawArt()
	 events() --Handles the menu stuff
end
	end


main()
Tassyr #8
Posted 02 April 2013 - 12:39 PM
Sorry to bump, but I still can't figure out why it won't actually perform the functions it should. I can't see any error in the code, but then I'm new to lua and menus especially…