Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local gpu = require("component").gpu
- local event = require("event")
- local objects = {}
- local function object.new(x, y, w, h,visible,shouldAutoUpdate)
- local obj = {
- type = "object",
- x = x,
- y = y,
- w = w,
- h = h,
- visible = visible,
- shouldAutoUpdate = shouldAutoUpdate,
- update = function() obj.drawNextFrame = true end,
- _drawNextFrame = false
- }
- return obj
- end
- local function button(x,y,w,h,label,color,textColor,shouldAutoUpdate,callback)
- local obj = object.new(x,y,w,h,true,shouldAutoUpdate)
- obj.label = label
- obj.color = color
- obj.textColor = textColor
- obj.callback = callback
- table.insert(objects,obj)
- return #objects
- end
- local function frame(x,y,w,h,color,textColor,text,shouldAutoUpdate)
- local obj = object.new(x,y,w,h,true,shouldAutoUpdate)
- obj.type = "frame"
- obj.color = color
- obj.textColor = textColor
- obj.text = text
- table.insert(objects, obj)
- return #objects
- end
- local function draw(bg)
- bg = bg or 0x333333
- gpu.setBackground(bg)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(1,1,resx,resy," ")
- for i,v in ipairs(objects) do
- local object = v
- if object.shouldAutoUpdate or object._drawNextFrame then
- object._drawNextFrame = false
- if object.type == "button" and object.visible == true then
- gpu.setBackground(object.color)
- gpu.setForeground(object.textColor)
- gpu.fill(object.x,object.y,object.w,object.h," ")
- gpu.set(object.x+math.floor(object.w/2)-math.floor(#object.label/2),object.y+math.floor(object.h/2),object.label)
- elseif object.type == "frame" and object.visible == true then
- gpu.setBackground(object.color)
- gpu.setForeground(object.textColor)
- gpu.fill(object.x,object.y,object.w,object.h," ")
- gpu.set(object.x+math.floor(object.w/2)-math.floor(#object.text/2),object.y+math.floor(object.h/2),object.text)
- end
- end
- end
- end
- event.listen("touch",function(_,_,x,y)
- for i,v in ipairs(objects) do
- if v.type == "button" then
- if x>=v.x and x<=v.x+v.w-1 and y>=v.y and y<=v.y+v.h-1 then
- v.callback()
- end
- end
- end
- end)
- while true do
- os.sleep(0.2)
- draw()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement