GuiTriangle.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. else
  54. {
  55. _scale = 1.2f;
  56. setScale(1.2f, 1.2f);
  57. }
  58. }
  59. else
  60. {
  61. if(_scale > 1.f)
  62. {
  63. _scale -= delta.asSeconds() * (1.25f - _scale) * 1.5f;
  64. setScale(_scale > 1.f ? _scale : 1.f, _scale > 1.f ? _scale : 1.f);
  65. }
  66. else
  67. {
  68. _scale = 1.f;
  69. setScale(1.f, 1.f);
  70. }
  71. }
  72. }
  73. void GuiTriangle::bind(const std::function<void()>& lambda)
  74. {
  75. onClick = lambda;
  76. }
  77. void GuiTriangle::redraw()
  78. {
  79. double degree = 3.14159265358 / 180.;
  80. _vertices[0].position = sf::Vector2f(0, -getSize());
  81. _vertices[1].position = sf::Vector2f(std::sin(165.f * degree) * getSize(), -std::cos(165.f * degree) * getSize());
  82. _vertices[2].position = sf::Vector2f(std::sin(195.f * degree) * getSize(), -std::cos(195.f * degree) * getSize());
  83. }