Advertisement
Redxone

[CC-PDA] Keypad Test

Oct 15th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. local w,h = 26,20
  2. local pos = term.setCursorPos
  3. local bcolor = term.setBackgroundColor
  4. local tcolor = term.setTextColor
  5. local blink = term.setCursorBlink
  6. local buffer = term.current().setVisible
  7. local getpos = term.getCursorPos
  8. local getbcolor = term.getBackgroundColor
  9. local gettcolor = term.getTextColor
  10. local printslow = textutils.slowPrint
  11. local clr = term.clear
  12. local clrln = term.clearLine
  13. local keylock = {
  14.   pins = 5,
  15.   code = '92345',
  16.   inp  = ''
  17. }
  18. clrlny = function(y)
  19.   local x,_ = getpos()
  20.   pos(x,y)
  21.   clrln()
  22. end
  23. cwrite = function(txt,y)
  24.   pos((w/2 - #txt/2)+1, y)
  25.   write(txt)
  26. end
  27. clrlnycolor = function(col,y) bcolor(col) clrlny(y) end
  28. box = paintutils.drawFilledBox
  29. hbox = paintutils.drawBox
  30. drawimage = function(img,x,y) paintutils.drawImage(paintutils.loadImage(img),x,y) end
  31. hbox(1,1,w,h,colors.green)
  32. local numpad = {
  33.     {'1','2','3'},
  34.     {'4','5','6'},
  35.     {'7','8','9'},
  36.     {' ','0',' '},
  37. }
  38.  
  39. local running = true
  40.  
  41. local yspace = 4
  42. local xspace = 8
  43. local center = (w/2) - (#numpad[1]+xspace-5/2)
  44. local yoffs = (h/2) - ((#numpad+yspace)/2)+1
  45.  
  46. function drawnums()
  47.   drawimage('scr.bg',0,0)
  48.   bcolor(colors.lightBlue)
  49.   tcolor(colors.black)
  50.   cwrite("Enter Passcode",1)
  51.   pos((w/2 - keylock.pins/2)+1,3)
  52.   bcolor(colors.lightGray)
  53.   tcolor(colors.gray)
  54.   write(string.rep('_',keylock.pins))
  55.     for y = 1, #numpad do
  56.         for x = 1, #numpad[y] do
  57.             if (numpad[y][x] ~= " ")then
  58.            
  59.                 box(center+ ( (x-1)*xspace ) ,  yoffs+( (y-1)*yspace )-1, center+ ( (x-1)*xspace )+2 , yoffs+( (y-1)*yspace )+1,colors.black)
  60.                 pos(center+ ( (x-1)*xspace ) ,  yoffs+( (y-1)*yspace ) )
  61.                 term.blit(' ' .. numpad[y][x] .. ' ', '000','fff')
  62.    
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. function dispinp()
  69.   pos((w/2 - keylock.pins/2)+1,3)
  70.   bcolor(colors.lightGray)
  71.   tcolor(colors.gray)
  72.   write(string.rep('*',#keylock.inp))
  73. end
  74.  
  75. function update(e)
  76.   if(e[1] == "mouse_click")then
  77.       local b = e[2]
  78.       local mx = e[3]
  79.       local my = e[4]
  80.  
  81.       if(mx >= center-1 and my >= yoffs-1)then
  82.           for y = 1, #numpad do
  83.           for x = 1, #numpad[y] do
  84.             if (numpad[y][x] ~= " ")then
  85.               if(mx >= center+ ( (x-1)*xspace )-1 and mx <=  center+ ( (x-1)*xspace )+2
  86.              and my >= yoffs+ ( (y-1)*yspace )-1 and my <= yoffs+ ( (y-1)*yspace )+1)then
  87.                 box(center+ ( (x-1)*xspace ) ,  yoffs+( (y-1)*yspace )-1, center+ ( (x-1)*xspace )+2 , yoffs+( (y-1)*yspace )+1,colors.white)
  88.                 pos(center+ ( (x-1)*xspace ) ,  yoffs+( (y-1)*yspace ) )
  89.                 term.blit(' ' .. numpad[y][x] .. ' ', 'fff','000')
  90.                 keylock.inp = keylock.inp .. numpad[y][x]
  91.                 dispinp()
  92.                 sleep(0.2)
  93.                 box(center+ ( (x-1)*xspace ) ,  yoffs+( (y-1)*yspace )-1, center+ ( (x-1)*xspace )+2 , yoffs+( (y-1)*yspace )+1,colors.black)
  94.                 pos(center+ ( (x-1)*xspace ) ,  yoffs+( (y-1)*yspace ) )
  95.                 term.blit(' ' .. numpad[y][x] .. ' ', '000','fff')
  96.                 if(#keylock.inp >= keylock.pins)then
  97.                     if(keylock.inp == keylock.code)then
  98.                         bcolor(colors.black)
  99.                         tcolor(colors.white)
  100.                         term.clear()
  101.                         pos(1,1)
  102.                         running = false
  103.                         return
  104.                     else
  105.                         local oldc = center
  106.                         center = center -1
  107.                         drawnums()
  108.                         sleep(0.2)
  109.                         center = center +2
  110.                         drawnums()
  111.                         sleep(0.2)
  112.                         center = center -1
  113.                         drawnums()
  114.                         sleep(0.2)
  115.                         center = center +1
  116.                         drawnums()
  117.                         sleep(0.2)
  118.                         center = oldc
  119.                         keylock.inp = ''
  120.                         drawnums()
  121.                     end
  122.                 end
  123.               end
  124.             end
  125.           end
  126.         end
  127.       end
  128.   end
  129. end
  130.  
  131.  
  132. --]] Transitions [[--
  133. function fadeIn(time)
  134.    
  135.    local ctbl = {
  136.      'black',
  137.      'gray',
  138.      'lightGray',
  139.      'white',
  140.    }
  141.  
  142.    for i = 1, #ctbl do
  143.       term.setBackgroundColor(colors[ ctbl[i] ])
  144.       term.clear()
  145.       sleep(time)  
  146.    end
  147. end
  148.  
  149. function fadeOut(time)
  150.    
  151.    local ctbl = {
  152.      'white',
  153.      'lightGray',
  154.      'gray',
  155.      'black',
  156.    }
  157.    
  158.    for i = 1, #ctbl do
  159.       term.setBackgroundColor(colors[ ctbl[i] ])
  160.       term.clear()
  161.       sleep(time)  
  162.    end
  163. end
  164.  
  165.  
  166. function loop()
  167.   while running do
  168.      local ev = {os.pullEvent()}
  169.      update(ev)
  170.   end
  171. end
  172.  
  173. drawnums()
  174. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement