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

Menus

Started by grand_mind1, 24 February 2013 - 11:53 AM
grand_mind1 #1
Posted 24 February 2013 - 12:53 PM
I am trying to make a menu that you can control using the arrow and enter keys like this:

Hello

[Goodbye]

Hi

"[]" representing what is selecting. I know you can detect what key you press with events but I don't know how I would change what is selected. If someone could explain this to me that would be great!
Help is appreciated!
Thanks! :D/>
AnDwHaT5 #2
Posted 24 February 2013 - 01:32 PM
try this its a little bigger then needed but.

repeat
term.setCursorPos(1,1)
print("hi")
print("Woah!")
while true do
local event, p1 = os.pullEvent("key")
if p1 == 200 then
term.clear()
term.setCursorPos(1,1)
print("[ hi ]")
print("Woah")
elseif p1 == 208 then
term.clear()
term.setCursorPos(1,1)
print("hi")
print("[ Woah ]")
end
until p1 == 208 or 200
end
end


It will work but enter is not on there nore will it recognize anything. It just selects and thats it.
grand_mind1 #3
Posted 24 February 2013 - 03:08 PM
Ok, so it's just a matter of reprinting the screen every time a key is pressed. Thanks!
darkrising #4
Posted 24 February 2013 - 03:56 PM
if you would like a button menu, pressing numbers keys for example check out my server program's interface, heres a snippet:

....
function RunServerMenu()
  dark.splash(1.5, "Powered by DarkGui")  
  state = "main"
  Page = 0  
  while true do
    term.clear()
    term.setCursorPos(1,1)
    if Co[state].draw then    
      Co[state].draw()
      _, key = os.pullEvent("char") 
      key = tonumber(key)
      if Co[Co[state].options[key]] then
        state = Co[state].options[key]
      else
        print("\nOption doesn't exist!")
        sleep(2)
      end
    else
      Co[state].Run()
      state = Co[state].Parent
    end
  end
end
....

Every time a key is pressed the screen is re-drawn from functions and data within a table.
remiX #5
Posted 24 February 2013 - 10:52 PM
Here's a simple menu function

local tab = {
	"Hi",
	"Goodbye",
	"Hello",
	"Exit"
}
local sX, sY = term.getSize()

local function menu( _table, startX, startY )
	local function getLongest( _table )
		local longest = 0
		for index, text in pairs( _table ) do
			longest = #text > longest and #text or longest
		end
		return longest + 2
	end
	
	local function printMenu( _table, long, sel )
		for index, text in pairs( _table ) do
			term.setCursorPos( startX, startY + index - 1 )
			write( ( index == sel and '[ ' or '  ' ) .. text .. string.rep(' ', long - #text) .. ( index == sel and ']' or ' ' ) )
		end
	end
	
	local longest = getLongest( _table )
	local sel = 1
	
	while true do
		printMenu( _table, longest, sel )
		local _, key = os.pullEvent( "key" )
		if key == keys.up then
			sel = sel - 1
		elseif key == keys.down then
			sel = sel + 1
		elseif key == keys.enter then
			return _table[sel], sel
		end
		sel = sel < 1 and #_table or sel > #_table and 1 or sel
	end
end

term.clear()

while true do
	sOption, sNumb = menu( tab, sX/2-7, sY/2-(#tab/2) )
	
	term.clear() term.setCursorPos( 1, 1 )

	if sOption == "Hi" then
		print( "Hello there! :D/>" )
	elseif sOption  == "Goodbye" then
		print( "Why are you going? :(/>" )
	elseif sOption == "Hello" then
		print( "Oh hello again :>" )
	elseif sOption == "Exit" then
		print( "Cheers :<" )
		break
	end
	sleep(2)
	term.clear() term.setCursorPos( 1, 1 )
end