Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gui::button::button()
- {
- isVisible = false;
- timeToNextInteraction = 10.f;
- //set position
- position = sf::Vector2f(0, 0);
- //set initial state
- state = gui::state::normal;
- //set button style
- style = 0;
- switch (style)
- {
- case gui::style::none:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(255, 255, 255, 100);
- backgroundHoverColor = sf::Color(200, 200, 200, 100);
- clickedBackgroundColor = sf::Color(150, 150, 150);
- borderColor = sf::Color(255, 255, 255, 100);
- }
- break;
- case gui::style::save:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(0, 255, 0, 100);
- backgroundHoverColor = sf::Color(0, 200, 0, 100);
- clickedBackgroundColor = sf::Color(0, 150, 0);
- borderColor = sf::Color(0, 0, 0, 100);
- }
- break;
- case gui::style::cancel:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(255, 0, 0, 100);
- backgroundHoverColor = sf::Color(200, 0, 0, 100);
- clickedBackgroundColor = sf::Color(150, 0, 0);
- borderColor = sf::Color(255, 255, 255, 100);
- }
- break;
- case gui::style::clean:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(0, 255, 255, 100);
- backgroundHoverColor = sf::Color(0, 200, 200, 100);
- clickedBackgroundColor = sf::Color(0, 150, 150);
- borderColor = sf::Color(255, 255, 255, 100);
- }
- break;
- default:
- break;
- }
- //set up text
- content.setString("");
- content.setFillColor(normalTextColor);
- content.setOrigin(content.getGlobalBounds().width / 2, content.getGlobalBounds().height / 2);
- //set some defauts
- borderRadius = 5.f;
- borderThickness = 0.f;
- size = sf::Vector2f(content.getGlobalBounds().width * 1.5f, content.getGlobalBounds().height * 1.5f);
- buttonShape.setPosition(position);
- buttonShape = sf::RectangleShape(size);
- buttonShape.setOutlineColor(borderColor);
- buttonShape.setFillColor(backgroundNormalColor);
- buttonShape.setOutlineThickness(borderThickness);
- buttonShape.setOrigin(buttonShape.getGlobalBounds().width / 2, buttonShape.getGlobalBounds().height / 2);
- sf::Vector2f textPosition = sf::Vector2f(buttonShape.getPosition().x, buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 4);
- content.setPosition(textPosition);
- shadow = content;
- shadow.setOrigin(shadow.getGlobalBounds().width / 2, shadow.getGlobalBounds().height / 2);
- shadow.setPosition(content.getPosition().x + 3.f, content.getPosition().y + 3.f);
- if (!buffer.loadFromFile("clickButton.wav"))
- {
- std::cout << "Sound loading error\n";
- }
- }
- gui::button::button(std::string s, sf::Font& font, sf::Vector2f position_, sf::Uint32 style_)
- {
- //set position
- position = position_;
- timeToNextInteraction = 10.f;
- //set initial state
- state = gui::state::normal;
- //set button style
- style = style_;
- switch (style)
- {
- case gui::style::none:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(255, 255, 255, 100);
- backgroundHoverColor = sf::Color(200, 200, 200, 100);
- clickedBackgroundColor = sf::Color(150, 150, 150);
- borderColor = sf::Color(255, 255, 255, 100);
- }
- break;
- case gui::style::save:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(0, 255, 0, 100);
- backgroundHoverColor = sf::Color(0, 200, 0, 100);
- clickedBackgroundColor = sf::Color(0, 150, 0);
- borderColor = sf::Color(0, 0, 0, 100);
- }
- break;
- case gui::style::cancel:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(255, 0, 0, 100);
- backgroundHoverColor = sf::Color(200, 0, 0, 100);
- clickedBackgroundColor = sf::Color(150, 0, 0);
- borderColor = sf::Color(255, 255, 255, 100);
- }
- break;
- case gui::style::clean:
- {
- normalTextColor = sf::Color(255, 255, 255);
- hoverTextColor = sf::Color(255, 255, 255);
- clickedTextColor = sf::Color(255, 255, 255);
- backgroundNormalColor = sf::Color(0, 255, 255, 100);
- backgroundHoverColor = sf::Color(0, 200, 200, 100);
- clickedBackgroundColor = sf::Color(0, 150, 150);
- borderColor = sf::Color(255, 255, 255, 100);
- }
- break;
- default:
- break;
- }
- //set up text
- content.setString(s);
- content.setFont(font);
- content.setFillColor(normalTextColor);
- content.setOrigin(content.getGlobalBounds().width / 2, content.getGlobalBounds().height / 2);
- //set some defauts
- borderRadius = 5.f;
- borderThickness = 0.f;
- size = sf::Vector2f(content.getGlobalBounds().width * 1.5f, content.getGlobalBounds().height * 1.5f);
- buttonShape.setPosition(position);
- buttonShape = sf::RectangleShape(size);
- buttonShape.setOutlineColor(borderColor);
- buttonShape.setFillColor(backgroundNormalColor);
- buttonShape.setOutlineThickness(borderThickness);
- buttonShape.setOrigin(buttonShape.getGlobalBounds().width / 2, buttonShape.getGlobalBounds().height / 2);
- sf::Vector2f textPosition = sf::Vector2f(buttonShape.getPosition().x, buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 4);
- content.setPosition(textPosition);
- shadow = content;
- shadow.setFont(font);
- shadow.setPosition(content.getPosition().x + 3.f, content.getPosition().y + 3.f);
- shadow.setOrigin(shadow.getGlobalBounds().width / 2, shadow.getGlobalBounds().height / 2);
- if (!buffer.loadFromFile("clickButton.wav"))
- {
- std::cout << "Sound loading error\n";
- }
- }
- void gui::button::update(sf::Event& e, sf::RenderWindow& window)
- {
- if (isVisible)
- {
- size = sf::Vector2f(content.getGlobalBounds().width * 1.5f, content.getGlobalBounds().height * 1.75f);
- buttonShape = sf::RectangleShape(size);
- buttonShape.setOutlineThickness(borderThickness);
- buttonShape.setFillColor(backgroundNormalColor);
- buttonShape.setOutlineColor(borderColor);
- buttonShape.setOrigin(buttonShape.getGlobalBounds().width / 2, buttonShape.getGlobalBounds().height / 2);
- buttonShape.setPosition(position);
- content.setOrigin(content.getGlobalBounds().width / 2, content.getGlobalBounds().height / 2);
- sf::Vector2f textPosition = sf::Vector2f(buttonShape.getPosition().x, buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 4);
- content.setPosition(textPosition);
- shadow.setOrigin(shadow.getGlobalBounds().width / 2, shadow.getGlobalBounds().height / 2);
- shadow.setPosition(content.getPosition().x + 3.f, content.getPosition().y + 3.f);
- shadow.setFillColor(sf::Color(0, 0, 0));
- //perform updates for user mouse interactions
- sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
- bool mouseInButton = mousePosition.x >= buttonShape.getPosition().x - buttonShape.getGlobalBounds().width / 2
- && mousePosition.x <= buttonShape.getPosition().x + buttonShape.getGlobalBounds().width / 2
- && mousePosition.y >= buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 2
- && mousePosition.y <= buttonShape.getPosition().y + buttonShape.getGlobalBounds().height / 2;
- std::cout << "1. TimeToNextInteraction: " << timeToNextInteraction << "\n";
- if (e.type == sf::Event::MouseMoved)
- {
- if (mouseInButton)
- {
- state = gui::state::hovered;
- }
- else
- {
- state = gui::state::normal;
- }
- }
- if (e.type == sf::Event::MouseButtonPressed)
- {
- switch (e.mouseButton.button)
- {
- case sf::Mouse::Left:
- {
- if (mouseInButton)
- {
- if (timeToNextInteraction <= 0)
- {
- state = gui::state::clicked;
- timeToNextInteraction = 10.f;
- }
- state = gui::state::clicked;
- }
- else
- {
- state = gui::state::normal;
- }
- }
- break;
- }
- }
- if (e.type == sf::Event::MouseButtonReleased)
- {
- switch (e.mouseButton.button)
- {
- case sf::Mouse::Left:
- {
- if (mouseInButton)
- {
- state = gui::state::hovered;
- }
- else
- {
- state = gui::state::normal;
- }
- }
- break;
- case sf::Mouse::Right:
- {
- if (mouseInButton)
- {
- }
- else
- {
- }
- }
- }
- }
- switch (state)
- {
- case gui::state::normal:
- {
- buttonShape.setFillColor(backgroundNormalColor);
- content.setOutlineColor(normalTextColor);
- }
- break;
- case gui::state::hovered:
- {
- buttonShape.setFillColor(backgroundHoverColor);
- content.setOutlineColor(hoverTextColor);
- }
- break;
- case gui::state::clicked:
- {
- buttonShape.setFillColor(clickedBackgroundColor);
- content.setOutlineColor(clickedTextColor);
- if (sound.getStatus() != sf::SoundSource::Status::Playing)
- {
- sound.setBuffer(buffer);
- sound.play();
- }
- }
- break;
- default:
- break;
- }
- }
- }
- void gui::button::draw(sf::RenderTarget& target, sf::RenderStates states) const
- {
- if (isVisible)
- {
- target.draw(buttonShape, states);
- target.draw(shadow, states);
- target.draw(content, states);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment