Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Button_Square
- {
- private:
- sf::RectangleShape button_box;
- sf::Text button_text;
- sf::Font font;
- sf::FloatRect bounds;
- bool toggleable;
- string eventID;
- int state; //-1: disabled (greyed-out), 0: nominal, 1: moused-over, 2: held (pressed), 3: activated ('toggled on')
- public:
- void Draw(sf::RenderWindow* window)
- {
- window->draw(button_box);
- window->draw(button_text);
- }
- Button_Square(string text, int x, int y, int xs, int ys, string eventID)
- {
- this->eventID = eventID;
- button_box.setSize(sf::Vector2f(xs, ys));
- button_box.setPosition(sf::Vector2f(x, y));
- button_box.setFillColor(sf::Color(0, 0, 0, 50));
- button_box.setOutlineColor(sf::Color(0, 0, 0, 255));
- button_box.setOutlineThickness(4);
- if (!font.loadFromFile("Starcraft.ttf")) { throw "Could not find font 'Starcraft.ttf'."; }
- button_text.setCharacterSize(y - 10);
- button_text.setFillColor(sf::Color::Black);
- button_text.setString(text);
- button_text.setPosition(sf::Vector2f(x, y));
- this->state = 0;
- this->toggleable = false;
- this->bounds = button_box.getGlobalBounds;
- }
- Button_Square(string text, int x, int y, int xs, int ys, string eventID, bool toggleable)
- {
- this->eventID = eventID;
- button_box.setSize(sf::Vector2f(xs, ys));
- button_box.setPosition(sf::Vector2f(x, y));
- button_box.setFillColor(sf::Color(26, 26, 26, 26));
- button_box.setOutlineColor(sf::Color(26, 26, 26, 255));
- button_box.setOutlineThickness(4);
- if (!font.loadFromFile("Starcraft.ttf")) { throw "Could not find font 'Starcraft.ttf'."; }
- button_text.setCharacterSize(y - 10);
- button_text.setFillColor(sf::Color(26, 26, 26, 255));
- button_text.setString(text);
- button_text.setPosition(sf::Vector2f(x, y));
- this->state = 0;
- this->toggleable = toggleable;
- this->bounds = button_box.getGlobalBounds;
- }
- void shape_moused()
- {
- button_box.setOutlineThickness(5);
- button_box.setOutlineColor(sf::Color(0, 0, 0, 255));
- button_box.setFillColor(sf::Color(0, 0, 0, 55));
- button_text.setFillColor(sf::Color(0, 0, 0, 255));
- }
- void shape_default()
- {
- button_box.setOutlineThickness(4);
- button_box.setFillColor(sf::Color(26, 26, 26, 26));
- button_box.setOutlineColor(sf::Color(26, 26, 26, 255));
- button_text.setFillColor(sf::Color(26, 26, 26, 255));
- }
- void shape_pressed()
- {
- button_box.setOutlineThickness(5);
- button_box.setOutlineColor(sf::Color(60, 216, 13, 255));
- button_box.setFillColor(sf::Color(102, 204, 0, 55));
- button_text.setColor(sf::Color(20, 40, 5, 255));
- }
- void shape_disabled()
- {
- button_box.setOutlineThickness(3);
- button_box.setFillColor(sf::Color(90, 90, 90, 90));
- button_box.setOutlineColor(sf::Color(90, 90, 90, 255));
- button_text.setFillColor(sf::Color(90, 90, 90, 255));
- }
- void shape_activated()
- {
- button_box.setOutlineThickness(4);
- button_box.setOutlineColor(sf::Color(255, 101, 0, 255));
- button_box.setFillColor(sf::Color(255, 101, 0, 55));
- button_text.setColor(sf::Color(60, 30, 0, 255));
- }
- void mouseMoved(sf::Event* mouseEvent)
- {
- if (state == 0 && bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
- {
- state = 1;
- this->shape_moused();
- }
- else if ((state == 1 || state == 2) && !bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
- {
- state = 0;
- this->shape_default();
- }
- }
- void mousePressed(sf::Event* mouseEvent)
- {
- if ((state == 1 || state == 0) && bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
- {
- state = 2;
- this->shape_pressed();
- }
- }
- string mouseReleased(sf::Event* mouseEvent)
- {
- if ((state == 2) && bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
- {
- if (toggleable)
- {
- state = 3;
- this->shape_activated();
- }
- else
- {
- state = 1;
- this->shape_moused();
- }
- return eventID;
- }
- return "";
- }
- void disable()
- {
- this->state = -1;
- this->shape_disabled();
- }
- void enable()
- {
- this->state = 0;
- this->shape_default();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment