------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------

local love       = require 'love'
local make_class = require 'src.utils.classes'
local UIElement  = require 'src.ui.element'
local Color      = require 'src.utils.color'


------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------

local Label = make_class(UIElement)


------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------

function Label:_init(x, y, text, font, color, float)
    UIElement._init(self, x, y, float)
    self.text  = text
    self.font  = font
    self.color = color ~= nil and color or Color(255, 255, 255)
end


function Label:set_dimensions()
    self.w = self.font:get_width(self.text)
    self.h = self.font:get_height(self.text)
end


function Label:load()
    if not self.font:is_loaded() then
        self.font:load()
    end

    -- If the label's size has not been computed then do it.
    if self.w == nil or self.h == nil then self:set_dimensions() end
end


function Label:unload()
    self.font:unload()
end


function Label:is_loaded()
    return self.font:is_loaded() and self.w ~= nil and self.h ~= nil
end


function Label:draw()
    love.graphics.setColor(self.color.r, self.color.g, self.color.b)
    self.font:set()
    love.graphics.print(self.text, self.x, self.y)
    self.font:unset()
    love.graphics.setColor()
end


------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------

return Label
