Guest User

Fixed

a guest
Jan 29th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. term.setTextColor(colors.white)
  2. local button = {}
  3. term.setBackgroundColor(colors.black)
  4.  
  5. function CurPos()
  6.  term.setCursorPos(4,4)
  7. end
  8.  
  9. function drawCalc()
  10.  term.clear()
  11.  term.setCursorPos(2,2)
  12.  term.setTextColor(colors.white)
  13.  term.setBackgroundColor(colors.black)
  14.  function printVerticalLine(lineX, startingY, endingY, lineColor)
  15.   local curX, curY = term.getCursorPos()
  16.   term.setBackgroundColor(lineColor)
  17.   for lineY = startingY, endingY do
  18.    term.setCursorPos(lineX, lineY)
  19.    term.write(" ")
  20.   end
  21.   term.setCursorPos(curX, curY)
  22.  end
  23.  
  24. function printHorizontalLine(yLine, startingX, endingX, lineColor)
  25.   local curX, curY = term.getCursorPos()
  26.   term.setBackgroundColor(lineColor)
  27.   for xLine = startingX, endingX do
  28.    term.setCursorPos(xLine, yLine)
  29.    term.write(" ")
  30.   end
  31.   term.setCursorPos(curX, curY)
  32.  end
  33.  
  34.  printVerticalLine(2,2,18, colors.white)
  35.  --Left Border
  36.  printHorizontalLine(2,2,20, colors.white)
  37.  --Top
  38.  printHorizontalLine(6,2, 20 , colors.white)
  39.  --Upper Before Keys
  40.  printVerticalLine(20,2,18, colors.white)
  41.  --Right Border
  42.  printHorizontalLine(18,2,20, colors.white)
  43.  --Bottom
  44.  printVerticalLine(8,6,18, colors.white)
  45.  --Left Seperator
  46.  printVerticalLine(14,6,18,colors.white)
  47.  --Right Seperator
  48.  printHorizontalLine(10,2,20,colors.white)
  49.  --Upper Seperator
  50.  printHorizontalLine(14,2,20,colors.white)
  51.  --Lower Seperator
  52. end
  53.  
  54. function setTable(name, func, xmin, xmax, ymin, ymax)
  55.  button[name] = {}
  56.  button[name]["func"] = func
  57.  button[name]["active"] = false
  58.  button[name]["xmin"] = xmin
  59.  button[name]["ymin"] = ymin
  60.  button[name]["xmax"] = xmax
  61.  button[name]["ymax"] = ymax
  62. end
  63.  
  64. function fillTable()
  65.  setTable("1",One,3,7,15,17)
  66.  setTable("2",Two,9,13,15,17)
  67.  setTable("3",Three,15,19,15,17)
  68.  setTable("4",Four,3,7,11,13)
  69.  setTable("5",Five,9,13,11,13)
  70.  setTable("6",Six,15,19,11,13)
  71.  setTable("7",Seven, 3,7,7,9)
  72.  setTable("8",Eight, 9,13,7,9)
  73.  setTable("9",Nine, 15,19,7,9)
  74. end
  75.  
  76. function fill(text, color, bData)
  77.    term.setBackgroundColor(color)
  78.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  79.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  80.    for j = bData["ymin"], bData["ymax"] do
  81.       term.setCursorPos(bData["xmin"], j)
  82.       if j == yspot then
  83.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  84.             if k == xspot then
  85.                term.write(text)
  86.             else
  87.                term.write(" ")
  88.             end
  89.          end
  90.       else
  91.          for i = bData["xmin"], bData["xmax"] do
  92.             term.write(" ")
  93.          end
  94.       end
  95.    end
  96.    term.setBackgroundColor(colors.black)
  97. end
  98.  
  99. function screen()
  100.    local currColor
  101.    for name,data in pairs(button) do
  102.       local on = data["active"]
  103.       if on == true then
  104.        currColor = colors.gray
  105.       else
  106.        currColor = colors.black
  107.       end
  108.       fill(name, currColor, data)
  109.    end
  110. end
  111.  
  112. function checkxy(x, y)
  113.    for name, data in pairs(button) do
  114.       if y>=data["ymin"] and  y <= data["ymax"] and x>=data["xmin"] and x<= data["xmax"] then
  115.             term.setCursorPos(1, 1)
  116.             write(name)
  117.             data["active"] = not data["active"]
  118.             sleep(0.1)
  119.             data["active"] = not data["active"]
  120.             return true, name
  121.          end
  122.    end
  123.    return false, nil
  124. end
  125. calc = ""
  126. drawCalc()
  127. fillTable()
  128. screen()
  129.  
  130. while true do
  131.    local e,button,x,y = os.pullEvent("mouse_click")
  132.    bValid, numb = checkxy(x,y)
  133.     if bValid then
  134.         calc = calc .. numb
  135.         term.setCursorPos(4, 4)
  136.         if #calc < 14 then
  137.             write(calc)
  138.         else
  139.             write(calc:sub(#calc-14))
  140.         end
  141.     end
  142. end
Advertisement
Add Comment
Please, Sign In to add comment