Advertisement
CCCoder

Cobalt

Jun 27th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.61 KB | None | 0 0
  1. cobalt = {
  2.     graphics = { },
  3.     filesystem = { },
  4.     event = { },
  5.     window = { },
  6.     _mainloop = true,
  7.     vars = {
  8.         backgroundcolour = colours.black,
  9.         textcolour = colours.white
  10.     },
  11.     keyboard = {
  12.         keysdown = { },
  13.     },
  14.     rednet = {
  15.  
  16.     },
  17.     mouse = { },
  18.     table = { },
  19.     state = "MAIN",
  20.     version = "1.0",
  21.     updatespeed = 0.1
  22. }
  23.  
  24. cobalt.updatetimer = os.startTimer( cobalt.updatespeed )
  25.  
  26. function cobalt.debug( item )
  27.     term.setBackgroundColour( colours.black )
  28.     term.setTextColour( colours.white )
  29.     term.clear()
  30.     term.setCursorPos( 1,1 )
  31.     print( "COBALT DEBUG" )
  32.     print( item )
  33.     cobalt.exit()
  34. end
  35.  
  36.  
  37. args = { ... }
  38. if args[1] == "version" then
  39.     print( "Cobalt " .. cobalt.version )
  40. elseif args[1] == "help" then
  41.     print( "You can find help resources here: ")
  42.     print( "https://github.com/ebernerd/Cobalt/wiki")
  43. elseif args[1] == "examples" then
  44.     if fs.isDir( "cobalt-examples" ) then
  45.         if args[2] then
  46.             if fs.exists( "cobalt-examples/" .. args[2] ) then
  47.                 shell.run( "cobalt-examples/" .. args[2] )
  48.             else
  49.                 print( "List of Cobalt examples: ")
  50.                 for i, v in pairs( fs.list( "cobalt-examples" ) ) do
  51.                     print( "-" .. v )
  52.                 end
  53.             end
  54.         else
  55.             print( "List of Cobalt examples: ")
  56.             for i, v in pairs( fs.list( "cobalt-examples" ) ) do
  57.                 print( "-" .. v )
  58.             end
  59.         end
  60.     else
  61.         print( "Download the Cobalt examples here" )
  62.         print( "http://github.com/ebernerd")
  63.     end
  64. end
  65.  
  66. cobalt.g = cobalt.graphics
  67. cobalt.k = cobalt.keyboard
  68. cobalt.fs = cobalt.filesystem
  69. cobalt.e = cobalt.event
  70. cobalt.w = cobalt.window
  71. cobalt.m = cobalt.mouse
  72.  
  73. local graphics_lighten = {
  74.     [32768] = colours.grey,
  75.     [128] = colours.lightGrey,
  76.     [256] = colours.white,
  77.     [1] = colours.white,
  78.  
  79.     [2048] = colours.cyan,
  80.     [512] = colours.lightBlue,
  81.     [8] = colours.white,
  82.  
  83.     [8192] = colours.lime,
  84.     [32] = colours.white,
  85.  
  86.     [4096] = colours.orange,
  87.     [2] = colours.yellow,
  88.     [16] = colours.white,
  89.  
  90.     [16384] = colours.pink,
  91.     [64] = colours.white,
  92.  
  93.     [1024] = colours.magenta,
  94.     [4] = colours.white,
  95.  
  96. }
  97.  
  98. local graphics_darken = {
  99.  
  100. }
  101.  
  102. --[=[ GRAPHICS FUNCTIONS ]=]--
  103. function cobalt.graphics.print( text, x, y, colour )
  104.     term.setTextColour( colour or term.getTextColour() )
  105.     term.setCursorPos( x, y )
  106.     print( text )
  107. end
  108. function cobalt.graphics.write( text, x, y, colour )
  109.     term.setTextColour( colour or term.getTextColour() )
  110.     term.setCursorPos( x, y )
  111.     write( text )
  112. end
  113. function cobalt.graphics.rect( mode, x, y, w, h, colour )
  114.     if mode == "fill" then
  115.         paintutils.drawFilledBox( x, y, x+w, y+h, colour )
  116.     elseif mode == "line" then
  117.         paintutils.drawBox( x, y, x+w, y+h, colour )
  118.     end
  119. end
  120. function cobalt.graphics.center( text, y, offset )
  121.     text = text or "Some Text"
  122.     y = y or 1
  123.     offset = offset or 0
  124.     cobalt.graphics.print( text, (cobalt.window.getWidth()/2-#text/2)-offset, y )
  125. end
  126. function cobalt.graphics.centerInArea( text, x1, y1, x2, y2 )
  127.     text = text or "Some Text"
  128.     x1 = x1 or 1
  129.     x2 = x2 or cobalt.window.getWidth()
  130.     y1 = y1 or 1
  131.     y2 = y2 or cobalt.window.getHeight()
  132.     cobalt.graphics.print( text, (x1+x2)/2-#text/2, (y1+y2)/2 )
  133. end
  134.  
  135.  
  136. cobalt.graphics.rectangle = cobalt.graphics.rect
  137. cobalt.graphics.pixel = paintutils.drawPixel
  138. cobalt.graphics.line = paintutils.drawLine
  139.  
  140. --[=[function cobalt.graphics.setBackgroundColour( colour )
  141.     cobalt.vars.backgroundcolour = colour
  142. end--]=]
  143. cobalt.graphics.setBackgroundColour = term.setBackgroundColour
  144.  
  145. --[=[function cobalt.graphics.setColour( colour )
  146.     cobalt.vars.textcolour = colour
  147. end--]=]
  148. cobalt.graphics.setColour = term.setTextColour
  149.  
  150. function cobalt.graphics.lighten( colour )
  151.     return graphics_lighten[colour]
  152. end
  153.  
  154. function cobalt.graphics.darken( colour )
  155.     return graphics_darken[colour]
  156. end
  157.  
  158.  
  159. --[=[WINDOW FUNCTIONS]=]--
  160. function cobalt.window.getWidth()
  161.     local x, _ = term.getSize()
  162.     return x
  163. end
  164.  
  165. function cobalt.window.getHeight()
  166.     local _, y = term.getSize()
  167.     return y
  168. end
  169.  
  170.  
  171. --[=[KEYBOARD FUNCTIONS]=]--
  172. function cobalt._keypressed( key, keycode )
  173.     cobalt.keyboard.keysdown[key] = true
  174.     cobalt.keyboard.keysdown[keycode] = true
  175.  
  176.     if cobalt.keypressed then cobalt.keypressed( key, keycode ) end
  177. end
  178.  
  179. function cobalt._keyreleased( key, keycode )
  180.     cobalt.keyboard.keysdown[key] = nil
  181.     cobalt.keyboard.keysdown[keycode] = nil
  182.  
  183.     if cobalt.keyreleased then cobalt.keyreleased( key, keycode ) end
  184. end
  185.  
  186. function cobalt.keyboard.isDown( key )
  187.     if cobalt.keyboard.keysdown[key] then
  188.         return true
  189.     end
  190.     return false
  191. end
  192.  
  193.  
  194.  
  195. --[=[REDNET FUNCTIONS]=]--
  196. function cobalt.rednet.receive(time, ret)
  197.  
  198. end
  199.  
  200. --[=[STATE FUNCTIONS]=]--
  201. function cobalt.getState()
  202.     return cobalt.state
  203. end
  204.  
  205.  
  206. --[=[FILESYSTEM FUNCTIONS]=]--
  207. cobalt.filesystem.list = fs.list
  208. cobalt.filesystem.getDirectoryItems = fs.list
  209. cobalt.filesystem.isFile = fs.exists
  210. cobalt.filesystem.isDirectory = fs.isDir
  211. --cobalt.filesystem.currentDir = shell.dir()
  212.  
  213. cobalt.raw = false
  214.  
  215. function cobalt.exit()
  216.     cobalt._mainloop = false
  217.     term.setBackgroundColour(colours.black)
  218.     term.setTextColour( colours.white )
  219.     term.clear()
  220.     term.setCursorPos( 1, 1 )
  221.     return
  222. end
  223.  
  224. function cobalt._draw()
  225.     term.setBackgroundColour( cobalt.vars.backgroundcolour )
  226.     term.clear()
  227.     term.setCursorPos( 1, 1 )
  228.     term.setTextColour( cobalt.vars.textcolour )
  229.     cobalt.draw()
  230. end
  231.  
  232. function cobalt.initLoop( runOnce )
  233.     while cobalt._mainloop do
  234.  
  235.         local e, a, b, c, d, e
  236.         if cobalt.raw then
  237.             e, a, b, c, d, e = os.pullEventRaw()
  238.         else
  239.             e, a, b, c, d, e = os.pullEvent()
  240.         end
  241.         if e == "char" then
  242.             if cobalt.textinput then    cobalt.textinput( a ) end
  243.         elseif e == "key" then
  244.             cobalt._keypressed( a, keys.getName( a ) )
  245.         elseif e == "key_up" then
  246.             cobalt._keyreleased( a, keys.getName( a ) )
  247.         elseif e == "mouse_click" then
  248.             if cobalt.mousepressed then cobalt.mousepressed( b, c, a ) end --b, c, a to keep love.mousepressed syntax as( x, y, button ), not ( button, x, y )
  249.         elseif e == "mouse_up" then
  250.             if cobalt.mousereleased then cobalt.mousereleased( b, c, a ) end
  251.         elseif e == "rednet_message" then
  252.             if cobalt.rednetreceive then cobalt.rednetreceive( a, b, c ) end
  253.         elseif e == "monitor_touch" then
  254.             if cobalt.monitortouch then cobalt.monitortouch( x, y ) else cobalt.mousereleased( x, y ) end
  255.         elseif e == "mouse_drag" then
  256.             if cobalt.mousedrag then cobalt.mousedrag( x, y ) end
  257.         elseif e == "timer" then
  258.             if a == cobalt.updatetimer then
  259.                 if cobalt.update then cobalt.update( cobalt.updatespeed ) end
  260.                 cobalt.updatetimer = os.startTimer( cobalt.updatespeed )
  261.                 cobalt._draw()
  262.             end
  263.         end
  264.  
  265.  
  266.         if runOnce then cobalt._mainloop = false; return end
  267.     end
  268. end
  269.  
  270. function cobalt.initOnce()
  271.     cobalt.initLoop( true )
  272. end
  273.  
  274.  
  275. return cobalt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement