mad1231999

GUI Util 1.0

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