QuickMuffin8782-Alt

Zipmenu Redux (version 2)

Dec 13th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. local params = {...}
  2. local cursor = 1
  3. local selectedColor = colors.white
  4. local titleColor = colors.white
  5. local titleBgColor = colors.gray
  6. local bgColor = colors.black
  7. local txtColor = colors.white
  8. local timerCount = nil
  9. local menuExtraText = ""
  10.  
  11. local function centertext(text, y)
  12. local w, h = term.getSize()
  13. term.setCursorPos((w/2)-(#text /2), y)
  14. term.write(text)
  15. end
  16.  
  17. local function draw(title, menu, extra)
  18. local w, h = term.getSize()
  19. term.setBackgroundColor(bgColor)
  20. term.clear()
  21. local t1 = ""
  22. if not type(extra) == "number" then
  23. t1 = (#extra > 0 and extra or "")
  24. else
  25. t1 = extra
  26. end
  27. for i=1, #menu do
  28. if(i == cursor) then
  29. term.setTextColor(txtColor)
  30. centertext("["..menu[i].."]", (h/2)+i-(cursor-1))
  31. else
  32. term.setTextColor(selectedColor)
  33. centertext(" "..menu[i].." ", (h/2)+i-(cursor-1))
  34. end
  35. end
  36. term.setCursorPos((w/2)-(#title/2)-4,2)
  37. term.setBackgroundColor(titleBgColor)
  38. term.setTextColor(titleColor)
  39. write((" "):rep(#title+8))
  40. centertext(title, 2)
  41. term.setCursorPos(1, 1)
  42. term.clearLine()
  43. write(" " .. cursor .. "/" .. #menu .. " ")
  44. term.setCursorPos(w - 1 - t1:len(), 1)
  45. term.write(t1)
  46. term.setCursorPos((w/2)-(#title/2)-4,2)
  47. term.setBackgroundColor(titleBgColor)
  48. term.setTextColor(bgColor)
  49. term.write(string.char(144))
  50. term.setCursorPos((w/2)+(#title/2)+4,2)
  51. term.setBackgroundColor(bgColor)
  52. term.setTextColor(titleBgColor)
  53. term.write(string.char(159))
  54. end
  55.  
  56. function setColors(title, titleBg, bg, selTxt, txt)
  57. if txt == nil or title == nil or titleBg == nil or selTxt == nil or bg == nil then error("Specify 5 colors: Title text, Title background, Selection background, Selection text, and Regular Selection text", 2) end
  58. titleColor, titleBgColor, bgColor, selectedColor, txtColor = title, titleBg, bg, selTxt, txt
  59. end
  60.  
  61. function setTimer(number) end
  62.  
  63. function setExtraText(text)
  64. local t = (text and text or "")
  65. menuExtraText = t
  66. end
  67.  
  68. function drawMenu(title, ...)
  69. local args = {...}
  70. local options
  71. if type(args[1]) == "table" then
  72. options = args[1]
  73. else
  74. options = {...}
  75. end
  76. cursor = 1
  77. count = timerCount
  78. countDownTimer = nil
  79. if count then
  80. countDownTimer = os.startTimer(1)
  81. end
  82.  
  83. while true do
  84. draw(title, options, menuExtraText)
  85. event, key = os.pullEvent()
  86. if event == "key" then
  87. if(key == keys.up) or (key == keys.w) then
  88. cursor = cursor-1
  89. end
  90. if(key == keys.down) or (key == keys.s) then
  91. cursor = cursor+1
  92. end
  93. if(key == keys.enter) then
  94. term.setBackgroundColor(colors.black)
  95. term.setTextColor(colors.white)
  96. term.clear()
  97. term.setCursorPos(1, 1)
  98. break
  99. end
  100. end
  101. if event == "timer" and key == countDownTimer then
  102. count = count - 1
  103. if count < 0 then
  104. break
  105. end
  106. end
  107. if(cursor > #options) then cursor = #options end
  108. if(cursor < 1) then cursor = 1 end
  109. end
  110.  
  111. if count then
  112. os.cancelTimer(countDownTimer)
  113. end
  114.  
  115. return cursor
  116. end
  117.  
  118. return {
  119. setTimer = setTimer,
  120. setExtraText = setExtraText,
  121. setColors = setColors,
  122. drawMenu = drawMenu
  123. }
Add Comment
Please, Sign In to add comment