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

computer menu assist

Started by shamefulChris, 30 June 2015 - 04:23 AM
shamefulChris #1
Posted 30 June 2015 - 06:23 AM
I am currently using a menu for players to select options from, but I want the menu to display in 2 columns instead of just 1. Any help is much appriciated.




term.clear()

local side = "back"
function pulse(color,amount,length)
for i=1, amount do
rs.setBundledOutput(side,color)
os.sleep(length)
rs.setBundledOutput(side,0)
os.sleep(length)
end
end

local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end

term.setCursorPos(1,1)

local function menu(...) -- ver 0.1
	local sel = 1
	local list = {...}
	while true do
			if sel > #list then sel = 1 end
			if sel < 1 then sel = #list end
			for i = 1,#list do
					term.setCursorPos(17,5+i-1)
					if sel == i then
							print("["..list[i].."]")
					else
							print(" "..list[i].." ")
					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
									term.setCursorPos(1,1)
									return list[sel],sel
							end
					end
			end
	end
end
centerText("Please select Option")
local selection = menu("1","2","3","4","5","B")
if selection == "1" then
	pulse(0,0,0)
elseif selection == "2" then
	pulse(4,5,.4)
elseif selection == "3" then
	pulse(4,10,.4)
elseif selection == "4" then
	pulse(4,15,.4)
elseif selection == "5" then
	pulse(4,20,.4)
elseif selection == "B" then
	pulse(8,5,.4)
end

shell.run("startup")


edit: I don't remember who I got the original menu code from, but if this is your code, thank you!
Edited on 30 June 2015 - 04:37 AM
Bomb Bloke #2
Posted 30 June 2015 - 01:33 PM
Well, here's the bit that draws the actual menu:

                        for i = 1,#list do
                                        term.setCursorPos(17,5+i-1)
                                        if sel == i then
                                                        print("["..list[i].."]")
                                        else
                                                        print(" "..list[i].." ")
                                        end
                        end

If you combine it with the below "automated button placement" code (using your "i" variable where it uses "count"), which I'm blatantly stealing from this thread out of pure laziness, then I'm sure you can see a way to go about it. :)/>

local Count = 0
local Width = 15
local Height = 3
for k, v in ipairs(Buttons) do
    local X = 3 + (Count%3)*(Width+1)
    local Y = 2 + (math.floor(Count/3)*(Height+1))
    Count = Count + 1
    -- use X and Y here to create a button.
    api.createButton(X, Y, Width, Height, colors.gray)
end
shamefulChris #3
Posted 30 June 2015 - 04:38 PM
Thanks :)/>
shamefulChris #4
Posted 30 June 2015 - 04:53 PM
I found another issue, since I am using CC 1.3 for this, I don't have mouse support, I need a way to select left and right. currently the only way to get to the second column is to scroll all the way down with the down arrow.



 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
                                                                        term.setCursorPos(1,1)
                                                                        return list[sel],sel
                                                        end
                                        end
                        end
        end
end



jerimo #5
Posted 30 June 2015 - 05:23 PM
You could add support for the side arrow keys?
You could either split your options into two distinct lists for left and right and then simply switch which list you are selecting from at the same index,
Or you could also add to the selection the number of items in the first column, leading to the same result but in a 1d table