Advertisement
k_goos

GuiAPI

Jan 20th, 2015
254
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---- This is an API used for drawing buttons,windows or labels on a ComputerCraft monitor ----
  2. ---- @Author ghosttje ----
  3.  
  4. ---- NOTES ----
  5. -- Also support for multiple monitors
  6. -- Animation still in development
  7. -- Still testing code
  8.  
  9. ---- EXAMPLE: HOW TO USE ----
  10. -- In progress (Still need to test first)
  11.  
  12. ---- FUNCTIONS ----
  13. -- [] = optional parameter
  14. -- function centerTextOnWindow(window, text)
  15. -- function initMonitor([backgroundColor], [textColor])
  16. -- function addMonitor(name, mon, [backgroundColor], [textColor])
  17. -- function addWindow(parentName, name, window, [backgroundColor], [textColor])
  18. -- function addNewWindow(parentName, name, x, y, width, height, [textColor], [backgroundColor])
  19. -- function addButtonMain(name, x, y, width, height, text, [func], [funcParam], [backgroundColor], [textColor])
  20. -- function addButton(parentName, name, x, y, width, height, text, [func], [funcParam], [backgroundColor], [textColor])
  21. -- function addLabelMain(name, x, y, text, [textColor])
  22. -- function addLabel(parentName, name, x, y, text, [textColor])
  23. -- function addAnimation(parentName, name, [func], [funcParam], [parentType])
  24. -- function draw()
  25. -- function drawItem(item)
  26. -- function toggleButton(button)
  27. -- function animateBlink(animation)
  28. -- function buttonClickEvent()
  29. -- function checkButton(x, y)
  30. -- function printButtonInfo()
  31. -- function initEventHandlerNone(eventHandler)
  32. -- function initEventHandler(eventHandler, mainFunc)
  33. -- function startParallelNone(function1, function2)
  34. -- function startParallel(function1, function2, function3)
  35.  
  36. ---- Getters And Setters Windows ----
  37. -- function getWindowName(index)
  38. -- function setWindowName(index, name)
  39. -- function changeWindowName(oldName, newName)
  40. -- function getWindowItem(name)
  41. -- function getWindow(name)
  42. -- function setWindow(name, window)
  43. -- function getWindowParent(name)
  44. -- function setWindowParent(name, parent)
  45. -- function getWindowBackgroundColor(name)
  46. -- function setWindowBackgroundColor(name, backgroundColor)
  47. -- function getWindowTextColor(name)
  48. -- function setWindowTextColor(name, textColor)
  49. -- function getWindowAnimationName(name)
  50. -- function setWindowAnimationName(name, animationName)
  51.  
  52. ---- Getters And Setters Buttons ----
  53. -- function getButtonName(index)
  54. -- function setButtonName(index, name)
  55. -- function changeButtonName(oldName, newName)
  56. -- function getButton(name)
  57. -- function getButtonFunction(name)
  58. -- function setButtonFunction(name, func)
  59. -- function getButtonFuncParam(name)
  60. -- function setButtonFuncParam(name, funcParam)
  61. -- function getButtonBackgroundColor(name)
  62. -- function setButtonBackgroundColor(name, backgroundColor)
  63. -- function getButtonTextColor(name)
  64. -- function setButtonTextColor(name, textColor)
  65. -- function getButtonText(name)
  66. -- function setButtonText(name, text)
  67. -- function getButtonWindow(name)
  68. -- function setButtonWindow(name, window)
  69. -- function getButtonParent(name)
  70. -- function setButtonParent(name, parent)
  71. -- function getButtonAnimationName(name)
  72. -- function setButtonAnimationName(name, animationName)
  73.  
  74. ---- Getters And Setters Labels ----
  75. -- function getLabelName(index)
  76. -- function setLabelName(index, name)
  77. -- function changeLabelName(oldName, newName)
  78. -- function getLabel(name)
  79. -- function getLabelX(name)
  80. -- function setLabelX(name, x)
  81. -- function getLabelY(name)
  82. -- function setLabelY(name, y)
  83. -- function getLabelText(name)
  84. -- function setLabelText(name, text)
  85. -- function getLabelParent(name)
  86. -- function setLabelParent(name, parentName)
  87. -- function getLabelTextColor(name)
  88. -- function setLabelTextColor(name, textColor)
  89. -- function getLabelAnimationName(name)
  90. -- function setLabelAnimationName(name, animationName)
  91.  
  92. ---- WINDOW DATA ----
  93. -- windows[name]["name"]
  94. -- windows[name]["window"]
  95. -- windows[name]["parent"]
  96. -- windows[name]["animationName"]
  97. -- windows[name]["textColor"]
  98. -- windows[name]["backgroundColor"]
  99. -- windows[name]["type"]
  100.  
  101. ---- BUTTON DATA ----
  102. -- buttons[name]["name"]
  103. -- buttons[name]["text"]
  104. -- buttons[name]["func"]
  105. -- buttons[name]["funcParam"]
  106. -- buttons[name]["animationName"]
  107. -- buttons[name]["window"]
  108. -- buttons[name]["textColor"]
  109. -- buttons[name]["backgroundColor"]
  110. -- buttons[name]["parent"]
  111. -- buttons[name]["type"]
  112.  
  113. ---- LABEL DATA ----
  114. -- labels[name]["name"]
  115. -- labels[name]["x"]
  116. -- labels[name]["y"]
  117. -- labels[name]["text"]
  118. -- labels[name]["parent"]
  119. -- labels[name]["animationName"]
  120. -- labels[name]["textColor"]
  121. -- labels[name]["type"]
  122.  
  123. local initMonitorName = "mainMonitor"
  124. local funcParamConst = {activeColor=colors.red, displayTime=2}
  125. local windows = {}
  126. local buttons = {}
  127. local labels = {}
  128. local animations = {}
  129.  
  130. ---- Local Functions ----
  131. local function createWindow(name, win, typeName, backgroundColor, textColor, parent, animationName)
  132.     parent = parent or ""
  133.     animationName = animationName or ""
  134.     textColor = textColor or colors.white
  135.     backgroundColor = backgroundColor or colors.black
  136.     typeName = typeName or "window"
  137.  
  138.     local window = {}
  139.     window["name"] = name
  140.     window["window"] = win
  141.     window["parent"] = parent
  142.     window["animationName"] = animationName
  143.     window["textColor"] = textColor
  144.     window["backgroundColor"] = backgroundColor
  145.     window["type"] = typeName
  146.    
  147.     return window
  148. end
  149. local function createButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
  150.     print("Adding button...")
  151.     print(name .. ": " .. text .. " (" .. x .. ", " .. y .. "),(" .. width .. ", " .. height .. ")")
  152.  
  153.     func = func or toggleButton
  154.     funcParam = funcParam or funcParamConst
  155.     backgroundColor = backgroundColor or colors.green
  156.     textColor = textColor or colors.white
  157.  
  158.     local button = {}
  159.     button["name"] = name
  160.     button["text"] = text
  161.     button["func"] = func
  162.     button["funcParam"] = funcParam
  163.     button["animationName"] = ""
  164.     button["window"] = window.create(windows[parentName]["window"], x, y, width, height, false)
  165.     button["textColor"] = textColor
  166.     button["backgroundColor"] = backgroundColor
  167.     button["parent"] = parentName
  168.     button["type"] = "button"
  169.     return button
  170. end
  171. local function createLabel(parentName, name, x, y, width, height, text, textColor, animationName)
  172.     textColor = textColor or ""
  173.     animationName = animationName or ""
  174.  
  175.     local label = {}
  176.     label["name"] = name
  177.     label["x"] = x
  178.     label["y"] = y
  179.     label["text"] = text
  180.     label["parent"] = parentName
  181.     label["animationName"] = animationName
  182.     label["textColor"] = textColor
  183.     label["type"] = "label"
  184.     return label
  185. end
  186. local function initColors(item)
  187.     if item["type"] ~= "label" then
  188.         item["window"].setBackgroundColor(item["backgroundColor"])
  189.     end
  190.     item["window"].setTextColor(item["textColor"])
  191. end
  192. local function drawScreen(window)
  193.     window["window"].setBackgroundColor(window["backgroundColor"])
  194.     window["window"].setTextColor(window["textColor"])
  195.     window["window"].clear()
  196. end
  197. local function drawMonitor(monitor)
  198.     drawScreen(monitor)
  199. end
  200. local function drawWindow(window)
  201.     drawScreen(window)
  202.     window["window"].setVisible(true)
  203. end
  204. local function drawButton(button)
  205.     button["window"].clear()
  206.     button["window"].setVisible(true)
  207.     centerTextOnWindow(button["window"], button["text"])
  208. end
  209. local function drawLabel(label)
  210.     label["window"].setTextColor(label["textColor"])
  211.     label["parent"].setCursorPos(label["x"], label["y"])
  212.     label["parent"].write(label["text"])
  213. end
  214.  
  215. ---- Public Functions ----
  216. function centerTextOnWindow(window, text)
  217.     local x, y = window.getPosition()
  218.     local width, height = window.getSize()
  219.     window.setCursorPos(math.ceil((width / 2) - (text:len() / 2) + 1), math.ceil(height / 2))
  220.     window.write(text)
  221. end
  222. function initMonitor(backgroundColor, textColor)
  223.     print("Init monitor")
  224.    
  225.     windows[initMonitorName] = createWindow(initMonitorName, peripheral.find("monitor"), "monitor", backgroundColor, textColor)
  226.     return "mainMonitor"
  227. end
  228. function addMonitor(name, mon, backgroundColor, textColor)
  229.     windows[name] = createWindow(name, mon, "monitor", backgroundColor, textColor)
  230. end
  231. function addWindow(parentName, name, window, backgroundColor, textColor)
  232.     textColor = textColor or window.getTextColor()
  233.     backgroundColor = backgroundColor or window.getBackgroundColor()
  234.  
  235.     windows[name] = createWindow(name, window, "window", backgroundColor, textColor, parentName)
  236. end
  237. function addNewWindow(parentName, name, x, y, width, height, backgroundColor, textColor)
  238.     local window = window.create(mon, x, y, width, height, false)
  239.     backgroundColor = backgroundColor or colors.lightGray
  240.     textColor = textColor or colors.white
  241.    
  242.     windows[name] = createWindow(name, window, "window", backgroundColor, textColor, parentName)
  243. end
  244. function addButtonMain(name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
  245.     print("Add to main monitor")
  246.     parentName = initMonitorName
  247.     buttons[name] = createButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
  248. end
  249. function addButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
  250.     print("Add to " .. parentName)
  251.     buttons[name] = createButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
  252. end
  253. function addLabelMain(name, x, y, text, textColor)
  254.     parentName = initMonitorName
  255.     labels[name] = createLabel(parentName, name, x, y, text, textColor)
  256. end
  257. function addLabel(parentName, name, x, y, text, textColor)
  258.     labels[name] = createLabel(parentName, name, x, y, text, textColor)
  259. end
  260. function addAnimation(parentName, name, func, param, parentType)
  261.     parentType = parentType or "button"
  262.     func = func or toggleButton
  263.     params = {displayTime = 1, activeColor = colors.blue}
  264.     funcParam = funcParam or params
  265.    
  266.     animations[name]["name"] = name
  267.     animations[name]["parent"] = parentName
  268.     animations[name]["func"] = func
  269.     animations[name]["funcParam"] = funcParam
  270.     animations[name]["parentType"] = parentType
  271.     animations[name]["type"] = "animation"
  272. end
  273. function draw()
  274.     print("Starting drawing...")
  275.    
  276.     for k, v in pairs(windows) do
  277.         initColors(v)
  278.         if v["type"] == "monitor" then
  279.             drawMonitor(v)
  280.         else
  281.             drawWindow(v)
  282.         end
  283.     end
  284.     for k, v in pairs(buttons) do
  285.         initColors(v)
  286.         drawButton(v)
  287.     end
  288.     for k, v in pairs(labels) do
  289.         initColors(v)
  290.         drawLabel(v)
  291.     end
  292.    
  293.     print("Drawing done")
  294. end
  295. function drawItem(item)
  296.     print("Redraw: " .. item["name"])
  297.     if item["type"] == "monitor" then
  298.         drawMonitor(item)
  299.     elseif item["type"] == "window" then
  300.         drawWindow(item)
  301.     elseif item["type"] == "button" then
  302.         drawButton(item)
  303.     elseif item["type"] == "label" then
  304.         drawLabel(item)
  305.     end
  306. end
  307. function toggleButton(button)
  308.     button["window"].setBackgroundColor(button["funcParam"]["activeColor"])
  309.     drawItem(button)
  310.     sleep(button["funcParam"]["displayTime"])
  311.     button["window"].setBackgroundColor(button["backgroundColor"])
  312.     drawItem(button)
  313. end
  314. function animateBlink(animation)
  315.     local item
  316.     if animation["parentType"] == "button" then
  317.         item = buttons[animation["parent"]]
  318.     elseif animation["parentType"] == "monitor" or animation["parentType"] == "window" then
  319.         item = windows[animation["parent"]]
  320.     else
  321.         return
  322.     end
  323.    
  324.     item["window"].setBackgroundColor(item["animateParam"]["blinkColor"])
  325.     sleep(animation["funcParam"]["displayTime"])
  326.     item["window"].setBackgroundColor(item["backgroundColor"])
  327. end
  328. function buttonClickEvent()
  329.     while true do
  330.         print("ButtonClickTest")
  331.         event, side, x, y = os.pullEvent("monitor_touch")
  332.         checkButton(x, y)
  333.     end
  334. end
  335. function checkButton(x, y)
  336.     for k, v in pairs(buttons) do
  337.         local xOffset = 0
  338.         local yOffset = 0
  339.         local parentName = v["parent"]
  340.    
  341.         while parentName ~= initMonitorName and parentName ~= "" do
  342.             local tX, tY = windows[parentName]["window"].getPosition()
  343.             xOffset = xOffset + tX
  344.             yffset = yOffset + tY
  345.            
  346.             parentName = windows[parentName]["parent"]
  347.         end
  348.        
  349.         local xButton, yButton = v["window"].getPosition()
  350.         local width, height = v["window"].getSize()
  351.        
  352.         if x >= xButton + xOffset and x <= xButton + xOffset + width and y >= yButton + yOffset and y <= yButton + yOffset + height then
  353.             os.queueEvent("button", v["name"], x, y)
  354.            
  355.             if v["func"] ~= "" then
  356.                 v["func"](v)
  357.             end
  358.         end
  359.     end
  360. end
  361. function printInfo()
  362.     for k, v in pairs(windows) do
  363.         print("--Window: " .. v["name"])
  364.         if v["type"] == "window" then
  365.             local x, y = v["window"].getPosition()
  366.             print("Location: (" .. x .. ", " .. y .. ")")
  367.         end
  368.         local width, height = v["window"].getSize()
  369.         print("Size: (" .. width .. ", " .. height .. ")")
  370.         print("Parent: " .. v["parent"])
  371.     end
  372.     for k, v in pairs(buttons) do
  373.         print("--Button: " .. v["name"])
  374.         local x, y = v["window"].getPosition()
  375.         print("Location: (" .. x .. ", " .. y .. ")")
  376.         local width, height = v["window"].getSize()
  377.         print("Size: (" .. width .. ", " .. height .. ")")
  378.         print("Text: " .. v["text"])
  379.     end
  380.     for k, v in pairs(labels) do
  381.         print("--Label: " .. v["name"])
  382.         print("Location: (" .. x .. ", " .. y .. ")")
  383.         print("Text: " .. v["text"])
  384.         print("Parent: " .. v["parent"])
  385.     end
  386. end
  387. function initEventHandlerNone(eventHandler)
  388.     startParallelNone(buttonClickEvent, eventHandler)
  389. end
  390. function initEventHandler(eventHandler, mainFunc)
  391.     startParallel(buttonClickEvent, eventHandler, mainFunc)
  392. end
  393. function startParallelNone(function1, function2)
  394.     parallel.waitForAll(function1, function2)
  395. end
  396. function startParallel(function1, function2, function3)
  397.     parallel.waitForAll(function1, function2, function3)
  398. end
  399.  
  400. ---- Getters And Setters Windows ----
  401. function getWindowName(index)
  402.     return windows[index]["name"]
  403. end
  404. function setWindowName(index, name)
  405.     local window = windows[index]
  406.     table.remove(index)
  407.    
  408.     windows[name] = window
  409. end
  410. function changeWindowName(oldName, newName)
  411.     local window = windows[oldName]
  412.     table.remove(oldName)
  413.    
  414.     windows[newName] = window
  415. end
  416. function getWindowItem(name)
  417.     return windows[name]
  418. end
  419. function getWindow(name)
  420.     return windows[name]["window"]
  421. end
  422. function setWindow(name, window)
  423.     windows[name]["window"] = window
  424. end
  425. function getWindowParent(name)
  426.     return windows[name]["parent"]
  427. end
  428. function setWindowParent(name, parent)
  429.     windows[name]["parent"] = parent
  430. end
  431. function getWindowBackgroundColor(name)
  432.     return windows[name]["backgroundColor"]
  433. end
  434. function setWindowBackgroundColor(name, backgroundColor)
  435.     windows[name]["backgroundColor"] = backgroundColor
  436. end
  437. function getWindowTextColor(name)
  438.     return windows[name]["textColor"]
  439. end
  440. function setWindowTextColor(name, textColor)
  441.     windows[name]["textColor"] = textColor
  442. end
  443. function getWindowAnimationName(name)
  444.     return windows[name]["animationName"]
  445. end
  446. function setWindowAnimationName(name, animationName)
  447.     windows[name]["animationName"] = animationName
  448. end
  449.  
  450. ---- Getters And Setters Buttons ----
  451. function getButtonName(index)
  452.     return buttons[index]["name"]
  453. end
  454. function setButtonName(index, name)
  455.     local button = buttons[index]
  456.     table.remove(index)
  457.    
  458.     buttons[name] = button
  459. end
  460. function changeButtonName(oldName, newName)
  461.     local button = buttons[oldName]
  462.     table.remove(oldName)
  463.    
  464.     buttons[newName] = button
  465. end
  466. function getButton(name)
  467.     return buttons[name]
  468. end
  469. function getButtonFunction(name)
  470.     return buttons[name]["func"]
  471. end
  472. function setButtonFunction(name, func)
  473.     buttons[name]["func"] = func
  474. end
  475. function getButtonFuncParam(name)
  476.     return buttons[name]["funcParam"]
  477. end
  478. function setButtonFuncParam(name, funcParam)
  479.     buttons[name]["funcParam"] = funcParam
  480. end
  481. function getButtonBackgroundColor(name)
  482.     return buttons[name]["backgroundColor"]
  483. end
  484. function setButtonBackgroundColor(name, backgroundColor)
  485.     buttons[name]["backgroundColor"] = backgroundColor
  486. end
  487. function getButtonTextColor(name)
  488.     return buttons[name]["textColor"]
  489. end
  490. function setButtonTextColor(name, textColor)
  491.     buttons[name]["textColor"] = textColor
  492. end
  493. function getButtonText(name)
  494.     return buttons[name]["text"]
  495. end
  496. function setButtonText(name, text)
  497.     buttons[name]["text"] = text
  498. end
  499. function getButtonWindow(name)
  500.     return buttons[name]["window"]
  501. end
  502. function setButtonWindow(name, window)
  503.     buttons[name]["window"] = text
  504. end
  505. function getButtonParent(name)
  506.     return buttons[name]["parent"]
  507. end
  508. function setButtonParent(name, parent)
  509.     buttons[name]["parent"] = parent
  510.    
  511.     local x, y = buttons[name]["window"].getPosition()
  512.     local width, height = buttons[name]["window"].getSize()
  513.    
  514.     buttons[name]["window"] = window.create(parent, x, y, width, height, buttons[name]["textColor"], buttons[name]["backgroundColor"])
  515. end
  516. function getButtonAnimationName(name)
  517.     return buttons[name]["animationName"]
  518. end
  519. function setButtonAnimationName(name, animationName)
  520.     buttons[name]["animationName"] = animationName
  521. end
  522.  
  523. ---- Getters And Setters Labels ----
  524. function getLabelName(index)
  525.     return buttons[index]["name"]
  526. end
  527. function setLabelName(index, name)
  528.     local label = labels[index]
  529.     table.remove(index)
  530.    
  531.     labels[name] = label
  532. end
  533. function changeLabelName(oldName, newName)
  534.     local label = labels[oldName]
  535.     table.remove(oldName)
  536.    
  537.     labels[newName] = label
  538. end
  539. function getLabel(name)
  540.     return labels[name]
  541. end
  542. function getLabelX(name)
  543.     return labels[name]["x"]
  544. end
  545. function setLabelX(name, x)
  546.     labels[name]["x"] = x
  547. end
  548. function getLabelY(name)
  549.     return labels[name]["y"]
  550. end
  551. function setLabelY(name, y)
  552.     labels[name]["y"] = y
  553. end
  554. function getLabelText(name)
  555.     return labels[name]["text"]
  556. end
  557. function setLabelText(name, text)
  558.     labels[name]["text"] = text
  559. end
  560. function getLabelParent(name)
  561.     return labels[name]["parent"]
  562. end
  563. function setLabelParent(name, parentName)
  564.     labels[name]["parent"] = parentName
  565. end
  566. function getLabelTextColor(name)
  567.     return labels[name]["textColor"]
  568. end
  569. function setLabelTextColor(name, textColor)
  570.     labels[name]["textColor"] = textColor
  571. end
  572. function getLabelAnimationName(name)
  573.     return labels[name]["animationName"]
  574. end
  575. function setLabelAnimationName(name, animationName)
  576.     labels[name]["animationName"] = animationName
  577. end
Advertisement
RAW Paste Data Copied
Advertisement