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. #include <iostream>
  25. bool GuiTriangle::mouseIntersection(sf::Vector2i mouseXY)
  26. {
  27. double degree = 3.14159265358 / 180.;
  28. sf::Vector2f A = sf::Vector2f(std::sin((0.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((0.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
  29. sf::Vector2f B = sf::Vector2f(std::sin((165.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((165.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
  30. sf::Vector2f C = sf::Vector2f(std::sin((195.f + getRotation()) * degree) * getSize() * getScale().x, -std::cos((195.f + getRotation()) * degree) * getSize() * getScale().x) + getPosition();
  31. bool b0 = ((mouseXY.x - B.x) * (A.y - B.y) - (A.x - B.x) * (mouseXY.y - B.y)) < 0.f;
  32. bool b1 = ((mouseXY.x - C.x) * (B.y - C.y) - (B.x - C.x) * (mouseXY.y - C.y)) < 0.f;
  33. bool b2 = ((mouseXY.x - A.x) * (C.y - A.y) - (C.x - A.x) * (mouseXY.y - A.y)) < 0.f;
  34. return (b0 == b1 && b1 == b2);
  35. }
  36. float GuiTriangle::getWidth()
  37. {
  38. return (std::sin((92.5 * 3.14159265358 / 180.) * getSize() * getScale().x)) - (std::sin((257.5 * 3.14159265358 / 180.)) * getSize() * getScale().x);
  39. }
  40. void GuiTriangle::click(sf::Vector2i mouseXY)
  41. {
  42. if(mouseIntersection(mouseXY))
  43. onClick();
  44. }
  45. void GuiTriangle::update(sf::Vector2i mouseXY, sf::Time delta)
  46. {
  47. if(mouseIntersection(mouseXY))
  48. {
  49. if(_scale < 1.2f)
  50. {
  51. _scale += delta.asSeconds() * (1.25f - _scale) * 2.5f;
  52. setScale(_scale < 1.2f ? _scale : 1.2f, _scale < 1.2f ? _scale : 1.2f);
  53. }
  54. }
  55. else
  56. {
  57. if(_scale > 1.f)
  58. {
  59. _scale -= delta.asSeconds() * (1.25f - _scale) * 1.5f;
  60. setScale(_scale > 1.f ? _scale : 1.f, _scale > 1.f ? _scale : 1.f);
  61. }
  62. }
  63. }
  64. void GuiTriangle::bind(const std::function<void()>& lambda)
  65. {
  66. onClick = lambda;
  67. }
  68. void GuiTriangle::redraw()
  69. {
  70. double degree = 3.14159265358 / 180.;
  71. _vertices[0].position = sf::Vector2f(0, -getSize());
  72. _vertices[1].position = sf::Vector2f(std::sin(165.f * degree) * getSize(), -std::cos(165.f * degree) * getSize());
  73. _vertices[2].position = sf::Vector2f(std::sin(195.f * degree) * getSize(), -std::cos(195.f * degree) * getSize());
  74. }