Advertisement
Redxone

Nova UI

Jun 17th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.56 KB | None | 0 0
  1. local sbc = term.setBackgroundColor
  2. local stc = term.setTextColor
  3. local scp = term.setCursorPos
  4.  
  5. local function uierr(source,err)
  6.     error ("[NovaUI]:(" .. source .. ") - " .. err)
  7. end
  8.  
  9. -- Load the template into a table or series of tables
  10. function exportTemplate(tplate)
  11.     local uitypes = {
  12.         ['button'] = {
  13.             eargs={'text','x','y','backcolor','textcolor','width','height'},
  14.             init = function(text,ux,uy,ubc,utc,uw,uh)
  15.                  local tbl = {
  16.                     x = ux,
  17.                     y = uy,
  18.                     bc = ubc,
  19.                     tc = utc,
  20.                     w = uw,
  21.                     h = uh,
  22.                     txt = text,
  23.                     clickfunc = function(self) end,
  24.                     drawfunc = function(self)
  25.                         return "old"
  26.                     end,
  27.  
  28.                     wait = function(t)
  29.                         sleep(t)
  30.                     end,
  31.  
  32.                     onclick = function(self,nfunc)
  33.                         self.clickfunc = nfunc
  34.                     end,
  35.                    
  36.                     isclick = function(self,x,y)
  37.                         return x >= self.x and y >= self.y and x <= (self.x+self.w)-1 and y <= (self.y+self.h)-1
  38.                     end,
  39.  
  40.                     ondraw = function(self,nfunc)
  41.                         self.drawfunc = nfunc
  42.                     end,
  43.  
  44.                     draw = function(self)
  45.                         local dx = self.x
  46.                         local edx = (self.x+self.w)-1
  47.                         local dy = self.y
  48.                         local edy = (self.y+self.h)-1
  49.                         local tdx = self.x+( (self.w/2) - #self.txt/2 )
  50.                         local tdy = (self.y + math.ceil(self.h/2))-1
  51.                         self:drawtrigger()
  52.                         paintutils.drawFilledBox(dx,dy,edx,edy,colors[self.bc])
  53.                         scp(tdx,tdy)
  54.                         stc(colors[self.tc])
  55.                         write(self.txt)
  56.                     end,
  57.  
  58.                     trigger = function(self)
  59.                         self:clickfunc()
  60.                     end,
  61.  
  62.                     drawtrigger = function(self)
  63.                         self:drawfunc()
  64.                     end,
  65.  
  66.                     update = function(self,e)
  67.                         if(e[1] == "mouse_click")then
  68.                             if(e[2] == 1)then
  69.                                 mx = e[3]
  70.                                 my = e[4]
  71.                                 if(self:isclick(mx,my))then
  72.                                     -- Button pressed trigger click function --
  73.                                     self:trigger()
  74.                                     self:draw()
  75.                                 end
  76.                             end
  77.                         end
  78.                     end,
  79.  
  80.                     flash = function(self,tc,bc,t)
  81.                         local otc, obc = self.tc, self.bc
  82.                         self.tc = tc
  83.                         self.bc = bc
  84.                         self:draw()
  85.                         sleep(t)
  86.                         self.tc = otc
  87.                         self.bc = obc
  88.                         self:draw()
  89.                     end,
  90.  
  91.                     getPos = function(self) return self.x,self.y end,
  92.                     setPos = function(self,nx,ny)
  93.                         self.x = nx
  94.                         self.y = ny
  95.                     end,
  96.                     getColor = function(self) return self.tc, self.bc end,
  97.                     setColors = function(self,ntc,nbc)
  98.                         self.tc = ntc
  99.                         self.bc = nbc
  100.                     end,
  101.                     getText = function(self) return self.txt end,
  102.                     setText = function(self,ntxt)
  103.                         self.txt = ntxt
  104.                     end,
  105.  
  106.                  }
  107.  
  108.                  return tbl
  109.             end,
  110.         },
  111.         ['dropdown'] = {
  112.             eargs={'text','x','y','backcolor','textcolor','mbackcolor','mtextcolor','shadow','opaquecolor','items'},
  113.             init = function(text,ux,uy,ubc,utc,mbc,mtc,shd,oc,itms)
  114.                 local ritms = {}
  115.                 local mlen = #text
  116.                 local tsize = #itms
  117.                 for i = 1, #itms do
  118.                     if(#itms[i] > mlen)then mlen = #itms[i] end
  119.                     ritms[i] = {name=itms[i]}
  120.                 end
  121.  
  122.                 local ddm = {
  123.                     t = text,
  124.                     x = ux,
  125.                     y = uy,
  126.                     w = mlen,
  127.                     bc = ubc,
  128.                     lsize = tsize,
  129.                     tc = utc,
  130.                     ibc = mbc,
  131.                     itc = mtc,
  132.                     list = ritms,
  133.                     scol = shd,
  134.                     cc = oc,
  135.                     pbc = term.getBackgroundColor(),
  136.                     inlist = false,
  137.                     onoc = function(self) end,
  138.  
  139.                     onlistopenclose = function(self,func)
  140.                         self.onoc = func
  141.                     end,
  142.  
  143.                     doOnOc = function(self)
  144.                         self:onoc()
  145.                     end,
  146.  
  147.                     update = function(self,ev)
  148.                         if(ev[1] == "mouse_click" and ev[2] == 1)then
  149.                             local x,y = ev[3], ev[4]
  150.                             if(self.inlist)then
  151.                                 if(x >= self.x and x <= self.x + self.w-1 and y > self.y and y <= self.y + #self.list)then
  152.                                     local lind = y - self.y    
  153.                                     if(self.list[lind])then
  154.                                         if(type(self.list[lind].onclick) == "function")then
  155.                                             self.inlist = false
  156.                                             self:draw()
  157.                                             self.list[lind].onclick(self)
  158.                                         end
  159.                                     end
  160.                                 end
  161.                             end
  162.                                 if(x >= self.x and y == self.y and x <= self.x + self.w)then
  163.                                     self:doOnOc()
  164.                                     self.inlist = not self.inlist
  165.                                     self:draw()
  166.                                 end
  167.                         end
  168.                     end,
  169.  
  170.                     isclick = function(self,x,y)
  171.                         if(self.inlist)then
  172.                             if(x >= self.x and x <= self.x + self.w-1 and y > self.y and y <= self.y + #self.list)then
  173.                                 local lind = y - self.y    
  174.                                 if(self.list[lind])then
  175.                                     return tonumber(lind)
  176.                                 end
  177.                             end
  178.                         end
  179.                         if(x >= self.x and y == self.y and x <= self.x + self.w)then
  180.                             return tonumber(0)
  181.                         end
  182.                         return false
  183.                     end,
  184.  
  185.                     flash = function(self,ind,bc,tc,t)
  186.                         local ry = self.y+1
  187.                         if(ind > 0)then
  188.                             stc(tc)
  189.                             sbc(bc)
  190.                             scp(self.x,ry+(ind-1))
  191.                             write(string.rep(" ",self.w))
  192.                             scp(self.x,ry+(ind-1))
  193.                             write(self.list[ind].name)
  194.                             sleep(t)
  195.                             stc(colors[self.itc])
  196.                             sbc(colors[self.ibc])
  197.                             scp(self.x,ry+(ind-1))
  198.                             write(string.rep(" ",self.w))
  199.                             scp(self.x,ry+(ind-1))
  200.                             write(self.list[ind].name)
  201.                         elseif(ind == 0)then
  202.                             scp(self.x,self.y)
  203.                             sbc(bc)
  204.                             write(string.rep(" ",self.w))
  205.                             scp(self.x,self.y)
  206.                             stc(tc)
  207.                             write(self.t)
  208.                             sleep(t)
  209.                             scp(self.x,self.y)
  210.                             sbc(colors[self.bc])
  211.                             write(string.rep(" ",self.w))
  212.                             scp(self.x,self.y)
  213.                             stc(colors[self.tc])
  214.                             write(self.t)
  215.                         end
  216.                     end,
  217.  
  218.                     draw = function(self)
  219.                         scp(self.x,self.y)
  220.                         sbc(colors[self.bc])
  221.                         write(string.rep(" ",self.w))
  222.                         scp(self.x,self.y)
  223.                         stc(colors[self.tc])
  224.                         write(self.t)
  225.                         if(self.inlist)then
  226.                             local ry = self.y+1
  227.                             for i = 1, #self.list do
  228.                                 paintutils.drawBox(self.x+1,ry+i,self.x + self.w,ry+i,colors[self.scol])
  229.                                 stc(colors[self.itc])
  230.                                 sbc(colors[self.ibc])
  231.                                 scp(self.x,ry+(i-1))
  232.                                 write(string.rep(" ",self.w))
  233.                                 scp(self.x,ry+(i-1))
  234.                                 write(self.list[i].name)
  235.                                 sleep(0.1)
  236.                             end
  237.                         else
  238.                             paintutils.drawFilledBox(self.x,self.y+1,self.x + self.w+1,(self.y+2) + (#self.list),colors[self.cc])
  239.                         end
  240.                     end,
  241.  
  242.                     drawitem = function(self,itm)
  243.                         local ry = self.y+1
  244.                         for i = 1, #self.list do
  245.                             if(self.list[i].name == itm)then
  246.                                 stc(colors[self.itc])
  247.                                 sbc(colors[self.ibc])
  248.                                 scp(self.x,ry+(i-1))
  249.                                 write(string.rep(" ",self.w))
  250.                                 scp(self.x,ry+(i-1))
  251.                                 write(self.list[i].name)
  252.                                 sleep(0.1)
  253.                             end
  254.                         end
  255.                     end,
  256.  
  257.                     onclick = function(self,opt,func)
  258.                         for i = 1, #self.list do
  259.                             if(self.list[i].name == opt)then
  260.                                 self.list[i].onclick = func
  261.                             end
  262.                         end
  263.                     end,
  264.  
  265.                     setitemcolors = function(self,ntc,nbc)
  266.                         self.itc = ntc
  267.                         self.ibc = nbc
  268.                     end,
  269.  
  270.                     clearitems = function(self)
  271.                         self.list = {}
  272.                     end,
  273.  
  274.                     setitems = function(self,nitems)
  275.                         self.list = nitems
  276.                     end,
  277.  
  278.                     additem = function(self,nitem)
  279.                         local ex = false
  280.                         for i = 1, #self.list do
  281.                             if(self.list[i].name == nitem)then
  282.                                 ex = true
  283.                             end
  284.                         end
  285.                         if(not ex)then
  286.                             self.list[i] = {name=nitem}
  287.                         end
  288.                     end,
  289.  
  290.                     removeitem = function(self,item)
  291.                         for i = 1, #self.list do
  292.                             if(self.list[i].name == item)then
  293.                                 table.remove(self.list,i)
  294.                             end
  295.                         end
  296.                     end,
  297.  
  298.                     getitemcolors = function(self)
  299.                         return self.ibc, self.itc
  300.                     end,
  301.  
  302.                     getitems = function(self)
  303.                         return self.list
  304.                     end,
  305.  
  306.                 }
  307.  
  308.                 return ddm
  309.             end,
  310.         }
  311.     }
  312.  
  313.     local codetbl = {}
  314.     -- End of GUI table --
  315.     for i = 1, #tplate do
  316.         if(uitypes[tplate[i].type] ~= nil)then
  317.             if(tplate[i].name == nil)then
  318.                 uierr("exportTemplate","Object of type: " .. tplate[i].type ..
  319.                     " Doesnt have a name, please define one. ")
  320.             end
  321.             -- If type is found then loop through the
  322.             -- expected arguments table and match that to the template
  323.              local pargs = {}
  324.              local margs = {}
  325.             for x = 1, #uitypes[tplate[i].type].eargs do
  326.                 if(tplate[i][uitypes[tplate[i].type].eargs[x]] ~= nil)then
  327.                     pargs[x] = tplate[i][uitypes[tplate[i].type].eargs[x]]
  328.                 else
  329.                     -- If we are missing an argument add it to missing args table
  330.                     -- this table's information will error out in end of code block
  331.                     margs[#margs+1] = uitypes[tplate[i].type].eargs[x]
  332.                 end
  333.             end
  334.  
  335.             if(#margs > 0)then
  336.                 uierr("exportTemplate","Arguments missing for object of type: " .. tplate[i].type ..
  337.                     ", Arguments Missing -> " .. unpack(margs))
  338.             end
  339.  
  340.             codetbl[tplate[i].name] = uitypes[tplate[i].type].init(unpack(pargs))
  341.         else
  342.             uierr("exportTemplate","Object of type: " .. tplate[i].type ..
  343.                     " Is not a valid type.")
  344.         end
  345.     end
  346.  
  347.     return codetbl
  348.  
  349. end
  350.  
  351. -- Support for template loading, file and table
  352. function loadTemplate(tplate)
  353.     if(type(tplate) == "string")then
  354.         if(fs.exists(tplate))then
  355.             local f = fs.open(tplate,"r")
  356.             tplate = textutils.unserialize(f.readAll())
  357.             f.close()
  358.         elseif(type(tplate) ~= "table" and not fs.exists(tplate))then
  359.             uierr("loadTemplate","Not a valid template. ")
  360.         end
  361.     end
  362.  
  363.     if(type(tplate) == "table")then
  364.         return exportTemplate(tplate)
  365.     else
  366.         uierr("loadTemplate","Failed to unserialize template type: " .. type(tplate))
  367.     end
  368. end
  369.  
  370. -- TODO: Support for adding a template onto another
  371.  
  372. -- TODO: Support for setting a template straight from another.
  373.  
  374. -- TODO:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement