/*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 Pblic Licensue
along with Hikou no mizu.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Button.hpp"

Button::Button() : Drawable()
{
    Button(100, 50, Color());
}

Button::Button(float width, float height, const Color &color) :
Drawable(), m_width(width), m_height(height)
{
    m_background = Polygon::rectangle(0, 0, width, height, color);
}

//Drawable virtual method
void Button::draw()
{
    //Update matrix
    Drawable::pushMatrix();
    Drawable::updateMatrix();
    
    //Draw elements
    m_background.draw();
    
    Drawable::popMatrix();
}

bool Button::mouseHitTest(const Vector &mousePosition)
{
    //Convert mouse coordinates to local
    Vector localMouse;
    localMouse.x = mousePosition.x - getXPosition();
    localMouse.y = mousePosition.y - getYPosition();
    
    float buttonWidth = m_width * getXScale();
    float buttonHeight = m_height * getYScale();
    
    Box collisionTest(0, 0, buttonWidth, buttonHeight);
    if (collisionTest.contains(localMouse))
        return true;
    
    return false;
}

void Button::enableBackground(bool background)
{
    m_background.enableBackground(background);
}

bool Button::backgroundEnabled() const
{
    return m_background.backgroundEnabled();
}

void Button::enableBorder(bool border)
{
    m_background.enableBorder(border);
}

bool Button::borderEnabled() const
{
    return m_background.borderEnabled();
}

//Get and set methods
void Button::setBackgroundColor(const Color &backgroundColor)
{
    std::vector<Point> points = m_background.getPoints();
    
    for (unsigned int i = 0; i < points.size(); i++)
    {
        m_background.setColor(backgroundColor, i);
    }
}

const Color &Button::getBackgroundColor() const
{
    std::vector<Point> points = m_background.getPoints();
    
    return points[0].color;
}

void Button::setBorderColor(const Color &borderColor)
{
    std::vector<Point> points = m_background.getPoints();
    
    for (unsigned int i = 0; i < points.size(); i++)
    {
        m_background.setBorderColor(borderColor, i);
    }
}

const Color &Button::getBorderColor() const
{
    std::vector<Point> points = m_background.getPoints();
    
    return points[0].borderColor;
}

void Button::setBorderSize(float borderSize)
{
    m_background.setBorderSize(borderSize);
}

float Button::getBorderSize() const
{
    return m_background.getBorderSize();
}

float Button::getWidth() const
{
    return m_width;
}

float Button::getHeight() const
{
    return m_height;
}
