Advertisement
Good_Pudge

GPAPI (NEW)

Dec 3rd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.45 KB | None | 0 0
  1. local gpu = require("component").gpu
  2. local unicode = require("unicode")
  3. local event = require("event")
  4. local GPAPI = { }
  5. local t_touch = { }
  6. local t_key_down = { }
  7. local track_touch
  8. track_touch = function(_, _, x, y, button, nick)
  9.   t_touch.x = x
  10.   t_touch.y = y
  11.   t_touch.button = button
  12.   t_touch.nick = nick
  13. end
  14. t_key_down = function(_, _, char, code, nick)
  15.   t_key_down.char = char
  16.   t_key_down.code = code
  17.   t_key_down.nick = nick
  18. end
  19. do
  20.   local _class_0
  21.   local _base_0 = {
  22.     setValue = function(self, value)
  23.       if value == nil then
  24.         value = "Simple Text"
  25.       end
  26.       self.value = value
  27.     end,
  28.     setPos = function(self, x, y)
  29.       if x == nil then
  30.         x = self.x
  31.       end
  32.       if y == nil then
  33.         y = self.y
  34.       end
  35.       if x == "auto" then
  36.         local w, _ = gpu.getResolution()
  37.         self.x = w / 2 - unicode.len(self.value) / 2
  38.       else
  39.         self.x = x
  40.         self.y = y
  41.       end
  42.     end,
  43.     setColors = function(self, back, fore)
  44.       if back == nil then
  45.         back = self.back
  46.       end
  47.       if fore == nil then
  48.         fore = self.back
  49.       end
  50.       self.back = back
  51.       self.fore = fore
  52.     end,
  53.     draw = function(self)
  54.       local oldb = gpu.getBackground()
  55.       local oldf = gpu.getForeground()
  56.       gpu.setBackground(self.back)
  57.       gpu.setForeground(self.fore)
  58.       gpu.set(self.x, self.y, self.value)
  59.       gpu.setBackground(oldb)
  60.       return gpu.setForeground(oldf)
  61.     end
  62.   }
  63.   _base_0.__index = _base_0
  64.   _class_0 = setmetatable({
  65.     __init = function(self)
  66.       self.x = 1
  67.       self.y = 1
  68.       self.value = "Simple Text"
  69.       self.back = gpu.getBackground()
  70.       self.fore = gpu.getForeground()
  71.     end,
  72.     __base = _base_0,
  73.     __name = "Textbox"
  74.   }, {
  75.     __index = _base_0,
  76.     __call = function(cls, ...)
  77.       local _self_0 = setmetatable({}, _base_0)
  78.       cls.__init(_self_0, ...)
  79.       return _self_0
  80.     end
  81.   })
  82.   _base_0.__class = _class_0
  83.   GPAPI.Textbox = _class_0
  84. end
  85. do
  86.   local _class_0
  87.   local _base_0 = {
  88.     setPos = function(self, x, y)
  89.       if x == nil then
  90.         x = self.x
  91.       end
  92.       if y == nil then
  93.         y = self.y
  94.       end
  95.       self.x = x
  96.       self.y = y
  97.     end,
  98.     setSize = function(self, width, height)
  99.       if width == nil then
  100.         width = self.width
  101.       end
  102.       if height == nil then
  103.         height = self.height
  104.       end
  105.       self.width = width
  106.       self.height = height
  107.     end,
  108.     setColors = function(self, back, fore)
  109.       if back == nil then
  110.         back = self.back
  111.       end
  112.       if fore == nil then
  113.         fore = self.fore
  114.       end
  115.       self.back = back
  116.       self.fore = fore
  117.     end,
  118.     draw = function(self, value)
  119.       local oldb = gpu.getBackground()
  120.       local oldf = gpu.getForeground()
  121.       gpu.setBackground(self.back)
  122.       gpu.setForeground(self.fore)
  123.       gpu.fill(self.x, self.y, self.width / 100 * value, self.height, " ")
  124.       gpu.setBackground(oldb)
  125.       return gpu.setForeground(oldf)
  126.     end
  127.   }
  128.   _base_0.__index = _base_0
  129.   _class_0 = setmetatable({
  130.     __init = function(self)
  131.       self.x = 1
  132.       self.y = 1
  133.       self.width = 15
  134.       self.height = 3
  135.       self.back = gpu.getBackground()
  136.       self.fore = gpu.getForeground()
  137.     end,
  138.     __base = _base_0,
  139.     __name = "LoadBar"
  140.   }, {
  141.     __index = _base_0,
  142.     __call = function(cls, ...)
  143.       local _self_0 = setmetatable({}, _base_0)
  144.       cls.__init(_self_0, ...)
  145.       return _self_0
  146.     end
  147.   })
  148.   _base_0.__class = _class_0
  149.   GPAPI.LoadBar = _class_0
  150. end
  151. do
  152.   local _class_0
  153.   local _base_0 = {
  154.     setPos = function(self, x, y)
  155.       if x == nil then
  156.         x = self.x
  157.       end
  158.       if y == nil then
  159.         y = self.y
  160.       end
  161.       self.x = x
  162.       self.y = y
  163.       if x == "auto" then
  164.         local w, _ = gpu.setResolution()
  165.         self.x = w / 2 - self.width / 2
  166.       end
  167.     end,
  168.     setValue = function(self, value)
  169.       if value == nil then
  170.         value = self.value
  171.       end
  172.       self.value = value
  173.     end,
  174.     setSize = function(self, width, height)
  175.       if width == nil then
  176.         width = self.width
  177.       end
  178.       if height == nil then
  179.         height = self.height
  180.       end
  181.       self.width = width
  182.       self.height = height
  183.     end,
  184.     setColors = function(self, back, fore)
  185.       if back == nil then
  186.         back = self.back
  187.       end
  188.       if fore == nil then
  189.         fore = self.back
  190.       end
  191.       self.back = back
  192.       self.fore = fore
  193.     end,
  194.     draw = function(self)
  195.       local oldb = gpu.getBackground()
  196.       local oldf = gpu.getForeground()
  197.       gpu.setBackground(self.back)
  198.       gpu.setForeground(self.fore)
  199.       gpu.fill(self.x, self.y, self.width, self.height, " ")
  200.       gpu.set(self.x + 1, self.y + self.height / 2, self.value)
  201.       return self:begin()
  202.     end,
  203.     begin = function(self)
  204.       event.listen("touch", track_touch)
  205.       if t_touch.x ~= nil then
  206.         if t_touch.x >= self.x and t_touch < self.x + self.width then
  207.           if t_touch.y >= self.y and t_touch < self.y + self.height then
  208.             event.listen("key_down", track_key_down)
  209.             if t_key_down.char ~= 0 and t_key_down.char ~= 8 and t_key_down.char ~= 9 and t_key_down.char ~= 13 and t_key_down.char ~= 14 then
  210.               self.enterData = self.enterData + unicode.char(t_key_down.char)
  211.               gpu.set(self.x + 1, self.y + self.height / 2, self.enterData)
  212.             end
  213.             if t_key_down.char == 8 and t_key_down.code == 14 then
  214.               self.enterData = unicode.sub(self.enterData, 1, unicode.len(self.enterData) - 2)
  215.             end
  216.             if t_key_down.char == 13 then
  217.               event.ignore("touch", track_touch)
  218.               return event.ignore("key_down", track_key_down)
  219.             end
  220.           else
  221.             event.ignore("touch", track_touch)
  222.             return event.ignore("key_down", track_key_down)
  223.           end
  224.         end
  225.       end
  226.     end,
  227.     returnData = function(self)
  228.       return self.enterData
  229.     end
  230.   }
  231.   _base_0.__index = _base_0
  232.   _class_0 = setmetatable({
  233.     __init = function(self)
  234.       self.x = 1
  235.       self.y = 1
  236.       self.width = 15
  237.       self.height = 3
  238.       self.back = gpu.getBackground()
  239.       self.fore = gpu.getForeground()
  240.       self.enterData = ""
  241.       self.value = "Simple Text"
  242.     end,
  243.     __base = _base_0,
  244.     __name = "EnterData"
  245.   }, {
  246.     __index = _base_0,
  247.     __call = function(cls, ...)
  248.       local _self_0 = setmetatable({}, _base_0)
  249.       cls.__init(_self_0, ...)
  250.       return _self_0
  251.     end
  252.   })
  253.   _base_0.__class = _class_0
  254.   GPAPI.EnterData = _class_0
  255. end
  256. GPAPI.clear = function()
  257.   local w, h = gpu.getResolution()
  258.   gpu.fill(1, 1, w, h, " ")
  259. end
  260. GPAPI.setColors = function(back, fore)
  261.   if back == nil then
  262.     back = gpu.getBackground()
  263.   end
  264.   if fore == nil then
  265.     fore = gpu.getForeground()
  266.   end
  267.   gpu.setBackground(back)
  268.   gpu.setForeground(fore)
  269. end
  270. GPAPI.line = function(x0, y0, x1, y1, back)
  271.   if back == nil then
  272.     back = gpu.getForeground()
  273.   end
  274.   local oldb = gpu.getBackground()
  275.   gpu.setBackground(back)
  276.   local deltax = math.abs(x1 - x0)
  277.   local deltay = math.abs(y1 - y0)
  278.   local err = 0
  279.   local deltaerr = deltay
  280.   local y = y0
  281.   for x = 1, x1 do
  282.     gpu.set(x, y, " ")
  283.     err = err + deltaerr
  284.     if 2 * err >= deltax then
  285.       y = y - 1
  286.       err = err - deltax
  287.     end
  288.   end
  289.   gpu.setBackground(oldb)
  290. end
  291. GPAPI.circle = function(x1, y1, r, back)
  292.   if back == nil then
  293.     back = gpu.getForeground()
  294.   end
  295.   local oldb = gpu.getBackground()
  296.   gpu.setBackground(back)
  297.   local x = 0
  298.   local y = r
  299.   local delta = 1 - 2 * r
  300.   local err = 0
  301.   while y >= 0 do
  302.     local _continue_0 = false
  303.     repeat
  304.       gpu.set(x1 + x, y1 + y, " ")
  305.       gpu.set(x1 + x, y1 - y, " ")
  306.       gpu.set(x1 - x, y1 + y, " ")
  307.       gpu.set(x1 - x, y1 - y, " ")
  308.       err = 2 * (delta + y) - 1
  309.       if delta < 0 and err <= 0 then
  310.         x = x + 1
  311.         delta = delta + (2 * x + 1)
  312.         _continue_0 = true
  313.         break
  314.       end
  315.       err = 2 * (delta - x) - 1
  316.       if delta > 0 and err > 0 then
  317.         y = y - 1
  318.         delta = delta + (1 - 2 * y)
  319.         _continue_0 = true
  320.         break
  321.       end
  322.       x = x + 1
  323.       delta = delta + (2 * (x - y))
  324.       y = y - 1
  325.       _continue_0 = true
  326.     until true
  327.     if not _continue_0 then
  328.       break
  329.     end
  330.   end
  331.   gpu.setBackground(oldb)
  332. end
  333. return GPAPI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement