1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #include <sstream>
- #include "GuiButton.hpp"
- void GuiButton::flip(bool val)
- {
- _flip = val;
- setRotation(_flip ? -97.5f : 97.5f);
- setPosition(getPosition());
- }
- void GuiButton::setCharacterSize(unsigned size)
- {
- _caption.setCharacterSize(size);
- }
- void GuiButton::create(const sf::Font& font, std::string caption, bool* value)
- {
- _string = caption;
- _caption.setFont(font);
- _caption.setColor(sf::Color::Black);
- _value = value;
- recreate();
- }
- void GuiButton::setColor(sf::Color color)
- {
- color.a = (*_value ? 255 : 80);
- Triangle::setColor(color);
- }
- void GuiButton::recreate()
- {
- std::ostringstream oss;
- oss << _string << ':' << (*_value ? "on" : "off");
- _caption.setString(oss.str());
- }
- void GuiButton::click(sf::Vector2i mouseXY)
- {
- if(mouseIntersection(mouseXY))
- (*_value) = !(*_value);
- recreate();
- }
- void GuiButton::draw(sf::RenderTarget& target, sf::RenderStates states) const
- {
- states.transform *= getTransform();
- states.texture = (_texture);
-
- target.draw(_vertices, states);
- target.draw(_caption);
- }
- void GuiButton::setPosition(sf::Vector2f position)
- {
- Triangle::setPosition(position);
- _caption.setPosition(position);
- _caption.setOrigin(_caption.getLocalBounds().width / 2.f + (_flip ? -60.f : 60.f), _caption.getLocalBounds().height / 2.f + 10.f);
- }
- void GuiButton::setPosition(float x, float y)
- {
- setPosition(sf::Vector2f(x, y));
- }
|