Advertisement
Guest User

Untitled

a guest
Feb 17th, 2013
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. -- Keypad Lock Program for Computercraft
  2. -- Written by Sora the Hedgehog
  3.  
  4. -- Uncomment these line to disable Crtl-Ting
  5. -- local oldPull = os.pullEvent;
  6. -- os.pullEvent = os.pullEventRaw;
  7.  
  8. local locations = {
  9. [1] = {13, 3},
  10. [2] = {17, 3},
  11. [3] = {21, 3},
  12. [4] = {13, 7},
  13. [5] = {17, 7},
  14. [6] = {21, 7},
  15. [7] = {13, 11},
  16. [8] = {17, 11},
  17. [9] = {21, 11},
  18. [0] = {17, 15};
  19. ["clear"] = {13, 15},
  20. ["enter"] = {21, 15},
  21. ["dsply"] = {26, 9},
  22. ["code"] = {28, 8};
  23. };
  24.  
  25. local numColor = colors.white;
  26. local butBgOff = colors.blue;
  27. local butBgOn = colors.red;
  28. local dispBad = colors.red;
  29. local dispGood = colors.lime;
  30. -- Code can be any int-representable 4 digit string
  31. local code = "";
  32. local input = "";
  33.  
  34. function onSuccess()
  35. -- When the code has been entered
  36.  
  37. -- Uncomment if you turned off Crtl-Ting above and wish to turn it back on
  38. -- os.pullEvent = oldPull
  39. end
  40.  
  41. function onFail()
  42. -- When the code has not been entered
  43. end
  44.  
  45. function drawBut(button, onoff)
  46. onoff = onoff or false;
  47.  
  48. if onoff then
  49. color = butBgOn;
  50. else
  51. color = butBgOff;
  52. end
  53.  
  54. local x, y = unpack(locations[button]);
  55. local text = "";
  56.  
  57. if type(button) == "string" then
  58. if button == "enter" then
  59. text = "E";
  60. elseif button == "clear" then
  61. text = "C";
  62. end
  63. else
  64. text = button;
  65. end
  66.  
  67. for a = x, x + 2 do
  68. for b = y, y + 2 do
  69. term.setCursorPos(a, b);
  70. term.setBackgroundColor(color);
  71. if a == x + 1 and b == y + 1 then
  72. term.setTextColor(numColor);
  73. write(text);
  74. else
  75. write(" ");
  76. end
  77. end
  78. end
  79. end
  80.  
  81. function drawDisp(input, state)
  82. local x, y = unpack(locations.dsply);
  83. local codeLen = input;
  84. state = state or 0;
  85.  
  86. local colors = {
  87. [0] = butBgOff,
  88. [1] = dispBad,
  89. [2] = dispGood,
  90. };
  91.  
  92. term.setBackgroundColor(colors[state]);
  93.  
  94. for a = x, x + 8 do
  95. for b = y, y + 2 do
  96. term.setCursorPos(a, b);
  97.  
  98. local diff = a - x;
  99.  
  100. if codeLen > 0 and (b == y + 1 and (diff % 2) == 1) then
  101. write("*");
  102. codeLen = codeLen - 1;
  103. else
  104. write(" ");
  105. end
  106. end
  107. end
  108. end
  109.  
  110. function getButPress(x, y)
  111. for k, v in pairs(locations) do
  112. local bx, by = unpack(v);
  113. if (k ~= "code" and k ~= "dsply") and ((x >= bx and x <= bx + 2) and (y >= by and y <= by + 2)) then
  114. return k;
  115. end
  116. end
  117.  
  118. return nil;
  119. end
  120.  
  121. if term.isColor and (not term.isColor()) then
  122. print("This program requires an advanced computer");
  123. return;
  124. end
  125. else
  126. print("This program requires a newer version of Computercraft");
  127. return;
  128. end
  129.  
  130. term.clear();
  131.  
  132. for i = 0, 9 do
  133. drawBut(i, false);
  134. end
  135.  
  136. drawBut("clear", false);
  137. drawBut("enter", false);
  138.  
  139. local cx, cy = unpack(locations["code"]);
  140. term.setCursorPos(cx, cy);
  141. term.setBackgroundColor(colors.black);
  142. term.setTextColor(colors.lime);
  143. write("Input");
  144.  
  145. drawDisp(0);
  146.  
  147. while true do
  148. local _, _, mx, my = os.pullEvent("mouse_click");
  149. local button = getButPress(mx, my);
  150.  
  151. if button ~= nil then
  152. drawBut(button, true);
  153. if type(button) == "number" then
  154. if string.len(input) < 4 then
  155. input = input..button;
  156. end
  157. else
  158. if button == "clear" then
  159. input = "";
  160. elseif button == "enter" then
  161. if input == code then
  162. drawDisp(string.len(input), 2);
  163. input = "";
  164. onSuccess();
  165. else
  166. drawDisp(string.len(input), 1);
  167. input = "";
  168. onFail();
  169. end
  170. end
  171. end
  172. sleep(0.1)
  173. drawBut(button, false);
  174. drawDisp(string.len(input));
  175. end
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement