/**
* Triangles
* Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
*
* 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, version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include "GuiTriangle.hpp"
GuiTriangle::GuiTriangle(): Triangle(), _scale(1.f)
{
/// A default empty lambda.
bind([](){});
}
bool GuiTriangle::mouseIntersection(sf::Vector2i mouseXY)
{
const double degree = 3.14159265358 / 180.;
sf::Vector2f A = sf::Vector2f(std::sin((0.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((0.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
sf::Vector2f B = sf::Vector2f(std::sin((165.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((165.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
sf::Vector2f C = sf::Vector2f(std::sin((195.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((195.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
bool b0 = ((mouseXY.x - B.x) * (A.y - B.y) - (A.x - B.x) * (mouseXY.y - B.y)) < 0.f;
bool b1 = ((mouseXY.x - C.x) * (B.y - C.y) - (B.x - C.x) * (mouseXY.y - C.y)) < 0.f;
bool b2 = ((mouseXY.x - A.x) * (C.y - A.y) - (C.x - A.x) * (mouseXY.y - A.y)) < 0.f;
return (b0 == b1 && b1 == b2);
}
float GuiTriangle::getWidth()
{
return (std::sin((92.5 * 3.14159265358 / 180.) * getSize() * getScale().x)) - (std::sin((257.5 * 3.14159265358 / 180.)) * getSize() * getScale().x);
}
void GuiTriangle::click(sf::Vector2i mouseXY)
{
if(mouseIntersection(mouseXY))
onClick();
}
void GuiTriangle::update(sf::Vector2i mouseXY, sf::Time delta)
{
if(mouseIntersection(mouseXY))
{
if(_scale < 1.2f)
{
_scale += delta.asSeconds() * (1.25f - _scale) * 2.5f;
setScale(_scale < 1.2f ? _scale : 1.2f, _scale < 1.2f ? _scale : 1.2f);
}
else
{
_scale = 1.2f;
setScale(1.2f, 1.2f);
}
}
else
{
if(_scale > 1.f)
{
_scale -= delta.asSeconds() * (1.25f - _scale) * 1.5f;
setScale(_scale > 1.f ? _scale : 1.f, _scale > 1.f ? _scale : 1.f);
}
else
{
_scale = 1.f;
setScale(1.f, 1.f);
}
}
}
void GuiTriangle::bind(const std::function& lambda)
{
onClick = lambda;
}
void GuiTriangle::redraw()
{
double degree = 3.14159265358 / 180.;
_vertices[0].position = sf::Vector2f(0, -getSize());
_vertices[1].position = sf::Vector2f(std::sin(165.f * degree) * getSize(), -std::cos(165.f * degree) * getSize());
_vertices[2].position = sf::Vector2f(std::sin(195.f * degree) * getSize(), -std::cos(195.f * degree) * getSize());
}