Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component=require("component")
- local event=require("event")
- local defaultTxt=16777215
- local defaultBg=0
- local unknownFunction=function(table,index)
- error("OOS does not contain "..index)
- end
- local unknownFunctionButton=function(table,index)
- error("buttonObject does not contain "..index)
- end
- local OOSRaw={
- version=0.5;
- defaultTxt=16777215;
- defaultBg=0;
- time=0;
- frame=0;
- eventHandle=function(timeOut)
- ev={}
- events,_1,_2,_3,_4,_5=event.pull(timeOut)
- if events=="component_added" then
- ev={event=events,adress=_1,componentType=_2}
- elseif events=="component_removed" then
- ev={event=events,adress=_1,componentType=_2}
- elseif events=="component_available" then
- ev={event=events,componentType=_1}
- elseif events=="component_unavailable" then
- ev={event=events,componentType=_1}
- --screen
- elseif events=="screen_resized" then
- ev={event=events,adress=_1,newWidth=_2,newHight=_3}
- elseif events=="touch" then
- ev={event=events,adress=_1,x=_2,y=_3,button=_4,player=_5}
- elseif events=="drag" then
- ev={event=events,adress=_1,x=_2,y=_3,button=_4,player=_5}
- elseif events=="drop" then
- ev={event=events,adress=_1,x=_2,y=_3,button=_4,player=_5}
- elseif events=="scroll" then
- ev={event=events,adress=_1,x=_2,y=_3,direction=_4,player=_5}
- elseif events=="walk" then
- ev={event=events,adress=_1,x=_2,y=_3,player=_4}
- --keyboard
- elseif events=="key_down" then
- ev={event=events,adress=_1,char=string.char(_2),key=_3,player=_4}
- elseif events=="key_up" then
- ev={event=events,adress=_1,char=string.char(_2),key=_3,player=_4}
- elseif events=="clipboard" then
- ev={event=events,adress=_1,paste=_2,player=_3}
- --redstone
- elseif events=="redstone_changed" then
- ev={event=events,adress=_1,side=_2}
- --motion sensor
- elseif events=="motion" then
- ev={event=events,adress=_1,x=_2,y=_3,z=_4}
- --network kards
- elseif events=="modem_message" then
- ev={event=events,receiveAdress=_1,sendAdress=_2,port=_3,distance=_4,data=_5}
- --otherwise
- --else
- --error("unknown event")
- end
- return ev
- end;
- makeButton=function(adress,coord,disp,colormapTxt,colormapBg,Args)
- if Args==nil then
- Args={}
- end
- if Args.button==nil then
- button=" "
- else
- button=Args.button
- end
- if Args.key==nil then
- key=" "
- else
- key=Args.key
- end
- if Args.char==nil then
- char=" "
- else
- char=string.lower(Args.char)
- end
- if Args.player==nil then
- player=" "
- else
- player=Args.player
- end
- if Args.isToggle~=true then
- isToggle=false
- else
- isToggle=true
- end
- if type(Args.func)=="function" then
- func=Args.func
- else
- func=" "
- end
- param=Args.param
- v={{}}
- if type(disp)=="string" then
- displayString=disp
- n=1
- for i in string.gmatch(disp,".") do
- v[1][n]=i
- n=n+1
- end
- disp=v
- elseif type(disp)=="table" then
- displayString=" "
- if type(disp[1])=="string" then
- --print(#disp)
- for lenght=1,#disp do
- v[lenght]={}
- n=1
- for c in string.gmatch(disp[lenght],".") do
- v[lenght][n]=c
- n=n+1
- end
- end
- disp=v
- else
- error("unsupported display format")
- end
- end
- if colormapTxt==nil then
- text=defaultTxt
- end
- if type(colormapTxt)=="number" then
- text={}
- textColor=" "
- for y=1,#disp do
- text[y]={}
- for x=1,#disp[y] do
- text[y][x]=colormapTxt
- end
- end
- else
- text=colormapTxt
- textColor=colormapTxt
- end
- if colormapBg==nil then
- bg=defaultBg
- end
- if type(colormapBg)=="number" then
- bg={}
- backgroundColor=" "
- for y=1,#disp do
- bg[y]={}
- for x=1,#disp[y] do
- bg[y][x]=colormapBg
- end
- end
- else
- bg=colormapBg
- backgroundColor=colormapBg
- end
- if type(adress)=="string" then
- if adress=="gpu" then
- surf=component.getPrimary("gpu")
- else
- surf=component.proxy(adress)
- end
- else
- surf=component.getPrimary("gpu")
- end
- button={
- surface=surf;
- side=side;
- key=key;
- char=char;
- button=button;
- func=func;
- player=player;
- param=param;
- coordinates={x=coord[1];y=coord[2]};
- display=disp;
- displayString=displayString;
- colormap={txt=text; bg=bg};
- color={txt=textColor;bg=backgroundColor};
- button=button;
- toggle=isToggle;
- hit=false;
- isToggle=toggle;
- default={txt=defaultTxt;bg=defaultBg};
- draw=function(self)
- if self.color.txt~=" " and self.color.bg~=" " and self.displayString~=" " then
- self.surface.setForeground(self.color.txt)
- self.surface.setBackground(self.color.bg)
- surf.surface.set(self.coordinates.x,self.coordinates.y,tostring(displayString),false)
- else
- for y=1,#self.display do
- for x=1,#self.display[y] do
- self.surface.setForeground(self.colormap.txt[y][x])
- self.surface.setBackground(self.colormap.bg[y][x])
- self.surface.set(self.coordinates.x+x-1,self.coordinates.y+y-1,tostring(self.display[y][x]),false)
- end
- end
- end
- self.surface.setForeground(16777215)--self.default.txt)
- self.surface.setBackground(0)--self.default.bg)
- end;
- vanish=function(self)
- self.surface.setForeground(self.default.txt)
- self.surface.setBackground(self.default.bg)
- for y=1,#self.display do
- for x=1,#self.display[y] do
- self.surface.set(self.coordinates.x+x-1,self.coordinates.y+y-1," ",false)
- end
- end
- end;
- detect=function(self,inputdata)
- if not self.toggle then
- self.hit=false
- end
- --print(self.key)
- --print(inputdata.key)
- if inputdata.event=="touch" then
- if self.player==inputdata.player or self.player==" " then
- if self.button==" " then
- 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
- if not self.toggle then self.hit=true else self.hit=not self.hit end
- else
- if not self.toggle then self.hit=false end
- end
- else
- if self.button==inputdata.button then
- 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
- if not self.toggle then self.hit=true else self.hit=not self.hit end
- else
- if not self.toggle then self.hit=false end
- end
- else
- if not self.toggle then self.hit=false end
- end
- end
- end
- elseif inputdata.event=="key_down" then
- if self.player==inputdata.player or self.player==" " then
- if inputdata.key==self.key or inputdata.char==self.char then
- if not self.toggle then self.hit=true else self.hit=not self.hit end
- else
- if not self.toggle then self.hit=false end
- end
- end
- end
- if self.hit and self.func~=" " then
- self.func(param)
- end
- return self.hit
- end;
- changeColor=function(self,colormapTxt,colormapBg)
- if colormapTxt==nil then
- text=defaultTxt
- end
- if type(colormapTxt)=="number" then
- text={}
- textColor=" "
- for y=1,#disp do
- text[y]={}
- for x=1,#disp[y] do
- text[y][x]=colormapTxt
- end
- end
- else
- text=colormapTxt
- textColor=colormapTxt
- end
- if colormapBg==nil then
- bg=defaultBg
- end
- if type(colormapBg)=="number" then
- bg={}
- backgroundColor=" "
- for y=1,#disp do
- bg[y]={}
- for x=1,#disp[y] do
- bg[y][x]=colormapBg
- end
- end
- else
- bg=colormapBg
- backgroundColor=colormapBg
- end
- self.colormap={txt=text; bg=bg};
- self.color={txt=textColor;bg=backgroundColor};
- end;
- }
- button=setmetatable(button,{__index=unknownFunctionButton})
- return button
- end;
- }
- local OOS=setmetatable(OOSRaw,{__index=unknownFunction})
- return OOS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement