Advertisement
Guest User

Untitled

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