local component = require("component") local event = require("event") local graphics = {} graphics.gpu = component.gpu graphics.w, graphics.h = graphics.gpu.getResolution() graphics.buttons = {} function graphics.clearScreen() local gpu = graphics.gpu gpu.setBackground(0x000000) gpu.fill(1,1,graphics.w,graphics.h," ") end function graphics.setGPU(gpu_proxy) graphics.gpu = gpu_proxy graphics.w, graphics.h = graphics.gpu.getResolution() end function graphics.bind_to_screen(screen_addr) graphics.gpu.bind(screen_addr) end function graphics.getGPU() return graphics.gpu end function graphics.setResolution(x,y) graphics.gpu.setResolution(x,y) graphics.w, graphics.h = graphics.gpu.getResolution() end function graphics.createBox(x,y,w,h,title,color) w = math.floor(w) h = math.floor(h) local gpu = graphics.gpu gpu.setBackground(color) gpu.setForeground(0xffffff) gpu.fill(x,y,w,h," ",color) gpu.fill(x,y,1,h,"║") gpu.fill(x,y,w,1,"═") gpu.fill(x,h+y,w,1,"═") gpu.fill(w+x,y,1,h,"║") gpu.set(x,y,"╔") gpu.set(w+x,h+y,"╝") gpu.set(w+x,y,"╗") gpu.set(x,h+y,"╚") title = " " .. title .. " " local magic = (w/2 + x) - math.ceil(title:len()/2) + 1 gpu.set(magic-1, y, "╡") gpu.set(magic+title:len(), y, "╞") gpu.setBackground(0xaaaaaa) gpu.setForeground(0x000000) gpu.set(magic, y, title) gpu.setBackground(0x0) gpu.setBackground(0xffffff) local tbl = {w=w,h=h,x=x,y=y,title=title,color=color, magic=magic} return tbl end function graphics.createTitledBar(x,y,w,h,title,foreColor,pct, value, unit, backColor) local gpu = graphics.gpu if backColor then gpu.setBackground(backColor) else gpu.setBackground(0xaaaaaa) end gpu.setForeground(0x000000) gpu.set(x,y," "..title.." ") gpu.fill(x,y+1,w,h," ") gpu.setBackground(foreColor) gpu.fill(x,y+1,(w * pct),h," ") if value ~= nil then local num = tostring(value) local _midpoint = math.floor(num:len() / 2) gpu.setBackground(0xaaaaaa) gpu.setForeground(0x000000) gpu.set(w/2-_midpoint, y, num..unit) end end function graphics.createText(x,y,text) local gpu = graphics.gpu gpu.setForeground(0x000000) gpu.setBackground(0xaaaaaa) gpu.set(x,y,text) end function graphics.createStatusText(x,y,title, value) local text = {} text.x, text.y, text.title, text.value = x,y,title,value return text end function graphics.drawStatusText(tbl) local gpu = graphics.gpu gpu.setBackground(0xaaaaaa) if value then gpu.set(tbl.x,tbl.y,tbl.title) gpu.setForeground(0xffffff) gpu.set(tbl.x+tbl.title:len(),tbl.y,"Online") else gpu.set(tbl.x,tbl.y,tbl.title) gpu.setForeground(0xaa0000) gpu.set(tbl.x+tbl.title:len(),tbl.y,"Offline") end end button = {} function button.createButton(x,y,w,h,text,bckcolor,forecolor,func) x = math.ceil(x) y = math.ceil(y) w = math.ceil(w) h = math.ceil(h) local btn = {} btn.onPressed = func -- btn.draw = function() graphics.drawBtn(btn) end btn.x, btn.y, btn.w, btn.h, btn.text, btn.bckcolor, btn.forecolor = x,y,w,h,text,bckcolor, forecolor table.insert(graphics.buttons, btn) return btn end function button:draw(self) local gpu = graphics.gpu gpu.setForeground(self.forecolor) gpu.setBackground(self.bckcolor) gpu.fill(self.x,self.y,self.w,self.h," ") local magic = (self.w/2 + self.x) - math.floor(self.text:len()/2) gpu.set(magic,math.floor(self.h/2 + self.y), self.text) end function graphics.handle_touchEvent(_,screenAddr, x,y,btn,ply) local gpu = graphics.gpu gpu.set(1,1,":"..x..","..y..":"..ply) if (screenAddr == graphics.gpu.getScreen()) then for _,button in pairs(graphics.buttons) do gpu.set(1,2,button.text) gpu.set(1,3,tostring(x >= button.x)) gpu.set(1,4,tostring(x <= button.x + button.w)) gpu.set(1,5,tostring(y >= button.y)) gpu.set(1,6,tostring(y <= button.y + button.h)) if (x >= button.x and x <= button.x + button.w-1) and (y >= button.y and y <= button.y + button.h-1) then gpu.set(1,7,"clicked") button.onPressed() end end end end function graphics.handle_interrupt(id, ...) if id == "interrupted" then print("interrupt?") graphics.unloadButtonRegister() event.ignore("interrupted", graphics.handle_interrupt) os.exit() end end function graphics.loadButtonRegister() print(event.listen("touch", graphics.handle_touchEvent)) event.listen("interrupted", graphics.handle_interrupt) end function graphics.unloadButtonRegister() event.ignore("touch", graphics.handle_touchEvent) end return graphics