Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sbc = term.setBackgroundColor
- local stc = term.setTextColor
- local scp = term.setCursorPos
- local function uierr(source,err)
- error ("[NovaUI]:(" .. source .. ") - " .. err)
- end
- -- Load the template into a table or series of tables
- function exportTemplate(tplate)
- local uitypes = {
- ['button'] = {
- eargs={'text','x','y','backcolor','textcolor','width','height'},
- init = function(text,ux,uy,ubc,utc,uw,uh)
- local tbl = {
- x = ux,
- y = uy,
- bc = ubc,
- tc = utc,
- w = uw,
- h = uh,
- txt = text,
- clickfunc = function(self) end,
- drawfunc = function(self)
- return "old"
- end,
- wait = function(t)
- sleep(t)
- end,
- onclick = function(self,nfunc)
- self.clickfunc = nfunc
- end,
- isclick = function(self,x,y)
- return x >= self.x and y >= self.y and x <= (self.x+self.w)-1 and y <= (self.y+self.h)-1
- end,
- ondraw = function(self,nfunc)
- self.drawfunc = nfunc
- end,
- draw = function(self)
- local dx = self.x
- local edx = (self.x+self.w)-1
- local dy = self.y
- local edy = (self.y+self.h)-1
- local tdx = self.x+( (self.w/2) - #self.txt/2 )
- local tdy = (self.y + math.ceil(self.h/2))-1
- self:drawtrigger()
- paintutils.drawFilledBox(dx,dy,edx,edy,colors[self.bc])
- scp(tdx,tdy)
- stc(colors[self.tc])
- write(self.txt)
- end,
- trigger = function(self)
- self:clickfunc()
- end,
- drawtrigger = function(self)
- self:drawfunc()
- end,
- update = function(self,e)
- if(e[1] == "mouse_click")then
- if(e[2] == 1)then
- mx = e[3]
- my = e[4]
- if(self:isclick(mx,my))then
- -- Button pressed trigger click function --
- self:trigger()
- self:draw()
- end
- end
- end
- end,
- flash = function(self,tc,bc,t)
- local otc, obc = self.tc, self.bc
- self.tc = tc
- self.bc = bc
- self:draw()
- sleep(t)
- self.tc = otc
- self.bc = obc
- self:draw()
- end,
- getPos = function(self) return self.x,self.y end,
- setPos = function(self,nx,ny)
- self.x = nx
- self.y = ny
- end,
- getColor = function(self) return self.tc, self.bc end,
- setColors = function(self,ntc,nbc)
- self.tc = ntc
- self.bc = nbc
- end,
- getText = function(self) return self.txt end,
- setText = function(self,ntxt)
- self.txt = ntxt
- end,
- }
- return tbl
- end,
- },
- ['dropdown'] = {
- eargs={'text','x','y','backcolor','textcolor','mbackcolor','mtextcolor','shadow','opaquecolor','items'},
- init = function(text,ux,uy,ubc,utc,mbc,mtc,shd,oc,itms)
- local ritms = {}
- local mlen = #text
- local tsize = #itms
- for i = 1, #itms do
- if(#itms[i] > mlen)then mlen = #itms[i] end
- ritms[i] = {name=itms[i]}
- end
- local ddm = {
- t = text,
- x = ux,
- y = uy,
- w = mlen,
- bc = ubc,
- lsize = tsize,
- tc = utc,
- ibc = mbc,
- itc = mtc,
- list = ritms,
- scol = shd,
- cc = oc,
- pbc = term.getBackgroundColor(),
- inlist = false,
- onoc = function(self) end,
- onlistopenclose = function(self,func)
- self.onoc = func
- end,
- doOnOc = function(self)
- self:onoc()
- end,
- update = function(self,ev)
- if(ev[1] == "mouse_click" and ev[2] == 1)then
- local x,y = ev[3], ev[4]
- if(self.inlist)then
- if(x >= self.x and x <= self.x + self.w-1 and y > self.y and y <= self.y + #self.list)then
- local lind = y - self.y
- if(self.list[lind])then
- if(type(self.list[lind].onclick) == "function")then
- self.inlist = false
- self:draw()
- self.list[lind].onclick(self)
- end
- end
- end
- end
- if(x >= self.x and y == self.y and x <= self.x + self.w)then
- self:doOnOc()
- self.inlist = not self.inlist
- self:draw()
- end
- end
- end,
- isclick = function(self,x,y)
- if(self.inlist)then
- if(x >= self.x and x <= self.x + self.w-1 and y > self.y and y <= self.y + #self.list)then
- local lind = y - self.y
- if(self.list[lind])then
- return tonumber(lind)
- end
- end
- end
- if(x >= self.x and y == self.y and x <= self.x + self.w)then
- return tonumber(0)
- end
- return false
- end,
- flash = function(self,ind,bc,tc,t)
- local ry = self.y+1
- if(ind > 0)then
- stc(tc)
- sbc(bc)
- scp(self.x,ry+(ind-1))
- write(string.rep(" ",self.w))
- scp(self.x,ry+(ind-1))
- write(self.list[ind].name)
- sleep(t)
- stc(colors[self.itc])
- sbc(colors[self.ibc])
- scp(self.x,ry+(ind-1))
- write(string.rep(" ",self.w))
- scp(self.x,ry+(ind-1))
- write(self.list[ind].name)
- elseif(ind == 0)then
- scp(self.x,self.y)
- sbc(bc)
- write(string.rep(" ",self.w))
- scp(self.x,self.y)
- stc(tc)
- write(self.t)
- sleep(t)
- scp(self.x,self.y)
- sbc(colors[self.bc])
- write(string.rep(" ",self.w))
- scp(self.x,self.y)
- stc(colors[self.tc])
- write(self.t)
- end
- end,
- draw = function(self)
- scp(self.x,self.y)
- sbc(colors[self.bc])
- write(string.rep(" ",self.w))
- scp(self.x,self.y)
- stc(colors[self.tc])
- write(self.t)
- if(self.inlist)then
- local ry = self.y+1
- for i = 1, #self.list do
- paintutils.drawBox(self.x+1,ry+i,self.x + self.w,ry+i,colors[self.scol])
- stc(colors[self.itc])
- sbc(colors[self.ibc])
- scp(self.x,ry+(i-1))
- write(string.rep(" ",self.w))
- scp(self.x,ry+(i-1))
- write(self.list[i].name)
- sleep(0.1)
- end
- else
- paintutils.drawFilledBox(self.x,self.y+1,self.x + self.w+1,(self.y+2) + (#self.list),colors[self.cc])
- end
- end,
- drawitem = function(self,itm)
- local ry = self.y+1
- for i = 1, #self.list do
- if(self.list[i].name == itm)then
- stc(colors[self.itc])
- sbc(colors[self.ibc])
- scp(self.x,ry+(i-1))
- write(string.rep(" ",self.w))
- scp(self.x,ry+(i-1))
- write(self.list[i].name)
- sleep(0.1)
- end
- end
- end,
- onclick = function(self,opt,func)
- for i = 1, #self.list do
- if(self.list[i].name == opt)then
- self.list[i].onclick = func
- end
- end
- end,
- setitemcolors = function(self,ntc,nbc)
- self.itc = ntc
- self.ibc = nbc
- end,
- clearitems = function(self)
- self.list = {}
- end,
- setitems = function(self,nitems)
- self.list = nitems
- end,
- additem = function(self,nitem)
- local ex = false
- for i = 1, #self.list do
- if(self.list[i].name == nitem)then
- ex = true
- end
- end
- if(not ex)then
- self.list[i] = {name=nitem}
- end
- end,
- removeitem = function(self,item)
- for i = 1, #self.list do
- if(self.list[i].name == item)then
- table.remove(self.list,i)
- end
- end
- end,
- getitemcolors = function(self)
- return self.ibc, self.itc
- end,
- getitems = function(self)
- return self.list
- end,
- }
- return ddm
- end,
- }
- }
- local codetbl = {}
- -- End of GUI table --
- for i = 1, #tplate do
- if(uitypes[tplate[i].type] ~= nil)then
- if(tplate[i].name == nil)then
- uierr("exportTemplate","Object of type: " .. tplate[i].type ..
- " Doesnt have a name, please define one. ")
- end
- -- If type is found then loop through the
- -- expected arguments table and match that to the template
- local pargs = {}
- local margs = {}
- for x = 1, #uitypes[tplate[i].type].eargs do
- if(tplate[i][uitypes[tplate[i].type].eargs[x]] ~= nil)then
- pargs[x] = tplate[i][uitypes[tplate[i].type].eargs[x]]
- else
- -- If we are missing an argument add it to missing args table
- -- this table's information will error out in end of code block
- margs[#margs+1] = uitypes[tplate[i].type].eargs[x]
- end
- end
- if(#margs > 0)then
- uierr("exportTemplate","Arguments missing for object of type: " .. tplate[i].type ..
- ", Arguments Missing -> " .. unpack(margs))
- end
- codetbl[tplate[i].name] = uitypes[tplate[i].type].init(unpack(pargs))
- else
- uierr("exportTemplate","Object of type: " .. tplate[i].type ..
- " Is not a valid type.")
- end
- end
- return codetbl
- end
- -- Support for template loading, file and table
- function loadTemplate(tplate)
- if(type(tplate) == "string")then
- if(fs.exists(tplate))then
- local f = fs.open(tplate,"r")
- tplate = textutils.unserialize(f.readAll())
- f.close()
- elseif(type(tplate) ~= "table" and not fs.exists(tplate))then
- uierr("loadTemplate","Not a valid template. ")
- end
- end
- if(type(tplate) == "table")then
- return exportTemplate(tplate)
- else
- uierr("loadTemplate","Failed to unserialize template type: " .. type(tplate))
- end
- end
- -- TODO: Support for adding a template onto another
- -- TODO: Support for setting a template straight from another.
- -- TODO:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement