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

[UI] CraftOS Themed MiniMenu

Started by KingofGamesYami, 02 November 2014 - 02:35 PM
KingofGamesYami #1
Posted 02 November 2014 - 03:35 PM
This is a small function I wrote to emulate the miniature menu of CraftOS's "edit" program. It is very easy to use, and can be used on both advanced and normal computers. It currently will not correct for smaller screen width, if you give it too many options (if you need more, see my menu). If this is a problem, I can update it, but I don't see anyone needing that many options out of this menu.

The Code

local function minimenu( ... )
	local tArgs = { ... }
	local tSelections = {}
	local _, y = term.getSize()
	local n = 1
	for i, str in ipairs( tArgs ) do
		tSelections[ i ] = { str = str }
		tSelections[ i ][ "x1" ] = n
		tSelections[ i ][ "x2" ] = n + #str + 1
		n = n + 2 + #str
	end
	for k, v in ipairs( tSelections ) do
		term.setCursorPos( v["x1"], y )
		term.write( ' ' .. v.str )
	end
	local slc = 1
	local last = 1
	while true do
		term.setCursorPos( tSelections[ last ]["x1"], y )
		term.write( ' ' )
		term.setCursorPos( tSelections[ last ]["x2"], y )
		term.write( ' ' )
		term.setCursorPos( tSelections[ slc ]["x1"], y )
		term.setTextColor( term.isColor() and colors.yellow or colors.white )
		term.write( "[" )
		term.setCursorPos( tSelections[ slc ]["x2"], y )
		term.write( "]" )
		while true do
			local event, key = os.pullEvent( "key" )
			if key == 203 and tSelections[ slc - 1 ] then
				last = slc
				slc = slc - 1
				break
			elseif key == 205 and tSelections[ slc + 1 ] then
				last = slc
				slc = slc + 1
				break
			elseif key == 28 then
				return tSelections[ slc ].str
			end
		end
	end
end

pastebin get V455UuCr

UsageThis menu is simple to use, simply call the function like so:

local selection = minimenu( "Option1", "Option2" )
And it will return either "Option1" or "Option2". You can add as many selections as you want, just remember to use string values.

Questions? Suggestions? Post them below!
Edited on 02 November 2014 - 02:37 PM
Creator #2
Posted 05 November 2015 - 03:33 PM
How comes no one responds to this. I mean, it works and it is great!
Awe2K #3
Posted 13 November 2015 - 02:26 PM
Wow, I think it would be cool to customize position and title of menu, this way it's even possible to make some kind of message dialog or something like that.
Waitdev_ #4
Posted 21 November 2015 - 10:44 PM
this is actually a really good tool! i like it a lot ;)/>