--[[

    gui.lua
    code.google.com/p/love-td/
    
    Copyright (C) 2014 love-td

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    **************************************************************
    
    Implements "hud", GUI components to display game data
    
    **************************************************************
    
--]]

local hud = {mt = {}}

local HUD_BAR_W = 250
local HUD_BAR_H = 50
local HUD_BAR_FADE = 30


function hud_createHUD ()
    local obj = {}
    
    obj.bar = {}
    local bar = obj.bar
    bar.bgImage = gui.createImage (WIN_W-HUD_BAR_W-50, 30, love.graphics.newImage('media/hud-bar-bg.png'))
    bar.bgImage.bgBox = gui.createRectangle ('fill', bar.bgImage.x-HUD_BAR_FADE, bar.bgImage.y-HUD_BAR_FADE, bar.bgImage.w+60, bar.bgImage.h+60, {0, 0, 0, 0})
    
    bar.bgImage.bgBox:setHoverHandler ('hud_barImageEnterHover', 'hud_barImageExitHover')
    
    bar.cashLabel = gui.createLabel ('0', bar.bgImage.x+50, bar.bgImage.y+18, COLORS.white, 'OstrichSans')
    bar.healthLabel = gui.createLabel ('20', bar.bgImage.x+138, bar.bgImage.y+18, COLORS.white, 'OstrichSans')
    
    bar.waveLabel = gui.createLabel ('0', bar.bgImage.x+190, bar.bgImage.y+9, COLORS.white, 'OstrichSans')
    bar.maxWaveLabel = gui.createLabel ('30', bar.bgImage.x+210, bar.bgImage.y+26, COLORS.white, 'OstrichSans')
    
    
    setmetatable (obj, hud.mt)
    hud.inst = obj
    
    return obj
end

function hud.mt:__index (k)
    return hud[k]
end

function hud:destroy ()
    for _,GUIObj in pairs (self.bar) do GUIObj:destroy() end
    return true
end

function hud:setCash (intCash)
    return self.bar.cashLabel:setText (tostring(intCash))
end

function hud:setHealth (intHealth)
    return self.bar.healthLabel:setText (tostring(intHealth))
end

function hud:setMaxWave (intMaxWave)
    return self.bar.maxWaveLabel:setText (tostring(intMaxWave))
end

function hud:setWave (intWave)
    return self.bar.waveLabel:setText (tostring(intWave))
end

function hud_barImageEnterHover ()
    registerGameCallbackFunc ('update', 'hud_updateBarColor')
end

function hud_barImageExitHover ()
    deregisterGameCallbackFunc ('update', 'hud_updateBarColor')
    for _,GUIObj in pairs (hud.inst.bar) do
        GUIObj:setColor (COLORS.white)
    end
end

function hud_updateBarColor ()
    local px, py = love.mouse.getPosition ()
    local img = hud.inst.bar.bgImage
    
    local cx = math.max (math.min(px, img.x+img.w), img.x)
    local cy = math.max (math.min(py, img.y+img.h), img.y)
    local d  = math.sqrt ((px-cx)*(px-cx) + (py-cy)*(py-cy))
    
    local sub_alpha = math.abs(d-HUD_BAR_FADE) * 8
    local t_Color = {255, 255, 255, 255-sub_alpha}
    for _,GUIObj in pairs (hud.inst.bar) do
        GUIObj:setColor (t_Color)
    end
end