Advertisement
natie3

Monitor

Nov 23rd, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. function setButton(name, x, x2, y, y2)
  2.    button[name] = {}
  3.    button[name]["x"] = x
  4.    button[name]["x2"] = x2
  5.    button[name]["y"] = y
  6.    button[name]["y2"] = y2
  7. end
  8.  
  9. function ini(side)
  10.   button = {}
  11.   setButton("1",1,20,6,14)
  12.   setButton("2",20,38,6,14)
  13.   setButton("3",38,57,6,14)
  14.   setButton("4",1,20,14,22)
  15.   setButton("5",20,38,14,22)
  16.   setButton("6",38,57,14,22)
  17.   setButton("7",1,20,22,30)
  18.   setButton("8",20,38,22,30)
  19.   setButton("9",38,57,22,30)
  20.   setButton("0",1,20,30,38)
  21.   setButton("Clear",20,38,30,38)  
  22.   setButton("Confirm",38,57,30,38)
  23.   m = peripheral.wrap(side)
  24.   m.setTextColor(colors.white)
  25.   m.setBackgroundColor(colors.black)
  26.   m.setTextScale(0.5)
  27.   input = false
  28.   current = ""
  29.   info = ""
  30. end
  31.  
  32. function setInfo(i)
  33.   info = i
  34.   draw()
  35. end
  36.  
  37. function drawButton(name,a,a2,b,b2)
  38.   for i = a, a2 do
  39.     for j = b, b2 do
  40.       m.setCursorPos(i,j)
  41.       if (i == a or i == a2 or j == b or j == b2) then
  42.         m.setBackgroundColor(colors.blue)
  43.       elseif input then
  44.         m.setBackgroundColor(colors.lime)
  45.       else
  46.         m.setBackgroundColor(colors.red)
  47.       end
  48.       m.write(" ")
  49.     end
  50.   end
  51.   if input then
  52.     m.setBackgroundColor(colors.lime)
  53.   else
  54.     m.setBackgroundColor(colors.red)
  55.   end
  56.   yspot = math.floor((b + b2) /2)
  57.   xspot= math.floor((a + a2 - string.len(name))/2) + 1
  58.   m.setCursorPos(xspot,yspot)
  59.   m.write(name)  
  60. end
  61.  
  62. function draw()
  63.   m.clear()
  64.   for name,data in pairs(button) do
  65.     drawButton(name,data["x"],data["x2"],data["y"],data["y2"])
  66.   end
  67.   m.setBackgroundColor(colors.black)
  68.   m.setCursorPos(1,2)
  69.   m.write(info)
  70.   m.setCursorPos(1,4)
  71.   m.write(current)
  72. end
  73.  
  74. function checkxy(x, y)
  75.   for name, data in pairs(button) do
  76.     if y >= data["y"] and  y <= data["y2"] then
  77.       if x >= data["x"] and x <= data["x2"] then
  78.         return name
  79.       end
  80.     end
  81.   end
  82.   return "none"
  83. end
  84.  
  85. function getNumber()
  86.   input = true
  87.   current = ""
  88.   confirm = false
  89.   draw()
  90.   while not confirm do
  91.     event = { os.pullEvent("monitor_touch") }
  92.     result = checkxy(event[3],event[4])
  93.     if result == "Confirm" then
  94.       confirm = true
  95.     elseif (result == "none" or string.len(current) == 10)then
  96.       -- no button is pressed or max lengh is reached
  97.     elseif result == "Clear" then
  98.       current = ""
  99.     else
  100.       current = current..result
  101.     end
  102.     draw()
  103.   end
  104.   input = false
  105.   temp = current
  106.   current = ""
  107.   draw()
  108.   if temp == "" then
  109.     return 0
  110.   else
  111.     return tonumber(temp)
  112.   end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement