Guest User

awsumben13's APIS

a guest
Mar 5th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.45 KB | None | 0 0
  1.  
  2. --Help:
  3. --[[
  4. how to use:
  5.     process:
  6.         create a process-variable = process:create( function to be run, function to be run if/when it errors )
  7.         to do things with the screen-variable:[any term functions]( args )
  8.         //please note that setCursorPos will set the cursor pos relevant to the screens position
  9.         to add a screen-variable:addWindow( name, x, y, width, height, default background colour, default text colour, default text, header ( can leave blank ) )
  10.         to change screen-variable:setScreen( name ( the name given to the window when created ) )
  11.         to run the process-variable:run( event )
  12.         //this will return different things depending on how it ran and what the function returned
  13.     window:
  14.         same as process but only the graphical parts of it ( anything that says screen or window )
  15.     shape:
  16.         create a blank shape-variable = shape.create( "any name", width, height )
  17.         add things to the shape-variable:addRectangle( x, y, width, height, fill?, background colour, text colour, text )/pixel( x, y, bc, tc, text )/circle( )
  18.         to load for drawing/adding to a screen-variable:load( )
  19.         to draw-variable:draw( )
  20.     log:
  21.         to add to the log-log.add( type, ...data )
  22.         to save the log-log.save( path )
  23.     variable:
  24.         to add a variable-variable.add( str name, data )
  25.         to save the variables-variable.save( path )
  26.         to load them again-variable.load( path )
  27.         to remove one-variable.remove( str name )
  28.     file:
  29.         to save data-file.save( path, tContents or sContents )
  30.         to read the file-file.read( path, into a table? )
  31.         to change one line-file.writeLine( path, line, data )
  32.         to read one line-file.readLine( path, line )
  33.     menu:
  34.         to make a menu-variable = menu.create( data, max width, max height )
  35.         to get all the contents-variable:getSelection( )
  36.         more coming soon or when i cba to do it
  37.         //use this if you want a list of items in table format
  38.     other:
  39.         split-splits a string with a specified pattern
  40.         join-joins a string with a specified pattern
  41.         splitChars-splits the string into each char
  42. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  43.    
  44. still to come:
  45.     master process = overlays to the screen
  46.     pause processes
  47.     have multiple screens drawn at the same time from each process ( currently limited to one screen at a time )
  48.     term overwriting for compatibility with functions that use term
  49.     a demo function
  50.     more shapes
  51.     advanced variable loading and protection
  52.     file protection & encryption
  53.     file classpath loading ( load files into classes )
  54. ]]
  55.  
  56. shape = {
  57.     create = function( name, x, y )
  58.         local t = { }
  59.         t.name = name
  60.         t.data = { }
  61.         for i = 1,y do
  62.             t.data[i] = { }
  63.             for k = 1,x do
  64.                 t.data[i][k] = { "", colours.black, colours.white }
  65.             end
  66.         end
  67.         return setmetatable( t, { __index = shape } )
  68.     end;
  69.     load = function( self )
  70.         local t = { }
  71.         for i = 1,#self.data do
  72.             for k = 1,#self.data[i] do
  73.                 if self.data[i][k][1] ~= "" then
  74.                     table.insert( t, { x = k, y = i, text = self.data[i][k][1], bc = self.data[i][k][2], tc = self.data[i][k][3] } )
  75.                 end
  76.             end
  77.         end
  78.         return t
  79.     end;
  80.     draw = function( self, x, y )
  81.         for i = y, ( y+#self.data )-1 do
  82.             for k = x, ( x+self.data[i] )-1 do
  83.                 term.native.setCursorPos( k, i )
  84.                 term.native.setBackgroundColour( self.data[i][k][2] )
  85.                 term.native.setTextColour( self.data[i][k][3] )
  86.                 term.native.write( self.data[i][k][1] )
  87.             end
  88.         end
  89.     end;
  90.     addRectangle = function( self, x, y, x2, y2, fill, bc, tc, text )
  91.         local bc = bc or colours.black
  92.         local tc = tc or colours.white
  93.         local text = text or " "
  94.         if not tc then tc = colours.white end
  95.         if x > x2 then x3 = x2 x2 = x x = x3 end
  96.         if y > y2 then y3 = y2 y2 = y y = y3 end
  97.         local t = { }
  98.         for i = y, y2 do
  99.             for k = x, x2 do
  100.                 table.insert( t, { x = k, y = i, text = text, bc = bc, tc = tc } )
  101.             end
  102.         end
  103.         if not fill then
  104.             local i = 1
  105.             while i < #t do
  106.                 if t[i].x > x and t[i].x < x2 and t[i].y > y and t[i].y < y2 then
  107.                     table.remove( t, i )
  108.                     i = i-1
  109.                 end
  110.                 i = i+1
  111.             end
  112.         end
  113.         for i = 1,#t do
  114.             if self.data[t[i].y][t[i].x] then
  115.                 self.data[t[i].y][t[i].x][3] = t[i].tc
  116.                 self.data[t[i].y][t[i].x][2] = t[i].bc
  117.                 self.data[t[i].y][t[i].x][1] = t[i].text
  118.             end
  119.         end
  120.     end;
  121.     addPixel = function( self, x, y, bc, tc, t )
  122.         self.data[y][x] = { t, bc, tc }
  123.     end;
  124.     addCircle = function( self, x, y, r, bc, tc, text, fill )
  125.         local t = { }
  126.         for tx = x-r,x+r do
  127.             for ty = y-r,y+r do
  128.                 if (math.sqrt((x-tx)^2+(y-ty)^2) >= r and math.sqrt((x-tx)^2+(y-ty)^2) <= r+(r/math.sqrt((x-tx)^2+(y-ty)^2))) then
  129.                     table.insert( t, { x = tx, y = ty, text = text, bc = bc, tc = tc } )
  130.                 elseif (math.sqrt((x-tx)^2+(y-ty)^2) <= r+(r/math.sqrt((x-tx)^2+(y-ty)^2))) and fill then
  131.                     table.insert( t, { x = tx, y = ty, text = text, bc = bc, tc = tc } )
  132.                 end
  133.             end
  134.         end
  135.         for i = 1,#t do
  136.             if self.data[t[i].y][t[i].x] then
  137.                 self.data[t[i].y][t[i].x][3] = t[i].tc
  138.                 self.data[t[i].y][t[i].x][2] = t[i].bc
  139.                 self.data[t[i].y][t[i].x][1] = t[i].text
  140.             end
  141.         end
  142.     end;
  143. }
  144.  
  145. window = {
  146.     create = function( link, x, y, w, h, bc, tc, dt, header )
  147.         if type( link ) ~= "table" then self = window end
  148.         local t = { }
  149.         t.xPos = 1
  150.         t.yPos = 1
  151.         t.x = x
  152.         t.y = y
  153.         t.w = w
  154.         t.h = h
  155.         t.dt = dt
  156.         t.bc = bc
  157.         t.tc = tc
  158.         t.header = { text = header, bc = bc, tc = tc }
  159.         t.screen = { }
  160.         for i = 1,h do
  161.             t.screen[i] = { }
  162.             for k = 1,w do
  163.                 t.screen[i][k] = { t = dt, bc = bc, tc = tc }
  164.             end
  165.         end
  166.         return setmetatable( t, { __index = link } )
  167.     end;
  168.     update = function( self )
  169.         if self.xPos > self.w then
  170.             self.xPos = self.w
  171.         end
  172.         if self.xPos < 1 then
  173.             self.xPos = 1
  174.         end
  175.         if self.yPos > self.h then
  176.             self.yPos = self.h
  177.         end
  178.         if self.yPos < 1 then
  179.             self.yPos = 1
  180.         end
  181.     end;
  182.     move = function( self, x, y )
  183.         if x + self.x >= 1 and x + self.x + ( self.w - 1 ) <= 51 then
  184.             self.x = self.x + x
  185.         end
  186.         if y + self.y >= 1 and y + self.y + ( self.h - 1 ) <= 19 then
  187.             self.y = self.y + y
  188.         end
  189.         self:update( )
  190.     end;
  191.     moveOutOfScreen = function( self, x, y )
  192.         self.x = x
  193.         self.y = y
  194.         self:update( )
  195.     end;
  196.     draw = function( self )
  197.         local s = self.screen
  198.         for i = 1,#s do
  199.             if i > self.h then break end
  200.             for k = 1,#s[i] do
  201.                 if k > self.w then break end
  202.                 term.native.setCursorPos( k + ( self.x - 1 ), i + ( self.y - 1 ) )
  203.                 if term.isColour( ) then
  204.                     term.native.setBackgroundColour( s[i][k].bc )
  205.                     term.native.setTextColour( s[i][k].tc )
  206.                 end
  207.                 term.native.write( string.sub( tostring( s[i][k].t ), 1, 1 ) )
  208.             end
  209.         end
  210.         if self.header.text then
  211.             self.header.text = string.sub( self.header.text, 1, self.w )
  212.             term.native.setCursorPos( ( ( self.w/2 ) - ( self.header.text:len( )/2 ) ) + self.x, self.y )
  213.             if term.isColour( ) then
  214.                 term.native.setBackgroundColour( self.header.bc )
  215.                 term.native.setTextColour( self.header.tc )
  216.             end
  217.             term.native.write( self.header.text )
  218.         end
  219.         self:update( )
  220.     end;
  221.     write = function( self, txt )
  222.         local t = { }
  223.         for i = 1,txt:len( ) do
  224.             table.insert( t, txt:sub( i, i ) )
  225.         end
  226.         for i = 1,#t do
  227.             if self.screen[self.yPos][self.xPos] then
  228.                 self.screen[self.yPos][self.xPos] = { t = t[i], bc = self.bc, tc = self.tc }
  229.                 self.xPos = self.xPos+1
  230.                 if self.xPos > self.w then
  231.                     self.xPos = 1
  232.                     self.yPos = self.yPos + 1
  233.                 end
  234.             end
  235.         end
  236.         self:update( )
  237.     end;
  238.     print = function( self, text )
  239.         self:write( text )
  240.         self.yPos = self.yPos+1
  241.         self.xPos = 1
  242.     end;
  243.     pixel = function( self, x, y, text, bc, tc )
  244.         if self.screen[y][x] then
  245.             self.screen[y][x] = { bc = bc, tc = tc, t = text }
  246.         end
  247.     end;
  248.     addShape = function( self, t )
  249.         y = self.yPos
  250.         x = self.xPos
  251.         for i = 1,#t do
  252.             self:pixel( x+t[i].x-1, y+t[i].y-1, t[i].text, t[i].bc, t[i].tc )
  253.         end
  254.     end;
  255.     setCursorPos = function( self, x, y )
  256.         self.xPos = x
  257.         self.yPos = y
  258.         self:update( )
  259.     end;
  260.     setBackgroundColour = function( self, col )
  261.         self.bc = col
  262.         self:update( )
  263.     end;
  264.     setTextColour = function( self, col )
  265.         self.tc = col
  266.         self:update( )
  267.     end;
  268.     clear = function( self )
  269.         for i = 1,#self.screen do
  270.             for k = 1,#self.screen[i] do
  271.                 self.screen[i][k] = { t = self.dt, bc = self.bc, tc = self.tc }
  272.             end
  273.         end
  274.         self:update( )
  275.     end;
  276.     setHeader = function( self, text )
  277.         self.header.text = text
  278.         self:update( )
  279.     end;
  280.     setHeaderColour = function( self, col, colo )
  281.         self.header = { text = self.header.text, bc = col, tc = colo }
  282.         self:update( )
  283.     end;
  284. }
  285.  
  286. process = {
  287.     current = false;
  288.     create = function( link, func, stopfunc )
  289.         if type( link ) ~= "table" then link = process end
  290.         local t = { }
  291.         t.co = coroutine.create( func )
  292.         t.stopco = coroutine.create( stopfunc )
  293.         t.status = "created"
  294.         t.windows = { window:create( 1, 1, 51, 19, colours.black, colours.white, " ", "[ default window ]" ) }
  295.         t.window = t.windows[1]
  296.         return setmetatable( t, { __index = link } )
  297.     end;
  298.     run = function( self, ... )
  299.         if coroutine.status( self.co ) == "dead" then
  300.             self = nil
  301.             return "removed"
  302.         end
  303.         if self.status == "stopping" then
  304.             process.current = self
  305.             err = { pcall( coroutine.resume, self.stopco, unpack( arg ) ) }
  306.         elseif self.status == "running" or self.status == "created" then
  307.             process.current = self
  308.             err = { pcall( coroutine.resume, self.co, unpack( arg ) ) }
  309.         end
  310.         process.current = false
  311.         if not err[1] then
  312.             if self.status == "stopping" then
  313.                 self = nil
  314.                 err[1] = "removed"
  315.             else
  316.                 self.status = "stopping"
  317.                 err[1] = "stopping"
  318.             end
  319.         end
  320.         return( unpack( err ) )
  321.     end;
  322.     move = function( self, x, y )
  323.         self.window:move( x, y )
  324.     end;
  325.     addWindow = function( self, index, x, y, w, h, bc, tc, dt, header )
  326.         self.windows[index] = window:create( x, y, w, h, bc, tc, dt, header )
  327.     end;
  328.     setWindow = function( self, index )
  329.         if self.windows[index] then
  330.             self.window = self.windows[index]
  331.         end
  332.     end;
  333.     getWindows = function( self )
  334.         local t = { }
  335.         for k, v in pairs( self.windows ) do
  336.             table.insert( t, k )
  337.         end
  338.         return t
  339.     end;
  340.     draw = function( self )
  341.         self.window:draw( )
  342.     end;
  343.     write = function( self, text )
  344.         self.window:write( text )
  345.     end;
  346.     print = function( self, text )
  347.         self.window:print( text )
  348.     end;
  349.     setCursorPos = function( self, x, y )
  350.         self.window:setCursorPos( x, y )
  351.     end;
  352.     setBackgroundColour = function( self, col )
  353.         self.window:setBackgroundColour( col )
  354.     end;
  355.     setTextColour = function( self, col )
  356.         self.window:setTextColour( col )
  357.     end;
  358.     clear = function( self )
  359.         self.window:clear( )
  360.     end;
  361.     setHeader = function( self, text )
  362.         self.window:setHeader( text )
  363.     end;
  364.     setHeaderColour = function( self, col, colo )
  365.         self.window:setHeaderColour( col, colo )
  366.     end;
  367. }
  368.  
  369. join = function( t, pat )
  370.     local str = ""
  371.     if not pat then pat = "" end
  372.     for i = 1,#t do
  373.         str = str..pat..tostring( t[i] )
  374.     end
  375.     return str
  376. end
  377.  
  378. splitChars = function( str )
  379.     t = { }
  380.     for i = 1,string.len( str ) do
  381.         table.insert( t, string.sub( str, i, i ) )
  382.     end
  383.     return t
  384. end
  385.  
  386. split = function(str,pat)
  387.     local t = {}
  388.     local fpat = "(.-)" .. pat
  389.     local last_end = 1
  390.     local s, e, cap = str:find(fpat, 1)
  391.     while s do
  392.         if s ~= 1 or cap ~= "" then
  393.             table.insert(t,cap)
  394.         end
  395.         last_end = e+1
  396.         s, e, cap = str:find(fpat, last_end)
  397.     end
  398.     if last_end <= #str then
  399.         cap = str:sub(last_end)
  400.         table.insert(t, cap)
  401.     end
  402.     return t
  403. end
  404.  
  405. menu = {
  406.     create = function( tContents, w, h )
  407.         local t = { }
  408.         t.contents = tContents
  409.         t.os = 0
  410.         t.w = w
  411.         t.h = h
  412.         return setmetatable( t, { __index = menu } )
  413.     end;
  414.     add = function( self, data )
  415.         table.insert( self.contents, data )
  416.     end;
  417.     remove = function( self, index )
  418.         table.remove( self.contents, index )
  419.     end;
  420.     set = function( self, tContents )
  421.         self.contents = tContents
  422.     end;
  423.     getSelection = function( self )
  424.         local h = self.h < 20 and self.h or 19
  425.         local t = { }
  426.         for i = 1+self.os,h+self.os do
  427.             if not self.contents[i] then
  428.                 table.insert( t, "" )
  429.             else
  430.                 table.insert( t, self.contents[i] )
  431.             end
  432.         end
  433.         for i = 1,#t do
  434.             if t[i]:len( ) > self.w then
  435.                 t[i] = string.sub( t[i], 1, self.w-3 ).."..."
  436.             end
  437.         end
  438.         return t
  439.     end;
  440.     getContents = function( self, index )
  441.         if index > self.h then return nil end
  442.         return self.contents[index + self.os]
  443.     end;
  444.     getIndex = function( self, index )
  445.         return self.contents[index]
  446.     end;
  447.     scroll = function( self, am )
  448.         self.os = self.os+am
  449.         if self.os < 0 then
  450.             self.os = 0
  451.         end
  452.         if self.os+self.h >= #self.contents and #self.contents > self.h then
  453.             self.os = #self.contents - self.h
  454.         elseif #self.contents <= self.h then
  455.             self.os = 0
  456.         end
  457.     end;
  458. }
  459.  
  460. file = {
  461.     read = function( path, intoTable )
  462.         local k = assert( fs.open( path, "r" ), "File not found" )
  463.         m = k.readAll( )
  464.         k.close( )
  465.         if intoTable then
  466.             m = split( m, "\n" )
  467.         end
  468.         return m
  469.     end;
  470.     save = function( file, data )
  471.         if type( data ) == "table" then isTable = true end
  472.         local f = fs.open( file, "w" )
  473.         if not isTable then
  474.             f.write( tostring( data ) )
  475.         else
  476.             for i = 1,#data do
  477.                 f.writeLine( tostring( data[i] ) )
  478.             end
  479.         end
  480.         f.close( )
  481.     end;
  482.     writeLine = function( path, line, data )
  483.         file = file.read( path, true )
  484.         file[line] = data
  485.         file.save( path, file )
  486.     end;
  487.     readLine = function( path, line )
  488.         local t = file.read( path, true )
  489.         return t[line]
  490.     end;
  491. }
  492.  
  493. variable = {
  494.     vars = { };
  495.     add = function( name, data )
  496.         table.insert( variable.vars, { name = name, type = type( data ), data = data } )
  497.     end;
  498.     save = function( file )
  499.         local t = { }
  500.         for i = 1,#variable.vars do
  501.             if variable.vars[i].type == "table" then
  502.                 variable.vars[i].data = textutils.serialize( variable.vars[i].data )
  503.             end
  504.             table.insert( t, variable.vars[i].name..";"..variable.vars[i].type..";"..tostring( variable.vars[i].data ) )
  505.         end
  506.         k = fs.open( file, "w" )
  507.         for i = 1,#t do
  508.             k.writeLine( t[i] )
  509.         end
  510.         k.close( )
  511.     end;
  512.     remove = function( index )
  513.         if type( index ) == "string" then
  514.             for i = 1,#variable.vars do
  515.                 if variable.vars[i].name == index then
  516.                     table.remove( variable.vars, i )
  517.                     return true
  518.                 end
  519.             end
  520.         else
  521.             table.remove( variable.vars, index )
  522.         end
  523.     end;
  524.     load = function( file )
  525.         local k = assert( fs.open( file, "r" ), "File not found" )
  526.         m = k.readAll( )
  527.         k.close( )
  528.         k = split( m, "\n" )
  529.         for i = 1,#k do
  530.             m = split( k[i], ";" )
  531.             if m[2] == "table" then
  532.                 m[3] = textutils.unserialize( m[3] )
  533.             elseif m[2] == "number" then
  534.                 m[3] = tonumber( m[3] )
  535.             elseif m[2] == "boolean" then
  536.                 if m[3] == "false" then
  537.                     m[3] = false
  538.                 else
  539.                     m[3] = true
  540.                 end
  541.             end
  542.             variable.add( m[1], m[3] )
  543.             _G[m[1]] = m[3]
  544.         end
  545.     end;
  546. }
  547.  
  548. log = {
  549.     data = {
  550.         exlude = { };
  551.     };
  552.     save = function( path )
  553.         local logging = { }
  554.         for i = 1,#sky.log.data do
  555.             local n = false
  556.             for k = 1,#sky.log.exclude do
  557.                 if sky.log.data[i].type == sky.log.exclude[k] or reading then
  558.                     n = true
  559.                 end
  560.             end
  561.             if not n then
  562.                 table.insert( logging, sky.log.data[i] )
  563.             end
  564.         end
  565.         sky.log.data = logging
  566.         sky.file.save( path, sky.log.data, true )
  567.         return true
  568.     end;
  569.     add = function( type, ... )
  570.         local str = ""
  571.         for i = 1,#arg do
  572.             str = str.." "..tostring( arg[i] )
  573.         end
  574.         table.insert( sky.log.data, { type = type, data = ":"..str } )
  575.     end;
  576.     load = function( path )
  577.         local t = { }
  578.         t = file.read( path, true )
  579.         for i = 1,#t do
  580.             t[i] = split( t[i], ":" )
  581.         end
  582.         return t
  583.     end;
  584. }
Advertisement
Add Comment
Please, Sign In to add comment