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

[Lua] Vertical Menu

Started by Sirharry0077, 16 February 2013 - 05:56 AM
Sirharry0077 #1
Posted 16 February 2013 - 06:56 AM
I have searched the internet and I have been unable to find a program that will create a vertical gui menu. I would like for it automatically center on the screen and return the selection made.

Example Vertical Menu (Not Centered):

Title
[Selection 1]
 Selection 2
 selection 3
tesla1889 #2
Posted 16 February 2013 - 07:13 AM
come here to ask for help, not for programs

all of us are happy to help you debug the program and answer questions about it, but you still should write it
bjornir90 #3
Posted 16 February 2013 - 07:16 AM
I think we should add a programs requests sections on the forum
Sirharry0077 #4
Posted 16 February 2013 - 07:21 AM
I found this program http://pastebin.com/cKW626VM and it works well, but I need one that would automatically center on the screen and return the selection.
remiX #5
Posted 16 February 2013 - 07:23 AM
Easiest way is using tables… take this for example (read comments)
If you need me to explain more, ask away :)/>

-- Create a table with the selections
local tSelections = {
	"Selection 1",
	"Selection 2",
	"Selection 3"
}
-- Starting co-ords for table
local startYPos = 3
local startXPos = 5

-- Menu to create/print ... do everything really :P/>
local function startMenu( tMenu, sTitle )
	-- This function just prints the menu
	local function printMenu( tMenu, sTitle, nSelected )
		for index, text in pairs( tMenu ) do
			term.setCursorPos( startXPos, startYPos + index - 1 )
			write( (nSelected == index and '[' or ' ') .. text .. (nSelected == index and ']' or ' ')  )
		end
	end
	
	-- Set default selection to the first one
	local selection = 1
	-- Inifinite loop until enter is clicked
	while true do
		printMenu( tMenu, sTitle, selection )
		event, but = os.pullEvent("key")
		if but == keys.up then
			-- Up
			selection = selection - 1
		elseif but == keys.down then
			-- Down
			selection = selection + 1
		elseif but == keys.enter then
			-- Enter
			return tMenu[selection], selection -- returns the text AND the number
		end
		-- Advanced way to make the selection 1 if you have gone past the amount of items in the menu,
		-- or make it the amount of items in the menu/table if you have gone past above the first selection
		selection = selection < 1 and #tMenu or selection > #tMenu and 1 or selection
	end
end

term.clear()

sOption, nNumb = startMenu( tSelections ) -- start the function ... 'sOption' and 'nNumb'

term.clear()
term.setCursorPos(1, 1)

print(sOption)
print(nNumb)

-- Two ways of doing it
if sOption == "Selection 1" then

elseif nNumb == 1 then

end
Sirharry0077 #6
Posted 16 February 2013 - 07:32 AM
Easiest way is using tables… take this for example (read comments)
If you need me to explain more, ask away :)/>

-- Create a table with the selections
local tSelections = {
	"Selection 1",
	"Selection 2",
	"Selection 3"
}
-- Starting co-ords for table
local startYPos = 3
local startXPos = 5

-- Menu to create/print ... do everything really :P/>/>
local function startMenu( tMenu, sTitle )
	-- This function just prints the menu
	local function printMenu( tMenu, sTitle, nSelected )
		for index, text in pairs( tMenu ) do
			term.setCursorPos( startXPos, startYPos + index - 1 )
			write( (nSelected == index and '[' or ' ') .. text .. (nSelected == index and ']' or ' ')  )
		end
	end
	
	-- Set default selection to the first one
	local selection = 1
	-- Inifinite loop until enter is clicked
	while true do
		printMenu( tMenu, sTitle, selection )
		event, but = os.pullEvent("key")
		if but == keys.up then
			-- Up
			selection = selection - 1
		elseif but == keys.down then
			-- Down
			selection = selection + 1
		elseif but == keys.enter then
			-- Enter
			return tMenu[selection], selection -- returns the text AND the number
		end
		-- Advanced way to make the selection 1 if you have gone past the amount of items in the menu,
		-- or make it the amount of items in the menu/table if you have gone past above the first selection
		selection = selection < 1 and #tMenu or selection > #tMenu and 1 or selection
	end
end

term.clear()

sOption, nNumb = startMenu( tSelections ) -- start the function ... 'sOption' and 'nNumb'

term.clear()
term.setCursorPos(1, 1)

print(sOption)
print(nNumb)

-- Two ways of doing it
if sOption == "Selection 1" then

elseif nNumb == 1 then

end

Thx this will work just fine.
tesla1889 #7
Posted 16 February 2013 - 09:05 PM
I think we should add a programs requests sections on the forum
this is a great idea :)/> let them get it out of their systems in that forum instead of plaguing this one