mad1231999

GUI Usage

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