Advertisement
koki2000

gombos menu

Sep 12th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. function monitor()
  2.  for _,b in pairs (rs.getSides()) do
  3.    if peripheral.getType(b) == "monitor" then
  4.     return peripheral.wrap(b)
  5.    end
  6.  end
  7. end
  8.  
  9. local mon = monitor()
  10.  
  11. local w, h = mon.getSize()
  12. local selectedItem = 1
  13. local run = true
  14.  rs.setOutput("bottom", false)
  15.  
  16. function megjegyzes(kiiras)
  17.  hossz = string.len(kiiras)
  18.  mon.clear()
  19.  mon.setCursorPos(w/2-(hossz/2), h/2)
  20.  mon.write(kiiras)
  21.  sleep(3)
  22. end
  23.  
  24. function menu1()
  25.  megjegyzes("menu1")
  26. end
  27.  
  28. function menu2()
  29.  megjegyzes("menu2")
  30. end
  31.  
  32. function exiting()
  33.  megjegyzes("kilepes a programbol")
  34.  mon.clear()
  35.  run = false
  36. end
  37.  
  38. mainmenu = {
  39. [1] = {text = "choice1", handler = menu1},
  40. [2] = {text = "choice2", handler = menu2},
  41. [3] = {text = "exit", handler = exiting}
  42. }
  43.  
  44. function printmenu( menu )
  45.  term.clear()
  46.  mon.clear()
  47.  for i=1,#menu do
  48.   hossz = string.len(menu[i].text)
  49.   if i == selectedItem then
  50.    term.setCursorPos(w/2-(hossz/2)-4, h/2-(#menu/2)-1+i)
  51.    term.write(">> " .. menu[i].text .. " <<")
  52.    mon.setCursorPos(w/2-(hossz/2)-4, h/2-(#menu/2)-1+i)
  53.    mon.write(">> " .. menu[i].text .. " <<")
  54.   else
  55.    term.setCursorPos(w/2-(hossz/2)-4, h/2-(#menu/2)-1+i)
  56.    term.write("   " .. menu[i].text)
  57.    mon.setCursorPos(w/2-(hossz/2)-4, h/2-(#menu/2)-1+i)
  58.    mon.write("   " .. menu[i].text)
  59.   end
  60.  end
  61. end
  62.  
  63. function onKeyPressed( key, menu )
  64.  if key == 28 then
  65.   menu[selectedItem].handler()
  66.  elseif key == 200 then
  67.   if selectedItem > 1 then
  68.    selectedItem = selectedItem - 1
  69.   end
  70.  elseif key == 208 then
  71.   if selectedItem < #menu then
  72.    selectedItem = selectedItem + 1
  73.   end
  74.  else
  75.   megjegyzes("nem definialt gomb")
  76.  end
  77. end
  78.  
  79. while run do
  80.  local timer = os.startTimer(2)
  81.  printmenu(mainmenu)
  82.  local event, par1 = os.pullEventRaw()
  83.  if event == "key" then
  84.   onKeyPressed(par1, mainmenu)
  85.  elseif event == "redstone" then
  86.   if rs.testBundledInput ("front", colors.white) then
  87.    onKeyPressed(200, mainmenu)
  88.   elseif rs.testBundledInput ("front", colors.orange) then
  89.    onKeyPressed(28, mainmenu)
  90.   elseif rs.testBundledInput ("front", colors.magenta) then
  91.    onKeyPressed(208, mainmenu)
  92.   end
  93.  elseif event == "terminate" then
  94.  megjegyzes("Ne kivancsiskodj, nem szep dolog lopni.")
  95.  end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement