Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local gpu = require("component").gpu
- local unicode = require("unicode")
- local event = require("event")
- local GPAPI = { }
- local t_touch = { }
- local t_key_down = { }
- local track_touch
- track_touch = function(_, _, x, y, button, nick)
- t_touch.x = x
- t_touch.y = y
- t_touch.button = button
- t_touch.nick = nick
- end
- t_key_down = function(_, _, char, code, nick)
- t_key_down.char = char
- t_key_down.code = code
- t_key_down.nick = nick
- end
- do
- local _class_0
- local _base_0 = {
- setValue = function(self, value)
- if value == nil then
- value = "Simple Text"
- end
- self.value = value
- end,
- setPos = function(self, x, y)
- if x == nil then
- x = self.x
- end
- if y == nil then
- y = self.y
- end
- if x == "auto" then
- local w, _ = gpu.getResolution()
- self.x = w / 2 - unicode.len(self.value) / 2
- else
- self.x = x
- self.y = y
- end
- end,
- setColors = function(self, back, fore)
- if back == nil then
- back = self.back
- end
- if fore == nil then
- fore = self.back
- end
- self.back = back
- self.fore = fore
- end,
- draw = function(self)
- local oldb = gpu.getBackground()
- local oldf = gpu.getForeground()
- gpu.setBackground(self.back)
- gpu.setForeground(self.fore)
- gpu.set(self.x, self.y, self.value)
- gpu.setBackground(oldb)
- return gpu.setForeground(oldf)
- end
- }
- _base_0.__index = _base_0
- _class_0 = setmetatable({
- __init = function(self)
- self.x = 1
- self.y = 1
- self.value = "Simple Text"
- self.back = gpu.getBackground()
- self.fore = gpu.getForeground()
- end,
- __base = _base_0,
- __name = "Textbox"
- }, {
- __index = _base_0,
- __call = function(cls, ...)
- local _self_0 = setmetatable({}, _base_0)
- cls.__init(_self_0, ...)
- return _self_0
- end
- })
- _base_0.__class = _class_0
- GPAPI.Textbox = _class_0
- end
- do
- local _class_0
- local _base_0 = {
- setPos = function(self, x, y)
- if x == nil then
- x = self.x
- end
- if y == nil then
- y = self.y
- end
- self.x = x
- self.y = y
- end,
- setSize = function(self, width, height)
- if width == nil then
- width = self.width
- end
- if height == nil then
- height = self.height
- end
- self.width = width
- self.height = height
- end,
- setColors = function(self, back, fore)
- if back == nil then
- back = self.back
- end
- if fore == nil then
- fore = self.fore
- end
- self.back = back
- self.fore = fore
- end,
- draw = function(self, value)
- local oldb = gpu.getBackground()
- local oldf = gpu.getForeground()
- gpu.setBackground(self.back)
- gpu.setForeground(self.fore)
- gpu.fill(self.x, self.y, self.width / 100 * value, self.height, " ")
- gpu.setBackground(oldb)
- return gpu.setForeground(oldf)
- end
- }
- _base_0.__index = _base_0
- _class_0 = setmetatable({
- __init = function(self)
- self.x = 1
- self.y = 1
- self.width = 15
- self.height = 3
- self.back = gpu.getBackground()
- self.fore = gpu.getForeground()
- end,
- __base = _base_0,
- __name = "LoadBar"
- }, {
- __index = _base_0,
- __call = function(cls, ...)
- local _self_0 = setmetatable({}, _base_0)
- cls.__init(_self_0, ...)
- return _self_0
- end
- })
- _base_0.__class = _class_0
- GPAPI.LoadBar = _class_0
- end
- do
- local _class_0
- local _base_0 = {
- setPos = function(self, x, y)
- if x == nil then
- x = self.x
- end
- if y == nil then
- y = self.y
- end
- self.x = x
- self.y = y
- if x == "auto" then
- local w, _ = gpu.setResolution()
- self.x = w / 2 - self.width / 2
- end
- end,
- setValue = function(self, value)
- if value == nil then
- value = self.value
- end
- self.value = value
- end,
- setSize = function(self, width, height)
- if width == nil then
- width = self.width
- end
- if height == nil then
- height = self.height
- end
- self.width = width
- self.height = height
- end,
- setColors = function(self, back, fore)
- if back == nil then
- back = self.back
- end
- if fore == nil then
- fore = self.back
- end
- self.back = back
- self.fore = fore
- end,
- draw = function(self)
- local oldb = gpu.getBackground()
- local oldf = gpu.getForeground()
- gpu.setBackground(self.back)
- gpu.setForeground(self.fore)
- gpu.fill(self.x, self.y, self.width, self.height, " ")
- gpu.set(self.x + 1, self.y + self.height / 2, self.value)
- return self:begin()
- end,
- begin = function(self)
- event.listen("touch", track_touch)
- if t_touch.x ~= nil then
- if t_touch.x >= self.x and t_touch < self.x + self.width then
- if t_touch.y >= self.y and t_touch < self.y + self.height then
- event.listen("key_down", track_key_down)
- 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
- self.enterData = self.enterData + unicode.char(t_key_down.char)
- gpu.set(self.x + 1, self.y + self.height / 2, self.enterData)
- end
- if t_key_down.char == 8 and t_key_down.code == 14 then
- self.enterData = unicode.sub(self.enterData, 1, unicode.len(self.enterData) - 2)
- end
- if t_key_down.char == 13 then
- event.ignore("touch", track_touch)
- return event.ignore("key_down", track_key_down)
- end
- else
- event.ignore("touch", track_touch)
- return event.ignore("key_down", track_key_down)
- end
- end
- end
- end,
- returnData = function(self)
- return self.enterData
- end
- }
- _base_0.__index = _base_0
- _class_0 = setmetatable({
- __init = function(self)
- self.x = 1
- self.y = 1
- self.width = 15
- self.height = 3
- self.back = gpu.getBackground()
- self.fore = gpu.getForeground()
- self.enterData = ""
- self.value = "Simple Text"
- end,
- __base = _base_0,
- __name = "EnterData"
- }, {
- __index = _base_0,
- __call = function(cls, ...)
- local _self_0 = setmetatable({}, _base_0)
- cls.__init(_self_0, ...)
- return _self_0
- end
- })
- _base_0.__class = _class_0
- GPAPI.EnterData = _class_0
- end
- GPAPI.clear = function()
- local w, h = gpu.getResolution()
- gpu.fill(1, 1, w, h, " ")
- end
- GPAPI.setColors = function(back, fore)
- if back == nil then
- back = gpu.getBackground()
- end
- if fore == nil then
- fore = gpu.getForeground()
- end
- gpu.setBackground(back)
- gpu.setForeground(fore)
- end
- GPAPI.line = function(x0, y0, x1, y1, back)
- if back == nil then
- back = gpu.getForeground()
- end
- local oldb = gpu.getBackground()
- gpu.setBackground(back)
- local deltax = math.abs(x1 - x0)
- local deltay = math.abs(y1 - y0)
- local err = 0
- local deltaerr = deltay
- local y = y0
- for x = 1, x1 do
- gpu.set(x, y, " ")
- err = err + deltaerr
- if 2 * err >= deltax then
- y = y - 1
- err = err - deltax
- end
- end
- gpu.setBackground(oldb)
- end
- GPAPI.circle = function(x1, y1, r, back)
- if back == nil then
- back = gpu.getForeground()
- end
- local oldb = gpu.getBackground()
- gpu.setBackground(back)
- local x = 0
- local y = r
- local delta = 1 - 2 * r
- local err = 0
- while y >= 0 do
- local _continue_0 = false
- repeat
- gpu.set(x1 + x, y1 + y, " ")
- gpu.set(x1 + x, y1 - y, " ")
- gpu.set(x1 - x, y1 + y, " ")
- gpu.set(x1 - x, y1 - y, " ")
- err = 2 * (delta + y) - 1
- if delta < 0 and err <= 0 then
- x = x + 1
- delta = delta + (2 * x + 1)
- _continue_0 = true
- break
- end
- err = 2 * (delta - x) - 1
- if delta > 0 and err > 0 then
- y = y - 1
- delta = delta + (1 - 2 * y)
- _continue_0 = true
- break
- end
- x = x + 1
- delta = delta + (2 * (x - y))
- y = y - 1
- _continue_0 = true
- until true
- if not _continue_0 then
- break
- end
- end
- gpu.setBackground(oldb)
- end
- return GPAPI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement