Advertisement
Bjornir90

guiAPI beta

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