mad1231999

Untitled

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