Advertisement
admicos

coolkit

Feb 24th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.52 KB | None | 0 0
  1. --- Coolkit is a OC and CC compatible toolkit to make cross-platform programs.
  2. -- @module coolkit
  3. -- @author admicos
  4. -- @copyright 2017+ admicos
  5. -- @release 0.0.0-beta
  6. -- @usage Add the following code at the top of your code:
  7. --local ck=nil if require then ck=require("coolkit") else ck=dofile("coolkit.lua") end
  8.  
  9. local coolkit = {
  10.     --- The current version of Coolkit
  11.     VERSION = "0.0.0",
  12.  
  13.     --- Is Coolkit processing events? (Set false to stop the loop)
  14.     -- @see coolkit.start
  15.     isRunning = true,
  16.  
  17.     --- The events that coolkit can handle
  18.     -- @field raw Fires when any event occurs. Allows you to register to events not handled by coolkit. Handling being cross-platform is up to you.
  19.     -- @field click Fires when the mouse is clicked
  20.     -- @field scroll Fires when the mouse is scrolled
  21.     -- @field keypress Fires when any key is pressed
  22.     -- @field keyrelease Fires when any key is released
  23.     -- @table events
  24.     -- @see coolkit.registerToEvent
  25.     events = {
  26.         --- Fires when any event occurs. Allows you to register to events not handled by coolkit. Handling being cross-platform is up to you.
  27.         -- @table events.raw
  28.         -- @tparam number btn The button clicked (1, 2 and 3 for left, right, and middle)
  29.         -- @tparam number x The x position of the cursor
  30.         -- @tparam number y The y position of the cursor
  31.         raw = {},
  32.  
  33.         --- Fires when the mouse is clicked
  34.         -- @table events.click
  35.         -- @tparam number btn The button clicked (1, 2 and 3 for left, right, and middle)
  36.         -- @tparam number x The x position of the cursor
  37.         -- @tparam number y The y position of the cursor
  38.         click = {},
  39.  
  40.         --- Fires when the mouse is scrolled
  41.         -- @table events.scroll
  42.         -- @tparam number dir The scroll direction (1 and -1 for up and down)
  43.         -- @tparam number x The x position of the cursor
  44.         -- @tparam number y The y position of the cursor
  45.         scroll = {},
  46.  
  47.         --- Fires when any key is pressed
  48.         -- @table events.keypress
  49.         -- @tparam number code The keycode
  50.         keypress = {},
  51.  
  52.         --- Fires when any key is released
  53.         -- @table events.keyrelease
  54.         -- @tparam number code The keycode
  55.         keyrelease = {},
  56.     },
  57.  
  58.     fs = {};
  59.     net = {};
  60.     screen = {};
  61.  
  62.     _internal = {},
  63. }
  64.  
  65. if require then
  66.     --- The system Coolkit is running under. Can be "OC" or "CC"
  67.     coolkit.SYSTEM = "OC"
  68.     coolkit._internal.oc = {
  69.         internet = require("component").internet,
  70.         event = require("event"),
  71.         term = require("term"),
  72.         gpu = {}
  73.     }
  74.     coolkit._internal.oc.gpu = coolkit._internal.oc.term.gpu()
  75.  
  76.     --- Colors to use with functions needing colors, can also be used with the native system functions
  77.     -- @see coolkit.screen.setBG
  78.     -- @see coolkit.screen.setFG
  79.     coolkit.screen.colors = {
  80.         white = 0xF0F0F0,
  81.         orange = 0xF2B233,
  82.         magenta = 0xE57FD8,
  83.         lightBlue = 0x99B2F2,
  84.         yellow = 0xDEDE6C,
  85.         lime = 0x7FCC19,
  86.         pink = 0xF2B2CC,
  87.         gray = 0x4C4C4C,
  88.         lightGray = 0x999999,
  89.         cyan = 0x4C99B2,
  90.         purple = 0xB266E5,
  91.         blue = 0x3366CC,
  92.         brown = 0x7F664C,
  93.         green = 0x57A64E,
  94.         red = 0xCC4C4C,
  95.         black = 0x191919,
  96.     }
  97.  
  98.     function coolkit._internal.ocProcessEvent(pulled)
  99.         coolkit._internal.callAll("raw", table.unpack(pulled))
  100.  
  101.         local eName = table.remove(pulled, 1);
  102.         if (eName == "key_down") then
  103.             coolkit._internal.callAll("keypress", pulled[3])
  104.         elseif (eName == "key_up") then
  105.             coolkit._internal.callAll("keyrelease", pulled[3])
  106.         elseif (eName == "touch") then
  107.             coolkit._internal.callAll("click", pulled[4] + 1, pulled[2], pulled[3])
  108.         elseif (eName == "scroll") then
  109.             coolkit._internal.callAll("scroll", pulled[4], pulled[2], pulled[3])
  110.         end
  111.     end
  112. else
  113.     coolkit.SYSTEM = "CC"
  114.     coolkit.screen.colors = _G.colors
  115.  
  116.     function coolkit._internal.ccProcessEvent(pulled)
  117.         coolkit._internal.callAll("raw", table.unpack(pulled))
  118.  
  119.         local eName = table.remove(pulled, 1);
  120.         if (eName == "key") then
  121.             coolkit._internal.callAll("keypress", pulled[1])
  122.         elseif (eName == "key_up") then
  123.             coolkit._internal.callAll("keyrelease", pulled[1])
  124.         elseif (eName == "mouse_click") then
  125.             coolkit._internal.callAll("click", table.unpack(pulled))
  126.         elseif (eName == "mouse_scroll") then
  127.             coolkit._internal.callAll("scroll", -pulled[1], pulled[2], pulled[3])
  128.         end
  129.     end
  130.  
  131. end
  132.  
  133. --- Runs the function <code>func</code> when event <code>e</code> occurs. The function will receive the event arguments individually.
  134. -- @tparam string event The event to register to.
  135. -- @tparam function func The function to register.
  136. -- @see coolkit.events
  137. -- @see coolkit.start
  138. -- @usage ck.registerToEvent("click", function(dir, x, y)
  139. --    print("clicked " .. x .. "x" .. y)
  140. --end)
  141. -- ck.start()
  142. function coolkit.registerToEvent(event, func)
  143.     coolkit.events[event][#coolkit.events[event] + 1] = func
  144. end
  145.  
  146. function coolkit._internal.callAll(eName, ...)
  147.     for _, eFunc in ipairs(coolkit.events[eName]) do
  148.         eFunc(table.unpack({ ... }))
  149.     end
  150. end
  151.  
  152. --- Sets the cursor position on screen.
  153. -- @tparam number x The new x coordinate of the cursor
  154. -- @tparam number y The new y coordinate of the cursor
  155. -- @usage ck.screen.setPos(5, 5)
  156. --print("hello, world!")
  157. function coolkit.screen.setPos(x, y)
  158.     if coolkit.SYSTEM == "OC" then
  159.         coolkit._internal.oc.term.setCursor(x, y)
  160.     else
  161.         term.setCursorPos(x, y)
  162.     end
  163. end
  164.  
  165. --- Sets the foreground color of screen.
  166. -- @tparam coolkit.screen.color color The new foreground color
  167. -- @see coolkit.screen.setBG
  168. -- @usage ck.screen.setFG(ck.screen.colors.red)
  169. --print("hello, world!")
  170. function coolkit.screen.setFG(color)
  171.     if coolkit.SYSTEM == "OC" then
  172.         coolkit._internal.oc.gpu.setForeground(color)
  173.     else
  174.         term.setTextColor(color)
  175.     end
  176. end
  177.  
  178. --- Sets the background color of screen.
  179. -- @tparam coolkit.screen.color color The new background color
  180. -- @see coolkit.screen.setFG
  181. function coolkit.screen.setBG(color)
  182.     if coolkit.SYSTEM == "OC" then
  183.         coolkit._internal.oc.gpu.setBackground(color)
  184.     else
  185.         term.setBackgroundColor(color)
  186.     end
  187. end
  188.  
  189. --- Writes a string to the screen, without going to the new line.
  190. -- @tparam string str String to write.
  191. function coolkit.screen.write(str)
  192.     if coolkit.SYSTEM == "OC" then
  193.         coolkit._internal.oc.term.write(str)
  194.     else
  195.         write(str)
  196.     end
  197. end
  198.  
  199. function coolkit._internal.ccSendGET(url, headers)
  200.     if not http then error("ck: CC internet is not enabled") end
  201.     if not http.checkURL(url) then error("ck: couldn't find url '" .. url .. "' in CC whitelist.") end
  202.  
  203.     local h = http.get(url, headers)
  204.     local ret = {
  205.         code = h.getResponseCode(),
  206.         headers = {},
  207.         message = h.readAll(),
  208.     }
  209.     h.close()
  210.  
  211.     return ret
  212. end
  213.  
  214. function coolkit._internal.ccSendPOST(url, postData, headers)
  215.     if not http then error("ck: CC internet is not enabled") end
  216.     if not http.checkURL(url) then error("ck: couldn't find url '" .. url .. "' in CC whitelist.") end
  217.  
  218.     local h = http.post(url, postData, headers)
  219.     local ret = {
  220.         code = h.getResponseCode(),
  221.         headers = {},
  222.         message = h.readAll(),
  223.     }
  224.     h.close()
  225.  
  226.     return ret
  227. end
  228.  
  229. function coolkit._internal.ocSendNet(url, postData, headers)
  230.     if not coolkit._internal.oc.internet then error("ck: OC no internet card") end
  231.     if not coolkit._internal.oc.internet.isHttpEnabled() then error("ck: OC internet is not enabled") end
  232.  
  233.     local h = coolkit._internal.oc.internet.request(url, postData, headers)
  234.     local rc, _, _rh = h.response()
  235.     local rm = ""
  236.     while true do
  237.         local data = h.read()
  238.         if not data then break
  239.         elseif #data > 0 then rm = rm .. data end
  240.     end
  241.  
  242.     local rh = {}
  243.     for k, v in pairs(_rh) do
  244.         rh[k] = tostring(v[1])
  245.     end
  246.  
  247.     local ret = {
  248.         code = rc,
  249.         headers = rh,
  250.         message = rm,
  251.     }
  252.     h.close()
  253.  
  254.     return ret
  255. end
  256.  
  257. --- Send a GET request to <code>url</code> with optional <code>headers</code>.
  258. -- @tparam string url URL to send the request to
  259. -- @tparam table headers [OPTIONAL] Headers to send with the request.
  260. -- @see coolkit.net.sendPOST
  261. -- @return Response as a table. Contains three keys: <code>code</code>, <code>headers</code> and <code>message</code><br>
  262. --<ul><li><code>code</code>: Returned HTTP status code<br></li>
  263. --<li><code>headers</code>: [EMPTY IN CC] Returned headers from the request.<br></li>
  264. --<li><code>message</code>: Contents of the request.</li></ul>
  265. function coolkit.net.sendGET(url, headers)
  266.     if coolkit.SYSTEM == "OC" then
  267.         return coolkit._internal.ocSendNet(url, nil, headers)
  268.     else
  269.         return coolkit._internal.ccSendGET(url, headers)
  270.     end
  271. end
  272.  
  273. --- Send a POST request to <code>url</code> with <code>data</code> and optional <code>headers</code>.
  274. -- @tparam string url URL to send the request to
  275. -- @tparam table form table of POST data to send with the request.
  276. -- @tparam table headers [OPTIONAL] Table of headers to send with the request.
  277. -- @see coolkit.net.sendGET
  278. -- @return Response as a table. Contains three keys: <code>code</code>, <code>headers</code> and <code>message</code><br>
  279. --<ul><li><code>code</code>: Returned HTTP status code<br></li>
  280. --<li><code>headers</code>: [EMPTY IN CC] Returned headers from the request.<br></li>
  281. --<li><code>message</code>: Contents of the request.</li></ul>
  282. function coolkit.net.sendPOST(url, form, headers)
  283.     if coolkit.SYSTEM == "OC" then
  284.         return coolkit._internal.ocSendNet(url, form, headers)
  285.     else
  286.         local ret = ""
  287.         for k, v in pairs(t) do
  288.            ret = ret .. k .. "=" .. tostring(v) .. "&"
  289.         end
  290.  
  291.         return coolkit._internal.ccSendPOST(url, ret:sub(1, #ret - 1), headers)
  292.     end
  293. end
  294.  
  295. --- Starts the main loop of your program. Run this after all event registering is done.
  296. -- @see coolkit.registerToEvent
  297. -- @see coolkit.isRunning
  298. function coolkit.start()
  299.     while coolkit.isRunning do
  300.         if coolkit.SYSTEM == "OC" then
  301.             coolkit._internal.ocProcessEvent{coolkit._internal.oc.event.pull()}
  302.         else
  303.             coolkit._internal.ccProcessEvent{os.pullEvent()}
  304.         end
  305.     end
  306. end
  307.  
  308. return coolkit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement