This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Grey_M62's profile picture

[Lua] Selection System

Started by Grey_M62, 15 July 2012 - 04:10 AM
Grey_M62 #1
Posted 15 July 2012 - 06:10 AM
So, i have been watching some videos, and i saw a nice looking selection system, but i can't figure out how to make it. it looks like this:
Secure Login
[1] Login
 2  Shutdown
 3  Reboot
(By the way, these inputs are only examples.) That is what I would want it to look like when it starts. I want it to be so that if you press the down arrow, then it moves the brackets to number 2 like this:
Secure Login
 1  Login
[2] Shutdown
 3  Reboot
It would basically move which direction the arrow key tells it to, until you press enter which would do the selected option. I have my own code for Login and i would rather not make it a different program, so I would just place it into the code somewhere.

EDIT: Also, when you press down at 3, it moves to 1. If you press up at 1, it moves to 3.
Lyqyd #2
Posted 15 July 2012 - 08:31 AM
This sort of system draws the menu, keeps track of where the selection is, adjusts the selection when a key is pressed and then redraws the menu after adjusting the selection. When another key is pressed, it acts on whichever option was currently selected. Things you'll use for this include a while true do loop, os.pullEvent(), etc.
Grey_M62 #3
Posted 15 July 2012 - 08:45 AM
This sort of system draws the menu, keeps track of where the selection is, adjusts the selection when a key is pressed and then redraws the menu after adjusting the selection. When another key is pressed, it acts on whichever option was currently selected. Things you'll use for this include a while true do loop, os.pullEvent(), etc.
I am still new to computercraft and lua itself, so i don't really know how i would set all of this up, but i sort of know what you are talking about. could you help me?
Exerro #4
Posted 15 July 2012 - 07:25 PM
try this code..but try to work things out on your own before asking

x = 1
y = 1
z = 3
while true do
term.clear()
term.setCursorPos(1,1)
print(" 1  Login")
print(" 2  Shutdown")
print(" 3  Reboot")
term.setCursorPos(x,y)
print("[")
term.setCursorPos(z,y)
print("]")
event, key = os.pullEvent()
if key == 208 and y < 3 then
y = y+1
elseif key == 208 and y == 3 then
y = 1
elseif key == 200 and y > 1 then
y = y-1
elseif key == 200 and y == 1 then
y = 3
elseif key == 28 then
if y == 1 then
-- code here
elseif y == 2 then
os.shutdown()
elseif y == 3 then
os.reboot()
else
term.setCursorPos(1,4)
print("something went wrong!?!?!?")
sleep(2)
end
end
end
MysticT #5
Posted 15 July 2012 - 08:10 PM
There's a tutorial about this in the tutorials section. It includes a yes/no menu and one like you want.
Grey_M62 #6
Posted 15 July 2012 - 10:45 PM
try this code..but try to work things out on your own before asking

x = 1
y = 1
z = 3
while true do
term.clear()
term.setCursorPos(1,1)
print(" 1  Login")
print(" 2  Shutdown")
print(" 3  Reboot")
term.setCursorPos(x,y)
print("[")
term.setCursorPos(z,y)
print("]")
event, key = os.pullEvent()
if key == 208 and y < 3 then
y = y+1
elseif key == 208 and y == 3 then
y = 1
elseif key == 200 and y > 1 then
y = y-1
elseif key == 200 and y == 1 then
y = 3
elseif key == 28 then
if y == 1 then
-- code here
elseif y == 2 then
os.shutdown()
elseif y == 3 then
os.reboot()
else
term.setCursorPos(1,4)
print("something went wrong!?!?!?")
sleep(2)
end
end
end
This works for me, exept for one thing. With my login code, once i successfully login, it basically reboots.
here is the code so far:

local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
x = 1
y = 3
z = 3
while true do
term.clear()
term.setCursorPos(1,1)
print("Secure PC v3.3")
print("")
print(" 1  Login")
print(" 2  Shutdown")
print(" 3  Reboot")
term.setCursorPos(x,y)
print("[")
term.setCursorPos(z,y)
print("]")
event, key = os.pullEvent()
if key == 208 and y < 5 then
y = y+1
elseif key == 208 and y == 5 then
y = 3
elseif key == 200 and y > 3 then
y = y-1
elseif key == 200 and y == 3 then
y = 5
elseif key == 28 then
if y == 3 then
tUsers = {}
local file = nil;
local userName = "";
if not fs.exists("Users") and not fs.isDir("Users") then
shell.run("mkdir", "Users");
end
function loadUsers()
tFiles = fs.list("Users");
for i=1, #tFiles do
  file = fs.open("Users/"..tFiles[i], "r");
  tUsers[i] = {name = file.readLine(), password = file.readLine()};
  file.close();
end
end
function printUsers()
for i,n in pairs(tUsers) do
  print(n.name);
end
end
function clear() term.clear(); term.setCursorPos(1, 1); end
function getLogin()
clear();
userName = ""; -- This will reference the global variable 'userName'
local userPass = "";
print( "Secure PC v3.2 - Locked" )
print("")
sleep(1)
term.write("Username: ");
print("");
term.write("Password: ");
term.setCursorPos(11, 3);
userName = read();
term.setCursorPos(11, 4);
userPass = read("*");
for i=1, #tUsers do
  if userName == tUsers[i].name then
   if userPass == tUsers[i].password then
			    return true;
   end
  end
end
return false;
end
loadUsers();
if getLogin() then
print("User Verified.");
sleep(1);
write("Welcome, ".. userName..".")
sleep(0.75);
clear();
print("Secure PC v3.2 - Unlocked");
print("")
sleep(0.5)
else
print("Incorrect Username and/or Password.");
sleep(1);
write( "Computer Shutting Down" ) sleep(1) write( "." ) sleep(1) write( "." ) sleep(1) write( "." )
sleep(0.5)
os.shutdown();
end
elseif y == 4 then
os.shutdown()
elseif y == 5 then
os.reboot()
else
term.setCursorPos(1,7)
print("something went wrong!?!?!?")
sleep(2)
end
end
end
os.pullEvent = oldPull

You will need to make a user for the login by typing 'edit Users/<username>' and making the first line <username> and the second line <password>.
Exerro #7
Posted 16 July 2012 - 04:30 PM
the problem is that when you leave it (just before else) it goes back to the loop instead of unlocking…use this
Spoiler

local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
x = 1
y = 3
z = 3
while true do
term.clear()
term.setCursorPos(1,1)
print("Secure PC v3.3")
print("")
print(" 1  Login")
print(" 2  Shutdown")
print(" 3  Reboot")
term.setCursorPos(x,y)
print("[")
term.setCursorPos(z,y)
print("]")
event, key = os.pullEvent()
if key == 208 and y < 5 then
y = y+1
elseif key == 208 and y == 5 then
y = 3
elseif key == 200 and y > 3 then
y = y-1
elseif key == 200 and y == 3 then
y = 5
elseif key == 28 then
if y == 3 then
tUsers = {}
local file = nil;
local userName = "";
if not fs.exists("Users") and not fs.isDir("Users") then
shell.run("mkdir", "Users");
end
function loadUsers()
tFiles = fs.list("Users");
for i=1, #tFiles do
  file = fs.open("Users/"..tFiles[i], "r");
  tUsers[i] = {name = file.readLine(), password = file.readLine()};
  file.close();
end
end
function printUsers()
for i,n in pairs(tUsers) do
  print(n.name);
end
end
function clear() term.clear(); term.setCursorPos(1, 1); end
function getLogin()
clear();
userName = ""; -- This will reference the global variable 'userName'
local userPass = "";
print( "Secure PC v3.2 - Locked" )
print("")
sleep(1)
term.write("Username: ");
print("");
term.write("Password: ");
term.setCursorPos(11, 3);
userName = read();
term.setCursorPos(11, 4);
userPass = read("*");
for i=1, #tUsers do
  if userName == tUsers[i].name then
   if userPass == tUsers[i].password then
							return true;
   end
  end
end
return false;
end
loadUsers();
if getLogin() then
print("User Verified.");
sleep(1);
write("Welcome, ".. userName..".")
sleep(0.75);
clear();
print("Secure PC v3.2 - Unlocked");
print("")
sleep(0.5)
break -- this breaks the loop
else
print("Incorrect Username and/or Password.");
sleep(1);
write( "Computer Shutting Down" ) sleep(1) write( "." ) sleep(1) write( "." ) sleep(1) write( "." )
sleep(0.5)
os.shutdown();
end
elseif y == 4 then
os.shutdown()
elseif y == 5 then
os.reboot()
else
term.setCursorPos(1,7)
print("something went wrong!?!?!?")
sleep(2)
end
end
end
os.pullEvent = oldPull
Grey_M62 #8
Posted 18 July 2012 - 07:36 AM
the problem is that when you leave it (just before else) it goes back to the loop instead of unlocking…use this
Spoiler

local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
x = 1
y = 3
z = 3
while true do
term.clear()
term.setCursorPos(1,1)
print("Secure PC v3.3")
print("")
print(" 1  Login")
print(" 2  Shutdown")
print(" 3  Reboot")
term.setCursorPos(x,y)
print("[")
term.setCursorPos(z,y)
print("]")
event, key = os.pullEvent()
if key == 208 and y < 5 then
y = y+1
elseif key == 208 and y == 5 then
y = 3
elseif key == 200 and y > 3 then
y = y-1
elseif key == 200 and y == 3 then
y = 5
elseif key == 28 then
if y == 3 then
tUsers = {}
local file = nil;
local userName = "";
if not fs.exists("Users") and not fs.isDir("Users") then
shell.run("mkdir", "Users");
end
function loadUsers()
tFiles = fs.list("Users");
for i=1, #tFiles do
  file = fs.open("Users/"..tFiles[i], "r");
  tUsers[i] = {name = file.readLine(), password = file.readLine()};
  file.close();
end
end
function printUsers()
for i,n in pairs(tUsers) do
  print(n.name);
end
end
function clear() term.clear(); term.setCursorPos(1, 1); end
function getLogin()
clear();
userName = ""; -- This will reference the global variable 'userName'
local userPass = "";
print( "Secure PC v3.2 - Locked" )
print("")
sleep(1)
term.write("Username: ");
print("");
term.write("Password: ");
term.setCursorPos(11, 3);
userName = read();
term.setCursorPos(11, 4);
userPass = read("*");
for i=1, #tUsers do
  if userName == tUsers[i].name then
   if userPass == tUsers[i].password then
							return true;
   end
  end
end
return false;
end
loadUsers();
if getLogin() then
print("User Verified.");
sleep(1);
write("Welcome, ".. userName..".")
sleep(0.75);
clear();
print("Secure PC v3.2 - Unlocked");
print("")
sleep(0.5)
break -- this breaks the loop
else
print("Incorrect Username and/or Password.");
sleep(1);
write( "Computer Shutting Down" ) sleep(1) write( "." ) sleep(1) write( "." ) sleep(1) write( "." )
sleep(0.5)
os.shutdown();
end
elseif y == 4 then
os.shutdown()
elseif y == 5 then
os.reboot()
else
term.setCursorPos(1,7)
print("something went wrong!?!?!?")
sleep(2)
end
end
end
os.pullEvent = oldPull
Can't believe it was that simple ._.
thanks…