--Helper.
function p.txt_col(wid, col)
    ga_win_set_front_color(wid, col)
end

function p.interp_col(col1, col2, frac)
    local col = {x=0, y=0, z=0}
    col.x = (1.0 - frac)*col1.x + frac*col2.x
    col.y = (1.0 - frac)*col1.y + frac*col2.y
    col.z = (1.0 - frac)*col1.z + frac*col2.z
    return col
end

function p.render(wid)
    -- if( ga_get_b("xar.hud.disable_hud") ) then return end

    local col_white  = std.vec(1.0, 1.0, 1.0)
    local col_red    = std.vec(1.0, 0.0, 0.0)
    local col_yellow = std.vec(1.0, 1.0, 0.0)
    local col_green  = std.vec(0.0, 1.0, 0.0)
    local col_blue   = std.vec(0.0, 0.25, 1.0)
    local col_cyan   = std.vec(0.0, 1.0, 1.0)

    --Lower left message.
    local disable_msgs = false
    -- local disable_msgs = ga_get_b("xar.hud.msg.disable_msgs")
    if( not disable_msgs ) then
        ga_win_set_char_size(wid, 0.015, 0.03)
        p.txt_col(wid, col_green)
        for i = 1,5 do
            local prefix = "game.messages.slot" .. tostring(i)
            local msg = ga_get_s(prefix .. ".msg")
            local ttl = ga_get_f(prefix .. ".ttl") 
            --
            local col = col_white
            if ttl < 4.0 then col = col_green end
            if( ttl > 4.0 and ttl < 5.0 ) then
                col = p.interp_col(col_green,col_white,ttl-4.0)
            end
            p.txt_col(wid, col)
            --
            local min_x = 0.01
            local min_y = 0.25 + 0.033*(i-1)
            local alpha_fade = 1.0
            if( ttl < 1.0 and ttl >= 0.0 ) then alpha_fade = ttl end
            if ttl > 0.0 then
                local color = std.vec(0.0,0.0,0.0)
                local back_alpha = 0.4 * alpha_fade
                local front_alpha = 1.0 * alpha_fade
                ga_win_set_back_params(wid, color, back_alpha, back_alpha)
                ga_win_txt_alpha_bg(wid, min_x, min_y, front_alpha, msg)
            end
        end
    end

    local cur_x = 0.01
    local step_x = 0.06
    local icon_bot = 0.01
    local icon_top = 0.09
    local icon_width = 0.05
    --
    if ga_get_b("game.movement.rocket") then
        local tex = "icon_rocket"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        cur_x = cur_x + step_x
    end
    if ga_get_b("game.movement.brake") then
        local tex = "icon_brake"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        cur_x = cur_x + step_x
    end

end
