Advertisement
VGToolBox

Touch screen

Jul 28th, 2013
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.64 KB | None | 0 0
  1. --Touch pad control panel
  2.  
  3. local SIDES = {"left", "right", "top", "bottom", "front", "back"}
  4.  
  5. local OUTPUT_SIDE = "bottom"
  6.  
  7. local INPUT_CODE = "1973"
  8.  
  9. local BUTTON_PUSH = 9
  10. local COMBO_SUCCESS = {11,13}
  11. local COMBO_FAIL = {4,5}
  12.  
  13. local codeDoorOpen = false
  14. local CODE_DOOR = {}
  15. CODE_DOOR.CLOSE = colours.black
  16. CODE_DOOR.OPEN = colours.white
  17. CODE_DOOR.PULSES = 4
  18.  
  19. local passDoorOpen = false
  20. local PASS_DOOR = {}
  21. PASS_DOOR.CLOSE = colours.red
  22. PASS_DOOR.OPEN = colours.green
  23. PASS_DOOR.PULSES = 4
  24.  
  25. local drawList = {} --Stores references to everything that needs to be drawn
  26. drawList.invalid = false;
  27.  
  28. local clickList = {} --Stores references to draw objects at each point in its 2D array
  29.  
  30. local outputs = {}
  31.  
  32. local count = 0
  33. local combo = ""
  34.  
  35. local defaultColour = colours.black
  36.  
  37. local currentColour = colours.black
  38.  
  39. -------------------------------------------------Classes--
  40.  
  41. local function newBox(width, height, colour, label)
  42.    
  43.  
  44.     local function draw(self)
  45.  
  46.         print("in updraw")
  47.         if not self.visible then
  48.             return 0
  49.         end
  50.         print("setBG colour")
  51.  
  52.         for i, output in ipairs(outputs) do
  53.  
  54.             output.setBackgroundColour(self.colour)
  55.  
  56.             local x
  57.             local y
  58.             print("local x and y set")
  59.             --print(self.x)
  60.             --print(self.width)
  61.             --xMax = self.x + (self.width - 1)
  62.  
  63.             print("pre loop")
  64.  
  65.             for x = self.x, self.x + (self.width - 1) do
  66.                 for y = self.y, self.y + (self.height - 1) do
  67.                     print("in loop at "..x.." by "..y)
  68.                     output.setCursorPos(x,y)
  69.                     output.write(" ")
  70.                 end
  71.             end
  72.  
  73.             print("after box draw")
  74.  
  75.             x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
  76.             y = self.y + math.floor(self.height * 0.5)
  77.  
  78.             output.setCursorPos(x,y)
  79.             output.write(self.label)
  80.  
  81.             output.setBackgroundColour(currentColour)
  82.         end
  83.     end
  84.  
  85.     local function invalidate(self)
  86.         if self.parent then
  87.             self.parent.invalid = true
  88.         end
  89.     end
  90.  
  91.     box = {}
  92.     box.x = 0
  93.     box.y = 0
  94.     box.width = width or 1
  95.     box.height = height or 1
  96.     box.visible = true
  97.     box.colour = colour or colours.white
  98.     box.draw = draw
  99.     box.invalidate = invalidate
  100.     box.label = label
  101.     box.type = "Display Object"
  102.  
  103.     return box
  104. end
  105.  
  106. local function newButton(width, height, colour, downColour, label, value)
  107.    
  108.     box = newBox(width, height, colour, label)
  109.  
  110.     local function draw(self)
  111.         if self.downCounter > 0 then
  112.             self:downDraw()
  113.         else
  114.             print("upDraw")
  115.             self:upDraw()
  116.         end
  117.     end
  118.  
  119.     local function downDraw(self)
  120.  
  121.         for i, output in ipairs(outputs) do
  122.  
  123.             output.setBackgroundColour(self.downColour)
  124.             for x = self.x, self.x + (self.width - 1) do
  125.                 for y = self.y, self.y + (self.height - 1) do
  126.                     output.setCursorPos(x,y)
  127.                     output.write(" ")
  128.                 end
  129.             end
  130.  
  131.             x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
  132.             y = self.y + math.floor(self.height * 0.5)
  133.  
  134.             output.setCursorPos(x,y)
  135.             output.write(self.label)
  136.  
  137.             output.setBackgroundColour(currentColour)
  138.         end
  139.  
  140.         self.downCounter = self.downCounter - 1
  141.  
  142.         self:invalidate()
  143.     end
  144.  
  145.     local function click(self)
  146.         self.downCounter = 1
  147.         return value
  148.     end
  149.  
  150.  
  151.  
  152.     box.downColour = downColour or colours.blue
  153.     box.upDraw = box.draw
  154.     box.downDraw = downDraw
  155.     box.draw = draw
  156.     box.type = "Clickable"
  157.     box.downCounter = 0
  158.     box.value = value
  159.     box.click = click
  160.  
  161.     return box
  162. end
  163.  
  164.  
  165. local function getNoteBlock()
  166.     for i, v in ipairs(SIDES) do
  167.         if peripheral.getType(v) == "note" then
  168.             return peripheral.wrap(v)
  169.         end
  170.     end
  171. end
  172.  
  173. local function getMonitors()
  174.     local monitors = {}
  175.     for i, v in ipairs(SIDES) do
  176.         if peripheral.getType(v) == "monitor" then
  177.             table.insert(monitors, peripheral.wrap(v))
  178.         end
  179.     end
  180.  
  181.     return monitors
  182. end
  183.  
  184. local function clearEvents()
  185.     os.queueEvent("clear")
  186.     while true do
  187.         e = os.pullEvent()
  188.         if e == "clear" then
  189.             break
  190.         end
  191.     end
  192. end
  193.  
  194.  
  195. local function pulseRS(side, n, colour)
  196.    
  197.     if colour then
  198.         for i = 1, n do
  199.             rs.setBundledOutput(side, colours.combine(rs.getBundledOutput(side), colour))
  200.             sleep(0.45)
  201.             rs.setBundledOutput(side, colours.subtract(rs.getBundledOutput(side), colour))
  202.             sleep(0.45)
  203.         end
  204.     else
  205.         for i = 1, n do
  206.             rs.setOutput(side, true)
  207.             sleep(0.45)
  208.             rs.setOutput(side, false)
  209.             sleep(0.45)
  210.         end
  211.     end
  212. end
  213.  
  214. local function playNote(notes)
  215.     for i = 1, table.getn(notes) do
  216.         note.playNote(0,notes[i])
  217.         sleep(0.2)
  218.     end
  219. end
  220.  
  221. local function addToClickList(object)
  222.     for x = object.x, object.x + (object.width - 1) do
  223.         clickList[x] = clickList[x] or {}
  224.         for y = object.y, object.y + (object.height - 1) do
  225.             table.insert(clickList[x], y, object)
  226.         end
  227.     end
  228. end
  229.  
  230. local function clearScreen(colour, text)
  231.     print("num of monitors: "..table.getn(outputs))
  232.     for i, v in ipairs(outputs) do
  233.         v.setBackgroundColour(colour)
  234.         v.clear()
  235.         print("monitor"..i)
  236.         if text then
  237.             local x = 3 - math.floor((string.len(text) * 0.5))
  238.             local y = 3
  239.  
  240.             x = x > 0 and x or 1
  241.  
  242.             v.setCursorPos(x,y)
  243.             print("set pos")
  244.             v.write(text)
  245.             print("write String")
  246.         end
  247.         v.setBackgroundColour(defaultColour)
  248.         print("colour back to default")
  249.     end
  250. end
  251.  
  252. local function drawScreen()
  253.     if drawList.invalid then
  254.         print("in draw")
  255.         drawList.invalid = true
  256.         clearScreen(defaultColour)
  257.  
  258.         for i, v in ipairs(drawList) do
  259.             print("indraw list loop")
  260.             v:draw()
  261.             print("objectDone")
  262.         end
  263.     end
  264. end
  265.  
  266. local function getObjectAt(x,y)
  267.     if clickList[x] then
  268.         if clickList[x][y] then
  269.             return clickList[x][y]
  270.         end
  271.     end
  272.     return false
  273. end
  274.  
  275. local function handleClick(x, y)
  276.     print(x)
  277.     print(y)
  278.  
  279.     object = getObjectAt(x, y)
  280.  
  281.     print("object")
  282.  
  283.     if object and object.type == "Clickable" then
  284.         combo = combo..object:click()
  285.         count = count + 1
  286.         note.playNote(0, BUTTON_PUSH)
  287.     end
  288.  
  289. end
  290.  
  291.  
  292.  
  293. local function moveDoor(door, open)
  294.     if open then
  295.         pulseRS(OUTPUT_SIDE, door.PULSES, door.OPEN)
  296.     else
  297.         pulseRS(OUTPUT_SIDE, door.PULSES, door.CLOSE)
  298.     end
  299. end
  300.  
  301. local function addToDrawList(object, i)
  302.     i = i or table.getn(drawList) + 1
  303.     table.insert(drawList, i, object)
  304.     object.parent = drawList
  305.     print(object)
  306.     if object.type == "Clickable" then
  307.         addToClickList(object) 
  308.     end
  309.     drawList.invalid = true
  310.  
  311. end
  312.  
  313.  
  314.  
  315. local function startingUpScreen()
  316.     clearScreen(colours.yellow, "Booting")
  317. end
  318.  
  319. local function successScreen()
  320.     clearScreen(colours.green, "Welcome")
  321. end
  322.  
  323. local function failScreen()
  324.     clearScreen(colours.red, "Wrong")
  325. end
  326.  
  327. local function main()
  328.  
  329.     outputs = getMonitors()
  330.     note = getNoteBlock()
  331.  
  332.     startingUpScreen()
  333.  
  334.     --reset system just in case
  335.     moveDoor(PASS_DOOR)
  336.     moveDoor(CODE_DOOR)
  337.     sleep(1)
  338.  
  339.     clearScreen(defaultColour)
  340.  
  341.     --(width, height, colour, downColour, label, value)
  342.  
  343.     button1 = newButton(1,1,colours.blue, colours.red, "1", "1")
  344.     button1.x = 2
  345.     button1.y = 1
  346.  
  347.     button2 = newButton(1,1,colours.blue, colours.red, "2", "2")
  348.     button2.x = 4
  349.     button2.y = 1
  350.  
  351.     button3 = newButton(1,1,colours.blue, colours.red, "3", "3")
  352.     button3.x = 6
  353.     button3.y = 1
  354.  
  355.     button4 = newButton(1,1,colours.blue, colours.red, "4", "4")
  356.     button4.x = 2
  357.     button4.y = 3
  358.  
  359.     button5 = newButton(1,1,colours.blue, colours.red, "5", "5")
  360.     button5.x = 4
  361.     button5.y = 3
  362.  
  363.     button6 = newButton(1,1,colours.blue, colours.red, "6", "6")
  364.     button6.x = 6
  365.     button6.y = 3
  366.  
  367.     button7 = newButton(1,1,colours.blue, colours.red, "7", "7")
  368.     button7.x = 2
  369.     button7.y = 5
  370.  
  371.     button8 = newButton(1,1,colours.blue, colours.red, "8", "8")
  372.     button8.x = 4
  373.     button8.y = 5
  374.  
  375.     button9 = newButton(1,1,colours.blue, colours.red, "9", "9")
  376.     button9.x = 6
  377.     button9.y = 5
  378.     --button0 = newButton(1,1,colours.blue, colours.red, "0", "0")
  379.  
  380.     addToDrawList(button1)
  381.     addToDrawList(button2)
  382.     addToDrawList(button3)
  383.     addToDrawList(button4)
  384.     addToDrawList(button5)
  385.     addToDrawList(button6)
  386.     addToDrawList(button7)
  387.     addToDrawList(button8)
  388.     addToDrawList(button9)
  389.  
  390.     local doorTimer
  391.     local eventTimer
  392.  
  393.     while true do
  394.         print("tick")
  395.         drawScreen()
  396.         print("post draw")
  397.  
  398.         clearEvents()
  399.         print("post clear Events")
  400.         while true do
  401.             print("timerActivate")
  402.             eventTimer = os.startTimer(6)
  403.  
  404.             e, side, x, y = os.pullEvent()
  405.             if e == "monitor_touch" then
  406.                 handleClick(x, y)
  407.                 print("click activated")
  408.  
  409.                 print("count is "..count)
  410.                 print("combo is "..combo)
  411.  
  412.                 if count == 4 then
  413.                     if combo == INPUT_CODE then
  414.                         sleep(0.5)
  415.                         playNote(COMBO_SUCCESS)
  416.                         successScreen()
  417.                         moveDoor(CODE_DOOR, true)
  418.                         sleep(3)
  419.                     else
  420.                         sleep(0.5)
  421.                         playNote(COMBO_FAIL)
  422.                         failScreen()
  423.                         count = 0
  424.                         combo = ""
  425.                         sleep(5)
  426.                     end
  427.  
  428.                     os.reboot()
  429.  
  430.                 end
  431.  
  432.                 break
  433.             end
  434.  
  435.             drawScreen()
  436.  
  437.             print("timeOut")
  438.  
  439.         end
  440.     end
  441. end
  442.  
  443. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement