Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Keypad Lock Program for Computercraft
- -- Written by Sora the Hedgehog
- -- Uncomment these line to disable Crtl-Ting
- -- local oldPull = os.pullEvent;
- -- os.pullEvent = os.pullEventRaw;
- local locations = {
- [1] = {13, 3},
- [2] = {17, 3},
- [3] = {21, 3},
- [4] = {13, 7},
- [5] = {17, 7},
- [6] = {21, 7},
- [7] = {13, 11},
- [8] = {17, 11},
- [9] = {21, 11},
- [0] = {17, 15};
- ["clear"] = {13, 15},
- ["enter"] = {21, 15},
- ["dsply"] = {26, 9},
- ["code"] = {28, 8};
- };
- local numColor = colors.white;
- local butBgOff = colors.blue;
- local butBgOn = colors.red;
- local dispBad = colors.red;
- local dispGood = colors.lime;
- -- Code can be any int-representable 4 digit string
- local code = "";
- local input = "";
- function onSuccess()
- -- When the code has been entered
- -- Uncomment if you turned off Crtl-Ting above and wish to turn it back on
- -- os.pullEvent = oldPull
- end
- function onFail()
- -- When the code has not been entered
- end
- function drawBut(button, onoff)
- onoff = onoff or false;
- if onoff then
- color = butBgOn;
- else
- color = butBgOff;
- end
- local x, y = unpack(locations[button]);
- local text = "";
- if type(button) == "string" then
- if button == "enter" then
- text = "E";
- elseif button == "clear" then
- text = "C";
- end
- else
- text = button;
- end
- for a = x, x + 2 do
- for b = y, y + 2 do
- term.setCursorPos(a, b);
- term.setBackgroundColor(color);
- if a == x + 1 and b == y + 1 then
- term.setTextColor(numColor);
- write(text);
- else
- write(" ");
- end
- end
- end
- end
- function drawDisp(input, state)
- local x, y = unpack(locations.dsply);
- local codeLen = input;
- state = state or 0;
- local colors = {
- [0] = butBgOff,
- [1] = dispBad,
- [2] = dispGood,
- };
- term.setBackgroundColor(colors[state]);
- for a = x, x + 8 do
- for b = y, y + 2 do
- term.setCursorPos(a, b);
- local diff = a - x;
- if codeLen > 0 and (b == y + 1 and (diff % 2) == 1) then
- write("*");
- codeLen = codeLen - 1;
- else
- write(" ");
- end
- end
- end
- end
- function getButPress(x, y)
- for k, v in pairs(locations) do
- local bx, by = unpack(v);
- if (k ~= "code" and k ~= "dsply") and ((x >= bx and x <= bx + 2) and (y >= by and y <= by + 2)) then
- return k;
- end
- end
- return nil;
- end
- if term.isColor and (not term.isColor()) then
- print("This program requires an advanced computer");
- return;
- end
- else
- print("This program requires a newer version of Computercraft");
- return;
- end
- term.clear();
- for i = 0, 9 do
- drawBut(i, false);
- end
- drawBut("clear", false);
- drawBut("enter", false);
- local cx, cy = unpack(locations["code"]);
- term.setCursorPos(cx, cy);
- term.setBackgroundColor(colors.black);
- term.setTextColor(colors.lime);
- write("Input");
- drawDisp(0);
- while true do
- local _, _, mx, my = os.pullEvent("mouse_click");
- local button = getButPress(mx, my);
- if button ~= nil then
- drawBut(button, true);
- if type(button) == "number" then
- if string.len(input) < 4 then
- input = input..button;
- end
- else
- if button == "clear" then
- input = "";
- elseif button == "enter" then
- if input == code then
- drawDisp(string.len(input), 2);
- input = "";
- onSuccess();
- else
- drawDisp(string.len(input), 1);
- input = "";
- onFail();
- end
- end
- end
- sleep(0.1)
- drawBut(button, false);
- drawDisp(string.len(input));
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement