Advertisement
k_goos

ButtonAPI

Jun 24th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.54 KB | None | 0 0
  1. -- This is an API used for drawing buttons on a ComputerCraft monitor --
  2.  
  3. -- How to use the API --
  4. --  ButtonAPI.initMonitor(side)
  5. --  ButtonAPI.addButton("button1", 1, 1, 11, 10, "test")
  6. --  ButtonAPI.addButton("button2", 11, 11, 5, 5, "test02")
  7. --  ButtonAPI.draw()
  8. --  ButtonAPI.initEventHandler(eventHandler)
  9. --  function eventHandler()
  10. --      while true do
  11. --          local event, name, x, y = os.pullEvent("button")
  12. --          print("Event: " .. event .. " Name: " .. name .. " X: " .. x .. " Y: " .. y)
  13. --      end
  14. --  end
  15.  
  16. -- FUNCTIONS --
  17. -- addButton(mon, name, x, y, width, height, text, [func], [funcParam], [buttonColor], [textColor]) -> add a new button
  18. -- removeButton(index) -> remove button
  19. -- centerTextOnWindow(window, text) -> centers the text in a window
  20. -- draw() -> draws all the buttons
  21. -- toggleButton(button) -> toggles the colors: can be given as func to addButton
  22. -- buttonClickEvent() -> check monitor and call checkButton when monitor is pressed
  23. -- checkButton(x, y) -> check a if the coordinates are inside a button: returns on eventStack
  24. -- printButtonInfo() -> printing all the information about all the buttons
  25. -- initEventHandler(eventHandler) -> function that handles the events
  26. -- initEventHandler(eventHandler, mainFunc) -> function that handles the events
  27. -- startParallel(function1, function2) -> start two functions
  28. -- startParallel(function1, function2, function3) -> start two functions
  29. -- getMonitor()
  30. -- setMonitor(monitor)
  31. -- getScreenSize()
  32. -- getButtonIndex()
  33. -- getButton(index)
  34. -- getFunc(index)
  35. -- setFunc(index, func)
  36. -- getFuncParam(index)
  37. -- setFuncParam(index, funcParam)
  38. -- getButtonColor(index)
  39. -- setButtonColor(index, buttonColor)
  40. -- getTextColor(index)
  41. -- setTextColor(index, textColor)
  42.  
  43. local buttons={}
  44.  
  45. function addButton(mon, name, x, y, width, height, text, func, funcParam, buttonColor, textColor)
  46.     print("Adding button...")
  47.     print(name .. ": " .. text .. " (" .. x .. ", " .. y .. "),(" .. width .. ", " .. height .. ")")
  48.    
  49.     func = func or toggleButton
  50.     params = {displayTime = 1, activeColor = colors.red}
  51.     funcParam = funcParam or params
  52.     buttonColor = buttonColor or colors.green
  53.     textColor = textColor or colors.white
  54.    
  55.     local button = {}
  56.     button["name"] = name
  57.     button["buttonColor"] = buttonColor
  58.     button["textColor"] = textColor
  59.     button["text"] = text
  60.     button["func"] = func
  61.     button["funcParam"] = funcParam
  62.     button["window"] = window.create(mon, x, y, width, height, false)
  63.     button["window"].setTextColor(textColor)
  64.     button["window"].setBackgroundColor(buttonColor)
  65.     button["window"].clear()
  66.    
  67.     table.insert(buttons, button)
  68. end
  69. function removeButton(index)
  70.     table.remove(index)
  71. end
  72. function centerTextOnWindow(window, text)
  73.     local x, y = window.getPosition()
  74.     local width, height = window.getSize()
  75.     window.setCursorPos(math.ceil((width / 2) - (text:len() / 2) + 1), math.ceil(height / 2))
  76.     window.write(text)
  77. end
  78. function draw()
  79.     print("Starting drawing...")
  80.     print("Length: ".. #buttons)
  81.     for i = 1, #buttons do
  82.         print("Drawing: " .. buttons[i]["name"])
  83.         buttons[i]["window"].clear()
  84.         centerTextOnWindow(buttons[i]["window"], buttons[i]["text"])
  85.         buttons[i]["window"].setVisible(true)
  86.     end
  87.     print("Drawing done")
  88. end
  89. function toggleButton(button)
  90.     button["window"].setBackgroundColor(button["funcParam"]["activeColor"])
  91.     draw()
  92.     sleep(button["funcParam"]["displayTime"])
  93.     button["window"].setBackgroundColor(button["buttonColor"])
  94.     draw()
  95. end
  96. function buttonClickEvent()
  97.     while true do
  98.         event, side, x, y = os.pullEvent("monitor_touch")
  99.         checkButton(x, y)
  100.     end
  101. end
  102. function checkButton(x, y)
  103.     for i = 1, #buttons do
  104.         local xButton, yButton = buttons[i]["window"].getPosition()
  105.         local width, height = buttons[i]["window"].getSize()
  106.        
  107.         if x >= xButton and x <= xButton + width and y >= yButton and y <= yButton + height then
  108.             os.queueEvent("button", buttons[i]["name"], x, y)
  109.            
  110.             buttons[i]["func"](buttons[i])
  111.         end
  112.     end
  113. end
  114. function printButtonInfo()
  115.     for i = 1, #buttons do
  116.         print("--Button: " .. buttons[i]["name"])
  117.         print("X: " .. buttons[i]["x"] .. " Y: " .. buttons[i]["y"])
  118.         print("Width: " .. buttons[i]["width"] .. " Height: " .. buttons[i]["height"])
  119.         print("Text: " .. buttons[i]["text"])
  120.     end
  121. end
  122. function initEventHandler(eventHandler)
  123.     startParallel(ButtonAPI.buttonClickEvent, eventHandler)
  124. end
  125. function initEventHandler(eventHandler, mainFunc)
  126.     startParallel(ButtonAPI.buttonClickEvent, eventHandler, mainFunc)
  127. end
  128. function startParallel(function1, function2)
  129.     parallel.waitForAll(function1, function2)
  130. end
  131. function startParallel(function1, function2, function3)
  132.     parallel.waitForAll(function1, function2, function3)
  133. end
  134. function getButtonIndex(buttonName)
  135.     for i = 1, #buttons do
  136.         if buttons[i]["name"] == buttonName then
  137.             return i
  138.         end
  139.     end
  140. end
  141. function getButton(index)
  142.     return buttons[index]
  143. end
  144. function getFunc(index)
  145.     return getButton(index)["func"]
  146. end
  147. function setFunc(index, func)
  148.     getButton(index)["func"] = func
  149. end
  150. function getFuncParam(index)
  151.     return getButton(index)["funcParam"]
  152. end
  153. function setFuncParam(index, funcParam)
  154.     getButton(index)["funcParam"] = funcParam
  155. end
  156. function getButtonColor(index)
  157.     return getButton(index)["buttonColor"]
  158. end
  159. function setButtonColor(index, buttonColor)
  160.     getButton(index)["buttonColor"] = buttonColor
  161.     button["window"].setBackgroundColor(buttonColor)
  162. end
  163. function getTextColor(index)
  164.     return getButton(index)["textColor"]
  165. end
  166. function setTextColor(index, textColor)
  167.     getButton(index)["textColor"] = textColor
  168.     button["window"].setBackgroundColor(buttonColor)
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement