sidekick_

Keypad

Jan 29th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. screenX, screenY = term.getSize()
  4. code = ""
  5. validOption = false
  6. pass = 22222
  7. terminateCode = 33333
  8. startXpos = math.ceil(screenX/2) - 5
  9. startYpos = math.floor(screenY/2)
  10.  
  11. t_keypad = {
  12.     {text = "1", xPos = startXpos, yPos = startYpos, color = colours.yellow},
  13.     {text = "2", xPos = startXpos + 4, yPos = startYpos, color = colours.yellow},
  14.     {text = "3", xPos = startXpos + 8, yPos = startYpos, color = colours.yellow},
  15.     {text = "4", xPos = startXpos, yPos = startYpos + 2, color = colours.yellow},
  16.     {text = "5", xPos = startXpos + 4, yPos = startYpos + 2, color = colours.yellow},
  17.     {text = "6", xPos = startXpos + 8, yPos = startYpos + 2, color = colours.yellow},
  18.     {text = "7", xPos = startXpos, yPos = startYpos + 4, color = colours.yellow},
  19.     {text = "8", xPos = startXpos + 4, yPos = startYpos + 4, color = colours.yellow},
  20.     {text = "9", xPos = startXpos + 8, yPos = startYpos + 4, color = colours.yellow},
  21.     {text = "0", xPos = startXpos + 4, yPos = startYpos + 6, color = colours.yellow},
  22.     {text = "CLR", xPos = startXpos - 1, yPos = startYpos + 6, color = colours.lightBlue},
  23.     {text = "ENT", xPos = startXpos + 7, yPos = startYpos + 6, color = colours.red}
  24. }
  25.  
  26. function drawKeyPad()
  27.     term.clear()
  28.     term.setCursorPos(startXpos - 2, startYpos - 4) write("-   Keypad  -")
  29.     term.setCursorPos(startXpos - 2, startYpos - 3) write("-------------")
  30.     term.setCursorPos(startXpos - 2, startYpos - 2) write("|---     ---|")
  31.     term.setCursorPos(startXpos - 2, startYpos - 1) write("-------------")
  32.     term.setCursorPos(startXpos - 2, startYpos)     write("|   |   |   |")
  33.     term.setCursorPos(startXpos - 2, startYpos + 1) write("-------------")
  34.     term.setCursorPos(startXpos - 2, startYpos + 2) write("|   |   |   |")
  35.     term.setCursorPos(startXpos - 2, startYpos + 3) write("-------------")
  36.     term.setCursorPos(startXpos - 2, startYpos + 4) write("|   |   |   |")
  37.     term.setCursorPos(startXpos - 2, startYpos + 5) write("-------------")
  38.     term.setCursorPos(startXpos - 2, startYpos + 6) write("|   |   |   |")
  39.     term.setCursorPos(startXpos - 2, startYpos + 7) write("-------------")
  40.     for i = 1, #t_keypad do
  41.         term.setCursorPos(t_keypad[i].xPos, t_keypad[i].yPos)
  42.         if type(t_keypad[i].color) == "number" then term.setTextColour(t_keypad[i].color) else term.setTextColour(colours.black) end
  43.         write(t_keypad[i].text)
  44.     end
  45. end
  46.  
  47. function checkMouseClick(_xPos, _yPos)
  48.     for i = 1, #t_keypad do
  49.         if _xPos >= t_keypad[i].xPos and _xPos <= (t_keypad[i].xPos + (string.len(t_keypad[i].text) - 1)) and
  50.         _yPos == t_keypad[i].yPos then
  51.             return true, t_keypad[i].text
  52.         end
  53.     end
  54.     return false, nil
  55. end
  56.  
  57. drawKeyPad()
  58.  
  59. while true do
  60.     event, button, x, y = os.pullEvent()
  61.     if event == "mouse_click" then
  62.         validOption, clicked = checkMouseClick(x, y)
  63.         if validOption then
  64.             if tonumber(clicked) and #code < 5 then
  65.                 code = code .. clicked
  66.                 term.setCursorPos(startXpos + 1 + #code, startYpos - 2)
  67.                 write("*")
  68.             elseif clicked == "CLR" then
  69.                 code = ""
  70.                 term.setTextColour(colours.white)
  71.                 term.setCursorPos(startXpos - 2, startYpos - 2)
  72.                 write("|---     ---|")
  73.                 term.setTextColour(colours.red)
  74.             elseif clicked == "ENT" then
  75.                 code = tonumber(code)
  76.                 if code == pass then
  77.                     rs.setOutput("bottom",true)
  78.                     sleep(3)
  79.                     rs.setOutput("bottom",false)
  80.                 elseif code == terminateCode then
  81.                     term.setCursorPos(1, 1)
  82.                     term.clear()
  83.                     term.setTextColour(colours.red)
  84.                     print("Aborted.\n")
  85.                     term.setTextColour(colours.black)
  86.                     break
  87.                 else
  88.                     term.setCursorPos(startXpos - 1, startYpos - 2)
  89.                     term.setTextColour(colours.red)
  90.                     write("  Invalid  ")
  91.                     sleep(1.5)
  92.                     term.setTextColour(colours.white)
  93.                     term.setCursorPos(startXpos - 2, startYpos - 2) write("|---     ---|")
  94.                     term.setTextColour(colours.red)
  95.                 end
  96.                 code = ""
  97.             end
  98.         end
  99.     end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment