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

Menu Help

Started by Taminus, 06 January 2013 - 01:58 PM
Taminus #1
Posted 06 January 2013 - 02:58 PM
I'm currently trying to modify someone else's code for a menu which I've seen a couple times but know very little of Lua and require some assistance. Currently I've added all 16 options that I want but I need them to be side by side rather than centred in the middle of the screen. Below is the code I have up to this point.

local w,h = term.getSize()
function printCentred( y, s )
	local x = math.floor((w - string.len(s)) / 2)
	term.setCursorPos(x,y)
	term.clearLine()
	term.write( s )
end
local nOption = 1
-- Display menu
local function drawMenu()
	term.clear()
	term.setCursorPos(1,1)
	term.write( "LVC Activation" )
	term.setCursorPos(w-17,1)
	if nOption == 1 then
	term.write( "1" )
	elseif nOption == 2 then
	term.write( "2" )
	elseif nOption == 3 then
	term.write( "3" )
elseif nOption == 4 then
	term.write( "4" )
elseif nOption == 5 then
	term.write( "5" )
elseif nOption == 6 then
	term.write( "6" )
elseif nOption == 7 then
	term.write( "7" )
elseif nOption == 8 then
	term.write( "8" )
	elseif nOption == 9 then
	term.write( "9" )
elseif nOption == 10 then
	term.write( "10" )
elseif nOption == 11 then
	term.write( "11" )
elseif nOption == 12 then
	term.write( "12" )
elseif nOption == 13 then
	term.write( "13" )
elseif nOption == 14 then
	term.write( "14" )
elseif nOption == 15 then
	term.write( "15" )
else
	term.write( "16" )
	end
end
-- Display the frontend
term.clear()
local function drawFrontend()
	printCentred( math.floor(h/2) - 6, "SELECT AN OPTION" )
	printCentred( math.floor(h/2) - 5, ((nOption == 1) and "[ 1 ]") or "1" )
	printCentred( math.floor(h/2) - 4, ((nOption == 2) and "[ 2 ]") or "2" )
	printCentred( math.floor(h/2) - 3, ((nOption == 3) and "[ 3 ]") or "3" )
	printCentred( math.floor(h/2) - 2, ((nOption == 4) and "[ 4 ]") or "4" )
	printCentred( math.floor(h/2) + 1, ((nOption == 5) and "[ 5 ]") or "5" )
	printCentred( math.floor(h/2) + 0, ((nOption == 6) and "[ 6 ]") or "6" )
	printCentred( math.floor(h/2) + 1, ((nOption == 7) and "[ 7 ]") or "7" )
	printCentred( math.floor(h/2) + 2, ((nOption == 8) and "[ 8 ]") or "8" )
	printCentred( math.floor(h/2) + 3, ((nOption == 9) and "[ 9 ]") or "9" )
	printCentred( math.floor(h/2) + 4, ((nOption == 10) and "[ 10 ]") or "10" )
	printCentred( math.floor(h/2) + 5, ((nOption == 11) and "[ 11 ]") or "11" )
	printCentred( math.floor(h/2) + 6, ((nOption == 12) and "[ 12 ]") or "12" )
	printCentred( math.floor(h/2) + 7, ((nOption == 13) and "[ 13 ]") or "13" )
	printCentred( math.floor(h/2) + 8, ((nOption == 14) and "[ 14 ]") or "14" )
	printCentred( math.floor(h/2) + 9, ((nOption == 15) and "[ 15 ]") or "15" )
	printCentred( math.floor(h/2) + 10, ((nOption == 16) and "[ 16 ]") or "16" )
end


-- Call functions for display menu
drawMenu()
drawFrontend()

while true do
	local e,p = os.pullEvent()
	if e == "key" then
		local key = p
		if key == 17 or key == 200 then
			-- Up
			if nOption > 1 then
				nOption = nOption - 1
				drawMenu()
				drawFrontend()
			end
		elseif key == 31 or key == 208 then
			-- Down
			if nOption < 16 then -- Change 3 by the number of option.
				nOption = nOption + 1
				drawMenu()
				drawFrontend()
			end
		elseif key == 28 then
			-- Enter
			break
		end
	end
end
term.clear()
-- Conditions
if nOption == 1 then
	print("Option 1 selected")
elseif nOption == 2 then
	print("Option 2 selected")
elseif nOption == 3 then
	print("Option 3 selected")
elseif nOption == 4 then
	print("Option 4 selected")
elseif nOption == 5 then
	print("Option 5 selected")
elseif nOption == 6 then
	print("Option 6 selected")
elseif nOption == 7 then
	print("Option 7 selected")
elseif nOption == 8 then
	print("Option 8 selected")
elseif nOption == 9 then
	print("Option 9 selected")
elseif nOption == 10 then
	print("Option 10 selected")
elseif nOption == 11 then
	print("Option 11 selected")
elseif nOption == 12 then
	print("Option 12 selected")
elseif nOption == 13 then
	print("Option 13 selected")
elseif nOption == 14 then
	print("Option 14 selected")
elseif nOption == 15 then
	print("Option 15 selected")
else
	print("Option 16 selected")
end

I also require a left and right option to move freely between the columns.
Any help with this would be greatly appreciated.
remiX #2
Posted 06 January 2013 - 10:55 PM
You will need to write the text instead of printing and changed the x value each time it prints.

How many items do you want in each row and hany rows do you want (ie, how many columns and rows do you want)

EDIT: Try this out, making the columns too much makes it look to much to the left (math is meh) I'd suggest 4 columns.


local w,h = term.getSize()
columns = 4
t = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"}

function menu(_table)
    local sel = 1
    while true do
        tempSpace = w/columns - 7
        tempLine = 0
        if sel > #_table then sel = 1 end
        if sel < 1 then sel = #_table end
        for i = 1,#_table do
            term.setCursorPos(tempSpace, 3 + tempLine)
            tempSpace = tempSpace + math.floor(w/columns)
            if i % columns == 0 then tempLine = tempLine + 1 tempSpace = w/columns - 7 end
            if sel == i then
                write("[ " .. _table[i] .. " ]")
            else
                write("  " .. _table[i] .. "  ")
            end
        end
        local e, key = os.pullEvent("key")
        if key == keys.right then -- right key
            sel = sel + 1
        elseif key == keys.left then -- left key
            sel = sel - 1
        elseif key == keys.up then -- up key
            sel = sel - 4
        elseif key == keys.down then -- down key
            sel = sel + 4
        elseif key == keys.enter then
            return _table[sel], sel
        end
    end
end

while true do
    term.clear()
    term.setCursorPos(1,1)
    term.write( "LVC Activation" )
    sOption, nOption = menu(t) -- sOption returns the string of the table index, nOption returns which number.
    term.clear()
    term.setCursorPos(1,1)
    -- Conditions
    print("Option " .. sOption .. " selected\n")
    if nOption == 1 then
        print("Option 1 selected")
    elseif nOption == 2 then
        print("Option 2 selected")
    elseif nOption == 3 then
        print("Option 3 selected")
    elseif nOption == 4 then
        print("Option 4 selected")
    elseif nOption == 5 then
        print("Option 5 selected")
    elseif nOption == 6 then
        print("Option 6 selected")
    elseif nOption == 7 then
        print("Option 7 selected")
    elseif nOption == 8 then
        print("Option 8 selected")
    elseif nOption == 9 then
        print("Option 9 selected")
    elseif nOption == 10 then
        print("Option 10 selected")
    elseif nOption == 11 then
        print("Option 11 selected")
    elseif nOption == 12 then
        print("Option 12 selected")
    elseif nOption == 13 then
        print("Option 13 selected")
    elseif nOption == 14 then
        print("Option 14 selected")
    elseif nOption == 15 then
        print("Option 15 selected")
    elseif nOption == 16 then
        print("Option 16 selected")
    end
    sleep(2)
end