currently I have this button but when I click on it when another button is behind it both buttons get pressed
Code: Select all
buttons = {}
function buttons:Create(text, x, y, w, h, callback)
local inst = {}
inst.state = 0;
inst.text = text;
inst.x = x;
inst.y = y;
inst.w = w;
inst.h = h;
inst.callback = callback or nil;
table.insert(buttons, inst);
return inst;
end
function buttons:Update()
for i,v in ipairs(buttons) do
v.state = 0;
if love.mouse.getX() >= v.x and
love.mouse.getY() >= v.y and
love.mouse.getX() <= v.x+v.w and
love.mouse.getY() <= v.y+v.h and
love.mouse.isDown(1) then
v.state = 1;
if v.callback ~= nil then
v.callback()
end
end
end
end
function buttons:Render()
for i,v in ipairs(buttons) do
if v.state == 0 then
love.graphics.setColor(255,255,255)
love.graphics.rectangle("fill", v.x+0, v.y+0, v.w-0, v.h-0)
love.graphics.setColor(48,48,48)
love.graphics.rectangle("fill", v.x+1, v.y+1, v.w-1, v.h-1)
love.graphics.setColor(96,96,96)
love.graphics.rectangle("fill", v.x+2, v.y+2, v.w-2, v.h-2)
love.graphics.setColor(192,192,192)
love.graphics.rectangle("fill", v.x+1, v.y+1, v.w-2, v.h-2)
else
love.graphics.setColor(48,48,48)
love.graphics.rectangle("fill", v.x+0, v.y+0, v.w-0, v.h-0)
love.graphics.setColor(96,96,96)
love.graphics.rectangle("fill", v.x+1, v.y+1, v.w-2, v.h-2)
love.graphics.setColor(192,192,192)
love.graphics.rectangle("fill", v.x+2, v.y+2, v.w-4, v.h-4)
end
love.graphics.setColor(0,0,0)
love.graphics.printf(v.text, v.x, v.y+v.h/4, v.w-2, "center")
end
end