mad1231999

Untitled

Jan 28th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.95 KB | None | 0 0
  1. function string:split(sep)
  2.     local sep, fields = sep or ":", {}
  3.     local pattern = string.format("([^%s]+)", sep)
  4.     self:gsub(pattern, function(c) fields[#fields+1] = c end)
  5.     return fields
  6. end
  7.  
  8. math.round = function(n)
  9.     return math.floor(n + .5)
  10. end
  11.  
  12. local function dostring(s)
  13.     local f = io.open("__tmp", "w")
  14.     f:write(s)
  15.     f:close()
  16.  
  17.     local t = dofile("__tmp")
  18.     fs.delete("__tmp")
  19.  
  20.     return t
  21. end
  22.  
  23. local function inheritsFrom(baseClass)
  24.     local new_class = {}
  25.     local class_mt = {__index = new_class}
  26.  
  27.     new_class.new = function()
  28.         local newinst = {}
  29.         setmetatable(newinst, class_mt)
  30.         return newinst
  31.     end
  32.  
  33.     if baseClass then
  34.         setmetatable(new_class, {__index = baseClass})
  35.     end
  36.  
  37.     return new_class
  38. end
  39.  
  40. local function getLines(data)
  41.     local f = io.open("__tmp", "w")
  42.     f:write(data)
  43.     f:close()
  44.  
  45.     local f = io.open("__tmp", "r")
  46.     local l = f:read("*l")
  47.     local lines = {}
  48.  
  49.     while l ~= nil do
  50.         table.insert(lines, l)
  51.  
  52.         l = f:read("*l")
  53.     end
  54.  
  55.     f:close()
  56.  
  57.     fs.delete("__tmp")
  58.  
  59.     return lines
  60. end
  61.  
  62. -------------------------------------------------------------
  63. -------------------------------------------------------------
  64. --                    GUI Event class                      --
  65. -------------------------------------------------------------
  66. -------------------------------------------------------------
  67. local BUTTON_LEFT = 1;
  68. local BUTTON_RIGHT = 2;
  69. local BUTTON_W_UP = 3;
  70. local BUTTON_W_DOWN = 4;
  71. local BUTTON_W = 5;
  72.  
  73. local gui_event = {
  74.     x = 0;
  75.     y = 0;
  76.     button = BUTTON_LEFT;
  77.     key = 0;
  78. }
  79. gui_event.__index = gui_event
  80.  
  81. gui_event.new = function()
  82.     local t = {}
  83.     setmetatable(t, gui_event)
  84.  
  85.     return t
  86. end
  87.  
  88. local gui_base = {
  89.     width = 0;
  90.     height = 0;
  91.     x = 0;
  92.     y = 0;
  93.     hasBgColor = true;
  94.     visible = true;
  95.     bgColor = colors.lightBlue;
  96. }
  97. gui_base.__index = gui_base
  98.  
  99. function gui_base:onMouseEvent(ev)
  100. end
  101.  
  102. function gui_base:onKeyEvent(ev)
  103. end
  104.  
  105. function gui_base:draw()
  106. end
  107.  
  108.  
  109.  
  110. -------------------------------------------------------------
  111. -------------------------------------------------------------
  112. --                    GUI Button class                     --
  113. -------------------------------------------------------------
  114. -------------------------------------------------------------
  115. local gui_button = inheritsFrom(gui_base)
  116.  
  117. function gui_button:init(text, x, y, w, h)
  118.     self.textColor = colors.black
  119.     self.bgColor = colors.white
  120.     self.text = text
  121.     self.x = x
  122.     self.y = y
  123.     self.width = w
  124.     self.height = h
  125.     self.onClick = function(x, y) end
  126.     self.centerText = true
  127.     self.clickable = true
  128. end
  129.  
  130. function gui_button:draw()
  131.     if self.visible then
  132.         term.setTextColor(self.textColor)
  133.  
  134.         if self.hasBgColor then
  135.             term.setBackgroundColor(self.bgColor)
  136.             local w, h = self.width, self.height
  137.             local x, y = math.floor(self.x), math.floor(self.y)
  138.  
  139.             -- print("x, y = " .. x .. ", " .. y)
  140.  
  141.             for _x = 0, w - 1 do
  142.                 for _y = 0, h - 1 do
  143.                     -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
  144.                     term.setCursorPos(_x + x, _y + y)
  145.                     io.write(" ")
  146.                 end
  147.             end
  148.         end
  149.  
  150.         local pX, pY = math.floor(self.x), math.floor(self.y)
  151.         local w, h = self.width, self.height
  152.         local cX, cY = (self.centerText and math.floor(pX + w / 2) - self.text:len() / 2 or pX), (self.centerText and math.floor(pY + h / 2) or pY)
  153.         term.setCursorPos(cX, cY)
  154.         if self.text:len() == 1 then
  155.             term.setCursorPos(cX + 1, cY)
  156.             io.write(self.text)
  157.         else
  158.             io.write(self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())))
  159.         end
  160.     end
  161. end
  162.  
  163. function gui_button:onMouseEvent(ev)
  164.     if self.visible then
  165.         local _x, _y = math.floor(self.x), math.floor(self.y)
  166.         local w, h = self.width, self.height
  167.         -- _x, _y = _x - 1, _y - 1
  168.  
  169.         -- print("(" .. tostring(ev.x) .. ", " .. tostring(ev.y) .. ")")
  170.  
  171.         if self.clickable and ev.x >= _x and ev.x < _x + w and ev.y >= _y and ev.y < _y + h then
  172.             self.onClick(self, ev.x, ev.y)
  173.         end
  174.     end
  175. end
  176.  
  177.  
  178.  
  179. -------------------------------------------------------------
  180. -------------------------------------------------------------
  181. --                    GUI Textbox class                    --
  182. -------------------------------------------------------------
  183. -------------------------------------------------------------
  184. local gui_textfield = inheritsFrom(gui_base)
  185.  
  186. function gui_textfield:init(text, x, y, w, m_ch)
  187.     self.text = text
  188.     self.textColor = colors.black
  189.     self.bgColor = colors.white
  190.     self.replaceChar = nil
  191.     self.chars = {}
  192.     self.editable = true
  193.  
  194.     for i = 1, text:len() do
  195.         table.insert(self.chars, text:sub(i, i))
  196.     end
  197.  
  198.     local _w, h = term.getSize()
  199.  
  200.     self.x = x
  201.     self.y = y
  202.     self.width = w or 30
  203.     self.height = 1
  204.  
  205.     --[[
  206.     local sW, sH = self.width, self.height
  207.    
  208.     while sH ~= 1 do
  209.         self.height = self.height - 0.2
  210.         sW, sH = self.width, self.height
  211.     end
  212.     ]]
  213.  
  214.     self.m_ch = m_ch or false
  215.     self.__onClick = function(x, y)
  216.         self:edit()
  217.     end
  218. end
  219.  
  220. function gui_textfield:draw()
  221.     if self.visible then
  222.         term.setTextColor(self.textColor)
  223.  
  224.         if self.hasBgColor then
  225.             term.setBackgroundColor(self.bgColor)
  226.             local w, h = self.width, self.height
  227.             local x, y = math.floor(self.x), math.floor(self.y)
  228.  
  229.             -- print("x, y = " .. x .. ", " .. y)
  230.  
  231.             for _x = 0, w - 1 do
  232.                 for _y = 0, h - 1 do
  233.                     -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
  234.                     term.setCursorPos(_x + x, _y + y)
  235.                     io.write(" ")
  236.                 end
  237.             end
  238.         end
  239.  
  240.         local pX, pY = math.floor(self.x), math.floor(self.y)
  241.         -- pY = pY - 1
  242.         -- print(pX .. ", " .. pY)
  243.         local w, h = self.width, self.height
  244.         local cX, cY = pX, math.floor(pY + h / 2)
  245.         term.setCursorPos(cX, cY)
  246.        
  247.         local line = self.text
  248.         local _line = line
  249.  
  250.         if self.replaceChar then
  251.             line = ""
  252.  
  253.             for i = 1, _line:len() do
  254.                 line = line .. self.replaceChar:sub(1, 1)
  255.             end
  256.         end
  257.  
  258.         io.write(line:sub(1, (line:len() > w - 1 and w - 1 or line:len())))
  259.     end
  260. end
  261.  
  262. function gui_textfield:edit()
  263.     if self.visible and self.editable then
  264.         local pX, pY = math.floor(self.x), math.floor(self.y)
  265.         -- pX, pY = pX, pY - 1
  266.         -- print(pX .. ", " .. pY)
  267.         local w, h = self.width, self.height
  268.         term.setCursorPos(pX, math.floor(pY + h / 2))
  269.         term.setCursorBlink(true)
  270.  
  271.         local line = self.text
  272.         local cursorX = line:len()
  273.         local chars = {}
  274.  
  275.         for i = 1, self.text:len() do
  276.             table.insert(chars, self.text:sub(i, i))
  277.         end
  278.  
  279.         local function redraw(r_ch)
  280.             term.setTextColor(self.textColor)
  281.             term.setBackgroundColor(self.bgColor)
  282.             term.setCursorPos(pX, math.floor(pY + h / 2))
  283.  
  284.             line = ""
  285.  
  286.             for _, c in ipairs(chars) do
  287.                 line = line .. c
  288.             end
  289.  
  290.             local _line = line
  291.  
  292.             if self.replaceChar then
  293.                 line = ""
  294.  
  295.                 for i = 1, _line:len() do
  296.                     line = line .. self.replaceChar:sub(1, 1)
  297.                 end
  298.             end
  299.  
  300.             for i = 1, w do
  301.                 io.write(" ")
  302.             end
  303.  
  304.             term.setCursorPos(pX, math.floor(pY + h / 2))
  305.  
  306.             local currentX = pX
  307.  
  308.             if line:len() >= w - 1 and cursorX >= w - 1 then
  309.                 io.write(line:sub(cursorX - (w - 2), cursorX))
  310.                 currentX = pX + cursorX - line:sub(1, cursorX - (w - 1)):len()
  311.             elseif line:len() >= w - 1 and cursorX < w - 1 then
  312.                 io.write(line:sub(1, w - 1))
  313.                 currentX = pX + cursorX
  314.             else
  315.                 io.write(line)
  316.                 currentX = pX + cursorX
  317.             end
  318.  
  319.             line = _line
  320.  
  321.             term.setCursorPos(currentX, math.floor(pY + h / 2))
  322.         end
  323.  
  324.         redraw(self.replaceChar)
  325.  
  326.         while true do
  327.             local ev, key = os.pullEvent()
  328.  
  329.             if ev == "key" and (m_ch and #chars < m_ch or true) then
  330.                 if key == keys.left then
  331.                     if cursorX > 0 then
  332.                         cursorX = cursorX - 1
  333.                     end
  334.                 elseif key == keys.right then
  335.                     if cursorX < line:len() then
  336.                         cursorX = cursorX + 1
  337.                     end
  338.                 elseif key == keys.enter then
  339.                     break
  340.                 elseif key == keys.backspace then
  341.                     if cursorX > 0 then
  342.                         table.remove(chars, cursorX)
  343.                         cursorX = cursorX - 1
  344.                     end
  345.                 elseif key == keys.delete then
  346.                     if cursorX < line:len() then
  347.                         table.remove(chars, cursorX + 1)
  348.                     end
  349.                 end
  350.             elseif ev == "char" then
  351.                 table.insert(chars, cursorX + 1, key)
  352.                 cursorX = cursorX + 1
  353.             end
  354.  
  355.             redraw()
  356.         end
  357.  
  358.         term.setCursorBlink(false)
  359.         self.text = line
  360.     end
  361. end
  362.  
  363. function gui_textfield:setText(t)
  364.     self.text = t
  365.  
  366.     for i = 1, t:len() do
  367.         self.chars[i] = t:sub(i, i)
  368.     end
  369. end
  370.  
  371. function gui_textfield:onMouseEvent(ev)
  372.     if self.visible then
  373.         local _x, _y = math.floor(self.x), math.floor(self.y)
  374.         local w, h = self.width, self.height
  375.  
  376.         if self.editable and ev.x >= _x and ev.x < _x + w and ev.y >= _y and ev.y < _y + h then
  377.             self.__onClick(ev.x, ev.y)
  378.         end
  379.     end
  380. end
  381.  
  382.  
  383.  
  384. -------------------------------------------------------------
  385. -------------------------------------------------------------
  386. --                    GUI Label class                      --
  387. -------------------------------------------------------------
  388. -------------------------------------------------------------
  389. local gui_label = inheritsFrom(gui_base)
  390.  
  391. function gui_label:init(text, x, y)
  392.     self.textColor = colors.black
  393.     self.bgColor = colors.white
  394.     self.text = text
  395.     self.x = x
  396.     self.y = y
  397.     self.centerText = false
  398.  
  399.     self.width = text:len() + 1
  400.     self.height = 1
  401. end
  402.  
  403. function gui_label:draw()
  404.     if self.visible then
  405.         term.setTextColor(self.textColor)
  406.  
  407.         if self.hasBgColor then
  408.             term.setBackgroundColor(self.bgColor)
  409.             local w, h = self.width, self.height
  410.             local x, y = math.floor(self.x), math.floor(self.y)
  411.  
  412.             -- print("x, y = " .. x .. ", " .. y)
  413.  
  414.             for _x = 0, w - 1 do
  415.                 for _y = 0, h - 1 do
  416.                     -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
  417.                     term.setCursorPos(_x + x, _y + y)
  418.                     io.write(" ")
  419.                 end
  420.             end
  421.         end
  422.  
  423.         local pX, pY = math.floor(self.x), math.floor(self.y)
  424.         -- print(pX .. ", " .. pY)
  425.         local w, h = self.width, self.height
  426.         local cX, cY = (self.centerText and math.floor(pX + w / 2) - self.text:len() / 2 or pX), (self.centerText and math.floor(pY + h / 2) or pY)
  427.         term.setCursorPos(cX, cY)
  428.         if self.text:len() == 1 then
  429.             term.setCursorPos(cX + 1, cY)
  430.             io.write(self.text)
  431.         else
  432.             io.write(self.text:sub(1, (self.text:len() > w - 1 and w - 1 or self.text:len())))
  433.         end
  434.     end
  435. end
  436.  
  437.  
  438.  
  439. -------------------------------------------------------------
  440. -------------------------------------------------------------
  441. --                 GUI Rectangle class                     --
  442. -------------------------------------------------------------
  443. -------------------------------------------------------------
  444. local gui_rect = inheritsFrom(gui_base)
  445.  
  446. function gui_rect:init(x, y, w, h, c)
  447.     self.x = x
  448.     self.y = y
  449.     self.width = w
  450.     self.height = h
  451.     self.bgColor = c
  452. end
  453.  
  454. function gui_rect:draw()
  455.     if self.visible then
  456.         if self.hasBgColor then
  457.             term.setBackgroundColor(self.bgColor)
  458.             local w, h = self.width, self.height
  459.             local x, y = math.floor(self.x), math.floor(self.y)
  460.  
  461.             -- print("x, y = " .. x .. ", " .. y)
  462.  
  463.             for _x = 0, w - 1 do
  464.                 for _y = 0, h - 1 do
  465.                     -- print("Drawing at (" .. _x + x .. ", " .. _y + y .. ")")
  466.                     term.setCursorPos(_x + x, _y + y)
  467.                     io.write(" ")
  468.                 end
  469.             end
  470.         end
  471.     end
  472. end
  473.  
  474.  
  475. local function createMessageBox(title, _msg, _type)
  476.     local host = {}
  477.  
  478.     local msgBox = gui_rect.new()
  479.     msgBox:init(S_WIDTH / 2 - 19, S_HEIGHT / 2 - 5, 20 * 2, 10)
  480.     msgBox.index = #host + 1
  481.     host.msgBox = msgBox
  482.  
  483.     local msgBoxTitleBar = gui_rect.new()
  484.     msgBoxTitleBar:init(S_WIDTH / 2 - 19, S_HEIGHT / 2 - 5, 20 * 2, 1)
  485.     msgBoxTitleBar.bgColor = colors.lightGray
  486.     host.msgBoxTitleBar = msgBoxTitleBar
  487.  
  488.     local msgBoxTitle = gui_label.new()
  489.     local t = "Failure"
  490.     msgBoxTitle:init(t, S_WIDTH / 2 - 19, S_HEIGHT / 2 - 5)
  491.     msgBoxTitle.bgColor = msgBoxTitleBar.bgColor
  492.     host.msgBoxTitle = msgBoxTitle
  493.  
  494.     local msgBoxQuit = gui_button.new()
  495.     msgBoxQuit:init("X", S_WIDTH / 2 - 19 + 20 * 2 - 1, S_HEIGHT / 2 - 5, 1, 1)
  496.     msgBoxQuit.bgColor = colors.red
  497.     msgBoxQuit.textColor = colors.white
  498.     msgBoxQuit.onClick = function(self, x, y)
  499.         for _, el in pairs(host) do
  500.             el.visible = false
  501.         end
  502.     end
  503.     host.msgBoxQuit = msgBoxQuit
  504.  
  505.     local msgLabel = gui_label.new()
  506.     msgLabel:init(_msg, S_WIDTH / 2 - _msg:len() / 2, S_HEIGHT / 2, _msg:len(), 1)
  507.     msgLabel.bgColor = msgBox.bgColor
  508.     msgLabel.textColor = colors.black
  509.  
  510.     if _type:lower() == "error" then
  511.         msgLabel.bgColor = colors.red
  512.     end
  513.  
  514.     host.msgLabel = msgLabel
  515.  
  516.     return host
  517. end
  518.  
  519.  
  520. local usr = "CCUTeacherProf"
  521. local psw = "ChickenWings"
  522.  
  523.  
  524. local guiElements = {}
  525. local loginScreen = {}
  526.  
  527. local bg = gui_rect.new()
  528. bg:init(0, 0, S_WIDTH + 1, S_HEIGHT + 1)
  529. bg.bgColor = colors.cyan
  530.  
  531. local userLabel = gui_label.new()
  532. userLabel:init("Username: ", S_WIDTH / 2 - 10, S_HEIGHT / 2 + 2, 10, 1)
  533. userLabel.bgColor = bg.bgColor
  534.  
  535. local userField = gui_textfield.new()
  536. userField:init("", S_WIDTH / 2 + 1, S_HEIGHT / 2 + 2, 15, 1)
  537.  
  538. local passLabel = gui_label.new()
  539. passLabel:init("Password: ", S_WIDTH / 2 - 10, S_HEIGHT / 2 + 2 + 2, 10, 1)
  540. passLabel.bgColor = bg.bgColor
  541.  
  542. local passField = gui_textfield.new()
  543. passField:init("", S_WIDTH / 2 + 1, S_HEIGHT / 2 + 2 + 2, 15, 1)
  544. passField.replaceChar = "*"
  545.  
  546. table.insert(loginScreen, bg)
  547. table.insert(loginScreen, userLabel)
  548. table.insert(loginScreen, userField)
  549. table.insert(loginScreen, passLabel)
  550. table.insert(loginScreen, passField)
  551.  
  552. local loginButton = gui_button.new()
  553. loginButton:init("Submit", S_WIDTH / 2 + 8, S_HEIGHT / 2 + 2 + 2 + 2, 8, 3)
  554. table.insert(loginScreen, loginButton)
  555.  
  556. loginButton.onClick = function(loginB, x, y)
  557.     local success = (userField.text == usr and passField.text == psw)
  558.  
  559.     if success then
  560.         redstone.setOutput("right", true)
  561.         sleep(2)
  562.         redstone.setOutput("right", false)
  563.         os.reboot()
  564.     else
  565.         local _msg = "Wrong username or password."
  566.  
  567.         userField.editable = false
  568.         passField.editable = false
  569.         loginButton.clickable = false
  570.         cancelButton.clickable = false
  571.  
  572.         local msg = createMessageBox("Failure", _msg, "error")
  573.         msg.msgBoxQuit.onClick = function(self, x, y)
  574.             userField.editable = true
  575.             passField.editable = true
  576.             loginButton.clickable = true
  577.             cancelButton.clickable = true
  578.  
  579.             for _, el in pairs(msg) do
  580.                 el.visible = false
  581.             end
  582.         end
  583.  
  584.         for _, el in pairs(msg) do
  585.             table.insert(loginScreen, el)
  586.         end
  587.     end
  588. end
  589.  
  590. guiElements = loginScreen
  591.  
  592. for _, el in ipairs(guiElements) do
  593.     el:draw()
  594. end
  595.  
  596. while true do
  597.     local ev, p1, p2, p3, p4 = os.pullEvent()
  598.  
  599.     local gui_ev = gui_event.new()
  600.  
  601.     if ev == "mouse_click" then
  602.         gui_ev.button = p1
  603.         if p1 == 3 then
  604.             gui_ev.button = BUTTON_W
  605.         end
  606.         gui_ev.x = p2
  607.         gui_ev.y = p3
  608.        
  609.         for _, el in ipairs(guiElements) do
  610.             el:onMouseEvent(gui_ev)
  611.         end    
  612.     elseif ev == "mouse_scroll" then
  613.         gui_ev.button = p1 == 1 and BUTTON_W_DOWN or BUTTON_W_UP
  614.         gui_ev.x = p2
  615.         gui_ev.y = p3
  616.        
  617.         for _, el in ipairs(guiElements) do
  618.             el:onMouseEvent(gui_ev)
  619.         end
  620.     elseif ev == "key" then
  621.         gui_ev.key = p1
  622.  
  623.         for _, el in ipairs(guiElements) do
  624.             el:onKeyEvent(gui_ev)
  625.         end
  626.     end
  627.    
  628.     for _, el in ipairs(guiElements) do
  629.         el:draw()
  630.     end
  631. end
Advertisement
Add Comment
Please, Sign In to add comment