Advertisement
Guest User

OOS.lua

a guest
Nov 24th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.01 KB | None | 0 0
  1. local component=require("component")
  2. local event=require("event")
  3. local defaultTxt=16777215
  4. local defaultBg=0
  5.  
  6. local unknownFunction=function(table,index)
  7.     error("OOS does not contain "..index)
  8. end
  9. local unknownFunctionButton=function(table,index)
  10.     error("buttonObject does not contain "..index)
  11. end
  12.  
  13.  
  14. local OOSRaw={ 
  15.     version=0.5;
  16.     defaultTxt=16777215;
  17.     defaultBg=0;
  18.    
  19.     time=0;
  20.     frame=0;
  21.    
  22.     eventHandle=function(timeOut)
  23.         ev={}
  24.         events,_1,_2,_3,_4,_5=event.pull(timeOut)
  25.         if events=="component_added" then
  26.             ev={event=events,adress=_1,componentType=_2}
  27.         elseif events=="component_removed" then
  28.             ev={event=events,adress=_1,componentType=_2}
  29.         elseif events=="component_available" then
  30.             ev={event=events,componentType=_1}
  31.         elseif events=="component_unavailable" then
  32.             ev={event=events,componentType=_1}
  33.     --screen
  34.         elseif events=="screen_resized" then
  35.             ev={event=events,adress=_1,newWidth=_2,newHight=_3}
  36.         elseif events=="touch" then
  37.             ev={event=events,adress=_1,x=_2,y=_3,button=_4,player=_5}
  38.         elseif events=="drag" then
  39.             ev={event=events,adress=_1,x=_2,y=_3,button=_4,player=_5}
  40.         elseif events=="drop" then
  41.             ev={event=events,adress=_1,x=_2,y=_3,button=_4,player=_5}
  42.         elseif events=="scroll" then
  43.             ev={event=events,adress=_1,x=_2,y=_3,direction=_4,player=_5}
  44.         elseif events=="walk" then
  45.             ev={event=events,adress=_1,x=_2,y=_3,player=_4}
  46.     --keyboard
  47.         elseif events=="key_down" then
  48.             ev={event=events,adress=_1,char=string.char(_2),key=_3,player=_4}
  49.         elseif events=="key_up" then
  50.             ev={event=events,adress=_1,char=string.char(_2),key=_3,player=_4}
  51.         elseif events=="clipboard" then
  52.             ev={event=events,adress=_1,paste=_2,player=_3}
  53.     --redstone
  54.         elseif events=="redstone_changed" then
  55.             ev={event=events,adress=_1,side=_2}
  56.     --motion sensor
  57.         elseif events=="motion" then
  58.             ev={event=events,adress=_1,x=_2,y=_3,z=_4}
  59.     --network kards
  60.         elseif events=="modem_message" then
  61.             ev={event=events,receiveAdress=_1,sendAdress=_2,port=_3,distance=_4,data=_5}
  62.     --otherwise
  63.         --else
  64.             --error("unknown event")
  65.         end
  66.         return ev
  67.     end;
  68.    
  69.    
  70.     makeButton=function(adress,coord,disp,colormapTxt,colormapBg,Args)
  71.         if Args==nil then
  72.             Args={}
  73.         end
  74.         if Args.button==nil then
  75.             button="   "
  76.         else
  77.             button=Args.button
  78.         end
  79.         if Args.key==nil then
  80.             key="   "
  81.         else
  82.             key=Args.key
  83.         end
  84.         if Args.char==nil then
  85.             char="   "
  86.         else
  87.             char=string.lower(Args.char)
  88.         end
  89.         if Args.player==nil then
  90.             player="   "
  91.         else
  92.             player=Args.player
  93.         end
  94.         if Args.isToggle~=true then
  95.             isToggle=false
  96.         else
  97.             isToggle=true
  98.         end
  99.         if  type(Args.func)=="function" then
  100.             func=Args.func
  101.         else
  102.             func="   "
  103.         end
  104.         param=Args.param
  105.    
  106.         v={{}}
  107.         if type(disp)=="string" then
  108.             displayString=disp
  109.             n=1
  110.             for i in string.gmatch(disp,".") do
  111.                 v[1][n]=i
  112.                 n=n+1
  113.             end
  114.             disp=v
  115.         elseif type(disp)=="table" then
  116.             displayString="   "
  117.             if type(disp[1])=="string" then
  118.                 --print(#disp)
  119.                 for lenght=1,#disp do
  120.                     v[lenght]={}
  121.                     n=1
  122.                     for c in string.gmatch(disp[lenght],".") do
  123.                         v[lenght][n]=c
  124.                         n=n+1
  125.                     end
  126.                 end
  127.                 disp=v
  128.             else
  129.                 error("unsupported display format")
  130.             end
  131.         end
  132.        
  133.    
  134.         if colormapTxt==nil then
  135.             text=defaultTxt
  136.         end
  137.    
  138.         if type(colormapTxt)=="number" then
  139.             text={}
  140.             textColor="   "
  141.             for y=1,#disp do
  142.                 text[y]={}
  143.                 for x=1,#disp[y] do
  144.                     text[y][x]=colormapTxt
  145.                 end
  146.             end
  147.         else
  148.             text=colormapTxt
  149.             textColor=colormapTxt
  150.         end
  151.    
  152.         if colormapBg==nil then
  153.             bg=defaultBg
  154.         end
  155.    
  156.         if type(colormapBg)=="number" then
  157.             bg={}
  158.             backgroundColor="   "
  159.             for y=1,#disp do
  160.                 bg[y]={}
  161.                 for x=1,#disp[y] do
  162.                     bg[y][x]=colormapBg
  163.                 end
  164.             end
  165.         else
  166.             bg=colormapBg
  167.             backgroundColor=colormapBg
  168.         end
  169.    
  170.         if type(adress)=="string" then
  171.             if adress=="gpu" then
  172.                 surf=component.getPrimary("gpu")
  173.             else
  174.                 surf=component.proxy(adress)
  175.             end
  176.         else
  177.             surf=component.getPrimary("gpu")
  178.         end
  179.    
  180.         button={
  181.             surface=surf;
  182.             side=side;
  183.             key=key;
  184.             char=char;
  185.             button=button;
  186.             func=func;
  187.             player=player;
  188.             param=param;
  189.             coordinates={x=coord[1];y=coord[2]};
  190.             display=disp;
  191.             displayString=displayString;
  192.             colormap={txt=text; bg=bg};
  193.             color={txt=textColor;bg=backgroundColor};
  194.             button=button;
  195.             toggle=isToggle;
  196.             hit=false;
  197.             isToggle=toggle;
  198.             default={txt=defaultTxt;bg=defaultBg};
  199.    
  200.             draw=function(self)
  201.                 if self.color.txt~="   " and self.color.bg~="   " and self.displayString~="   " then
  202.                     self.surface.setForeground(self.color.txt)
  203.                     self.surface.setBackground(self.color.bg)
  204.                     surf.surface.set(self.coordinates.x,self.coordinates.y,tostring(displayString),false)
  205.                 else
  206.                     for y=1,#self.display do
  207.                         for x=1,#self.display[y] do
  208.                             self.surface.setForeground(self.colormap.txt[y][x])
  209.                             self.surface.setBackground(self.colormap.bg[y][x])
  210.                            
  211.                             self.surface.set(self.coordinates.x+x-1,self.coordinates.y+y-1,tostring(self.display[y][x]),false)
  212.                         end
  213.                     end
  214.                 end
  215.                 self.surface.setForeground(16777215)--self.default.txt)
  216.                 self.surface.setBackground(0)--self.default.bg)
  217.             end;
  218.    
  219.             vanish=function(self)
  220.                
  221.                 self.surface.setForeground(self.default.txt)
  222.                 self.surface.setBackground(self.default.bg)
  223.                 for y=1,#self.display do
  224.                     for x=1,#self.display[y] do
  225.                         self.surface.set(self.coordinates.x+x-1,self.coordinates.y+y-1," ",false)
  226.                     end
  227.                 end
  228.             end;
  229.    
  230.             detect=function(self,inputdata)
  231.                 if not self.toggle then
  232.                     self.hit=false
  233.                 end
  234.                 --print(self.key)
  235.                 --print(inputdata.key)
  236.                 if inputdata.event=="touch" then
  237.                     if self.player==inputdata.player or self.player=="   " then
  238.                         if self.button=="   " then
  239.                             if (inputdata.x<=(self.coordinates.x+#self.display[1]-1) and (inputdata.x>=self.coordinates.x)) and (inputdata.y<=(self.coordinates.y+#self.display-1) and (inputdata.y>=self.coordinates.y)) then
  240.                                 if not self.toggle then self.hit=true else self.hit=not self.hit end
  241.                             else
  242.                                 if not self.toggle then self.hit=false end
  243.                             end
  244.                         else
  245.                             if self.button==inputdata.button then
  246.                                 if (inputdata.x<=(self.coordinates.x+#self.display[1]) and inputdata.x>=(self.coordinates.x)) and   (inputdata.y<=(self.coordinates.y+#self.display) and inputdata.y>=(self.coordinates.y)) then
  247.                                     if not self.toggle then self.hit=true else self.hit=not self.hit end
  248.                                 else
  249.                                     if not self.toggle then self.hit=false end
  250.                                 end
  251.                             else
  252.                                 if not self.toggle then self.hit=false end
  253.                             end
  254.                         end
  255.                     end
  256.                 elseif inputdata.event=="key_down" then
  257.                     if self.player==inputdata.player or self.player=="   " then
  258.                         if inputdata.key==self.key or inputdata.char==self.char then
  259.                             if not self.toggle then self.hit=true else self.hit=not self.hit end
  260.                         else
  261.                             if not self.toggle then self.hit=false end
  262.                         end
  263.                     end
  264.                 end
  265.                 if self.hit and self.func~="   " then
  266.                     self.func(param)
  267.                 end
  268.                 return self.hit
  269.             end;
  270.  
  271.             changeColor=function(self,colormapTxt,colormapBg)
  272.  
  273.                 if colormapTxt==nil then
  274.                     text=defaultTxt
  275.                 end
  276.            
  277.                 if type(colormapTxt)=="number" then
  278.                     text={}
  279.                     textColor="   "
  280.                     for y=1,#disp do
  281.                         text[y]={}
  282.                         for x=1,#disp[y] do
  283.                             text[y][x]=colormapTxt
  284.                         end
  285.                     end
  286.                 else
  287.                     text=colormapTxt
  288.                     textColor=colormapTxt
  289.                 end
  290.            
  291.                 if colormapBg==nil then
  292.                     bg=defaultBg
  293.                 end
  294.            
  295.                 if type(colormapBg)=="number" then
  296.                     bg={}
  297.                     backgroundColor="   "
  298.                     for y=1,#disp do
  299.                         bg[y]={}
  300.                         for x=1,#disp[y] do
  301.                             bg[y][x]=colormapBg
  302.                         end
  303.                     end
  304.                 else
  305.                     bg=colormapBg
  306.                     backgroundColor=colormapBg
  307.                 end
  308.  
  309.                 self.colormap={txt=text; bg=bg};
  310.                 self.color={txt=textColor;bg=backgroundColor};
  311.             end;
  312.         }
  313.         button=setmetatable(button,{__index=unknownFunctionButton})
  314.         return button
  315.     end;
  316. }
  317.  
  318. local OOS=setmetatable(OOSRaw,{__index=unknownFunction})
  319.  
  320. return OOS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement