Advertisement
Redxone

[OC] - Port of UGAPI

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