Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. termX, termY = term.getSize()
  2. loged = nil
  3. local customColors = {
  4. colors.blue, --Background color
  5. colors.white, --Login field color
  6. colors.black --Text color
  7. }
  8.  
  9. function drawBackground()
  10. term.setBackgroundColor(customColors[1])
  11. term.clear()
  12. term.setCursorPos(1,1)
  13. end
  14.  
  15. function drawLoginField()
  16.  
  17. paintutils.drawLine(termX/2 - 10, termY/2 - 3, termX/2 + 10, termY/2 - 3, customColors[2])
  18. paintutils.drawLine(termX/2 - 10, termY/2 - 1, termX/2 + 10, termY/2 - 1, customColors[2])
  19. 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])
  20. paintutils.drawLine(termX/2 - 10, termY/2, termX/2 + 10, termY/2, customColors[2])
  21. paintutils.drawLine(termX/2 - 10, termY/2 + 2, termX/2 + 10, termY/2 + 2, customColors[2])
  22. paintutils.drawLine(termX/2 - 10, termY/2 - 4, termX/2 + 10, termY/2 - 4, customColors[2])
  23. 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])
  24. paintutils.drawLine(termX/2 - 10, termY/2 + 3, termX/2 + 10, termY/2 + 3, customColors[2])
  25. paintutils.drawLine(termX/2 - 10, termY/2 + 4, termX/2 + 10, termY/2 + 4, customColors[2])
  26.  
  27. term.setTextColor(customColors[3])
  28. term.setCursorPos(termX/2 - 4, termY/2 - 4)
  29. term.write("Login GUI")
  30. term.setCursorPos(termX/2 - 7, termY/2 - 2)
  31. term.write("Login:")
  32. term.setCursorPos(termX/2 - 10, termY/2 + 1)
  33. term.write("Password:")
  34. end
  35.  
  36. function loginFunction()
  37. term.setBackgroundColor(customColors[1])
  38. term.setCursorPos(termX/2 - 1, termY/2 - 2)
  39. local inputLogin = input(nil, 10)
  40. term.setCursorPos(termX/2 - 1, termY/2 + 1)
  41. local inputPassword = input("*", 10)
  42. local Table = findUser(inputLogin)
  43.  
  44. if Table ~= nil then
  45. local user = textutils.unserialize(Table)
  46.  
  47. if inputLogin == user.login and inputPassword == user.password then
  48. term.setCursorPos(termX/2 - 8, termY/2 + 6)
  49. term.setTextColor(colors.green)
  50. term.write("Successful login")
  51. sleep(2)
  52. successfulLogin()
  53. else
  54. term.setCursorPos(termX/2 - 9, termY/2 + 6)
  55. term.setTextColor(colors.red)
  56. term.write("Incorrect password")
  57. sleep(2)
  58. start()
  59. end
  60. else
  61. term.setCursorPos(termX/2 - 8, termY/2 + 6)
  62. term.setTextColor(colors.red)
  63. term.write("User not found")
  64. sleep(2)
  65. start()
  66. end
  67. end
  68.  
  69. function successfulLogin()
  70. loged = user
  71. term.setBackgroundColor(colors.black)
  72. term.clear()
  73. term.setCursorPos(1,1)
  74. end
  75.  
  76. function input( _sReplaceChar, charLimit, _tHistory )
  77. term.setCursorBlink( true )
  78.  
  79. local charNum = 0
  80. local sLine = ""
  81. local nHistoryPos = nil
  82. local nPos = 0
  83. if _sReplaceChar then
  84. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  85. end
  86.  
  87. local w, h = term.getSize()
  88. local sx, sy = term.getCursorPos()
  89.  
  90. local function redraw( _sCustomReplaceChar )
  91. local nScroll = 0
  92. if sx + nPos >= w then
  93. nScroll = (sx + nPos) - w
  94. end
  95.  
  96. term.setCursorPos( sx, sy )
  97. local sReplace = _sCustomReplaceChar or _sReplaceChar
  98. if sReplace then
  99. term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  100. else
  101. term.write( string.sub( sLine, nScroll + 1 ) )
  102. end
  103. term.setCursorPos( sx + nPos - nScroll, sy )
  104. end
  105.  
  106. while true do
  107. local sEvent, param = os.pullEvent()
  108. if sEvent == "char" then
  109. if charNum ~= charLimit then
  110. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  111. nPos = nPos + 1
  112. charNum = charNum + 1
  113. redraw()
  114. else
  115. break
  116. end
  117.  
  118. elseif sEvent == "key" then
  119. if param == keys.enter then
  120. -- Enter
  121. break
  122.  
  123. elseif param == keys.left then
  124. -- Left
  125. if nPos > 0 then
  126. nPos = nPos - 1
  127. redraw()
  128. end
  129.  
  130. elseif param == keys.right then
  131. -- Right
  132. if nPos < string.len(sLine) then
  133. nPos = nPos + 1
  134. redraw()
  135. end
  136.  
  137. elseif param == keys.up or param == keys.down then
  138. -- Up or down
  139. if _tHistory then
  140. redraw(" ");
  141. if param == keys.up then
  142. -- Up
  143. if nHistoryPos == nil then
  144. if #_tHistory > 0 then
  145. nHistoryPos = #_tHistory
  146. end
  147. elseif nHistoryPos > 1 then
  148. nHistoryPos = nHistoryPos - 1
  149. end
  150. else
  151. -- Down
  152. if nHistoryPos == #_tHistory then
  153. nHistoryPos = nil
  154. elseif nHistoryPos ~= nil then
  155. nHistoryPos = nHistoryPos + 1
  156. end
  157. end
  158.  
  159. if nHistoryPos then
  160. sLine = _tHistory[nHistoryPos]
  161. nPos = string.len( sLine )
  162. else
  163. sLine = ""
  164. nPos = 0
  165. end
  166. redraw()
  167. end
  168. elseif param == keys.backspace then
  169. -- Backspace
  170. if nPos > 0 then
  171. redraw(" ");
  172. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  173. nPos = nPos - 1
  174. charNum = charNum - 1
  175. redraw()
  176. end
  177. elseif param == keys.home then
  178. -- Home
  179. nPos = 0
  180. redraw()
  181. elseif param == keys.delete then
  182. if nPos < string.len(sLine) then
  183. redraw(" ");
  184. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  185. redraw()
  186. end
  187. elseif param == keys["end"] then
  188. -- End
  189. nPos = string.len(sLine)
  190. redraw()
  191. end
  192. end
  193. end
  194.  
  195. term.setCursorBlink( false )
  196. term.setCursorPos( w + 1, sy )
  197. print()
  198.  
  199. return sLine
  200. end
  201.  
  202. function start()
  203. drawBackground()
  204. drawLoginField()
  205. loginFunction()
  206. end
  207.  
  208. function findUser(name)
  209. local list = fs.open("/Users/list", "r")
  210. if list then
  211. local curLine = list.readLine()
  212.  
  213. while curLine ~= name do
  214. curLine = list.readLine()
  215. if curLine == nil then
  216. return nil
  217. end
  218. end
  219. if curLine == name then
  220. local userTable = list.readLine()
  221. list.close()
  222. return userTable
  223. end
  224. else
  225. generateUserList()
  226. end
  227. end
  228.  
  229. function generateUserList()
  230. if not fs.isDir("Users") then
  231. fs.makeDir("Users")
  232. end
  233.  
  234. local list = fs.open("Users/list", "w")
  235. local defaultUser = {login="login", password="password"}
  236. list.writeLine("login")
  237. list.writeLine(textutils.serialize(defaultUser))
  238.  
  239. end
  240.  
  241. --Starts the program
  242. start()
  243.  
  244. --Additional functions(Not working yet...)
  245.  
  246. function register(name, password)
  247. local list
  248.  
  249. if not fs.isDir("Users") then
  250. fs.makeDir("Users")
  251. end
  252.  
  253. if not fs.exists("Users/list") then
  254. list = fs.open("Users/list", "w")
  255. end
  256.  
  257. list.writeLine(name)
  258. list.writeLine("= {login=" .. name .. "," .. "password=" .. password .. "}")
  259. list.close()
  260. end
  261.  
  262. function getLogedUserName()
  263. if loged ~= nil then
  264. return loged.login
  265. else
  266. return "No user loged-in"
  267. end
  268. end
  269.  
  270. function getLogedPassword()
  271. if loged ~= nil then
  272. return loged.password
  273. else
  274. return "No user loged-in"
  275. end
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement