Advertisement
Good_Pudge

GPAPI

Sep 8th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. local com = require("component")
  2. local unicode = require("unicode")
  3. local color = require("colors")
  4. local term = require("term")
  5. local gpu = com.gpu
  6. local seri = require("serialization")
  7.  
  8. local W,H = gpu.getResolution()
  9.  
  10. local GPAPI =
  11. {
  12.     colors =
  13.     {
  14.         white = 0xFFFFFF,
  15.         orange = 0xFEA500,
  16.         magenta = 0xFF00FF,
  17.         lightblue = 0xABDAE4,
  18.         yellow = 0xFFFF00,
  19.         lime = 0x06FC05,
  20.         pink = 0xFBC2CB,
  21.         gray = 0x808080,
  22.         silver = 0xC0C0C0,
  23.         cyan = 0x01FFFD,
  24.         purple = 0x880087,
  25.         blue = 0x0000FE,
  26.         brown = 0xA52B2A,
  27.         green = 0x008001,
  28.         red = 0xFD0200,
  29.         black = 0x000000,
  30.     }
  31. }
  32.  
  33. function cnsColor(colorB,colorF,hex)
  34.     if hex == nil then
  35.         if colorB ~= nil then
  36.             gpu.setBackground(colorB)
  37.         end
  38.  
  39.         if colorF ~= nil then
  40.             gpu.setForeground(colorF)
  41.         end
  42.     else
  43.         if colorB ~= nil then
  44.             gpu.setBackground(colorB,true)
  45.         end
  46.  
  47.         if colorF ~= nil then
  48.             gpu.setForeground(colorF,true)
  49.         end
  50.     end
  51. end
  52.  
  53. function GPAPI.textbox(x,y,text,typeT,colorBT,colorFT,hexT)
  54.     local OldB = gpu.getBackground()
  55.     local OldF = gpu.getForeground()
  56.  
  57.     cnsColor(colorBT,colorFT,hexT)
  58.  
  59.     if typeT ~= nil then
  60.         if typeT == "center" then
  61.             x = W/2 - unicode.len(text)/2
  62.         elseif typeT == "left" then
  63.             x = 5
  64.         elseif typeT == "right" then
  65.             x = W - 5 - unicode.len(text)/2
  66.         end
  67.     end
  68.  
  69.     if type(text) ~= "table" or type(text) ~= "function" then
  70.         gpu.set(x,y,tostring(text))
  71.     else
  72.         error("function or table expected")
  73.  
  74.     gpu.setBackground(OldB)
  75.     gpu.setForeground(OldF)
  76. end
  77.  
  78. function GPAPI.draw(x,y,weight,height,colorBD,colorFD,hexD,typeD)
  79.     local OldB = gpu.getBackground()
  80.     local OldF = gpu.getForeground()
  81.  
  82.  
  83.     cnsColor(colorBD,colorFD,hexD)
  84.  
  85.     if typeD ~= nil then
  86.         gpu.fill(x,y,weight,height," ")
  87.         gpu.fill(x,y,weight/160*typeD,height," ")
  88.  
  89.         if weight/160*typeD > weight then
  90.             gpu.fill(x+weight,y,weight,height," ")
  91.         end
  92.     end
  93.  
  94.     gpu.fill(x,y,weight,height," ")
  95.  
  96.     gpu.setBackground(OldB)
  97.     gpu.setForeground(OldF)
  98. end
  99.  
  100. function GPAPI.button(sX,sY,x,y,weight,height,func,...)
  101.     if sX >= x and sX < x + weight then
  102.         if sY >= y and sY < y + height then
  103.             DO = coroutine.create(func)
  104.             coroutine.resume(DO,...)
  105.         end
  106.     end
  107. end
  108.    
  109. function GPAPI.code(text)
  110.     local byted = {}
  111.  
  112.     for i = 1,#text do
  113.         byted[i] = string.reverse(string.byte(text,i,i))
  114.     end
  115.  
  116.     sbyted = string.reverse(seri.serialize(byted))
  117.  
  118.     return sbyted
  119. end
  120.  
  121. function GPAPI.uncode(sbyted)
  122.     local sbyted = seri.unserialize(string.reverse(sbyted))
  123.     local text = ""
  124.  
  125.     for i = 1,#sbyted do
  126.         sbyted[i] = string.char(string.reverse(sbyted[i]))
  127.     end
  128.  
  129.     for i = 1,#sbyted do
  130.         text = text..sbyted[i]
  131.     end
  132.  
  133.     return text
  134. end
  135.  
  136. function GPAPI.split(str, separator)
  137.     local separator, fields, start, i = separator or ",", {}, 1, 1
  138.     while true do
  139.         i = str:find(separator, start, true)
  140.         if i then
  141.             fields[#fields + 1] = str:sub(start, i - 1)
  142.             start = i + #separator
  143.         elseif start < #str then
  144.             fields[#fields + 1] = str:sub(start)
  145.             break
  146.         else
  147.             break
  148.         end
  149.     end
  150.     return fields
  151. end
  152.  
  153. function GPAPI.exit(OldB,OldF,W,H)
  154.     gpu.setBackground(OldB,true)
  155.     gpu.setForeground(OldF,true)
  156.     gpu.setResolution(W,H)
  157.     term.clear()
  158.     os.exit()
  159. end
  160.  
  161. function GPAPI.write(path,value,mode)
  162.     local file
  163.     if mode == nil then
  164.         file = io.open(path,"a")
  165.     else
  166.         file = io.open(path,mode)
  167.     end
  168.  
  169.     file:write(value)
  170.     file:close()
  171. end
  172.  
  173. function GPAPI.read(path)
  174.     local file = io.open(path,"r")
  175.     local value = file:read("*a")
  176.     file:close()
  177.     return value
  178. end
  179.  
  180. function GPAPI.time()
  181.     Time = os.date("*t")
  182.     TimeF = "["..Time["hour"]..":"..Time["min"]..":"..Time["sec"].."_"..Time["day"].."."..Time["month"].."."..Time["year"].."]"
  183.     return TimeF
  184. end
  185.  
  186. --[[GPAPI.signals =
  187. {
  188.     "component_added" = {address,componentType},
  189.     "component_removed" = {address,componentType},
  190.     "component_available" = componentType,
  191.     "component_unavailable" = componentType,
  192.     "term_available",
  193.     "term_unavailable",
  194.     "screen_resize" = {address,width,height},
  195.     "touch" = {address,x,y,button,nick},
  196.     "drag" = signals["touch"],
  197.     "drop" = signals["touch"],
  198.     "scroll" = {address,x,y,direction,nick},
  199.     "walk" = {address,x,y,nick},
  200.     "key_down" = {address,char,code,nick}, 
  201.     "key_up" = {address,char,code,nick},
  202.     "clipboard" = {address,value,nick},
  203.     "redstone_changed" = {address,side},
  204.     "modem_message" = {recieverAddress,senderAddress,port,distance,message},
  205.     "inventory_changed" = slot,
  206.     "motion" = {address,x,y,z,nick},
  207. }]]
  208.  
  209. return GPAPI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement