Advertisement
ezra3131

Untitled

Nov 17th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. --config
  2. local amountOfButtons = 8
  3. local buttonWidth = 11
  4. local buttonHeight = 3
  5. local verticalButtonSpace = 2
  6. local
  7. --end of config
  8.  
  9. local screenSizeX,screenSizeY = term.getSize()
  10.  
  11. local buttons = {}
  12. for i=1,amountOfButtons do
  13. buttons[i] = {xPos=0, yPos=0, width=buttonWidth, height=buttonHeight, name="", color=colors.red, action=function()}
  14. end
  15.  
  16. local calculatePositions = function()
  17. for i=1,math.floor(amountOfButtons/2 + 0.5) do
  18. if amountOfButtons > 1 then
  19. buttons[i].xPos = math.floor((screenSizeX - 2 * buttonWidth)/3) + 1
  20. else
  21. buttons[i].xPos = math.floor((screenSizeX - buttonWidth)/2) + 1
  22. end
  23. buttons[i].yPos = 3 + (i-1) * verticalButtonSpace + (i-1) * buttonHeight
  24. end
  25. for i=1,math.floor(amountOfButtons/2) do
  26. buttons[i + math.floor(amountOfButtons/2)].xPos = math.floor(buttonWidth + 2 * (screenSizeX - 2 * buttonWidth)/3) + 1
  27. buttons[i + math.floor(amountOfButtons/2)].yPos = 3 + (i-1) * verticalButtonSpace + (i-1) * buttonHeight
  28. end
  29. end
  30.  
  31. local isClicked = function(button, xClick, yClick)
  32. if xClick >= buttons.xPos and xClick < buttons.xPos + buttons.width and yClick >= buttons.yPos and yClick < buttons.yPos + buttons.height then
  33. return true
  34. else
  35. return false
  36. end
  37. end
  38.  
  39. local event,mouseButton,xPos,yPos
  40. local handleInput = function()
  41. event,mouseButton,xPos,yPos = os.pullEventRaw()
  42. if event = "mouse_click" then
  43. for i=1,amountOfButtons do
  44. if isClicked(buttons[i],xPos,yPos) then
  45. buttons[i].action()
  46. end
  47. end
  48. end
  49. end
  50.  
  51. local draw = function()
  52. for i=1,amountOfButtons do
  53. term.setBackgroundColor(buttons[i].color)
  54. for j=1,buttons[i].height do
  55. for k=1,buttons[i].width do
  56. term.setCursorPos(buttons[i].xPos + k -1, buttons[i].yPos + j -1)
  57. term.write(" ")
  58. end
  59. end
  60. term.setCursorPos(buttons[i].xPos + math.floor((buttons[i].width - string.len(buttons[i].name))/2), buttons[i].yPos + math.floor(buttons[i].height/2))
  61. term.write(buttons[i].name)
  62. end
  63. end
  64.  
  65. local loop = function()
  66. while true do
  67. draw()
  68. handleInput()
  69. end
  70. end
  71.  
  72.  
  73. --actually running the program
  74. calculatePositions()
  75.  
  76. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement