GuiTriangle.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Triangles
  3. * Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <cmath>
  18. #include "GuiTriangle.hpp"
  19. GuiTriangle::GuiTriangle(): Triangle(), _scale(1.f)
  20. {
  21. /// A default empty lambda.
  22. bind([](){});
  23. }
  24. bool GuiTriangle::mouseIntersection(sf::Vector2i mouseXY)
  25. {
  26. const double degree = 3.14159265358 / 180.;
  27. sf::Vector2f A = sf::Vector2f(std::sin((0.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((0.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
  28. sf::Vector2f B = sf::Vector2f(std::sin((165.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((165.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
  29. sf::Vector2f C = sf::Vector2f(std::sin((195.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((195.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
  30. bool b0 = ((mouseXY.x - B.x) * (A.y - B.y) - (A.x - B.x) * (mouseXY.y - B.y)) < 0.f;
  31. bool b1 = ((mouseXY.x - C.x) * (B.y - C.y) - (B.x - C.x) * (mouseXY.y - C.y)) < 0.f;
  32. bool b2 = ((mouseXY.x - A.x) * (C.y - A.y) - (C.x - A.x) * (mouseXY.y - A.y)) < 0.f;
  33. return (b0 == b1 && b1 == b2);
  34. }
  35. float GuiTriangle::getWidth()
  36. {
  37. return (std::sin((92.5 * 3.14159265358 / 180.) * getSize() * getScale().x)) - (std::sin((257.5 * 3.14159265358 / 180.)) * getSize() * getScale().x);
  38. }
  39. void GuiTriangle::click(sf::Vector2i mouseXY)
  40. {
  41. if(mouseIntersection(mouseXY))
  42. onClick();
  43. }
  44. void GuiTriangle::update(sf::Vector2i mouseXY, sf::Time delta)
  45. {
  46. if(mouseIntersection(mouseXY))
  47. {
  48. if(_scale < 1.2f)
  49. {
  50. _scale += delta.asSeconds() * (1.25f - _scale) * 2.5f;
  51. setScale(_scale < 1.2f ? _scale : 1.2f, _scale < 1.2f ? _scale : 1.2f);
  52. }
  53. }
  54. else
  55. {
  56. if(_scale > 1.f)
  57. {
  58. _scale -= delta.asSeconds() * (1.25f - _scale) * 1.5f;
  59. setScale(_scale > 1.f ? _scale : 1.f, _scale > 1.f ? _scale : 1.f);
  60. }
  61. }
  62. }
  63. void GuiTriangle::bind(const std::function<void()>& lambda)
  64. {
  65. onClick = lambda;
  66. }
  67. void GuiTriangle::redraw()
  68. {
  69. double degree = 3.14159265358 / 180.;
  70. _vertices[0].position = sf::Vector2f(0, -getSize());
  71. _vertices[1].position = sf::Vector2f(std::sin(165.f * degree) * getSize(), -std::cos(165.f * degree) * getSize());
  72. _vertices[2].position = sf::Vector2f(std::sin(195.f * degree) * getSize(), -std::cos(195.f * degree) * getSize());
  73. }