I'm trying to render 2 groups where the first is attached to the camera view, and the second is attached to what's being drawn within the camera. The elements are drawing correctly but the click functions are not registering.
Code: Select all
stages = {}
local worldMap = {}
local camera = {}
local guiStagesMenu = {}
local guiVolcanoIcon = {}
local function hideGUI()
guiStagesMenu:hide()
guiVolcanoIcon:hide()
end
local function showGUI()
guiStagesMenu:show()
guiVolcanoIcon:show()
end
function stages:init()
local btnBackImg = lg.newImage('lib/assets/menus/btn_back.png')
guiStagesMenu = gui:group('', {5, 5, btnBackImg:getWidth(), btnBackImg:getHeight()})
guiStagesMenu.style = {
unit = 20,
font = lg.newFont(20),
fg = {1, 1, 1, 1},
bg = {1, 1, 1, 0},
labelfg = {1, 1, 1, 1},
default = {1, 1, 1, 0},
hilite = {1, 1, 1, 0},
focus = {1, 1, 1, 0},
hs = 'auto'
}
local btnBack = gui:imgbutton('', {0, 0, btnBackImg:getWidth(), btnBackImg:getHeight()}, guiStagesMenu, btnBackImg)
btnBack.click = function()
playSound('btnTone')
hideGUI()
Gamestate.pop()
end
local btnStageIcon = lg.newImage('lib/assets/menus/btn_stage_icon.png')
guiVolcanoIcon = gui:group('', {340, 510, btnStageIcon:getWidth(), btnStageIcon:getHeight()})
guiStagesMenu.style = {
unit = 20,
font = lg.newFont(20),
fg = {1, 1, 1, 1},
bg = {1, 1, 1, 0},
labelfg = {1, 1, 1, 1},
default = {1, 1, 1, 0},
hilite = {1, 1, 1, 0},
focus = {1, 1, 1, 0},
hs = 'auto'
}
local btnVolcanoStage = gui:imgbutton('', {0, 0, btnStageIcon:getWidth(), btnStageIcon:getHeight()}, guiVolcanoIcon, btnStageIcon)
btnVolcanoStage.click = function()
playSound('btnTone')
--hideGUI()
--Gamestate.push(s01volcano)
end
worldMap.x = 0
worldMap.y = 0
worldMap.w = 2520
worldMap.h = 1440
worldMap.image = lg.newImage('lib/assets/menus/stage_menu_bg.png')
camera.x = worldMap.w/2
camera.y = worldMap.h/2
camera.w = gWIDTH
camera.h = gHEIGHT
camera.scale = 0.3 -- minimum 0.3
camera.rotation = 0
camera.view = Camera(camera.x, camera.y, camera.scale, camera.rotation)
camera.origin = {
x = 0,
y = 0
}
camera.bounds = {
x1 = worldMap.x + camera.w/2,
y1 = worldMap.y + camera.h/2,
x2 = worldMap.w - camera.w/2,
y2 = worldMap.h - camera.h/2
}
end
local function introAnimation()
Timer.script(function()
introSequence = true
camera.view:lookAt(worldMap.w/2, worldMap.h/2)
camera.view:zoomTo(0.3)
Timer.tween(3, camera.view, {scale = 1}, 'in-out-quad')
Timer.tween(3, camera.view, {x = 340, y = 510}, 'in-out-quad')
end)
end
function stages:enter()
cameraDragging = false
camera.x = worldMap.w/2
camera.y = worldMap.h/2
introAnimation()
Timer.after(3, function()
introSequence = false
showGUI()
end)
end
function stages:update(dt)
if cameraDragging and not introSequence then
local dx = prevCameraX + (mClickX - mGetX)
local dy = prevCameraY + (mClickY - mGetY)
camera.x = math.max(camera.bounds.x1, math.min(dx, camera.bounds.x2))
camera.y = math.max(camera.bounds.y1, math.min(dy, camera.bounds.y2))
camera.view:lookAt(camera.x, camera.y)
end
guiStagesMenu.pos.x = ((camera.view.x+5) - (camera.w/2))
guiStagesMenu.pos.y = ((camera.view.y+5) - (camera.h/2))
Timer.update(dt)
gui:update(dt)
end
function stages:draw()
camera.view:attach()
lg.setColor(1, 1, 1, 1)
lg.draw(worldMap.image, worldMap.x, worldMap.y)
if not introSequence then
gui:draw()
end
camera.view:detach()
if gDEBUG then
local mx, my = camera.view:mousePosition()
lg.print(mx..'|'..my, mGetX+10, mGetY)
end
end
function stages:mousepressed(x, y, button)
if not introSequence then
if button == 1
and not mCollision(guiStagesMenu.pos)
then
cameraDragging = true
mClickX , mClickY = x, y
prevCameraX = camera.view.x
prevCameraY = camera.view.y
end
end
gui:mousepress(x, y, button)
end
function stages:mousereleased(x, y, button)
if not introSequence then
if button == 1 then
cameraDragging = false
end
end
gui:mouserelease(x, y, button)
end