GuiButton.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <sstream>
  2. #include "GuiButton.hpp"
  3. void GuiButton::flip(bool val)
  4. {
  5. _flip = val;
  6. setRotation(_flip ? -97.5f : 97.5f);
  7. setPosition(getPosition());
  8. }
  9. void GuiButton::setCharacterSize(unsigned size)
  10. {
  11. _caption.setCharacterSize(size);
  12. }
  13. void GuiButton::create(const sf::Font& font, std::string caption, bool* value)
  14. {
  15. _string = caption;
  16. _caption.setFont(font);
  17. _caption.setColor(sf::Color::Black);
  18. _value = value;
  19. recreate();
  20. }
  21. void GuiButton::setColor(sf::Color color)
  22. {
  23. color.a = (*_value ? 255 : 80);
  24. Triangle::setColor(color);
  25. }
  26. void GuiButton::recreate()
  27. {
  28. std::ostringstream oss;
  29. oss << _string << ':' << (*_value ? "on" : "off");
  30. _caption.setString(oss.str());
  31. }
  32. void GuiButton::click(sf::Vector2i mouseXY)
  33. {
  34. if(mouseIntersection(mouseXY))
  35. (*_value) = !(*_value);
  36. recreate();
  37. }
  38. void GuiButton::draw(sf::RenderTarget& target, sf::RenderStates states) const
  39. {
  40. states.transform *= getTransform();
  41. states.texture = (_texture);
  42. target.draw(_vertices, states);
  43. target.draw(_caption);
  44. }
  45. void GuiButton::setPosition(sf::Vector2f position)
  46. {
  47. Triangle::setPosition(position);
  48. _caption.setPosition(position);
  49. _caption.setOrigin(_caption.getLocalBounds().width / 2.f + (_flip ? -60.f : 60.f), _caption.getLocalBounds().height / 2.f + 10.f);
  50. }
  51. void GuiButton::setPosition(float x, float y)
  52. {
  53. setPosition(sf::Vector2f(x, y));
  54. }