Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Variables ]]--
- calc = ""
- screenX, screenY = term.getSize()
- if fs.exists("calcColor") == true then
- h = fs.open("calcColor", "r")
- borderColour = loadstring("return "..h.readLine())()
- h.close()
- else
- h = fs.open("calcColor", "w")
- h.write("colors.white")
- h.close()
- borderColour = colors.white
- end --if true then read file and set color to what was set before, if not then set to default of white
- -- Tables
- buttonsMain = {}
- buttonsExtra = {}
- buttonsColors = {}
- input = {nil, nil, nil}
- --[[ End of variables ]]--
- --[[ Functions ]]--
- function printVerticalLine(lineX, startingY, endingY)
- term.setBackgroundColor(borderColour)
- for lineY = startingY, endingY do
- term.setCursorPos(lineX, lineY)
- term.write(" ")
- end
- end
- function printHorizontalLine(yLine, startingX, endingX)
- term.setBackgroundColor(borderColour)
- term.setCursorPos(startingX, yLine)
- write(string.rep(" ", endingX - startingX + 1))
- end
- function drawMain()
- -- Left part (for numbers)
- printVerticalLine(2,2,18)
- --Left Border
- printVerticalLine(20,2,18)
- --Right Border
- printVerticalLine(8,6,18)
- --Left Seperator
- printVerticalLine(14,6,18)
- --Right Seperator
- printHorizontalLine(2,2,20)
- --Top
- printHorizontalLine(6,2, 20)
- --Upper Before Keys
- printHorizontalLine(18,2,20)
- --Bottom
- printHorizontalLine(10,2,20)
- --Upper Seperator
- printHorizontalLine(14,2,20)
- --Lower Seperator
- term.setBackgroundColor(colors.black)
- -- Middle area (F, H, c)
- printVerticalLine(23,6,18)
- --Left Border
- printVerticalLine(29,6,18)
- --Right Border
- printHorizontalLine(6,23,29)
- --Top
- printHorizontalLine(10,23,29)
- printHorizontalLine(14,23,29)
- printHorizontalLine(18,23,29)
- -- Right side (0, math synbols)
- printHorizontalLine(6,32,50)
- --Top
- printHorizontalLine(10,32,50)
- printHorizontalLine(14,32,50)
- printHorizontalLine(18,32,50)
- --Bottom
- printVerticalLine(32,6,18)
- --Left Border
- printVerticalLine(38,6,14)
- printVerticalLine(44,6,18)
- printVerticalLine(50,6,18)
- term.setBackgroundColor(colors.black)
- end
- function drawColorsTable()
- printVerticalLine(2,2,18)
- printVerticalLine(14,2,18)
- printVerticalLine(26,2,18)
- printVerticalLine(40,2,18)
- printVerticalLine(50,2,18)
- printHorizontalLine(2, 3, screenX-2)
- printHorizontalLine(6, 3, screenX-2)
- printHorizontalLine(10, 3, screenX-2)
- printHorizontalLine(14, 3, screenX-2)
- printHorizontalLine(18, 3, screenX-2)
- term.setBackgroundColor(colors.black)
- end
- function setTable(_table, name, xMin, xMax, yMin, yMax, customX, col)
- table.insert(_table, {
- numb = name,
- xmin = xMin,
- xmax = xMax,
- ymin = yMin,
- ymax = yMax,
- cX = customX or nil,
- txtCol = col or colours.white
- })
- end
- function fillTable()
- setTable(buttonsMain, "1", 3, 7, 15, 17)
- setTable(buttonsMain, "2", 9, 13, 15, 17)
- setTable(buttonsMain, "3", 15, 19, 15, 17)
- setTable(buttonsMain, "4", 3, 7, 11, 13)
- setTable(buttonsMain, "5", 9, 13, 11, 13)
- setTable(buttonsMain, "6", 15, 19, 11, 13)
- setTable(buttonsMain, "7", 3, 7, 7, 9)
- setTable(buttonsMain, "8", 9, 13, 7, 9)
- setTable(buttonsMain, "9", 15, 19, 7, 9)
- setTable(buttonsExtra, "1", 3, 7, 15, 17)
- setTable(buttonsExtra, "2", 9, 13, 15, 17)
- setTable(buttonsExtra, "3", 15, 19, 15, 17)
- setTable(buttonsExtra, "4", 3, 7, 11, 13)
- setTable(buttonsExtra, "5", 9, 13, 11, 13)
- setTable(buttonsExtra, "6", 15, 19, 11, 13)
- setTable(buttonsExtra, "7", 3, 7, 7, 9)
- setTable(buttonsExtra, "8", 9, 13, 7, 9)
- setTable(buttonsExtra, "9", 15, 19, 7, 9)
- end
- function fillFunctionsTable()
- setTable(buttonsMain, "C", 33,37,7,9)
- setTable(buttonsMain, ".", 39,43,7,9)
- setTable(buttonsMain, "/", 45,49,7,9)
- setTable(buttonsMain, "+", 33,37,11,13)
- setTable(buttonsMain, "-", 39,43,11,13)
- setTable(buttonsMain, "*", 45,49,11,13)
- setTable(buttonsMain, "0", 33,43,15,17)
- setTable(buttonsMain, "=", 45,49,15,17)
- end
- function fillExtraFunctions()
- setTable(buttonsExtra, "^", 33,37,7,9)
- setTable(buttonsExtra, "rt", 39,43,7,9)
- setTable(buttonsExtra, "/", 45,49,7,9)
- setTable(buttonsExtra, "+", 33,37,11,13)
- setTable(buttonsExtra, "-", 39,43,11,13)
- setTable(buttonsExtra, "*", 45,49,11,13)
- setTable(buttonsExtra, "0", 33,43,15,17)
- setTable(buttonsExtra, "=", 45,49,15,17)
- end
- function fillExtraButtonsTable()
- setTable(buttonsExtra, "F",24,28,7,9)
- setTable(buttonsExtra, "H",24,28,11,13)
- setTable(buttonsExtra, "c",24,28,15,17)
- setTable(buttonsMain, "F",24,28,7,9)
- setTable(buttonsMain, "H",24,28,11,13)
- setTable(buttonsMain, "c",24,28,15,17)
- end
- function fillColorOptions()
- setTable(buttonsColors,"White",3,13,3,5, 6, colours.white)
- setTable(buttonsColors,"Orange",3,13,7,9, 6, colours.orange)
- setTable(buttonsColors,"Magenta",3,13,11,13, 5, colours.magenta)
- setTable(buttonsColors,"Light Blue",3,13,15,17, 4, colours.lightBlue)
- setTable(buttonsColors,"Yellow",15,25,3,5, 17, colours.yellow)
- setTable(buttonsColors,"Lime",15,25,7,9, 18, colours.lime)
- setTable(buttonsColors,"Pink",15,25,11,13, 18, colours.pink)
- setTable(buttonsColors,"Gray",15,25,15,17, 18, colours.grey)
- setTable(buttonsColors,"Light Gray",27,37,3,5, 28, colours.lightGrey)
- setTable(buttonsColors,"Cyan",27,37,7,9, 32, colours.cyan)
- setTable(buttonsColors,"Purple",27,37,11,13, 30, colours.purple)
- setTable(buttonsColors,"Blue",27,37,15,17, 31, colours.blue)
- setTable(buttonsColors,"Brown",39,49,3,5, 43, colours.brown)
- setTable(buttonsColors,"Green",39,49,7,9, 43, colours.green)
- setTable(buttonsColors,"Red",39,49,11,13, 44, colours.red)
- setTable(buttonsColors,"Black",39,49,15,17, 43, colours.white)
- end
- function checkClick(tab, mx, my)
- for i, v in pairs(tab) do
- if mx >= v.xmin and mx <= v.xmax and my >= v.ymin and my <= v.ymax then
- return true, v.numb
- end
- end
- return false, nil
- end
- function screen(tab)
- term.setBackgroundColour(colours.black)
- for i, data in pairs(tab) do
- local yspot = (data.ymin + data.ymax) / 2
- local xspot = data.cX or (data.xmin + data.xmax) / 2
- term.setCursorPos(xspot, yspot)
- term.setTextColour(data.txtCol)
- write(data.numb)
- end
- end
- function writeColor()
- h = fs.open("calcColor", "w")
- h.write(borderColour)
- h.close()
- end
- function redraw(t)
- term.setCursorPos(4, 4)
- write(string.rep(" ", 15))
- term.setCursorPos(4, 4)
- write(t or "")
- end
- function doSum()
- input[1] = tonumber(calc) or input[1]
- input[2] = numb
- redraw()
- calc = ""
- numb = ""
- end
- function enterPress()
- input[3] = tonumber(calc)
- if input[2] == "+" then
- Output = input[1] + input[3]
- elseif input[2] == "-" then
- Output = input[1] - input[3]
- elseif input[2] == "*" then
- Output = input[1] * input[3]
- elseif input[2] == "/" then
- Output = input[1] / input[3]
- elseif input[2] == "^" then
- Output = input[1] ^ input[3]
- elseif input[2] == "rt" then
- Output = math.sqrt(input[1])
- end
- redraw(Output)
- calc = tostring(Output)
- input[1] = Output
- end
- --[[ Functions for the middle section: Choosing a new border colour
- and to display help.
- --]]
- function chooseColour()
- term.setBackgroundColour(colours.black)
- term.setCursorPos(1, 1)
- term.clear()
- term.setBackgroundColour(colours.grey)
- write(string.rep(" ", screenX))
- term.setBackgroundColour(colours.red)
- term.setCursorPos(screenX, 1)
- write("X")
- while true do
- screen(buttonsColors)
- drawColorsTable()
- local e, but, x, y = os.pullEvent("mouse_click")
- if but == 1 then
- if x == screenX and y == 1 then
- term.clear()
- screen(buttonsMain)
- drawMain()
- redraw(calc)
- break
- else
- bValid, colChoice = checkClick(buttonsColors, x, y)
- if bValid then
- if colChoice == "White" then
- borderColour = colours.white
- writeColor()
- elseif colChoice == "Orange" then
- borderColour = colours.orange
- writeColor()
- elseif colChoice == "Magenta" then
- borderColour = colours.magenta
- writeColor()
- elseif colChoice == "Light Blue" then
- borderColour = colours.lightBlue
- writeColor()
- elseif colChoice == "Yellow" then
- borderColour = colours.yellow
- writeColor()
- elseif colChoice == "Lime" then
- borderColour = colours.lime
- writeColor()
- elseif colChoice == "Pink" then
- borderColour = colours.pink
- writeColor()
- elseif colChoice == "Gray" then
- borderColour = colours.grey
- writeColor()
- elseif colChoice == "Light Gray" then
- borderColour = colours.lightGrey
- writeColor()
- elseif colChoice == "Cyan" then
- borderColour = colours.cyan
- writeColor()
- elseif colChoice == "Purple" then
- borderColour = colours.purple
- writeColor()
- elseif colChoice == "Blue" then
- borderColour = colours.blue
- writeColor()
- elseif colChoice == "Brown" then
- borderColour = colours.brown
- writeColor()
- elseif colChoice == "Green" then
- borderColour = colours.green
- writeColor()
- elseif colChoice == "Red" then
- borderColour = colours.red
- writeColor()
- elseif colChoice == "Black" then
- borderColour = colours.black
- writeColor()
- end
- end
- end
- end
- end
- end
- function displayHelp()
- term.setBackgroundColour(colours.white)
- term.setCursorPos(1, 1)
- term.clear()
- term.setBackgroundColour(colours.grey)
- write(string.rep(" ", screenX))
- term.setBackgroundColour(colours.red)
- term.setCursorPos(screenX, 1)
- write("X")
- term.setBackgroundColour(colours.white)
- term.setTextColour(colours.black)
- term.setCursorPos(1, 2)
- print("There will be help here once I decide to not be lazy and fill it out, for now check the forums!")
- repeat local _, but, x, y = os.pullEvent("mouse_click") until but == 1 and x == screenX and y == 1
- term.setBackgroundColour(colours.black)
- term.clear()
- drawMain()
- screen(buttonsMain)
- end
- --[[ End of functions ]]--
- --[[ Main ]]--
- -- Run the functions to fill the table with math functions
- fillExtraFunctions()
- fillTable()
- fillFunctionsTable()
- fillExtraButtonsTable()
- fillColorOptions()
- -- Run the functions to draw the tables
- term.clear()
- drawMain()
- screen(buttonsMain)
- state = "main"
- while true do
- local e, but, x, y = os.pullEvent("mouse_click")
- if but == 1 then
- bValid, numb = checkClick(state == "main" and buttonsMain or state == "extra" and buttonsExtra, x, y)
- if bValid then
- if numb == "+" or numb == "-" or numb == "*" or numb == "/" or
- numb == "^" or numb == "rt" then
- doSum()
- elseif numb == "H" then
- displayHelp()
- elseif numb == "F" then
- term.clear()
- redraw(calc)
- drawMain()
- if state == "main" then
- state = "extra"
- screen(buttonsExtra)
- elseif state == "extra" then
- state = "main"
- screen(buttonsMain)
- end
- elseif numb == "=" then
- enterPress()
- elseif numb == "c" then
- chooseColour()
- elseif numb == "C" then
- for i = 1,3 do
- input[i] = nil
- end
- calc = ""
- Output = ""
- redraw()
- elseif numb == "." then
- calc = calc .. numb
- term.setCursorPos(4,4)
- if #calc < 14 then
- write(calc)
- else
- write(calc:sub(#calc-14))
- end
- elseif tonumber(numb) then -- if it is a number
- calc = calc .. numb
- term.setCursorPos(4, 4)
- if #calc < 14 then
- write(calc)
- else
- write(calc:sub(#calc-14))
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment