Guest User

Calcfix

a guest
Feb 2nd, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.71 KB | None | 0 0
  1. buttonsMain = {}
  2. buttonsExtra = {}
  3. buttonsColors = {}
  4.  
  5. input = {nil, nil, nil}
  6. calc = ""
  7. screenX, screenY = term.getSize()
  8.  
  9. local color = colors.white
  10.  
  11. term.setBackgroundColor(colors.black)
  12. term.setTextColor(colors.white)
  13. term.clear()
  14.  
  15. function colorSet()
  16.   if name == "White" then
  17.    term.setBackgroundColor(colors.white)
  18.   elseif name == "Orange" then
  19.    term.setBackgroundColor(colors.orange)
  20.   elseif name == "Magenta" then
  21.    term.setBackgroundColor(colors.magenta)
  22.   elseif name == "Light Blue" then
  23.    term.setBackgroundColor(colors.lightBlue)
  24.   elseif name == "Yellow" then
  25.    term.setBackgroundColor(colors.yellow)
  26.   elseif name == "Lime" then
  27.    term.setBackgroundColor(colors.lime)
  28.   elseif name == "Pink" then
  29.    term.setBackgroundColor(colors.pink)
  30.   elseif name == "Gray" then
  31.    term.setBackgroundColor(colors.gray)
  32.   elseif name == "Light Gray" then
  33.    term.setBackgroundColor(colors.lightGray)
  34.   elseif name == "Cyan" then
  35.    term.setBackgroundColor(colors.cyan)
  36.   elseif name == "Purple" then
  37.    term.setBackgroundColor(colors.purple)
  38.   elseif name == "Blue" then
  39.    term.setBackgroundColor(colors.blue)
  40.   elseif name == "Brown" then
  41.    term.setBackgroundColor(colors.brown)
  42.   elseif name == "Green" then
  43.    term.setBackgroundColor(colors.green)
  44.   elseif name == "Red" then
  45.    term.setBackgroundColor(colors.red)
  46.   elseif name == "Black" then
  47.    term.setBackgroundColor(colors.black)
  48.   end
  49. end
  50.  
  51. function printVerticalLine(lineX, startingY, endingY)
  52.     local curX, curY = term.getCursorPos()
  53.     term.setBackgroundColor(colors.white)
  54.     for lineY = startingY, endingY do
  55.         term.setCursorPos(lineX, lineY)
  56.         term.write(" ")
  57.     end
  58.     term.setCursorPos(curX, curY)
  59.     term.setBackgroundColour(colours.black)
  60. end
  61.  
  62. function printHorizontalLine(yLine, startingX, endingX)
  63.     local curX, curY = term.getCursorPos()
  64.     term.setBackgroundColor(colors.white)
  65.     for xLine = startingX, endingX do
  66.         term.setCursorPos(xLine, yLine)
  67.         term.write(" ")
  68.     end
  69.     term.setCursorPos(curX, curY)
  70.     term.setBackgroundColour(colours.black)
  71. end
  72.  
  73. function drawTable()
  74.     printVerticalLine(2,2,18)
  75.     --Left Border
  76.     printVerticalLine(20,2,18)
  77.     --Right Border
  78.     printVerticalLine(8,6,18)
  79.     --Left Seperator
  80.     printVerticalLine(14,6,18)
  81.     --Right Seperator
  82.     printHorizontalLine(2,2,20)
  83.     --Top
  84.     printHorizontalLine(6,2, 20)
  85.     --Upper Before Keys
  86.     printHorizontalLine(18,2,20)
  87.     --Bottom
  88.     printHorizontalLine(10,2,20)
  89.     --Upper Seperator
  90.     printHorizontalLine(14,2,20)
  91.     --Lower Seperator
  92.     term.setBackgroundColor(colors.black)
  93. end
  94.  
  95. function drawFunctionsTable()
  96.     printHorizontalLine(6,32,50)
  97.     --Top
  98.     printHorizontalLine(10,32,50)
  99.     printHorizontalLine(14,32,50)
  100.     printHorizontalLine(18,32,50)
  101.     --Bottom
  102.     printVerticalLine(32,6,18)
  103.     --Left Border
  104.     printVerticalLine(38,6,14)
  105.     printVerticalLine(44,6,18)
  106.     printVerticalLine(50,6,18)
  107.     --Right Border
  108.     term.setBackgroundColor(colors.black)
  109. end
  110.  
  111. function drawColorsTable()
  112.  printVerticalLine(2,2,18)
  113.  --Left
  114.  printVerticalLine(14,2,18)
  115.  printVerticalLine(26,2,18)
  116.  printVerticalLine(40,2,18)
  117.  printVerticalLine(50,2,18)
  118. end
  119.  
  120. function drawExtraButtonsTable()
  121.     --Help, Color settings, 2nd button for filling table with ExtraFunctions
  122.     printVerticalLine(23,6,18)
  123.     --Left Border
  124.     printVerticalLine(29,6,18)
  125.     --Right Border
  126.     printHorizontalLine(6,23,29)
  127.     --Top
  128.     printHorizontalLine(10,23,29)
  129.     printHorizontalLine(14,23,29)
  130.     printHorizontalLine(18,23,29)
  131.     --Bottom
  132.     term.setBackgroundColour(colours.black)
  133. end
  134.  
  135. function setTable(_table, name, xMin, xMax, yMin, yMax)
  136.     table.insert(_table, {
  137.         numb = name,
  138.         xmin = xMin,
  139.         xmax = xMax,
  140.         ymin = yMin,
  141.         ymax = yMax
  142.     })
  143. end
  144.  
  145. function fillTable()
  146.     setTable(buttonsMain, "1", 3, 7, 15, 17)
  147.     setTable(buttonsMain, "2", 9, 13, 15, 17)
  148.     setTable(buttonsMain, "3", 15, 19, 15, 17)
  149.     setTable(buttonsMain, "4", 3, 7, 11, 13)
  150.     setTable(buttonsMain, "5", 9, 13, 11, 13)
  151.     setTable(buttonsMain, "6", 15, 19, 11, 13)
  152.     setTable(buttonsMain, "7", 3, 7, 7, 9)
  153.     setTable(buttonsMain, "8", 9, 13, 7, 9)
  154.     setTable(buttonsMain, "9", 15, 19, 7, 9)
  155.     setTable(buttonsExtra, "1", 3, 7, 15, 17)
  156.     setTable(buttonsExtra, "2", 9, 13, 15, 17)
  157.     setTable(buttonsExtra, "3", 15, 19, 15, 17)
  158.     setTable(buttonsExtra, "4", 3, 7, 11, 13)
  159.     setTable(buttonsExtra, "5", 9, 13, 11, 13)
  160.     setTable(buttonsExtra, "6", 15, 19, 11, 13)
  161.     setTable(buttonsExtra, "7", 3, 7, 7, 9)
  162.     setTable(buttonsExtra, "8", 9, 13, 7, 9)
  163.     setTable(buttonsExtra, "9", 15, 19, 7, 9)
  164. end
  165.  
  166. function fillFunctionsTable()
  167.     setTable(buttonsMain, "C", 33,37,7,9)
  168.     setTable(buttonsMain, ".", 39,43,7,9)
  169.     setTable(buttonsMain, "/", 45,49,7,9)
  170.     setTable(buttonsMain, "+", 33,37,11,13)
  171.     setTable(buttonsMain, "-", 39,43,11,13)
  172.     setTable(buttonsMain, "*", 45,49,11,13)
  173.     setTable(buttonsMain, "0", 33,43,15,17)
  174.     setTable(buttonsMain, "=", 45,49,15,17)
  175. end
  176.  
  177. function fillExtraFunctions()
  178.     setTable(buttonsExtra, "^", 33,37,7,9)
  179.     setTable(buttonsExtra, "rt", 39,43,7,9)
  180.     setTable(buttonsExtra, "/", 45,49,7,9)
  181.     setTable(buttonsExtra, "+", 33,37,11,13)
  182.     setTable(buttonsExtra, "-", 39,43,11,13)
  183.     setTable(buttonsExtra, "*", 45,49,11,13)
  184.     setTable(buttonsExtra, "0", 33,43,15,17)
  185.     setTable(buttonsExtra, "=", 45,49,15,17)
  186. end
  187.  
  188. function fillExtraButtonsTable()
  189.     setTable(buttonsExtra, "F",24,28,7,9)
  190.     setTable(buttonsExtra, "H",24,28,11,13)
  191.     setTable(buttonsExtra, "c",24,28,15,17)
  192.     setTable(buttonsMain, "F",24,28,7,9)
  193.     setTable(buttonsMain, "H",24,28,11,13)
  194.     setTable(buttonsMain, "c",24,28,15,17)
  195. end
  196.  
  197. function fillColorOptions()
  198.  setTable(buttonsColors,"White",3,13,3,5)
  199.  setTable(buttonsColors,"Orange",3,13,7,9)
  200.  setTable(buttonsColors,"Magenta",3,13,11,13)
  201.  setTable(buttonsColors,"Light Blue",3,13,15,17)
  202.  setTable(buttonsColors,"Yellow",15,25,3,5)
  203.  setTable(buttonsColors,"Lime",15,25,7,9)
  204.  setTable(buttonsColors,"Pink",15,25,11,13)
  205.  setTable(buttonsColors,"Gray",15,25,15,17)
  206.  setTable(buttonsColors,"Light Gray",27,37,3,5)
  207.  setTable(buttonsColors,"Cyan",27,37,7,9)
  208.  setTable(buttonsColors,"Purple",27,37,11,13)
  209.  setTable(buttonsColors,"Blue",27,37,15,17)
  210.  setTable(buttonsColors,"Brown",39,49,3,5)
  211.  setTable(buttonsColors,"Green",39,49,7,9)
  212.  setTable(buttonsColors,"Red",39,49,11,13)
  213.  setTable(buttonsColors,"Black",39,49,15,17)
  214. end
  215.  
  216. function checkClick(tab, mx, my)
  217.     for i, v in pairs(tab) do
  218.         if mx >= v.xmin and mx <= v.xmax and my >= v.ymin and my <= v.ymax then
  219.             return true, v.numb
  220.         end
  221.     end
  222.     return false, nil
  223. end
  224.  
  225. function screen(tab)
  226.     term.setBackgroundColour(colours.black)
  227.     term.setTextColour(colours.white)
  228.     for name, data in pairs(tab) do
  229.         local yspot = (data["ymin"] + data["ymax"]) / 2
  230.         local xspot = (data["xmin"] + data["xmax"]) / 2
  231.         term.setCursorPos(xspot, yspot)
  232.         colorSet()
  233.         --should be sets background of the color to its name
  234.         write(data.numb)
  235.     end
  236. end
  237.  
  238. function redraw()
  239.     term.setCursorPos(4, 4)
  240.     write(string.rep(" ", 15))
  241.     term.setCursorPos(4, 4)
  242. end
  243.  
  244. function displayHelp()
  245.     term.setBackgroundColour(colours.white)
  246.     term.setCursorPos(1, 1)
  247.     term.clear()
  248.    
  249.     term.setBackgroundColour(colours.grey)
  250.     write(string.rep(" ", screenX))
  251.    
  252.     term.setBackgroundColour(colours.red)
  253.     term.setCursorPos(screenX, 1)
  254.     write("X")
  255.    
  256.     term.setBackgroundColour(colours.white)
  257.     term.setTextColour(colours.black)
  258.     term.setCursorPos(1, 2)
  259.     print("There will be help here once I decide to not be lazy and fill it out, for now check the forums!")
  260.    
  261.     repeat local _, but, x, y = os.pullEvent("mouse_click") until but == 1 and x == screenX and y == 1
  262.    
  263.     term.setBackgroundColour(colours.black)
  264.     term.clear()
  265.    
  266.     drawTable()
  267.     screen(buttonsMain)
  268.     drawFunctionsTable()
  269.     drawExtraButtonsTable()
  270. end
  271.  
  272. function setInput1()
  273.     input[1] = tonumber(calc) or input[1]
  274.     input[2] = numb
  275.     redraw()
  276.     calc = ""
  277.     numb = ""
  278. end
  279.  
  280. function enterPress()
  281.     input[3] = tonumber(calc)
  282.     redraw()
  283.     calc = ""
  284.     if input[2] == "+" then
  285.         Output = input[1] + input[3]
  286.     elseif input[2] == "-" then
  287.         Output = input[1] - input[3]
  288.     elseif input[2] == "*" then
  289.         Output = input[1] * input[3]
  290.     elseif input[2] == "/" then
  291.         Output = input[1] / input[3]
  292.     elseif input[2] == "^" then
  293.         Output = input[1] ^ input[3]
  294.     elseif input[2] == "rt" then
  295.         Output = math.sqrt(input[1])
  296.     end
  297.     write(Output)
  298.     input[1] = Output  
  299. end
  300. --[[ Running Code ]]--
  301.  
  302. -- Run the functions to fill the table with math functions
  303. fillExtraFunctions()
  304. fillTable()
  305. fillFunctionsTable()
  306. fillExtraButtonsTable()
  307.  
  308. -- Run the functions to draw the tables
  309. drawTable()
  310. drawFunctionsTable()
  311. drawExtraButtonsTable()
  312.  
  313. screen(buttonsMain)
  314. state = "main"
  315.  
  316. while true do
  317.     local e, but, x, y = os.pullEvent("mouse_click")
  318.     if but == 1 then
  319.         bValid, numb = checkClick(state == "main" and buttonsMain or state == "extra" and buttonsExtra, x, y)
  320.         if bValid then
  321.             if numb == "+" then
  322.                 setInput1()
  323.             elseif numb == "-" then
  324.                 setInput1()
  325.             elseif numb == "*" then
  326.                 setInput1()
  327.             elseif numb == "/" then
  328.                 setInput1()
  329.             elseif numb == "^" then
  330.                 setInput1()
  331.             elseif numb == "rt" then
  332.                 setInput1()
  333.             elseif numb == "H" then
  334.                 displayHelp()
  335.             elseif numb == "F" then
  336.                 drawTable()
  337.                 drawFunctionsTable()
  338.                 drawExtraButtonsTable()
  339.                 if state == "main" then
  340.                     state = "extra"
  341.                     screen(buttonsExtra)
  342.                 elseif state == "extra" then
  343.                     state = "main"
  344.                     screen(buttonsMain)
  345.                 end
  346.             elseif numb == "=" then
  347.                 enterPress()
  348.             elseif numb == "c" then
  349.                  term.clear()
  350.                  drawColorsTable()
  351.                  screen(buttonsColors)
  352.                  if numb == "White" then
  353.                   color = colors.white
  354.                  elseif numb == "Orange" then
  355.                   color = colors.orange
  356.                  elseif numb == "Magenta" then
  357.                   color = colors.magenta
  358.                  elseif numb == "Light Blue" then
  359.                   color = colors.lightBlue
  360.                  elseif numb == "Yellow" then
  361.                   color = colors.yellow
  362.                  elseif numb == "Lime" then
  363.                   color = colors.lime
  364.                  elseif numb == "Pink" then
  365.                   color = colors.pink
  366.                  elseif numb == "Gray" then
  367.                   color = colors.gray
  368.                  elseif numb == "Light Gray" then
  369.                   color = colors.lightGray
  370.                  elseif numb == "Cyan" then
  371.                   color = colors.cyan
  372.                  elseif numb == "Purple" then
  373.                   color = colors.purple
  374.                  elseif numb == "Blue" then
  375.                   color = colors.blue
  376.                  elseif numb == "Brown" then
  377.                   color = colors.brown
  378.                  elseif numb == "Green" then
  379.                   color = colors.green
  380.                  elseif numb == "Red" then
  381.                   color = colors.red
  382.                  elseif numb == "Black" then
  383.                   color = colors.black
  384.                  end
  385.      
  386.                  term.clear()
  387.                  fillExtraFunctions()
  388.                  fillTable()
  389.                  fillFunctionsTable()
  390.                  fillExtraButtonsTable()
  391.      
  392.                  drawTable()
  393.                  drawFunctionsTable()
  394.                  drawExtraButtonsTable()
  395.      
  396.                  screen(buttonsMain)
  397.                  state = "main"
  398.             elseif numb == "C" then
  399.                 for i = 1,3 do
  400.                     input[i] = nil
  401.                 end
  402.                 calc = ""
  403.                 redraw()
  404.             elseif numb == "." then
  405.                 calc = calc .. numb
  406.                 term.setCursorPos(4,4)
  407.                 if #calc < 14 then
  408.                     write(calc)
  409.                 else
  410.                     write(calc:sub(#calc-14))
  411.                 end
  412.             elseif tonumber(numb) then -- if it is a number
  413.                 calc = calc .. numb
  414.                 term.setCursorPos(4, 4)
  415.                 if #calc < 14 then
  416.                     write(calc)
  417.                 else
  418.                     write(calc:sub(#calc-14))
  419.                 end
  420.             end
  421.         end
  422.     end
  423. end
Advertisement
Add Comment
Please, Sign In to add comment