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

user friendly menu

Started by eddieo, 13 June 2012 - 09:42 PM
eddieo #1
Posted 13 June 2012 - 11:42 PM
Hey all
I'm not a programmer but with the help of this board and some examples I came up with this Menu it works and is easy to edit to suite your needs. It may look confusing but once you look it over you will see its very simple to understand and edit



--[[
This is a blank menu template as is it has 5 options and a exit
options can be added or removed to suite the user.
I would like to thank the users of this board whos know how helped in making this menu
--]]
function cls()	 -- function to clear the screen and set the cursor position
term.clear()
term.setCursorPos( 1, 1 )
end
function WTS()	  -- a function to print some stuff to the screen
for i = 1,10 do
  term.setCursorPos( 18, i )
  print("This is a Test")
  sleep(.2)
end
end
function menu(...)	  -- Menu function
choice = {...}
keypress = 1
positionX,positionY = 1,1	-- sets the position of the menu on the screen

while true do
  if keypress > #choice then  -- this while true loop causes the selection to go
   keypress = 1	 -- from the first to the last selection
  end		 -- the last selection to the first
  if keypress < 1 then   -- looping it so to speak
   keypress = #choice
  end
  for i = 1 , #choice do		-- this for loop moves the selection bracket
  term.setCursorPos(positionX , positionY + i - 1) -- with the keypresses [highlighting] the options
   if keypress == i then
	print("["..choice[i].."]")
   else
	print(" "..choice[i].." ")
   end
  end
  while true do	   -- this While true loop deals with the users choices
  local event,param1 = os.pullEvent()
   if event == "key" then  
	if param1 == 200 then	-- if the up key is pressed
	 keypress = keypress - 1
	 break
	end
	if param1 == 208 then	-- if the down key is pressed
	 keypress = keypress + 1
	 break
	end
	if param1 == 28 then   -- if the enter key is pressed
	 if keypress == 1 then  -- the next 5 lines are excuted you can call a function or functions
	  cls()	 -- cls() is just a function that clears the screen	
	  WTS()	 -- wts() is just a function that just prints some stuff to the screen
	  sleep(1)	-- i like things to step
	  shell.run("menublank") -- reloads the menublank program
      
	 end
	 if keypress == 2 then  -- the next 5 lines are excuted when the
	  cls()	 -- enter key is pressed over a menu option 2
	  print("Selection  " , keypress)
	  sleep(1)
	  shell.run("menublank")
      
	 end
	 if keypress == 3 then  -- the next 5 lines are excuted when the
	  cls()	 -- enter key is pressed over a menu option 3
	  print("Selection  " , keypress)
	  sleep(1)
	  shell.run("menublank")
      
	 end
	 if keypress == 4 then  -- the next 5 lines are excuted when the
	  cls()	 -- enter key is pressed over a menu option 4
	  print("Selection  " , keypress)
	  sleep(1)
	  shell.run("menublank")
      
	 end
	 if keypress == 5 then  -- the next 5 lines are excuted when the
	  cls()	 -- enter key is pressed over a menu option 5
	  print("Selection  " , keypress)
	  sleep(1)
	  shell.run("menublank")
      
	 end
	 if keypress == 6 then  -- the next 3 lines are excuted when the
	  cls()	 -- enter key is pressed over a menu option 6
	  return	 -- in this case pressing the enter key exits
	 end	   -- the menu and returns to the prompt
	end
   end
  end
end
end
cls()
print("Main Menu")
print(menu( "Selection 1",  -- you can add as many options as you like
			"Selection 2",  -- and name them what you want
			"Selection 3",
			"Selection 4",
			"Selection 5",
			"Exit Menu"))
Deathknight0897 #2
Posted 05 July 2012 - 10:20 PM
Hey so i have used this menu multiple times and have run into an issue with it it wont do what i want it just says program doesn't exist i have 4 programs all running in a folder called startup_kit and the 4 programs are
Boot
Core
Generic
Launch


--[[
This is a blank menu template as is it has 5 options and a exit
options can be added or removed to suite the user.
I would like to thank the users of this board whos know how helped in making this menu
--]]
function cls()   -- function to clear the screen and set the cursor position
term.clear()
term.setCursorPos( 1, 1 )
end
function menu2(...)		-- Menu function
choice = {...}
keypress = 1
positionX,positionY = 1,1	   -- sets the position of the menu on the screen
while true do
  if keypress > #choice then  -- this while true loop causes the selection to go
   keypress = 1  -- from the first to the last selection
  end			-- the last selection to the first
  if keypress < 1 then   -- looping it so to speak
   keypress = #choice
  end
  for i = 1 , #choice do				-- this for loop moves the selection bracket
  term.setCursorPos(positionX , positionY + i - 1) -- with the keypresses [highlighting] the options
   if keypress == i then
		print("["..choice[i].."]")
   else
		print(" "..choice[i].." ")
   end
  end
  while true do	-- this While true loop deals with the users choices
  local event,param1 = os.pullEvent()
   if event == "key" then
		if param1 == 200 then   -- if the up key is pressed
		 keypress = keypress - 1
		 break
		end
		if param1 == 208 then   -- if the down key is pressed
		 keypress = keypress + 1
		 break
		end
		if param1 == 28 then   -- if the enter key is pressed
		 if keypress == 1 then  -- the next 5 lines are excuted you can call a function or functions
		  cls()  -- cls() is just a function that clears the screen	
		  WTS()  -- wts() is just a function that just prints some stuff to the screen
		  sleep(1)	  -- i like things to step
		  shell.run("startup_kit/launch") -- reloads the menublank program
	
		 end
		 if keypress == 2 then  -- the next 5 lines are excuted when the
		  cls()  -- enter key is pressed over a menu option 2
		  print("Selection  " , keypress)
		
	c = colors.combine( c, colors.pink)
		  rs.setBundledOutput("back", c )
	sleep(20)
	c = colors.subtract( c, colors.pink )
		  rs.setBundledOutput("back", c )
  
	sleep(5)
	shell.run("startup_kit/Generic")
	
		 end
		 if keypress == 3 then  -- the next 5 lines are excuted when the
		  cls()  -- enter key is pressed over a menu option 3
		  print("Selection  " , keypress)
		  sleep(1)
		  shell.run("startup_kit/generic")
	
		 end
		 if keypress == 4 then  -- the next 5 lines are excuted when the
		  cls()  -- enter key is pressed over a menu option 4
		  print("Selection  " , keypress)
		  sleep(1)
		  shell.run("startup_kit/generic")
  
	
		 end
		 if keypress == 5 then  -- the next 5 lines are excuted when the
		  cls()  -- enter key is pressed over a menu option 5
		  print("Selection  " , keypress)
		  sleep(1)
		  shell.run("menublank")
	
		 end
		 if keypress == 6 then  -- the next 3 lines are excuted when the
		  cls()  -- enter key is pressed over a menu option 6
		  return		 -- in this case pressing the enter key exits
		 end	   -- the menu and returns to the prompt
		end
   end
  end
end
end
cls()
print("Main Menu")
print(menu( "Chose your class?",  -- you can add as many options as you like
						"Knight",  -- and name them what you want
						"Archer",
						"Mage"))
	
	 --Choice 1
--What is your style
--Knight /pink
--Iron Helmet,Iron Chest,Iron legs,Iron Boots
--Iron Sword
--Archer /Magenta/White
--128 arrows/White /only send 64 signals 2 chests
--bow
--leather helmet,leather boots,leather legs,leather chest
--Wizard /Purple
--

as you can see im in the middle of editing selection 2 oh also i use one as a prompt for the user if they select it it will reload the program
Deathknight0897 #3
Posted 15 July 2012 - 11:39 PM
Dont worry fixed it just me herp derping