sidekick_

Simple keypad

Jan 10th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 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. abbruch = 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) print("-Zugangscode-")
  29.     term.setCursorPos(startXpos - 2, startYpos - 3) print("-------------")
  30.     term.setCursorPos(startXpos - 2, startYpos - 2) print("|---     ---|")
  31.     term.setCursorPos(startXpos - 2, startYpos - 1) print("-------------\n")
  32.     term.setCursorPos(startXpos - 2, startYpos)     print("|   |   |   |")
  33.     term.setCursorPos(startXpos - 2, startYpos + 1) print("-------------")
  34.     term.setCursorPos(startXpos - 2, startYpos + 2) print("|   |   |   |")
  35.     term.setCursorPos(startXpos - 2, startYpos + 3) print("-------------")
  36.     term.setCursorPos(startXpos - 2, startYpos + 4) print("|   |   |   |")
  37.     term.setCursorPos(startXpos - 2, startYpos + 5) print("-------------")
  38.     term.setCursorPos(startXpos - 2, startYpos + 6) print("|   |   |   |")
  39.     term.setCursorPos(startXpos - 2, startYpos + 7) print("-------------")
  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.     clicked = ""
  49.     bool = false
  50.     for i = 1, #t_keypad do
  51.         if _xPos >= t_keypad[i].xPos and _xPos <= (t_keypad[i].xPos + (string.len(t_keypad[i].text) - 1)) and
  52.         _yPos == t_keypad[i].yPos then
  53.             text = t_keypad[i].text
  54.             bool = true
  55.             break
  56.         end
  57.     end
  58.     return bool, text
  59. end
  60.  
  61. drawKeyPad()
  62.  
  63. while true do
  64.     term.setCursorPos(startXpos + 2, startYpos - 2)
  65.     write("     ")
  66.     term.setCursorPos(startXpos + 2, startYpos - 2)
  67.     write(string.rep("*", #code))
  68.     event, button, x, y = os.pullEvent()
  69.     if event == "mouse_click" then
  70.         validOption, clicked = checkMouseClick(x, y)
  71.         if validOption then
  72.             if tonumber(clicked) and #code < 5 then
  73.                 code = code .. clicked
  74.             elseif clicked == "CLR" then
  75.                 code = ""
  76.             elseif clicked == "ENT" then
  77.                 code = tonumber(code)
  78.                 if code == pass then
  79.                     rs.setOutput("bottom",true)
  80.                     sleep(3)
  81.                     rs.setOutput("bottom",false)
  82.                 elseif code == abbruch then
  83.                     term.setCursorPos(1, 1)
  84.                     term.clear()
  85.                     term.setTextColour(colours.red)
  86.                     print("Aborted.\n")
  87.                     term.setTextColour(colours.black)
  88.                     break
  89.                 else
  90.                     term.setCursorPos(startXpos - 1, startYpos - 2)
  91.                     term.setTextColour(colours.red)
  92.                     write("  Invalid  ")
  93.                     sleep(1.5)
  94.                     term.setTextColour(colours.white)
  95.                     term.setCursorPos(startXpos - 2, startYpos - 2) print("|---     ---|")
  96.                     term.setTextColour(colours.red)
  97.                 end
  98.                 code = ""
  99.             end
  100.         end
  101.     end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment