Advertisement
Bjornir90

guiAPI

Nov 23rd, 2012
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.30 KB | None | 0 0
  1. function getVersion()
  2.  return "1.6"
  3. end
  4.  
  5. --Declaration of variables
  6. runButton = {}
  7. runHeader = {}
  8. coordButton = {}
  9. list = {}
  10. sizeButtons = nil
  11. tickBoxT = {}
  12. coordTickBox = {}
  13. local path = "/guiAPI/log"
  14. --End of declaration
  15.  
  16. function initializeLog()
  17.  if fs.isDir("/guiAPI") then
  18.   local logFile = fs.open(path, "w")
  19.   logFile.write("Log initialized\n")
  20.   logFile.close()
  21.   isInitialized = true
  22.  else
  23.   if fs.exists("/guiAPI") then
  24.    error("Gui API : /guiAPI is not a directory, cannot create log file")
  25.   else
  26.    fs.makeDir("/guiAPI")
  27.    initializeLog()
  28.   end
  29.  end
  30. end
  31.  
  32.  
  33. function buttonClick(x, y, a, b)
  34.  event, clic, x1, y1 = os.pullEvent("mouse_click")
  35.  if x1 <= a and x1 >= x and y1 <= b and y1 >= y then
  36.  return "true"
  37.  else
  38.  return "false"
  39.  end
  40. end
  41.  
  42. function buttonWaitClick(x, y, a, b)
  43.  clicked = "false"
  44.  while clicked == "false" do
  45.   event, button, x1, y1 = os.pullEvent("mouse_click")
  46.   if  x1 <= a and x1 >= x and y1 <= b and y1 >= y then
  47.    clicked = "true"
  48.   else
  49.    clicked = "false"
  50.   end
  51.  end
  52.  return clicked
  53. end
  54.  
  55. function setButton(x, y, text, colorLine, color, clears)
  56. if clears == nil then
  57.  local clears = "false"
  58. end
  59. if tostring(clears) == "true" then
  60.  clear()
  61. end
  62. if colorLine then
  63.  colorL = colorLine
  64. else
  65.  colorL = colors.white
  66. end
  67. if color then
  68.  colorB = color
  69. else
  70.  colorB = colors.black
  71. end
  72.  local sizeText = #tostring(text)
  73.  a = x+1+sizeText
  74.  b = y+2
  75.  table.insert(runButton, x)
  76.  table.insert(runButton, y)
  77.  table.insert(runButton, a)
  78.  table.insert(runButton, b)
  79.  table.insert(runButton, text)
  80.  table.insert(runButton, colorL)
  81.  table.insert(runButton, colorB)
  82.  table.insert(coordButton, x)
  83.  table.insert(coordButton, y)
  84.  table.insert(coordButton, a)
  85.  table.insert(coordButton, b)
  86.  if isInitialized == true then
  87.   local logFile = fs.open(path, "a")
  88.   logFile.write("Set a button at "..x.."X and at "..y.."Y with text "..text.."\n")
  89.   logFile.close()
  90.  end
  91. end
  92.  
  93. function drawButton(x, y, a, b, text, colorLine, color)
  94.  if colorLine then
  95.   colorL = colorLine
  96.  else
  97.   colorL = colors.white
  98.  end
  99.  if color then
  100.   colorB = color
  101.  else
  102.   colorB = colors.black
  103.  end
  104.  paintutils.drawLine(x, y, a, y, colorL) --draw the top line
  105.  paintutils.drawLine(x, y, x, b, colorL) --draw the left line
  106.  paintutils.drawLine(a, y, a, b, colorL) --draw the right line
  107.  paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
  108.  fill(x+1, y+1, a-1, b-1, colorB)
  109.  term.setCursorPos(x+1, y+1)--Print the text in the button
  110.  term.write(text)
  111.  if isInitialized == true then
  112.   local logFile = fs.open(path, "a")
  113.   logFile.write("Drawn a button at "..x.."X and at "..y.."Y with text "..text.."\n")
  114.   logFile.close()
  115.  end
  116. end
  117.  
  118. function setHeader(text, colorLine)
  119.  if colorLine then
  120.   colorL = colorLine
  121.  else
  122.   colorL = colors.white
  123.  end
  124.  local text = tostring(text)
  125.  table.insert(runHeader, text)
  126.  table.insert(runHeader, colorL)
  127.  if isInitialized == true then
  128.   local logFile = fs.open(path, "a")
  129.   logFile.write("Set a header with text "..text.."\n")
  130.   logFile.close()
  131.  end
  132. end
  133.  
  134. function drawHeader(text, colorLine)
  135.  if colorLine then
  136.   colorL = colorLine
  137.  else
  138.   colorL = colors.white
  139.  end
  140.  xScreen, yScreen = term.getSize()
  141.  paintutils.drawLine(1, 1, xScreen, 1)
  142.  term.setCursorPos(1, 1)
  143.  write(text)
  144.  paintutils.drawLine(1, 2, xScreen, 2, colorL)
  145.  term.setCursorPos(1, 3)
  146.  term.setBackgroundColor(colors.black)
  147.  if isInitialized == true then
  148.   local logFile = fs.open(path, "a")
  149.   logFile.write("Drawn a header with text "..text.."\n")
  150.   logFile.close()
  151.  end
  152. end
  153.  
  154. function clear()
  155.  term.setCursorPos(1, 1)
  156.  term.setBackgroundColor(colors.black)
  157.  term.setTextColor(colors.white)
  158.  term.clear()
  159.  if isInitialized == true then
  160.   local logFile = fs.open(path, "a")
  161.   logFile.write("Cleared the screen\n")
  162.   logFile.close()
  163.  end
  164. end
  165.  
  166. function fill(x, y, a, b, color)
  167.  term.setBackgroundColor(color)
  168.  oldY = y
  169.  for x=x, a do
  170.   for y=y, b do
  171.    term.setCursorPos(x,y)
  172.    print(" ")
  173.   end
  174.  end
  175. end
  176.  
  177. function render()
  178.  if isInitialized == true then
  179.   local logFile = fs.open(path, "a")
  180.   logFile.write("----------------Started rendering------------------\n")
  181.   logFile.close()
  182.  end
  183.  clear()
  184.  local sizeBut = #runButton
  185.  local sizeHead = #runHeader
  186.  local sizeTickBox = #tickBoxT
  187.  for x=1, sizeBut, 7 do
  188.   drawButton(runButton[x], runButton[x+1], runButton[x+2], runButton[x+3], runButton[x+4], runButton[x+5], runButton[x+6]) --Run the functions to redraw it
  189.  end
  190.  for x=1, sizeHead, 2 do
  191.   drawHeader(runHeader[x], runHeader[x+1]) --Run the function to redraw it
  192.  end
  193.  for x=1, sizeTickBox, 4 do
  194.   drawTickBox(tickBoxT[x], tickBoxT[x+1], tickBoxT[x+2], tickBoxT[x+3])
  195.  
  196.  end
  197.  if reset == nil then
  198.   local reset = "false"
  199.  end
  200.  if tostring(reset) == "true" then
  201.   clearBuffer()
  202.  end
  203.  if isInitialized == true then
  204.   local logFile = fs.open(path, "a")
  205.   logFile.write("--------------Succesfully rendered-----------------\n")
  206.   logFile.close()
  207.  end
  208. end
  209.  
  210. function clearBuffer()
  211.  runButton = {}
  212.  runHeader = {}
  213.  tickBoxT = {}
  214.  if isInitialized == true then
  215.   local logFile = fs.open(path, "a")
  216.   logFile.write("Cleared buffer\n")
  217.   logFile.close()
  218.  end
  219. end
  220.  
  221. function getSelection()
  222.  local sizeButton = #coordButton
  223.  detectButton = {}
  224.  for x=1, sizeButton, 4 do
  225.   table.insert(detectButton, function() buttonWaitClick(coordButton[x], coordButton[x+1], coordButton[x+2], coordButton[x+3]) end)
  226.   end
  227.   local input = parallel.waitForAny(unpack(detectButton))
  228.  coordButton = {}
  229.  if isInitialized == true then
  230.   local logFile = fs.open(path, "a")
  231.   logFile.write("Got user selection : "..input.."\n")
  232.   logFile.close()
  233.  end
  234.  return input
  235. end
  236.  
  237. function animation(x, y, a, b, colorL)
  238.  m = a-x
  239.  n = x-a
  240.  if isInitialized == true then
  241.   local logFile = fs.open(path, "a")
  242.   logFile.write("-----------**Started pop-up animation**------------\nX : "..x.." Y : "..y.."\n")
  243.   logFile.close()
  244.  end
  245.  paintutils.drawLine(x+3, y+3, a-3, y-3, colorL) --draw the top line
  246.  paintutils.drawLine(x+3, y+3, x-3, b-3, colorL) --draw the left line
  247.  paintutils.drawLine(a+3, y+3, a-3, b-3, colorL) --draw the right line
  248.  paintutils.drawLine(x+3, b+3, a-3, b-3, colorL) --draw the bottom line
  249.  sleep(0.05)
  250.  render()
  251.  paintutils.drawLine(x+2, y+2, a-2, y-2, colorL) --draw the top line
  252.  paintutils.drawLine(x+2, y+2, x-2, b-2, colorL) --draw the left line
  253.  paintutils.drawLine(a+2, y+2, a-2, b-2, colorL) --draw the right line
  254.  paintutils.drawLine(x+2, b+2, a-2, b-2, colorL) --draw the bottom line
  255.  sleep(0.05)
  256.  render()
  257.  paintutils.drawLine(x+1, y+1, a-1, y-1, colorL) --draw the top line
  258.  paintutils.drawLine(x+1, y+1, x-1, b-1, colorL) --draw the left line
  259.  paintutils.drawLine(a+1, y+1, a-1, b-1, colorL) --draw the right line
  260.  paintutils.drawLine(x+1, b+1, a-1, b-1, colorL) --draw the bottom line
  261.  sleep(0.05)
  262.  render()
  263.  if isInitialized == true then
  264.   local logFile = fs.open(path, "a")
  265.   logFile.write("-----**Successfully ended pop-up animation**-------\n")
  266.   logFile.close()
  267.  end
  268. end
  269.  
  270. function popUp(title, text, x, y, colorLine, color, textB1, textB2)
  271.  render() --Render so the things drawn behind appears
  272.  sizeTitle = #title
  273.  sizeText = #text
  274. if textB2 ~= nil then
  275.  sizeB1 = #textB1
  276.  sizeB2 = #textB2
  277.  sizeButtons = sizeB2+sizeB1
  278.  mustButton = "true"
  279. else
  280.  sizeButtons = 0
  281.  mustButton = "false"
  282. end
  283.  if sizeTitle > sizeText and sizeTitle > sizeButtons then
  284.   greaterS = sizeTitle
  285.  elseif sizeText > sizeTitle and sizeText > sizeButtons then
  286.   greaterS = sizeText
  287.  else
  288.   greaterS = sizeButtons
  289.  end
  290. if colorLine then
  291.  colorL = colorLine
  292. else
  293.  colorL = colors.white
  294. end
  295. if color then
  296.  colorB = color
  297. else
  298. colorB = colors.black
  299. end
  300. local a = x + greaterS + 1
  301. local b = y + 4
  302. animation(x, y, a, b, colorL)
  303. paintutils.drawLine(x, y, a, y, colorL) --draw the top line
  304. paintutils.drawLine(x, y, x, b, colorL) --draw the left line
  305. paintutils.drawLine(a, y, a, b, colorL) --draw the right line
  306. paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
  307. fill(x+1, y+1, a-1, b-1, colorB)
  308. term.setCursorPos(x+1, y+1) --Print the title
  309. term.write(title)
  310. paintutils.drawLine(x, y+2, a, b-2, colorL) --Draw the line for the title
  311. term.setBackgroundColor(colorB)
  312. term.setCursorPos(x+1, y+3)
  313. term.write(text) --Print the text
  314.  if isInitialized == true then
  315.   local logFile = fs.open(path, "a")
  316.   logFile.write("Drawn a pop-up with :\nTitle : "..title.."\nText : "..text.."\nAt coordinates x : "..x.." and y : "..y.."\nDrawn buttons : "..mustButton.."\n--------------End of pop-up description------------\n")
  317.   logFile.close()
  318.  end
  319. if mustButton == "true" then
  320.  local midX = math.ceil((a-x)/2)
  321.  term.setCursorPos(midX-1-sizeB1, b)
  322.  term.write(textB1)
  323.  term.setCursorPos(midX+1, b)
  324.  term.write(textB2)
  325.  clickedB = parallel.waitForAny(function() buttonWaitClick(midX-sizeB1-1, b, midX-1, b) end, function() buttonWaitClick(midX+1, b, midX+sizeB2+1, b) end)
  326.  return clickedB
  327. else
  328.  clickedPopup = buttonWaitClick(x, y, a, b)
  329.  return clickedPopup
  330. end
  331. end
  332.  
  333. function setTickBox(x, y, isTrue, colorLine)
  334.  if colorLine then
  335.   colorL = colorLine
  336.  else
  337.   colorL = colors.white
  338.  end
  339.  if isTrue == nil or tostring(isTrue) == "false" then
  340.   isTrue = "false"
  341.  else
  342.   isTrue = "true"
  343.  end
  344.  table.insert(tickBoxT, x)
  345.  table.insert(tickBoxT, y)
  346.  table.insert(tickBoxT, colorL)
  347.  table.insert(tickBoxT, isTrue)
  348.  table.insert(coordTickBox, x)
  349.  table.insert(coordTickBox, y)
  350.  table.insert(coordTickBox, x+3)
  351.  table.insert(coordTickBox, y+3)
  352.  if isInitialized == true then
  353.   local logFile = fs.open(path, "a")
  354.   logFile.write("Set a tickbox at "..x.."X and at "..y.."Y isTrue = "..isTrue.."\n")
  355.   logFile.close()
  356.  end
  357. end
  358.  
  359. function drawTickBox(x, y, colorL, isTrue)
  360.  a = x+2
  361.  b = y+2
  362.  paintutils.drawLine(x, y, a, y, colorL) --draw the top line
  363.  paintutils.drawLine(x, y, x, b, colorL) --draw the left line
  364.  paintutils.drawLine(a, y, a, b, colorL) --draw the right line
  365.  paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
  366.  if isTrue == "true" then
  367.   colorC = colors.green
  368.   tW = "T"
  369.  elseif isTrue == "false" then
  370.   colorC = colors.red
  371.   tW = " "
  372.  end
  373.  term.setCursorPos(x+1, y+1)
  374.  term.setBackgroundColor(colors.black)
  375.  term.setTextColor(colorC)
  376.  write(tW)
  377.  term.setTextColor(colors.white)
  378.  if isInitialized == true then
  379.   local logFile = fs.open(path, "a")
  380.   logFile.write("Drawn a tickbox at "..x.."X and at "..y.."Y isTrue = "..isTrue.."\n")
  381.   logFile.close()
  382.  end
  383. end
  384.  
  385. function statTickBox(x,y,colorL,isTrue)
  386.  local tickBoxClicked = buttonClick(x, y, x+3, y+3)
  387.  if tickBoxClicked == true then
  388.   if isTrue == "true" then
  389.    isTrue = "false"
  390.   else
  391.    isTrue = "true"
  392.   end
  393.   drawTickBox(x,y,colorL,isTrue)
  394.  end
  395. end
  396.  
  397. function getSelectionTickBox()
  398.  local sizeTickBox = #coordTickBox
  399.  detectTickBox = {}
  400.  for x=1, sizeTickBox, 4 do
  401.   table.insert(detectTickBox, function() buttonWaitClick(coordTickBox[x], coordTickBox[x+1], coordTickBox[x+2], coordTickBox[x+3]) end)
  402.  end
  403.   local input = parallel.waitForAny(unpack(detectTickBox))
  404.  if isInitialized == true then
  405.   local logFile = fs.open(path, "a")
  406.   logFile.write("Got user selection : "..input.."\n")
  407.   logFile.close()
  408.  end
  409.  return input
  410. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement