add opt param scale for ImageButton
This commit is contained in:
parent
1767782603
commit
c628a47e53
1 changed files with 14 additions and 7 deletions
|
@ -12,21 +12,27 @@ return function(core, normal, ...)
|
||||||
opt.hovered = opt.hovered or opt[2] or opt.normal
|
opt.hovered = opt.hovered or opt[2] or opt.normal
|
||||||
opt.active = opt.active or opt[3] or opt.hovered
|
opt.active = opt.active or opt[3] or opt.hovered
|
||||||
opt.id = opt.id or opt.normal
|
opt.id = opt.id or opt.normal
|
||||||
|
opt.scale = opt.scale or 1
|
||||||
|
|
||||||
local image = assert(opt.normal, "No image for state `normal'")
|
local image = assert(opt.normal, "No image for state `normal'")
|
||||||
|
|
||||||
core:registerMouseHit(opt.id, x, y, function(u,v)
|
core:registerMouseHit(opt.id, x, y, function(u,v)
|
||||||
-- mouse in image?
|
-- mouse in image?
|
||||||
u, v = math.floor(u+.5), math.floor(v+.5)
|
u, v = math.floor(u+.5), math.floor(v+.5)
|
||||||
if u < 0 or u >= image:getWidth() or v < 0 or v >= image:getHeight() then
|
local scaledWidth = image:getWidth() * opt.scale
|
||||||
|
local scaledHeight = image:getHeight() * opt.scale
|
||||||
|
if u < 0 or u >= scaledWidth or v < 0 or v >= scaledHeight then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if opt.mask then
|
if opt.mask then
|
||||||
-- alpha test
|
-- alpha test
|
||||||
|
-- convert scaled coordinates back to original image coordinates
|
||||||
|
local originalU = math.floor(u / opt.scale + 0.5)
|
||||||
|
local originalV = math.floor(v / opt.scale + 0.5)
|
||||||
assert(isType(opt.mask, "ImageData"), "Option `mask` is not a love.image.ImageData")
|
assert(isType(opt.mask, "ImageData"), "Option `mask` is not a love.image.ImageData")
|
||||||
assert(u < mask:getWidth() and v < mask:getHeight(), "Mask may not be smaller than image.")
|
assert(originalU < opt.mask:getWidth() and originalV < opt.mask:getHeight(), "Mask may not be smaller than image.")
|
||||||
local _,_,_,a = mask:getPixel(u,v)
|
local _,_,_,a = opt.mask:getPixel(originalU, originalV)
|
||||||
return a > 0
|
return a > 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -41,10 +47,11 @@ return function(core, normal, ...)
|
||||||
|
|
||||||
assert(isType(image, "Image"), "state image is not a love.graphics.image")
|
assert(isType(image, "Image"), "state image is not a love.graphics.image")
|
||||||
|
|
||||||
core:registerDraw(opt.draw or function(image,x,y, r,g,b,a)
|
local r, g, b, a = love.graphics.getColor()
|
||||||
love.graphics.setColor(r,g,b,a)
|
core:registerDraw(opt.draw or function(image,x,y, r,g,b,a, scale)
|
||||||
love.graphics.draw(image,x,y)
|
love.graphics.setColor(r or 1, g or 1, b or 1, a or 1)
|
||||||
end, image, x,y, love.graphics.getColor())
|
love.graphics.draw(image, x, y, 0, scale, scale)
|
||||||
|
end, image, x,y, r or 1, g or 1, b or 1, a or 1, opt.scale)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id = opt.id,
|
id = opt.id,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue