Cavious

api_button

Jun 19th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. --[[
  2. FILENAME: api_button.lua(API)
  3. AUTHOR: Donald R. Valverde (Cavious)
  4. VERSION: 1.0-ALPHA
  5. --]]
  6.  
  7. local button = {}
  8.  
  9. function createButton(list, id, text, fontColor, backgroundColor, xPos, yPos, funct)
  10.     button[id] = {}
  11.     button[id]["text"] = text
  12.     button[id]["fontColor"] = fontColor
  13.     button[id]["backgroundColor"] = backgroundColor
  14.     button[id]["xPos"] = xPos
  15.     button[id]["yPos"] = yPos
  16.     button[id]["funct"] = funct
  17.    
  18.     table.insert(list, button[id])
  19. end
  20.  
  21. function removeButton(list, id)
  22.     table.remove(list, id)
  23. end
  24.  
  25. function drawButtons(output, list)
  26.     for i = 1, #list do
  27.         output.setCursorPos(list[i].xPos, list[i].yPos)
  28.         output.setTextColor(list[i].fontColor)
  29.         output.setBackgroundColor(list[i].backgroundColor)
  30.         output.write(list[i].text)
  31.     end
  32. end
  33.  
  34. function drawButton(output, list, i)
  35.     output.setCursorPos(list[i].xPos, list[i].yPos)
  36.     output.setTextColor(list[i].fontColor)
  37.     output.setBackgroundColor(list[i].backgroundColor)
  38.     output.write(list[i].text)
  39. end
  40.  
  41. function runButtonEvent(buttonList, activeButtonColor, inactiveButtonColor, event_type)
  42.     state = true
  43.    
  44.     while(state) do
  45.         event, button, xPos, yPos = os.pullEvent(event_type)
  46.        
  47.         for i = 1, #buttonList do
  48.             if(yPos == buttonList[i].yPos and xPos >= buttonList[i].xPos and xPos < (buttonList[i].text:len() + buttonList[i].xPos)) then
  49.                 buttonList[i].backgroundColor = activeButtonColor
  50.                 api_button.drawButton(term, buttonList, i)
  51.                 os.sleep(0.5)
  52.                 buttonList[i].backgroundColor = inactiveButtonColor
  53.                 api_button.drawButton(term, buttonList, i)
  54.                 os.sleep(0.5)
  55.                 buttonList[i].funct()
  56.                 state = false
  57.             end
  58.         end
  59.     end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment