Guest User

Selective Buttons

a guest
Sep 19th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. defaultColors = {colors.white, colors.orange,
  2. colors.magenta, colors.lightBlue,
  3. colors.yellow, colors.lime,
  4. colors.pink, colors.gray,
  5. colors.lightGray, colors.cyan,
  6. colors.purple, colors.blue,
  7. colors.brown, colors.green,
  8. colors.red, colors.black}
  9.  
  10. local registeredButtons = {}
  11.  
  12. function addButton(id, decrement, increment, y, type, color, value)
  13.     registeredButtons[id] = {}
  14.     registeredButtons[id]['minX'] = decrement
  15.     registeredButtons[id]['maxX'] = increment
  16.     registeredButtons[id]['y'] = y
  17.     registeredButtons[id]['type'] = type
  18.     registeredButtons[id]['btn_color'] = color
  19.     registeredButtons[id]['value'] = value
  20. end
  21.  
  22. local function drawButtons(bcColor)
  23.     term.setBackgroundColor(bcColor)
  24.     term.clear()
  25.     print('Start drawing') --Debug
  26.     for i = 1, #registeredButtons do
  27.         local data = registeredButtons[i]
  28.         local x1 = data.minX
  29.         local x2 = data.maxX
  30.         local yPos = data.y
  31.         local bType = data.type
  32.         local bColor = defaultColors[data.btn_color]
  33.         if bType == 'color' then
  34.             local sColor =defaultColors[data.value]
  35.         else
  36.             local sColor = defaultColors[9]
  37.         end
  38.         print('All variables gathered') --Debug
  39.         term.setCursorPos(x1, yPos)
  40.         term.setBackgroundColor(bColor)
  41.         if not bType == 'numberical' then
  42.             write('<')
  43.             term.setCursorPos(x2, yPos)
  44.             write('>')
  45.         else
  46.             term.write('-')
  47.             term.setCursorPos(x2, yPos)
  48.             term.write('+')
  49.         end
  50.         term.setCursorPos(x1+1, yPos)
  51.         term.setBackgroundColor(sColor)
  52.         if not bType == 'numberical' then
  53.             term.write('] [')
  54.         else
  55.             term.write(data.value)
  56.         end
  57.     end
  58. end
  59.  
  60. local function checkForUpdates(event, mouse, x, y)
  61.     for index, value in pairs(registeredButtons) do
  62.         local decrement, increment, bY = value.minX, value.maxX, value.y
  63.         if event == 'mouse_click' and mouse == 1 and y == bY then
  64.             if x == decrement then
  65.                 return index, 1
  66.             elseif x == increment then
  67.                 return index, -1
  68.             end
  69.         elseif event == 'mouse_scroll' and y == bY then
  70.             if x >= decrement or x <= increment then
  71.                 return index, mouse*(-1)
  72.             end
  73.         end
  74.     end
  75.     return false
  76. end
  77.  
  78. local function changeValue(index, multiplier)
  79.     local data = registeredButtons[index]
  80.     data.value = data.value + multiplier
  81. end
  82.  
  83. local function updater()
  84.     local event, mouse, x, y = os.pullEvent()
  85.     local index, multiplier = checkForUpdates(event, mouse, x, y)
  86.     changeValue(index, multiplier)
  87.     drawButtons(defaultColors[16])
  88. end
  89.  
  90. local function callMain()
  91.     addButton(1, 5, 9, 3, 'color', defaultColors[9], 2)
  92.     addButton(2, 5, 9, 5, 'color', defaultColors[9], 3)
  93.     addButton(3, 5, 9, 7, 'color', defaultColors[9], 10)
  94.     addButton(4, 4, 10, 9, 'numberical', defaultColors[9], 1)
  95.     addButton(5, 5, 9, 11, 'color', defaultColors[9], 11)
  96.     addButton(6, 4, 10, 13, 'numberical', defaultColors[9], 0)
  97.     print('All buttons set') --Debug
  98.     sleep(2) --Debug
  99.     drawButtons(defaultColors[16])
  100.     while true do
  101.         updater()
  102.     end
  103. end
  104.  
  105. callMain()
Advertisement
Add Comment
Please, Sign In to add comment