/*Copyright (C) 2010-2012 Duncan Deveaux

This file is part of Hikou no mizu.

Hikou no mizu 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 3 of the License, or
(at your option) any later version.

Hikou no mizu 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.

You should have received a copy of the GNU General Public License
along with Hikou no mizu.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Checkbox.hpp"

Checkbox::Checkbox() : Widget(), m_checked(false), m_size(20)
{
    m_checkbox = Polygon::rectangle(0, 0, m_size, m_size, Color(), Color());
    m_checkbox.setBorderSize(2);
    
    updateBox();
}

Checkbox::Checkbox(bool checked) : Widget(), m_checked(checked), m_size(20)
{
    m_checkbox = Polygon::rectangle(0, 0, m_size, m_size, Color(), Color());
    m_checkbox.setBorderSize(2);
    
    updateBox();
}

void Checkbox::draw()
{
    //Update matrix
    Drawable::pushMatrix();
    Drawable::updateMatrix();
    
    m_checkbox.draw();
    
    Drawable::popMatrix();
}

bool Checkbox::mouseClick(const Vector &mouseCoords)
{
    if ( Widget::contains( Widget::localCoords(mouseCoords) ) )
    {
        m_checked = !m_checked;
        updateBox();
        
        return true;
    }
    
    return false;
}

bool Checkbox::mouseMove(const Vector &mouseCoords)
{
    return Widget::contains( Widget::localCoords(mouseCoords) );
}

bool Checkbox::isChecked() const
{
    return m_checked;
}

void Checkbox::setSize(float size)
{
    if (size > 0)
    {
        m_size = size;
        
        m_checkbox.setCoords(m_size, 0, 1);
        m_checkbox.setCoords(m_size, m_size, 2);
        m_checkbox.setCoords(0, m_size, 3);
    }
}

void Checkbox::setColor(const Color &color)
{
    m_color = color;
    updateBox();
}

float Checkbox::getWidth() const
{
    return m_size;
}

float Checkbox::getHeight() const
{
    return m_size;
}

void Checkbox::updateBox()
{
    Color boxColor = m_color;
    if (!m_checked) boxColor = Color(0, 0, 0, 0);
    
    for (unsigned int i = 0; i < m_checkbox.getPoints().size(); i++)
    {
        m_checkbox.setColor(boxColor, i);
        m_checkbox.setBorderColor(Color(), i);
    }
}
