Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gui = {}
- function gui.w(obj)
- return obj.a - obj.x + 1
- end
- function gui.h(obj)
- return obj.b - obj.y + 1
- end
- local function twrite(str) -- From the Googol API
- local tbl = {}
- for char in string.gmatch(str, ".") do
- table.insert(tbl, char)
- end
- local skip = false
- for i=1, #tbl do
- if tbl[i] == "&" then
- if string.match(tbl[i+1], "%x") then
- skip = true
- term.setTextColor(2^tonumber(tbl[i+1], 16))
- else
- write("&")
- end
- elseif tbl[i] == "#" then
- if string.match(tbl[i+1], "%x") then
- skip = true
- term.setBackgroundColor(2^tonumber(tbl[i+1], 16))
- else
- write("#")
- end
- elseif not skip then
- write(tbl[i])
- else
- skip = false
- end
- end
- --term.setTextColor(colors.white)
- end
- local function tlen(str)
- local len = 0
- local tbl = {}
- for char in string.gmatch(str, ".") do
- table.insert(tbl, char)
- end
- local skip = false
- for i=1, #tbl do
- if tbl[i] == "&" then
- if string.match(tbl[i+1], "%x") then
- skip = true
- else
- len = len + 1
- end
- elseif tbl[i] == "#" then
- if string.match(tbl[i+1], "%x") then
- skip = true
- else
- len = len + 1
- end
- elseif not skip then
- len = len + 1
- else
- skip = false
- end
- end
- return len
- end
- local function wordwrap(text, w)
- local lines = {} -- stores all the lines
- for match in string.gmatch(text, "[^\n]+") do -- seperate the block of text into seperate lines
- local len = 0
- local line = ""
- for word in string.gmatch(match, "[^%s]+") do -- word wrap if the line is too long
- word = word .. " "
- if tlen(line..word) > w then
- table.insert(lines, line)
- line = ""
- end
- line = line .. word
- while tlen(line) > w do
- table.insert(lines, line:sub(1, w))
- line = line:sub(w+1)
- end
- end
- table.insert(lines, line)
- end
- local delete = {}
- for i,v in pairs(lines) do
- lines[i] = v:match("^%s*(.-)%s*$")
- if lines[i] == "" then
- table.insert(delete, i)
- end
- end
- for i,v in pairs(delete) do lines[v] = nil end
- return lines
- end
- function gui.loadRaw(data)
- local guiobj = {}
- guiobj.data = data
- guiobj.menu = "default"
- function guiobj:getObject(i)
- if type(i) == "number" then
- return self.data[self.menu][i], i
- elseif type(i) == "string" then
- for i2,v2 in pairs(self.data[self.menu]) do
- if v.n == name then
- return v2, i2
- end
- end
- end
- end
- function guiobj:setMenu(m)
- self.menu = m
- end
- function guiobj:connect(i, func)
- local obj = guiobj:getObject(i)
- if obj then
- obj.onClick = func
- else
- error("No object index "..i, 2)
- end
- end
- function guiobj:draw()
- term.setBackgroundColor(colors.black)
- term.clear()
- for i,v in pairs(self.data[self.menu]) do
- if not v.nfp then
- term.setBackgroundColor(v.bc)
- term.setTextColor(v.tc)
- for y = v.y, v.b do
- term.setCursorPos(v.x, y)
- term.write(string.rep(" ", gui.w(v)))
- end
- if v.t then
- v.t = v.t:match("^%s*(.+)%s*$") or v.t
- local lines = wordwrap(v.t, gui.w(v) - 2)
- for i2,line in pairs(lines) do
- 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))
- twrite(line)
- end
- end
- else
- paintutils.drawImage(v.d, v.x, v.y)
- end
- end
- end
- function guiobj:process_event(event)
- if event[1] == "mouse_click" then
- for i = #self.data[self.menu], 1, -1 do
- local obj = self:getObject(i)
- if event[3] >= obj.x and event[3] <= obj.a and event[4] >= obj.y and event[4] <= obj.b then
- if obj.onClick then obj:onClick() end
- os.queueEvent("gui_click", event[2], i, obj.n, event[3] - obj.x + 1, event[4] - obj.y + 1)
- break
- end
- end
- end
- end
- function guiobj:update(event)
- self:process_event(event)
- self:draw()
- end
- function guiobj:pairs()
- return pairs(self.data[self.menu])
- end
- return guiobj
- end
- function gui.load(path)
- local file = io.open(path, "r")
- local data = textutils.unserialize(file:read("*a"))
- file:close()
- return gui.loadRaw(data)
- end
- -- Misc. Useful Funcitons --
- function gui.read( _sReplaceChar, _tHistory, wid, starttext)
- term.setCursorBlink( true )
- local sLine = starttext or ""
- local nHistoryPos = nil
- local nPos = (starttext and starttext:len()) or 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- w = sx + wid - 1
- local function redraw( _sCustomReplaceChar )
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- local sReplace = _sCustomReplaceChar or _sReplaceChar
- if sReplace then
- term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- redraw()
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- redraw()
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- break
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if _tHistory then
- redraw(" ");
- if param == keys.up then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.home then
- -- Home
- nPos = 0
- redraw()
- elseif param == keys.delete then
- if nPos < string.len(sLine) then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
- redraw()
- end
- elseif param == keys["end"] then
- -- End
- nPos = string.len(sLine)
- redraw()
- elseif param == keys.tab then
- term.setCursorBlink(false)
- return sLine, true
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
- function gui.textbox(prompt, starttext)
- term.setBackgroundColor(colors.black)
- term.clear()
- drawGui()
- local wid = ((prompt:len() <= 15 and 20) or prompt:len() + 5)
- local startx = tw/2 - wid/2
- local starty = ((th%2==1 and th/2) or ((th/2)-1)) - 2 -- 4/2
- term.setBackgroundColor(colors.lightGray)
- for i = 0, 3 do
- term.setCursorPos(startx, starty + i)
- print(string.rep(" ", wid))
- end
- term.setCursorPos(startx + 1, starty + 1)
- term.setTextColor(colors.black)
- print(prompt)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(startx + 1, starty + 2)
- print(string.rep(" ", wid-2))
- term.setCursorPos(startx + 1, starty + 2)
- term.setTextColor(colors.white)
- return gui.read(nil, nil, wid-2, starttext)
- end
- function gui.text(...)
- term.setBackgroundColor(colors.black)
- term.clear()
- drawGui()
- local wid = 0
- local strs = {...}
- for i,v in pairs(strs) do
- local _wid = ((v:len() <= 15 and 20) or v:len() + 5)
- wid = (_wid > wid and _wid) or wid
- end
- local startx = tw/2 - wid/2
- local starty = ((th%2==1 and th/2) or ((th/2)-1)) - 1 -- ~ 3/2
- term.setBackgroundColor(colors.lightGray)
- for i = 0, #strs + 1 do
- term.setCursorPos(startx, starty + i)
- print(string.rep(" ", wid))
- end
- for i,v in pairs(strs) do
- term.setCursorPos(startx + 1, starty + i)
- term.setTextColor(colors.gray)
- print(v)
- end
- os.pullEvent()
- return true
- end
- return gui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement