Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- termX, termY = term.getSize()
- loged = nil
- local customColors = {
- colors.red, --Background color
- colors.white, --Login field color
- colors.black --Text color
- }
- function drawBackground()
- term.setBackgroundColor(customColors[1])
- term.clear()
- term.setCursorPos(1,1)
- end
- function drawLoginField()
- paintutils.drawLine(termX/2 - 10, termY/2 - 3, termX/2 + 10, termY/2 - 3, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2 - 1, termX/2 + 10, termY/2 - 1, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2 + 1, termX/2 - 5, termY/2 + 1, customColors[2]) paintutils.drawLine(termX/2 + 9, termY/2 + 1, termX/2 + 10, termY/2 + 1, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2, termX/2 + 10, termY/2, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2 + 2, termX/2 + 10, termY/2 + 2, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2 - 4, termX/2 + 10, termY/2 - 4, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2 - 2, termX/2 - 2, termY/2 - 2, customColors[2]) paintutils.drawLine(termX/2 + 9, termY/2 - 2, termX/2 + 10, termY/2 - 2, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2 + 3, termX/2 + 10, termY/2 + 3, customColors[2])
- paintutils.drawLine(termX/2 - 10, termY/2 + 4, termX/2 + 10, termY/2 + 4, customColors[2])
- term.setTextColor(customColors[3])
- term.setCursorPos(termX/2 - 4, termY/2 - 4)
- term.write("Login GUI")
- term.setCursorPos(termX/2 - 7, termY/2 - 2)
- term.write("Login:")
- term.setCursorPos(termX/2 - 10, termY/2 + 1)
- term.write("Password:")
- end
- function loginFunction()
- term.setBackgroundColor(customColors[1])
- term.setCursorPos(termX/2 - 1, termY/2 - 2)
- local inputLogin = input(nil, 10)
- term.setCursorPos(termX/2 - 1, termY/2 + 1)
- local inputPassword = input("*", 10)
- rednet.open("top")
- rednet.send(54, inputLogin)
- senderId, message, distance = rednet.receive(5)
- if password == message then
- term.setCursorPos(termX/2 - 8, termY/2 + 6)
- term.setTextColor(colors.green)
- term.write("Successful login")
- sleep(2)
- successfulLogin()
- else
- term.setCursorPos(termX/2 - 9, termY/2 + 6)
- term.setTextColor(colors.red)
- term.write("Incorrect password")
- sleep(2)
- start()
- end
- end
- function successfulLogin()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
- function input( _sReplaceChar, charLimit, _tHistory )
- term.setCursorBlink( true )
- local charNum = 0
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw( _sCustomReplaceChar )
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- local sReplace = _sCustomReplaceChar or _sReplaceChar
- if sReplace then
- term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- if charNum ~= charLimit then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- charNum = charNum + 1
- redraw()
- else
- break
- end
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- break
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if _tHistory then
- redraw(" ");
- if param == keys.up then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- charNum = charNum - 1
- redraw()
- end
- elseif param == keys.home then
- -- Home
- nPos = 0
- redraw()
- elseif param == keys.delete then
- if nPos < string.len(sLine) then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
- redraw()
- end
- elseif param == keys["end"] then
- -- End
- nPos = string.len(sLine)
- redraw()
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
- function start()
- drawBackground()
- drawLoginField()
- loginFunction()
- end
- function findUser(name)
- local list = fs.open("/Users/list", "r")
- if list then
- local curLine = list.readLine()
- while curLine ~= name do
- curLine = list.readLine()
- if curLine == nil then
- return nil
- end
- end
- if curLine == name then
- local userTable = list.readLine()
- list.close()
- return userTable
- end
- else
- generateUserList()
- end
- end
- function generateUserList()
- if not fs.isDir("Users") then
- fs.makeDir("Users")
- end
- local list = fs.open("Users/list", "w")
- local defaultUser = {login="login", password="password"}
- list.writeLine("login")
- list.writeLine(textutils.serialize(defaultUser))
- end
- --Starts the program
- start()
- --Additional functions(Not working yet...)
- function register(name, password)
- local list
- if not fs.isDir("Users") then
- fs.makeDir("Users")
- end
- if not fs.exists("Users/list") then
- list = fs.open("Users/list", "w")
- end
- list.writeLine(name)
- list.writeLine("= {login=" .. name .. "," .. "password=" .. password .. "}")
- list.close()
- end
- function getLogedUserName()
- if loged ~= nil then
- return loged.login
- else
- return "No user loged-in"
- end
- end
- function getLogedPassword()
- if loged ~= nil then
- return loged.password
- else
- return "No user loged-in"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement