Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getVersion()
- return "1.6"
- end
- --Declaration of variables
- runButton = {}
- runHeader = {}
- coordButton = {}
- list = {}
- sizeButtons = nil
- tickBoxT = {}
- coordTickBox = {}
- local path = "/guiAPI/log"
- --End of declaration
- function initializeLog()
- if fs.isDir("/guiAPI") then
- local logFile = fs.open(path, "w")
- logFile.write("Log initialized\n")
- logFile.close()
- isInitialized = true
- else
- if fs.exists("/guiAPI") then
- error("Gui API : /guiAPI is not a directory, cannot create log file")
- else
- fs.makeDir("/guiAPI")
- initializeLog()
- end
- end
- end
- function buttonClick(x, y, a, b)
- event, clic, x1, y1 = os.pullEvent("mouse_click")
- if x1 <= a and x1 >= x and y1 <= b and y1 >= y then
- return "true"
- else
- return "false"
- end
- end
- function buttonWaitClick(x, y, a, b)
- clicked = "false"
- while clicked == "false" do
- event, button, x1, y1 = os.pullEvent("mouse_click")
- if x1 <= a and x1 >= x and y1 <= b and y1 >= y then
- clicked = "true"
- else
- clicked = "false"
- end
- end
- return clicked
- end
- function setButton(x, y, text, colorLine, color, clears)
- if clears == nil then
- local clears = "false"
- end
- if tostring(clears) == "true" then
- clear()
- end
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- if color then
- colorB = color
- else
- colorB = colors.black
- end
- local sizeText = #tostring(text)
- a = x+1+sizeText
- b = y+2
- table.insert(runButton, x)
- table.insert(runButton, y)
- table.insert(runButton, a)
- table.insert(runButton, b)
- table.insert(runButton, text)
- table.insert(runButton, colorL)
- table.insert(runButton, colorB)
- table.insert(coordButton, x)
- table.insert(coordButton, y)
- table.insert(coordButton, a)
- table.insert(coordButton, b)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Set a button at "..x.."X and at "..y.."Y with text "..text.."\n")
- logFile.close()
- end
- end
- function drawButton(x, y, a, b, text, colorLine, color)
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- if color then
- colorB = color
- else
- colorB = colors.black
- end
- paintutils.drawLine(x, y, a, y, colorL) --draw the top line
- paintutils.drawLine(x, y, x, b, colorL) --draw the left line
- paintutils.drawLine(a, y, a, b, colorL) --draw the right line
- paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
- fill(x+1, y+1, a-1, b-1, colorB)
- term.setCursorPos(x+1, y+1)--Print the text in the button
- term.write(text)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Drawn a button at "..x.."X and at "..y.."Y with text "..text.."\n")
- logFile.close()
- end
- end
- function setHeader(text, colorLine)
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- local text = tostring(text)
- table.insert(runHeader, text)
- table.insert(runHeader, colorL)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Set a header with text "..text.."\n")
- logFile.close()
- end
- end
- function drawHeader(text, colorLine)
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- xScreen, yScreen = term.getSize()
- paintutils.drawLine(1, 1, xScreen, 1)
- term.setCursorPos(1, 1)
- write(text)
- paintutils.drawLine(1, 2, xScreen, 2, colorL)
- term.setCursorPos(1, 3)
- term.setBackgroundColor(colors.black)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Drawn a header with text "..text.."\n")
- logFile.close()
- end
- end
- function clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Cleared the screen\n")
- logFile.close()
- end
- end
- function fill(x, y, a, b, color)
- term.setBackgroundColor(color)
- oldY = y
- for x=x, a do
- for y=y, b do
- term.setCursorPos(x,y)
- print(" ")
- end
- end
- end
- function render()
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("----------------Started rendering------------------\n")
- logFile.close()
- end
- clear()
- local sizeBut = #runButton
- local sizeHead = #runHeader
- local sizeTickBox = #tickBoxT
- for x=1, sizeBut, 7 do
- 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
- end
- for x=1, sizeHead, 2 do
- drawHeader(runHeader[x], runHeader[x+1]) --Run the function to redraw it
- end
- for x=1, sizeTickBox, 4 do
- drawTickBox(tickBoxT[x], tickBoxT[x+1], tickBoxT[x+2], tickBoxT[x+3])
- end
- if reset == nil then
- local reset = "false"
- end
- if tostring(reset) == "true" then
- clearBuffer()
- end
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("--------------Succesfully rendered-----------------\n")
- logFile.close()
- end
- end
- function clearBuffer()
- runButton = {}
- runHeader = {}
- tickBoxT = {}
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Cleared buffer\n")
- logFile.close()
- end
- end
- function getSelection()
- local sizeButton = #coordButton
- detectButton = {}
- for x=1, sizeButton, 4 do
- table.insert(detectButton, function() buttonWaitClick(coordButton[x], coordButton[x+1], coordButton[x+2], coordButton[x+3]) end)
- end
- local input = parallel.waitForAny(unpack(detectButton))
- coordButton = {}
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Got user selection : "..input.."\n")
- logFile.close()
- end
- return input
- end
- function animation(x, y, a, b, colorL)
- m = a-x
- n = x-a
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("-----------**Started pop-up animation**------------\nX : "..x.." Y : "..y.."\n")
- logFile.close()
- end
- paintutils.drawLine(x+3, y+3, a-3, y-3, colorL) --draw the top line
- paintutils.drawLine(x+3, y+3, x-3, b-3, colorL) --draw the left line
- paintutils.drawLine(a+3, y+3, a-3, b-3, colorL) --draw the right line
- paintutils.drawLine(x+3, b+3, a-3, b-3, colorL) --draw the bottom line
- sleep(0.05)
- render()
- paintutils.drawLine(x+2, y+2, a-2, y-2, colorL) --draw the top line
- paintutils.drawLine(x+2, y+2, x-2, b-2, colorL) --draw the left line
- paintutils.drawLine(a+2, y+2, a-2, b-2, colorL) --draw the right line
- paintutils.drawLine(x+2, b+2, a-2, b-2, colorL) --draw the bottom line
- sleep(0.05)
- render()
- paintutils.drawLine(x+1, y+1, a-1, y-1, colorL) --draw the top line
- paintutils.drawLine(x+1, y+1, x-1, b-1, colorL) --draw the left line
- paintutils.drawLine(a+1, y+1, a-1, b-1, colorL) --draw the right line
- paintutils.drawLine(x+1, b+1, a-1, b-1, colorL) --draw the bottom line
- sleep(0.05)
- render()
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("-----**Successfully ended pop-up animation**-------\n")
- logFile.close()
- end
- end
- function popUp(title, text, x, y, colorLine, color, textB1, textB2)
- render() --Render so the things drawn behind appears
- sizeTitle = #title
- sizeText = #text
- if textB2 ~= nil then
- sizeB1 = #textB1
- sizeB2 = #textB2
- sizeButtons = sizeB2+sizeB1
- mustButton = "true"
- else
- sizeButtons = 0
- mustButton = "false"
- end
- if sizeTitle > sizeText and sizeTitle > sizeButtons then
- greaterS = sizeTitle
- elseif sizeText > sizeTitle and sizeText > sizeButtons then
- greaterS = sizeText
- else
- greaterS = sizeButtons
- end
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- if color then
- colorB = color
- else
- colorB = colors.black
- end
- local a = x + greaterS + 1
- local b = y + 4
- animation(x, y, a, b, colorL)
- paintutils.drawLine(x, y, a, y, colorL) --draw the top line
- paintutils.drawLine(x, y, x, b, colorL) --draw the left line
- paintutils.drawLine(a, y, a, b, colorL) --draw the right line
- paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
- fill(x+1, y+1, a-1, b-1, colorB)
- term.setCursorPos(x+1, y+1) --Print the title
- term.write(title)
- paintutils.drawLine(x, y+2, a, b-2, colorL) --Draw the line for the title
- term.setBackgroundColor(colorB)
- term.setCursorPos(x+1, y+3)
- term.write(text) --Print the text
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- 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")
- logFile.close()
- end
- if mustButton == "true" then
- local midX = math.ceil((a-x)/2)
- term.setCursorPos(midX-1-sizeB1, b)
- term.write(textB1)
- term.setCursorPos(midX+1, b)
- term.write(textB2)
- clickedB = parallel.waitForAny(function() buttonWaitClick(midX-sizeB1-1, b, midX-1, b) end, function() buttonWaitClick(midX+1, b, midX+sizeB2+1, b) end)
- return clickedB
- else
- clickedPopup = buttonWaitClick(x, y, a, b)
- return clickedPopup
- end
- end
- function setTickBox(x, y, isTrue, colorLine)
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- if isTrue == nil or tostring(isTrue) == "false" then
- isTrue = "false"
- else
- isTrue = "true"
- end
- table.insert(tickBoxT, x)
- table.insert(tickBoxT, y)
- table.insert(tickBoxT, colorL)
- table.insert(tickBoxT, isTrue)
- table.insert(coordTickBox, x)
- table.insert(coordTickBox, y)
- table.insert(coordTickBox, x+3)
- table.insert(coordTickBox, y+3)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Set a tickbox at "..x.."X and at "..y.."Y isTrue = "..isTrue.."\n")
- logFile.close()
- end
- end
- function drawTickBox(x, y, colorL, isTrue)
- a = x+2
- b = y+2
- paintutils.drawLine(x, y, a, y, colorL) --draw the top line
- paintutils.drawLine(x, y, x, b, colorL) --draw the left line
- paintutils.drawLine(a, y, a, b, colorL) --draw the right line
- paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
- if isTrue == "true" then
- colorC = colors.green
- tW = "T"
- elseif isTrue == "false" then
- colorC = colors.red
- tW = " "
- end
- term.setCursorPos(x+1, y+1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colorC)
- write(tW)
- term.setTextColor(colors.white)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Drawn a tickbox at "..x.."X and at "..y.."Y isTrue = "..isTrue.."\n")
- logFile.close()
- end
- end
- function statTickBox(x,y,colorL,isTrue)
- local tickBoxClicked = buttonClick(x, y, x+3, y+3)
- if tickBoxClicked == true then
- if isTrue == "true" then
- isTrue = "false"
- else
- isTrue = "true"
- end
- drawTickBox(x,y,colorL,isTrue)
- end
- end
- function getSelectionTickBox()
- local sizeTickBox = #coordTickBox
- detectTickBox = {}
- for x=1, sizeTickBox, 4 do
- table.insert(detectTickBox, function() buttonWaitClick(coordTickBox[x], coordTickBox[x+1], coordTickBox[x+2], coordTickBox[x+3]) end)
- end
- local input = parallel.waitForAny(unpack(detectTickBox))
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Got user selection : "..input.."\n")
- logFile.close()
- end
- return input
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement