Guest User

checker

a guest
Jan 31st, 2013
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. local disp = peripheral.wrap("left")
  3. local button = {}
  4.  
  5. --forward declarations
  6. setTable = function() end
  7. fill = function() end
  8. fillTable = function() end
  9. checkxy = function() end
  10. screen = function() end
  11.  
  12. function main()
  13.   mon.setTextColor(colors.white)
  14.   mon.setBackgroundColor(colors.black)
  15.   fillTable()
  16.   while true do
  17.     mon.clear()
  18.     screen()
  19.     local e,side,x,y = os.pullEvent("monitor_touch")
  20.     print(x..":"..y)
  21.     disp.clear()
  22.     disp.setCursorPos(1,1)
  23.     disp.write(x..":"..y)
  24.     checkxy(x,y)
  25.     sleep(.1)
  26.   end
  27. end
  28.  
  29. fillTable = function()
  30.   setTable("1",1,1,1,1)
  31.   setTable("2",3,3,1,1)
  32.   setTable("3",5,5,1,1)
  33.   setTable("4",7,7,1,1)
  34.   setTable("5",2,2,2,2)
  35.   setTable("6",4,4,2,2)
  36.   setTable("7",6,6,2,2)
  37. end
  38.  
  39. setTable = function(name, xmin, xmax, ymin, ymax)
  40.   button[name] = {}
  41.   button[name]["active"] = false
  42.   button[name]["xmin"] = xmin
  43.   button[name]["xmax"] = xmax
  44.   button[name]["ymin"] = ymin
  45.   button[name]["ymax"] = ymax
  46.  
  47. end
  48.  
  49. fill = function(text,color,bData)
  50.   mon.setBackgroundColor(color)
  51.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) / 2)
  52.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) / 2) + 1
  53.   for j = bData["ymin"],bData["ymax"] do
  54.     mon.setCursorPos(bData["xmin"],j)
  55.     if j == yspot  then
  56.       for  k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
  57.         if  k == xspot then
  58.           mon.write(text)
  59.         else
  60.           mon.write(" ")
  61.         end
  62.       end
  63.     else
  64.       for i = bData["xmin"], bData["xmax"] do
  65.         mon.write(" ")
  66.       end
  67.     end
  68.   end
  69.   mon.setBackgroundColor(colors.black)
  70. end
  71.  
  72. screen = function()
  73.   local currColor
  74.   for name, data in pairs(button) do
  75.     local on = data["active"]
  76.     if on == true then currColor = colors.lime else currColor = colors.red end
  77.     fill(name, currColor, data)
  78.   end
  79. end
  80.  
  81. checkxy = function(x,y)
  82.   for name, data in pairs(button) do
  83.     if y >= data["ymin"] and y <= data["ymax"] then
  84.       if x >= data["xmin"] and x <= data["xmax"] then
  85.         data["active"] = not data["active"]
  86.       end
  87.     end
  88.   end
  89. end
  90.  
  91.  
  92.  
  93. main()
Advertisement
Add Comment
Please, Sign In to add comment