Advertisement
johnnic431

gAPI

Jun 21st, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. --revolutOS graphics library
  2.  
  3. --[[
  4.     functions to add:
  5.     paintSlidingBox
  6.     paintSlidingText
  7. ]]
  8.  
  9. --So i can auto update this
  10. build=1;
  11.  
  12. write=function(_sText)
  13.     if save then
  14.         print("Saving")
  15.         local x,y=term.getCursorPos()
  16.         for i=1,_sText:len() do
  17.             scr.tx[x+i-1][y]=_sText:sub(i,i);
  18.             scr.tc[x+i-1][y]=col;
  19.             scr.bg[x+i-1][y]=tcol;
  20.         end
  21.         term.write(_sText)
  22.     else
  23.         term.write(_sText)
  24.     end
  25. end
  26.  
  27. scr={};
  28. local save=true;
  29. local p=paintutils;
  30. local p=paintutils;
  31. local sX,sY=term.getSize();
  32. local col=colors.black
  33. local tcol=colors.gray;
  34. local setTcol=function(c) term.setTextColor(c or tcol); tcol=c or tcol;end
  35. local setBcol=function(c) term.setBackgroundColor(c or col); col=c or col;end
  36. local line=function(x1,y1,x2,y2) p.drawLine(x1,y1,x2,y2,col); end
  37. local cursorPos=function(x,y) term.setCursorPos(x,y); end
  38. local round=function(num,idp) local mult=10^(idp or 0); if num>=0 then return math.floor(num*mult+0.5)/mult; else return math.ceil(num*mult-0.5)/mult end end
  39. local getHex=function(color) return string.format("%X",math.floor(math.log(color)/math.log(2))); end
  40. local getDecimal=function(hexRepresentation) return math.pow(2,tonumber(hexRepresentation,16)); end
  41.  
  42. for x=1,sX do
  43.     scr[x]={};
  44.     for y=1,sY do
  45.         scr[x][y]={bg=col,tc=tcol,tx=""};
  46.     end
  47. end
  48.  
  49. paintFullScreen=function(color)
  50.     setBcol(color);
  51.     for y=1,sY,1 do
  52.         p.drawLine(1,y,sX,y,col);
  53.     end
  54.     return true;
  55. end
  56.  
  57. paintBox=function(x1,y1,x2,y2,color)
  58.     if x2<=x1 or y2<=y1 then return false; end
  59.     setBcol(color);
  60.     for y=y1,y2 do
  61.         p.drawLine(x1,y,x2,y,col);
  62.     end
  63.     return true;
  64. end
  65.  
  66. paintSizedBorder=function(x1,y1,x2,y2,color)
  67.     if x2<=x1 or y2<=y1 then return false; end
  68.     setBcol(color);
  69.     line(x1,y1,x2,y1);
  70.     line(x1,y2,x2,y2);
  71.     line(x1,y1+1,x1,y2-1);
  72.     line(x2,y1+1,x2,y2-1);
  73.     return true;
  74. end
  75.  
  76. paintBorder=function(color)
  77.     if paintSizedBorder(1,1,sX,sY,color) then return true; end
  78.     return false;
  79. end
  80.  
  81. paintRightPanel=function(width,depth,color)
  82.     if paintBox(sX-width+1,1,sX,depth,color) then return true; end
  83.     return false;
  84. end
  85.  
  86. paintLeftPanel=function(width,depth,color)
  87.     if paintBox(1,1,width-1,depth,color) then return true; end
  88.     return false;
  89. end
  90.  
  91. paintNamedLeftPanel=function(width,depth,name,color,topColor,textColor)
  92.     if #name>width then return false; end
  93.     setTcol(textColor);
  94.     paintLeftPanel(width,depth,color);
  95.     setBcol(topColor);
  96.     line(1,1,width-1,1);
  97.     cursorPos(1,1);
  98.     write(name);
  99.     return true;
  100. end
  101.  
  102. paintNamedRightPanel=function(width,depth,name,color,topColor,textColor)
  103.     if #name>width then return false; end
  104.     setTcol(textColor);
  105.     paintRightPanel(width,depth,color);
  106.     setBcol(topColor);
  107.     line(sX-width+1,1,sX,1);
  108.     writeTextCentered(sX-width,sX,1,name);
  109.     return true;
  110. end
  111.  
  112. paintSlidingLine=function(x,y,length,timea,color)
  113.     setBcol(color);
  114.     c=function()
  115.         l=length/timea;
  116.         for i=1,timea do
  117.             line(x,y,x+(l*i),y);
  118.             sleep(0.05);
  119.         end
  120.     end
  121.     l=coroutine.create(c);
  122.     coroutine.resume(l);
  123.     return true;
  124. end
  125.  
  126. writeTextCentered=function(x1,x2,y,text,tCol)
  127.     setTcol(tCol);
  128.     if #text>x2-x1 then return false; end
  129.     toshowlen=string.len(text);
  130.     toshowlen=(x2-x1)-toshowlen;
  131.     toshowlen=toshowlen/2;
  132.     m=round(toshowlen);
  133.     cursorPos(toshowlen+(sX-(x2-x1))+1,y);
  134.     write(text);
  135. end
  136.  
  137. clear=function()
  138.     save=false;
  139.     oCol=col;
  140.     paintFullScreen(colors.black);
  141.     setBcol(oCol);
  142.     save=true;
  143. end
  144.  
  145. restore=function()
  146.     for x=1,sX do
  147.         for y=1,sY do
  148.             if scr[x][y][bg] then
  149.                 p.drawPixel(x,y,getDecimal(scr[x][y][bg]));
  150.             end
  151.             cursorPos(x,y);
  152.             setTcol(scr[x][y][tc]);
  153.             write(scr[x][y][tx]);
  154.         end
  155.     end
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement