Advertisement
Redxone

CC - Ultimite GUI API

Feb 17th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.76 KB | None | 0 0
  1. --]] Language assigning [[--
  2.  
  3. pos = term.setCursorPos
  4. bcolor = term.setBackgroundColor
  5. tcolor = term.setTextColor
  6. blink = term.setCursorBlink
  7. buffer = term.current().setVisible
  8. getpos = term.getCursorPos
  9. getbcolor = term.getBackgroundColor
  10. gettcolor = term.getTextColor
  11. printslow = textutils.slowPrint
  12. clr = term.clear
  13. clrln = term.clearLine
  14. clrlny = function(y)
  15.   local x,_ = getpos()
  16.   pos(x,y)
  17.   clrln()
  18. end
  19. clrlnycolor = function(col,y) bcolor(col) clrlny(y) end
  20. box = paintutils.drawFilledBox
  21. hbox = paintutils.drawBox
  22. drawimage = function(img,x,y) paintutils.drawImage(paintutils.loadImage(img),x,y) end
  23.  
  24.  
  25. -- ]] Write functions [[--
  26. function writexy(text,x,y)
  27.   term.setCursorPos(x,y)
  28.   term.write(text)
  29. end
  30.  
  31. function writec(text)
  32.   local w, h = term.getSize()
  33.   term.setCursorPos(w/2 - #text/2, (h/2)- 1)
  34.   term.write(text)
  35. end
  36.  
  37. function writecy(text,y)
  38.     local w,_ = term.getSize()
  39.     term.setCursorPos(w/2 - #text/2, y)
  40.     term.write(text)
  41. end
  42.  
  43.  
  44.  
  45. -- ]] Print functions [[--
  46. function printc(text)
  47.   local w,h = term.getSize()
  48.   term.setCursorPos( (w/2) - #text/2,(h/2) - 1)
  49.   print(text)
  50. end
  51.  
  52. function printcy(text,y)
  53.   local w,_ = term.getSize()
  54.   term.setCursorPos(w/2 - #text/2, y)
  55.   print(text)
  56. end
  57.  
  58. function println(text)
  59.    local xx, yy = term.getCursorPos()
  60.    term.setCursorPos(xx,yy+1)
  61.    print(text)
  62. end
  63.  
  64. function printxy(text,x,y)
  65.   term.setCursorPos(x,y)
  66.   print(text)
  67. end
  68.  
  69. --]] Transitions [[--
  70. function fadeIn(time)
  71.    
  72.    local ctbl = {
  73.      'black',
  74.      'gray',
  75.      'lightGray',
  76.      'white',
  77.    }
  78.  
  79.    for i = 1, #ctbl do
  80.       term.setBackgroundColor(colors[ ctbl[i] ])
  81.       term.clear()
  82.       sleep(time)  
  83.    end
  84. end
  85.  
  86. function fadeOut(time)
  87.    
  88.    local ctbl = {
  89.      'white',
  90.      'lightGray',
  91.      'gray',
  92.      'black',
  93.    }
  94.    
  95.    for i = 1, #ctbl do
  96.       term.setBackgroundColor(colors[ ctbl[i] ])
  97.       term.clear()
  98.       sleep(time)  
  99.    end
  100. end
  101.  
  102.  
  103. function fadecolors(time,ctbl)
  104.   for i = 1, #ctbl do
  105.     term.setBackgroundColor(colors[ctbl[i]])
  106.     term.clear()
  107.     sleep(time)
  108.   end
  109. end
  110.  
  111. function transitionIn(pixeltime, linetime,smoothness)
  112.     local w, h = term.getSize()
  113.     local left = true
  114.     for hh = 1, h/2+1 do
  115.         for ww = 1, w do
  116.             bcolor(colors.white)
  117.             if(left)then writexy(string.rep(" ",smoothness),w-(ww-1),hh) else writexy(" ",ww,hh) end
  118.             bcolor(colors.white)
  119.             if(left)then writexy(string.rep(" ",smoothness),ww,h-(hh-1) ) else writexy(" ",w-(ww-1),h-(hh-1)) end
  120.             if(pixeltime > 0) then sleep(pixeltime) end
  121.         end
  122.         if(left)then left = false else left = true end
  123.         if(linetime > 0)then sleep(linetime) end
  124.     end
  125.  
  126. end
  127.  
  128. function transitionOut(pixeltime, linetime,smoothness)
  129.     local w, h = term.getSize()
  130.     local left = true
  131.     for hh = 1, h/2+1 do
  132.         for ww = 1, w do
  133.             bcolor(colors.black)
  134.             if(left)then writexy(string.rep(" ",smoothness),w-(ww-1),hh) else writexy(" ",ww,hh) end
  135.             bcolor(colors.black)
  136.             if(left)then writexy(string.rep(" ",smoothness),ww,h-(hh-1) ) else writexy(" ",w-(ww-1),h-(hh-1)) end
  137.             if(pixeltime > 0) then sleep(pixeltime) end
  138.         end
  139.         if(left)then left = false else left = true end
  140.         if(linetime > 0)then sleep(linetime) end
  141.     end
  142.  
  143. end
  144.  
  145. --]] Button API [[ --
  146.  
  147. function newButton(name,x,y,width,height,namecolor,color,activecolor)
  148.     local button = {
  149.         name = name,
  150.         x = x,
  151.         y = y,
  152.         w = width-1,
  153.         h = height-1,
  154.         tcol = namecolor,
  155.         col = color,
  156.         acol = activecolor,
  157.         active = false,
  158.         rfun = function() end,
  159.         fun = function() end,
  160.  
  161.         draw = function(self)
  162.             local dbcol
  163.             if(self.active)then dbcol = self.acol
  164.             else dbcol = self.col end
  165.             box(self.x,self.y,self.x+self.w,self.y+self.h,dbcol)
  166.             -- Center text on button --
  167.             tcolor(self.tcol)
  168.             writexy(self.name,
  169.                 ( (self.x+self.w/2) - (#self.name/2) ),
  170.                 ( (self.y+self.h) - (self.h/2) )
  171.             )
  172.         end,
  173.  
  174.         update = function(self,ev)
  175.             if(ev[1] == "mouse_click")then
  176.                 -- Display active for a bit then switch off
  177.                 if(ev[3] >= self.x and ev[3] <= self.x + self.w and ev[4] >= self.y and ev[4] <= self.y + self.h)then
  178.                     self:setActive(true)
  179.                     sleep(0.2)
  180.                     self:setActive(false)
  181.  
  182.                     if(ev[2] == 1)then self.fun() end
  183.                     if(ev[2] == 2)then self.rfun() end
  184.  
  185.                 end
  186.             end
  187.         end,
  188.  
  189.         onPress = function(self,func)
  190.             self.fun = func
  191.         end,
  192.  
  193.         setName = function(self,nname)
  194.             self.name = nname
  195.         end,
  196.  
  197.         getName = function(self,nname)
  198.             return self.name
  199.         end,
  200.  
  201.         setPos = function(self,x,y)
  202.             self.x = x
  203.             self.y = y
  204.             self:draw()
  205.         end,
  206.  
  207.         getPos = function(self)
  208.             return self.x, self.y
  209.         end,
  210.  
  211.         onRightPress = function(self,func)
  212.             self.rfun = func
  213.         end,
  214.  
  215.         setActive = function(self,act)
  216.             self.active = act
  217.             self:draw()
  218.         end,
  219.  
  220.         getActive = function(self)
  221.             return self.active
  222.         end,
  223.  
  224.     }
  225.  
  226.     return button
  227. end
  228.  
  229. -- Menu API --
  230. function newMenu(x,y,width,height,bcolor)
  231.  
  232.     local menu = {
  233.         x = x,
  234.         y = y,
  235.         w = width,
  236.         h = height,
  237.         bcol = bcolor,
  238.         options={},
  239.         presets={},
  240.  
  241.         draw = function(self)
  242.             box(self.x,self.y,self.x+self.w-1,self.y+self.h-1,self.bcol)
  243.             for i = 1, #self.options do
  244.                 if(self.options[i].name ~= nil)then
  245.                     local bcol = self.presets[self.options[i].pset].bc
  246.                     local tcol = self.presets[self.options[i].pset].tc
  247.  
  248.                     if(self.options[i].active)then
  249.                         bcol = self.presets[self.options[i].pset].ab
  250.                         tcol = self.presets[self.options[i].pset].at
  251.                     end
  252.  
  253.                     term.setBackgroundColor(bcol)
  254.                     term.setTextColor(tcol)
  255.  
  256.                     pos( self.x, self.y+(i-1) )
  257.  
  258.                     write(self.options[i].name)
  259.                 end
  260.             end
  261.         end,
  262.  
  263.         update = function(self, ev)
  264.             if(ev[1] == "mouse_click")then
  265.  
  266.                 for i = 1, #self.options do
  267.                     if(self.options[i].name ~= nil)then
  268.                         if(ev[3] >= self.x and ev[3] <= #self.options[i].name + self.x
  269.                           and ev[4] == self.y + (i-1))then
  270.                             self:setActiveOption(self.options[i].name,true)
  271.                             sleep(0.2)
  272.                             self:setActiveOption(self.options[i].name,false)
  273.  
  274.                             self.options[i].act()
  275.                         end
  276.                     end
  277.                 end
  278.  
  279.             end
  280.         end,
  281.  
  282.         addOption = function(self,name,preset,action)
  283.             if(self.presets[preset].tc ~= nil)then
  284.                 self.options[#self.options+1] = {name=name,pset=preset,act=action,active=false}
  285.             else
  286.                 error("UGAPI: ->menu->addOption: No such preset.")
  287.             end
  288.         end,
  289.  
  290.         setPreset = function(self,name,tcolor,bcolor,atcolor,abcolor,action)
  291.             self.presets[name] = {tc=tcolor,bc=bcolor,at=atcolor,ab=abcolor}
  292.         end,
  293.  
  294.         addWhiteSpace = function(self)
  295.             self.options[#self.options+1] = {name=nil}
  296.         end,
  297.  
  298.         setActiveOption = function(self,nname,isact)
  299.             for i = 1, #self.options do
  300.                 if(self.options[i].name == nname and nname ~= nil)then
  301.                     self.options[i].active = isact
  302.                 end
  303.             end
  304.  
  305.             self:draw()
  306.         end,
  307.  
  308.     }
  309.  
  310.     return menu
  311. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement