Advertisement
Imgoodisher

guiapi

May 27th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.48 KB | None | 0 0
  1. gui = {}
  2.  
  3. function gui.w(obj)
  4.     return obj.a - obj.x + 1
  5. end
  6. function gui.h(obj)
  7.     return obj.b - obj.y + 1
  8. end
  9.  
  10. local function twrite(str) -- From the Googol API
  11.     local tbl = {}
  12.     for char in string.gmatch(str, ".") do
  13.         table.insert(tbl, char)
  14.     end
  15.    
  16.     local skip = false
  17.     for i=1, #tbl do
  18.         if tbl[i] == "&" then
  19.             if string.match(tbl[i+1], "%x") then
  20.                 skip = true
  21.                 term.setTextColor(2^tonumber(tbl[i+1], 16))
  22.             else
  23.                 write("&")
  24.             end
  25.            
  26.         elseif tbl[i] == "#" then
  27.             if string.match(tbl[i+1], "%x") then
  28.                 skip = true
  29.                 term.setBackgroundColor(2^tonumber(tbl[i+1], 16))
  30.             else
  31.                 write("#")
  32.             end
  33.            
  34.         elseif not skip then
  35.             write(tbl[i])
  36.         else
  37.             skip = false
  38.         end
  39.     end
  40.     --term.setTextColor(colors.white)
  41. end
  42.  
  43. local function tlen(str)
  44.     local len = 0
  45.     local tbl = {}
  46.     for char in string.gmatch(str, ".") do
  47.         table.insert(tbl, char)
  48.     end
  49.    
  50.     local skip = false
  51.     for i=1, #tbl do
  52.         if tbl[i] == "&" then
  53.             if string.match(tbl[i+1], "%x") then
  54.                 skip = true
  55.             else
  56.                 len = len + 1
  57.             end
  58.            
  59.         elseif tbl[i] == "#" then
  60.             if string.match(tbl[i+1], "%x") then
  61.                 skip = true
  62.             else
  63.                 len = len + 1
  64.             end
  65.            
  66.         elseif not skip then
  67.             len = len + 1
  68.         else
  69.             skip = false
  70.         end
  71.     end
  72.     return len
  73. end
  74.  
  75. local function wordwrap(text, w)
  76.     local lines = {} -- stores all the lines
  77.     for match in string.gmatch(text, "[^\n]+") do -- seperate the block of text into seperate lines
  78.         local len = 0
  79.         local line = ""
  80.         for word in string.gmatch(match, "[^%s]+") do -- word wrap if the line is too long
  81.             word = word .. " "
  82.             if tlen(line..word) > w then
  83.                 table.insert(lines, line)
  84.                 line = ""
  85.             end
  86.             line = line .. word
  87.             while tlen(line) > w do
  88.                 table.insert(lines, line:sub(1, w))
  89.                 line = line:sub(w+1)
  90.             end
  91.         end
  92.         table.insert(lines, line)
  93.     end
  94.     local delete = {}
  95.     for i,v in pairs(lines) do
  96.         lines[i] = v:match("^%s*(.-)%s*$")
  97.         if lines[i] == "" then
  98.             table.insert(delete, i)
  99.         end
  100.     end
  101.     for i,v in pairs(delete) do lines[v] = nil end
  102.     return lines
  103. end
  104.  
  105. function gui.loadRaw(data)
  106.     local guiobj = {}
  107.  
  108.     guiobj.data = data
  109.     guiobj.menu = "default"
  110.  
  111.     function guiobj:getObject(i)
  112.         if type(i) == "number" then
  113.             return self.data[self.menu][i], i
  114.         elseif type(i) == "string" then
  115.             for i2,v2 in pairs(self.data[self.menu]) do
  116.                 if v.n == name then
  117.                     return v2, i2
  118.                 end
  119.             end
  120.         end
  121.     end
  122.  
  123.     function guiobj:setMenu(m)
  124.         self.menu = m
  125.     end
  126.  
  127.     function guiobj:connect(i, func)
  128.         local obj = guiobj:getObject(i)
  129.         if obj then
  130.             obj.onClick = func
  131.         else
  132.             error("No object index "..i, 2)
  133.         end
  134.     end
  135.  
  136.     function guiobj:draw()
  137.         term.setBackgroundColor(colors.black)
  138.         term.clear()
  139.         for i,v in pairs(self.data[self.menu]) do
  140.             if not v.nfp then
  141.                 term.setBackgroundColor(v.bc)
  142.                 term.setTextColor(v.tc)
  143.                 for y = v.y, v.b do
  144.                     term.setCursorPos(v.x, y)
  145.                     term.write(string.rep(" ", gui.w(v)))
  146.                 end
  147.                 if v.t then
  148.                     v.t = v.t:match("^%s*(.+)%s*$") or v.t
  149.                     local lines = wordwrap(v.t, gui.w(v) - 2)
  150.                     for i2,line in pairs(lines) do
  151.                         term.setCursorPos(v.x + math.floor(gui.w(v)/2-tlen(line)/2), v.y + gui.h(v)/2 - #lines/2 + i2 - 1) -- ((h(v)%2==1 and h(v)/2) or ((h(v)/2)-1))
  152.                         twrite(line)
  153.                     end
  154.                 end
  155.             else
  156.                 paintutils.drawImage(v.d, v.x, v.y)
  157.             end
  158.         end
  159.     end
  160.  
  161.     function guiobj:process_event(event)
  162.         if event[1] == "mouse_click" then
  163.             for i = #self.data[self.menu], 1, -1 do
  164.                 local obj = self:getObject(i)
  165.                 if event[3] >= obj.x and event[3] <= obj.a and event[4] >= obj.y and event[4] <= obj.b then
  166.                     if obj.onClick then obj:onClick() end
  167.                     os.queueEvent("gui_click", event[2], i, obj.n, event[3] - obj.x + 1, event[4] - obj.y + 1)
  168.                     break
  169.                 end
  170.             end
  171.         end
  172.     end
  173.  
  174.     function guiobj:update(event)
  175.         self:process_event(event)
  176.         self:draw()
  177.     end
  178.  
  179.     function guiobj:pairs()
  180.         return pairs(self.data[self.menu])
  181.     end
  182.  
  183.     return guiobj
  184. end
  185.  
  186. function gui.load(path)
  187.     local file = io.open(path, "r")
  188.     local data = textutils.unserialize(file:read("*a"))
  189.     file:close()
  190.  
  191.     return gui.loadRaw(data)
  192. end
  193.  
  194. -- Misc. Useful Funcitons --
  195.  
  196. function gui.read( _sReplaceChar, _tHistory, wid, starttext)
  197.     term.setCursorBlink( true )
  198.  
  199.     local sLine = starttext or ""
  200.     local nHistoryPos = nil
  201.     local nPos = (starttext and starttext:len()) or 0
  202.     if _sReplaceChar then
  203.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  204.     end
  205.    
  206.     local w, h = term.getSize()
  207.     local sx, sy = term.getCursorPos()
  208.  
  209.     w = sx + wid - 1
  210.    
  211.     local function redraw( _sCustomReplaceChar )
  212.         local nScroll = 0
  213.         if sx + nPos >= w then
  214.             nScroll = (sx + nPos) - w
  215.         end
  216.            
  217.         term.setCursorPos( sx, sy )
  218.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  219.         if sReplace then
  220.             term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  221.         else
  222.             term.write( string.sub( sLine, nScroll + 1 ) )
  223.         end
  224.         term.setCursorPos( sx + nPos - nScroll, sy )
  225.     end
  226.    
  227.     redraw()
  228.     while true do
  229.         local sEvent, param = os.pullEvent()
  230.         if sEvent == "char" then
  231.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  232.             nPos = nPos + 1
  233.             redraw()
  234.            
  235.         elseif sEvent == "key" then
  236.             if param == keys.enter then
  237.                 -- Enter
  238.                 break
  239.                
  240.             elseif param == keys.left then
  241.                 -- Left
  242.                 if nPos > 0 then
  243.                     nPos = nPos - 1
  244.                     redraw()
  245.                 end
  246.                
  247.             elseif param == keys.right then
  248.                 -- Right               
  249.                 if nPos < string.len(sLine) then
  250.                     nPos = nPos + 1
  251.                     redraw()
  252.                 end
  253.            
  254.             elseif param == keys.up or param == keys.down then
  255.                 -- Up or down
  256.                 if _tHistory then
  257.                     redraw(" ");
  258.                     if param == keys.up then
  259.                         -- Up
  260.                         if nHistoryPos == nil then
  261.                             if #_tHistory > 0 then
  262.                                 nHistoryPos = #_tHistory
  263.                             end
  264.                         elseif nHistoryPos > 1 then
  265.                             nHistoryPos = nHistoryPos - 1
  266.                         end
  267.                     else
  268.                         -- Down
  269.                         if nHistoryPos == #_tHistory then
  270.                             nHistoryPos = nil
  271.                         elseif nHistoryPos ~= nil then
  272.                             nHistoryPos = nHistoryPos + 1
  273.                         end                    
  274.                     end
  275.                    
  276.                     if nHistoryPos then
  277.                         sLine = _tHistory[nHistoryPos]
  278.                         nPos = string.len( sLine )
  279.                     else
  280.                         sLine = ""
  281.                         nPos = 0
  282.                     end
  283.                     redraw()
  284.                 end
  285.             elseif param == keys.backspace then
  286.                 -- Backspace
  287.                 if nPos > 0 then
  288.                     redraw(" ");
  289.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  290.                     nPos = nPos - 1                
  291.                     redraw()
  292.                 end
  293.             elseif param == keys.home then
  294.                 -- Home
  295.                 nPos = 0
  296.                 redraw()       
  297.             elseif param == keys.delete then
  298.                 if nPos < string.len(sLine) then
  299.                     redraw(" ");
  300.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  301.                     redraw()
  302.                 end
  303.             elseif param == keys["end"] then
  304.                 -- End
  305.                 nPos = string.len(sLine)
  306.                 redraw()
  307.             elseif param == keys.tab then
  308.                 term.setCursorBlink(false)
  309.                 return sLine, true
  310.             end
  311.         end
  312.     end
  313.    
  314.     term.setCursorBlink( false )
  315.     term.setCursorPos( w + 1, sy )
  316.     print()
  317.    
  318.     return sLine
  319. end
  320.  
  321. function gui.textbox(prompt, starttext)
  322.     term.setBackgroundColor(colors.black)
  323.     term.clear()
  324.     drawGui()
  325.  
  326.     local wid = ((prompt:len() <= 15 and 20) or prompt:len() + 5)
  327.     local startx = tw/2 - wid/2
  328.     local starty = ((th%2==1 and th/2) or ((th/2)-1)) - 2 -- 4/2
  329.  
  330.     term.setBackgroundColor(colors.lightGray)
  331.     for i = 0, 3 do
  332.         term.setCursorPos(startx, starty + i)
  333.         print(string.rep(" ", wid))
  334.     end
  335.  
  336.     term.setCursorPos(startx + 1, starty + 1)
  337.     term.setTextColor(colors.black)
  338.     print(prompt)
  339.  
  340.     term.setBackgroundColor(colors.gray)
  341.     term.setCursorPos(startx + 1, starty + 2)
  342.     print(string.rep(" ", wid-2))
  343.  
  344.     term.setCursorPos(startx + 1, starty + 2)
  345.     term.setTextColor(colors.white)
  346.     return gui.read(nil, nil, wid-2, starttext)
  347. end
  348.  
  349. function gui.text(...)
  350.     term.setBackgroundColor(colors.black)
  351.     term.clear()
  352.     drawGui()
  353.  
  354.     local wid = 0
  355.     local strs = {...}
  356.     for i,v in pairs(strs) do
  357.         local _wid = ((v:len() <= 15 and 20) or v:len() + 5)
  358.         wid = (_wid > wid and _wid) or wid
  359.     end
  360.     local startx = tw/2 - wid/2
  361.     local starty = ((th%2==1 and th/2) or ((th/2)-1)) - 1 -- ~ 3/2
  362.  
  363.     term.setBackgroundColor(colors.lightGray)
  364.     for i = 0, #strs + 1 do
  365.         term.setCursorPos(startx, starty + i)
  366.         print(string.rep(" ", wid))
  367.     end
  368.  
  369.     for i,v in pairs(strs) do
  370.         term.setCursorPos(startx + 1, starty + i)
  371.         term.setTextColor(colors.gray)
  372.         print(v)
  373.     end
  374.  
  375.     os.pullEvent()
  376.     return true
  377. end
  378.  
  379. return gui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement