local graphics = require("graphics") local event = require("event") Button = {x = 1, y = 1, w = 8, h=1, title="Button", bColor=0x00aaaa, fColor=0x000000, onPressed=function() end} Button.__index = Button function Button:new(x,y,w,h,title,onPressed,bColor,fColor) local self = {} setmetatable(self, Button) self.x = math.floor(x) self.y = math.floor(y) self.w = math.floor(w) self.h = math.floor(h) self.title = title self.bColor = bColor self.fColor = fColor self.onPressed = onPressed self._eventID = event.listen("touch", function(_,screen,x,y,mb,ply) self:update(screen,x,y,mb,ply) end) return self end function Button:draw() local gpu = graphics.gpu local _, _, bck = gpu.get(self.x, self.y) gpu.setForeground(self.fColor) gpu.setBackground(self.bColor) gpu.fill(self.x,self.y,self.w,self.h," ") local magic = math.floor(self.x + self.w/2) - math.floor(self.title:len() / 2) gpu.set(magic,self.y + math.floor(self.h/2), self.title) end function Button:update(screen, x, y, mouseButton, player) local gpu = graphics.gpu if (x >= self.x and x <= self.x + self.w-1) and (y >= self.y and y <= self.h + self.y) then if self.onPressed then gpu.set(1,1,"clicked:"..self.title) self.onPressed(self) end end end function Button:destroy() event.cancel(tonumber(self._eventID)) end return Button -- function button.create(x,y,w,h,text,fcolor,bcolor, func) -- o = { -- draw = function() -- local gpu = graphics.gpu -- gpu.setForeground(self.fcolor) -- gpu.setBackground(self.bcolor) -- gpu.fill(self.x,self.y,self.w,self.h," ") -- gpu.set(self.x,self.y, self.text) -- end, -- onPressed = function () -- self.bcolor = 0x00aaaa -- end, -- update = function(screen, x, y, mouseButton, player) -- local gpu = graphics.gpu -- -- print("pos:"..x..","..y) -- -- gpu.set(1,1,":"..tostring(x >= self.x)..","..tostring(x <= self.x + self.w)..","..tostring(y >= self.y)..",".. tostring(y <= self.y + self.h)) -- -- gpu.set(1,2,":"..tostring(self.w)..","..self.h) -- if (x >= self.x and x <= self.x + self.w) and (y >= self.y and y <= self.y + self.h-1) then -- gpu.set(1,7,"clicked") -- self:onPressed() -- end -- end -- } -- create object if user does not provide one -- setmetatable(o, self) -- self.__index = self -- self.x = x -- self.y = y -- self.w = w -- self.h = h -- self.text = text -- self.fcolor = fcolor -- self.bcolor = bcolor -- event.listen("touch", function(_,screen,x,y,mb,ply) self:update(screen,x,y,mb,ply) end) -- return o -- end -- function objBtn:draw() -- local gpu = graphics.gpu -- gpu.setForeground(self.fcolor) -- gpu.setBackground(self.bcolor) -- gpu.fill(self.x,self.y,self.w,self.h," ") -- gpu.set(self.x,self.y, self.text) -- end -- function objBtn:onPressed() -- self.bcolor = 0x00aaaa -- end -- function objBtn:update(screen, x, y, mouseButton, player) -- local gpu = graphics.gpu -- -- print("pos:"..x..","..y) -- -- gpu.set(1,1,":"..tostring(x >= self.x)..","..tostring(x <= self.x + self.w)..","..tostring(y >= self.y)..",".. tostring(y <= self.y + self.h)) -- -- gpu.set(1,2,":"..tostring(self.w)..","..self.h) -- if (x >= self.x and x <= self.x + self.w) and (y >= self.y and y <= self.y + self.h-1) then -- gpu.set(1,7,"clicked") -- self:onPressed() -- end -- end -- function objBtn:destroy() -- objBtn = nil -- end