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

Menu, Scrollable!

Started by libraryaddict, 14 May 2012 - 09:42 AM
libraryaddict #1
Posted 14 May 2012 - 11:42 AM
function Menu(...) -- Put the menu text in. User chooses one. The function returns the selection he choose.
--You now need to type in the number the menu starts, and the number it ends. Made by libraryaddict
  local MenuStuff = {...}
  local first = table.remove(MenuStuff, 1)
  local second = table.remove(MenuStuff, 1)
  local Scrolled = 1
  local DownDown = 0
  local Mouse = first
  local function rewrite()
	local i = 0
	for n=first, second do
	  i = i+1
	  if MenuStuff[i+DownDown] then
		term.setCursorPos(4, n)
		term.write(string.rep(" ", string.len(MenuStuff[i+DownDown])))
	  end
	end
  end
  local function Draw()
	local i = 0
	for n=first,second do
	  i = i+1
	  term.setCursorPos(4, n)
	  term.write(MenuStuff[i+DownDown])
	end
	term.setCursorPos(1, Mouse)
	term.write(">>")
  end
  Draw()
  while true do
	event,param1 = os.pullEvent()
	if event == "key" then
	  term.setCursorPos(1, Mouse)
	  term.write("  ")
	  rewrite()
	  if param1 == 200 then -- Up
		if Scrolled > 1 then
		  if Mouse == first and MenuStuff[Scrolled-1] then
			DownDown = DownDown-1
		  else
			Mouse = Mouse-1
		  end
		  Scrolled = Scrolled-1
		end
	  elseif param1 == 208 then -- Down
		if Scrolled < #MenuStuff then
		  if Mouse == second then
			DownDown = DownDown+1
		  else
			Mouse = Mouse+1
		  end
		  Scrolled = Scrolled+1
		end
	  elseif param1 == 28 then -- Enter
		rewrite()
		return MenuStuff[Scrolled]
	   -- break
	  end	
	  Draw()
	end
  end
end

if menu(1, 10, "Cake", "Cow") == "Cake" then print("You choose the cake!") else print("You milked the cow") end

Choosen = menu(5, 20, "God", "Admin", "Ban")
print("Player chose "..Choosen)
libraryaddict #2
Posted 26 May 2012 - 02:04 AM
Updated!

Now it doesn't need to clear your screen
Hexicube #3
Posted 08 June 2012 - 04:40 PM
One quick suggestion, change:
local first = MenuStuff[1]
local second = MenuStuff[2]
table.remove(MenuStuff, 1)
table.remove(MenuStuff, 1)
To:
local first = table.remove(MenuStuff, 1)
local second = table.remove(MenuStuff, 1)

Lua returns the removed element. :)/>/>
libraryaddict #4
Posted 15 June 2012 - 11:01 AM
One quick suggestion, change:
local first = MenuStuff[1]
local second = MenuStuff[2]
table.remove(MenuStuff, 1)
table.remove(MenuStuff, 1)
To:
local first = table.remove(MenuStuff, 1)
local second = table.remove(MenuStuff, 1)

Lua returns the removed element. :(/>/>

Didn't know that
Edited.
eperdeme #5
Posted 01 July 2012 - 11:55 AM
The functions you have to call the 'Menu' are calling lowercase 'menu' instead of the valid function 'Menu' took me 15mins (sadly) to figure out where I was going wrong :)/>/>

If only there was a Perl based computercraft :P/>/>
libraryaddict #6
Posted 01 July 2012 - 01:13 PM
The functions you have to call the 'Menu' are calling lowercase 'menu' instead of the valid function 'Menu' took me 15mins (sadly) to figure out where I was going wrong :)/>/>

If only there was a Perl based computercraft :P/>/>

Didn't notice, Fixed
Wolvan #7
Posted 01 September 2012 - 01:08 PM
OMG You're a genious! I always did the menus myself in a complicated way :l
Is there maybe a way so it returns the chosen ID? I will make it myself it was just an idea what you can add
KaoS #8
Posted 01 September 2012 - 01:16 PM
yeah me too, not sure why I don't just return the choice and then interpret it, my menu function is in the API section if you like, only thing it really has on this one is you can define rows and columns and it can scroll down and sideways
Wolvan #9
Posted 01 September 2012 - 02:37 PM
I managed to add table functionality! Now you can either give a table as 3rd parameter or you just give strings as 3+ Parameter like you would normally do.
Just change:

local MenuStuff = {...}
  local first = table.remove(MenuStuff, 1)
  local second = table.remove(MenuStuff, 1)
  local Scrolled = 1
  local DownDown = 0
to

local Parameters = {...}
  local MenuStuff = nil
  local first = Parameters[1]
  local second = Parameters[2]
  if type(Parameters[3]) == string or type(Parameters[3]) == "string" then
    local temptable = Parameters
    table.remove(temptable, 1)
    table.remove(temptable, 1)
    MenuStuff = temptable
  else
    MenuStuff = Parameters[3]
  end


I also created a 2nd function which returns the selected index by changing

return MenuStuff[Scrolled]
to

return Scrolled