GuiButton.cpp 2.0 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 <sstream>
  18. #include "GuiButton.hpp"
  19. void GuiButton::flip(bool val)
  20. {
  21. _flip = val;
  22. setRotation(_flip ? -97.5f : 97.5f);
  23. setPosition(getPosition());
  24. }
  25. void GuiButton::setCharacterSize(unsigned size)
  26. {
  27. _caption.setCharacterSize(size);
  28. }
  29. void GuiButton::create(const sf::Font& font, std::string caption, bool* value)
  30. {
  31. _string = caption;
  32. _caption.setFont(font);
  33. _caption.setColor(sf::Color::Black);
  34. _value = value;
  35. recreate();
  36. }
  37. void GuiButton::setColor(sf::Color color)
  38. {
  39. color.a = (*_value ? 255 : 80);
  40. Triangle::setColor(color);
  41. }
  42. void GuiButton::recreate()
  43. {
  44. std::ostringstream oss;
  45. oss << _string << ':' << (*_value ? "on" : "off");
  46. _caption.setString(oss.str());
  47. }
  48. void GuiButton::click(sf::Vector2i mouseXY)
  49. {
  50. if(mouseIntersection(mouseXY))
  51. (*_value) = !(*_value);
  52. recreate();
  53. }
  54. void GuiButton::draw(sf::RenderTarget& target, sf::RenderStates states) const
  55. {
  56. states.transform *= getTransform();
  57. states.texture = (_texture);
  58. target.draw(_vertices, states);
  59. target.draw(_caption);
  60. }
  61. void GuiButton::setPosition(sf::Vector2f position)
  62. {
  63. Triangle::setPosition(position);
  64. _caption.setPosition(position);
  65. _caption.setOrigin(_caption.getLocalBounds().width / 2.f + (_flip ? -60.f : 60.f), _caption.getLocalBounds().height / 2.f + 10.f);
  66. }
  67. void GuiButton::setPosition(float x, float y)
  68. {
  69. setPosition(sf::Vector2f(x, y));
  70. }