Fix #17: Support multiple instances

This commit is contained in:
Matthias Richter 2016-01-03 18:33:39 +01:00
parent f77ab8e5e8
commit aca8a297bb
14 changed files with 385 additions and 244 deletions

View file

@ -1,17 +1,16 @@
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
local BASE = (...):match('(.-)[^%.]+$')
local core = require(BASE .. 'core')
return function(normal, ...)
return function(core, normal, ...)
local opt, x,y = core.getOptionsAndSize(...)
opt.normal = normal or opt.normal or opt[1]
opt.hover = opt.hover or opt[2] or opt.normal
opt.active = opt.active or opt[3] or opt.hover
opt.hovered = opt.hovered or opt[2] or opt.normal
opt.active = opt.active or opt[3] or opt.hovered
assert(opt.normal, "Need at least `normal' state image")
opt.id = opt.id or opt.normal
core.registerMouseHit(opt.id, x,y, function(u,v)
opt.state = core:registerMouseHit(opt.id, x,y, function(u,v)
local id = opt.normal:getData()
assert(id:typeOf("ImageData"), "Can only use uncompressed images")
u, v = math.floor(u+.5), math.floor(v+.5)
@ -23,20 +22,20 @@ return function(normal, ...)
end)
local img = opt.normal
if core.isActive(opt.id) then
if core:isActive(opt.id) then
img = opt.active
elseif core.isHot(opt.id) then
img = opt.hover
elseif core:isHovered(opt.id) then
img = opt.hovered
end
core.registerDraw(love.graphics.setColor, 255,255,255)
core.registerDraw(love.graphics.draw, img, x,y)
core:registerDraw(love.graphics.setColor, 255,255,255)
core:registerDraw(love.graphics.draw, img, x,y)
return {
id = opt.id,
hit = core.mouseReleasedOn(opt.id),
hover = core.isHot(opt.id),
entered = core.isHot(opt.id) and not core.wasHot(opt.id),
left = not core.isHot(opt.id) and core.wasHot(opt.id)
hit = core:mouseReleasedOn(opt.id),
hovered = core:isHovered(opt.id),
entered = core:isHovered(opt.id) and not core:wasHovered(opt.id),
left = not core:isHovered(opt.id) and core:wasHovered(opt.id)
}
end