Rafpast

Button

Jul 11th, 2023
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.43 KB | None | 0 0
  1. gui::button::button()
  2. {
  3.     isVisible = false;
  4.  
  5.     timeToNextInteraction = 10.f;
  6.  
  7.     //set position
  8.     position = sf::Vector2f(0, 0);
  9.  
  10.     //set initial state
  11.     state = gui::state::normal;
  12.  
  13.     //set button style
  14.     style = 0;
  15.  
  16.     switch (style)
  17.     {
  18.     case gui::style::none:
  19.     {
  20.         normalTextColor = sf::Color(255, 255, 255);
  21.         hoverTextColor = sf::Color(255, 255, 255);
  22.         clickedTextColor = sf::Color(255, 255, 255);
  23.         backgroundNormalColor = sf::Color(255, 255, 255, 100);
  24.         backgroundHoverColor = sf::Color(200, 200, 200, 100);
  25.         clickedBackgroundColor = sf::Color(150, 150, 150);
  26.         borderColor = sf::Color(255, 255, 255, 100);
  27.     }
  28.     break;
  29.  
  30.     case gui::style::save:
  31.     {
  32.         normalTextColor = sf::Color(255, 255, 255);
  33.         hoverTextColor = sf::Color(255, 255, 255);
  34.         clickedTextColor = sf::Color(255, 255, 255);
  35.         backgroundNormalColor = sf::Color(0, 255, 0, 100);
  36.         backgroundHoverColor = sf::Color(0, 200, 0, 100);
  37.         clickedBackgroundColor = sf::Color(0, 150, 0);
  38.         borderColor = sf::Color(0, 0, 0, 100);
  39.     }
  40.     break;
  41.  
  42.     case gui::style::cancel:
  43.     {
  44.         normalTextColor = sf::Color(255, 255, 255);
  45.         hoverTextColor = sf::Color(255, 255, 255);
  46.         clickedTextColor = sf::Color(255, 255, 255);
  47.         backgroundNormalColor = sf::Color(255, 0, 0, 100);
  48.         backgroundHoverColor = sf::Color(200, 0, 0, 100);
  49.         clickedBackgroundColor = sf::Color(150, 0, 0);
  50.         borderColor = sf::Color(255, 255, 255, 100);
  51.     }
  52.     break;
  53.  
  54.     case gui::style::clean:
  55.     {
  56.         normalTextColor = sf::Color(255, 255, 255);
  57.         hoverTextColor = sf::Color(255, 255, 255);
  58.         clickedTextColor = sf::Color(255, 255, 255);
  59.         backgroundNormalColor = sf::Color(0, 255, 255, 100);
  60.         backgroundHoverColor = sf::Color(0, 200, 200, 100);
  61.         clickedBackgroundColor = sf::Color(0, 150, 150);
  62.         borderColor = sf::Color(255, 255, 255, 100);
  63.     }
  64.     break;
  65.  
  66.     default:
  67.         break;
  68.     }
  69.  
  70.     //set up text
  71.     content.setString("");
  72.     content.setFillColor(normalTextColor);
  73.     content.setOrigin(content.getGlobalBounds().width / 2, content.getGlobalBounds().height / 2);
  74.  
  75.     //set some defauts
  76.     borderRadius = 5.f;
  77.     borderThickness = 0.f;
  78.     size = sf::Vector2f(content.getGlobalBounds().width * 1.5f, content.getGlobalBounds().height * 1.5f);
  79.  
  80.     buttonShape.setPosition(position);
  81.     buttonShape = sf::RectangleShape(size);
  82.     buttonShape.setOutlineColor(borderColor);
  83.     buttonShape.setFillColor(backgroundNormalColor);
  84.     buttonShape.setOutlineThickness(borderThickness);
  85.     buttonShape.setOrigin(buttonShape.getGlobalBounds().width / 2, buttonShape.getGlobalBounds().height / 2);
  86.  
  87.     sf::Vector2f textPosition = sf::Vector2f(buttonShape.getPosition().x, buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 4);
  88.  
  89.     content.setPosition(textPosition);
  90.  
  91.     shadow = content;
  92.     shadow.setOrigin(shadow.getGlobalBounds().width / 2, shadow.getGlobalBounds().height / 2);
  93.     shadow.setPosition(content.getPosition().x + 3.f, content.getPosition().y + 3.f);
  94.  
  95.     if (!buffer.loadFromFile("clickButton.wav"))
  96.     {
  97.         std::cout << "Sound loading error\n";
  98.     }
  99. }
  100.  
  101. gui::button::button(std::string s, sf::Font& font, sf::Vector2f position_, sf::Uint32 style_)
  102. {
  103.     //set position
  104.     position = position_;
  105.  
  106.     timeToNextInteraction = 10.f;
  107.  
  108.     //set initial state
  109.     state = gui::state::normal;
  110.  
  111.     //set button style
  112.     style = style_;
  113.  
  114.     switch (style)
  115.     {
  116.     case gui::style::none:
  117.     {
  118.         normalTextColor = sf::Color(255, 255, 255);
  119.         hoverTextColor = sf::Color(255, 255, 255);
  120.         clickedTextColor = sf::Color(255, 255, 255);
  121.         backgroundNormalColor = sf::Color(255, 255, 255, 100);
  122.         backgroundHoverColor = sf::Color(200, 200, 200, 100);
  123.         clickedBackgroundColor = sf::Color(150, 150, 150);
  124.         borderColor = sf::Color(255, 255, 255, 100);
  125.     }
  126.     break;
  127.  
  128.     case gui::style::save:
  129.     {
  130.         normalTextColor = sf::Color(255, 255, 255);
  131.         hoverTextColor = sf::Color(255, 255, 255);
  132.         clickedTextColor = sf::Color(255, 255, 255);
  133.         backgroundNormalColor = sf::Color(0, 255, 0, 100);
  134.         backgroundHoverColor = sf::Color(0, 200, 0, 100);
  135.         clickedBackgroundColor = sf::Color(0, 150, 0);
  136.         borderColor = sf::Color(0, 0, 0, 100);
  137.     }
  138.     break;
  139.  
  140.     case gui::style::cancel:
  141.     {
  142.         normalTextColor = sf::Color(255, 255, 255);
  143.         hoverTextColor = sf::Color(255, 255, 255);
  144.         clickedTextColor = sf::Color(255, 255, 255);
  145.         backgroundNormalColor = sf::Color(255, 0, 0, 100);
  146.         backgroundHoverColor = sf::Color(200, 0, 0, 100);
  147.         clickedBackgroundColor = sf::Color(150, 0, 0);
  148.         borderColor = sf::Color(255, 255, 255, 100);
  149.     }
  150.     break;
  151.  
  152.     case gui::style::clean:
  153.     {
  154.         normalTextColor = sf::Color(255, 255, 255);
  155.         hoverTextColor = sf::Color(255, 255, 255);
  156.         clickedTextColor = sf::Color(255, 255, 255);
  157.         backgroundNormalColor = sf::Color(0, 255, 255, 100);
  158.         backgroundHoverColor = sf::Color(0, 200, 200, 100);
  159.         clickedBackgroundColor = sf::Color(0, 150, 150);
  160.         borderColor = sf::Color(255, 255, 255, 100);
  161.     }
  162.     break;
  163.  
  164.     default:
  165.         break;
  166.     }
  167.  
  168.     //set up text
  169.     content.setString(s);
  170.     content.setFont(font);
  171.     content.setFillColor(normalTextColor);
  172.     content.setOrigin(content.getGlobalBounds().width / 2, content.getGlobalBounds().height / 2);
  173.  
  174.     //set some defauts
  175.     borderRadius = 5.f;
  176.     borderThickness = 0.f;
  177.     size = sf::Vector2f(content.getGlobalBounds().width * 1.5f, content.getGlobalBounds().height * 1.5f);
  178.  
  179.     buttonShape.setPosition(position);
  180.     buttonShape = sf::RectangleShape(size);
  181.     buttonShape.setOutlineColor(borderColor);
  182.     buttonShape.setFillColor(backgroundNormalColor);
  183.     buttonShape.setOutlineThickness(borderThickness);
  184.     buttonShape.setOrigin(buttonShape.getGlobalBounds().width / 2, buttonShape.getGlobalBounds().height / 2);
  185.  
  186.     sf::Vector2f textPosition = sf::Vector2f(buttonShape.getPosition().x, buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 4);
  187.  
  188.     content.setPosition(textPosition);
  189.  
  190.     shadow = content;
  191.     shadow.setFont(font);
  192.     shadow.setPosition(content.getPosition().x + 3.f, content.getPosition().y + 3.f);
  193.     shadow.setOrigin(shadow.getGlobalBounds().width / 2, shadow.getGlobalBounds().height / 2);
  194.  
  195.     if (!buffer.loadFromFile("clickButton.wav"))
  196.     {
  197.         std::cout << "Sound loading error\n";
  198.     }
  199.  
  200. }
  201.  
  202. void gui::button::update(sf::Event& e, sf::RenderWindow& window)
  203. {
  204.  
  205.     if (isVisible)
  206.     {
  207.         size = sf::Vector2f(content.getGlobalBounds().width * 1.5f, content.getGlobalBounds().height * 1.75f);
  208.         buttonShape = sf::RectangleShape(size);
  209.         buttonShape.setOutlineThickness(borderThickness);
  210.         buttonShape.setFillColor(backgroundNormalColor);
  211.         buttonShape.setOutlineColor(borderColor);
  212.         buttonShape.setOrigin(buttonShape.getGlobalBounds().width / 2, buttonShape.getGlobalBounds().height / 2);
  213.         buttonShape.setPosition(position);
  214.  
  215.  
  216.         content.setOrigin(content.getGlobalBounds().width / 2, content.getGlobalBounds().height / 2);
  217.         sf::Vector2f textPosition = sf::Vector2f(buttonShape.getPosition().x, buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 4);
  218.         content.setPosition(textPosition);
  219.  
  220.  
  221.         shadow.setOrigin(shadow.getGlobalBounds().width / 2, shadow.getGlobalBounds().height / 2);
  222.         shadow.setPosition(content.getPosition().x + 3.f, content.getPosition().y + 3.f);
  223.         shadow.setFillColor(sf::Color(0, 0, 0));
  224.  
  225.  
  226.         //perform updates for user mouse interactions
  227.         sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
  228.  
  229.         bool mouseInButton = mousePosition.x >= buttonShape.getPosition().x - buttonShape.getGlobalBounds().width / 2
  230.             && mousePosition.x <= buttonShape.getPosition().x + buttonShape.getGlobalBounds().width / 2
  231.             && mousePosition.y >= buttonShape.getPosition().y - buttonShape.getGlobalBounds().height / 2
  232.             && mousePosition.y <= buttonShape.getPosition().y + buttonShape.getGlobalBounds().height / 2;
  233.  
  234.         std::cout << "1. TimeToNextInteraction: " << timeToNextInteraction << "\n";
  235.  
  236.         if (e.type == sf::Event::MouseMoved)
  237.         {
  238.             if (mouseInButton)
  239.             {
  240.                 state = gui::state::hovered;
  241.             }
  242.             else
  243.             {
  244.                 state = gui::state::normal;
  245.             }
  246.         }
  247.  
  248.         if (e.type == sf::Event::MouseButtonPressed)
  249.         {
  250.             switch (e.mouseButton.button)
  251.             {
  252.             case sf::Mouse::Left:
  253.             {
  254.                 if (mouseInButton)
  255.                 {
  256.                     if (timeToNextInteraction <= 0)
  257.                     {
  258.                         state = gui::state::clicked;
  259.  
  260.                         timeToNextInteraction = 10.f;
  261.                     }
  262.  
  263.                     state = gui::state::clicked;
  264.  
  265.                 }
  266.                 else
  267.                 {
  268.                     state = gui::state::normal;
  269.                 }
  270.             }
  271.             break;
  272.             }
  273.         }
  274.  
  275.         if (e.type == sf::Event::MouseButtonReleased)
  276.         {
  277.             switch (e.mouseButton.button)
  278.             {
  279.             case sf::Mouse::Left:
  280.             {
  281.                 if (mouseInButton)
  282.                 {
  283.                     state = gui::state::hovered;
  284.                 }
  285.                 else
  286.                 {
  287.                     state = gui::state::normal;
  288.                 }
  289.  
  290.             }
  291.             break;
  292.             case sf::Mouse::Right:
  293.             {
  294.                 if (mouseInButton)
  295.                 {
  296.  
  297.                 }
  298.                 else
  299.                 {
  300.  
  301.                 }
  302.             }
  303.             }
  304.         }
  305.  
  306.         switch (state)
  307.         {
  308.         case gui::state::normal:
  309.         {
  310.             buttonShape.setFillColor(backgroundNormalColor);
  311.             content.setOutlineColor(normalTextColor);
  312.         }
  313.         break;
  314.  
  315.         case gui::state::hovered:
  316.         {
  317.             buttonShape.setFillColor(backgroundHoverColor);
  318.             content.setOutlineColor(hoverTextColor);
  319.         }
  320.         break;
  321.  
  322.         case gui::state::clicked:
  323.         {
  324.             buttonShape.setFillColor(clickedBackgroundColor);
  325.             content.setOutlineColor(clickedTextColor);
  326.  
  327.             if (sound.getStatus() != sf::SoundSource::Status::Playing)
  328.             {
  329.                 sound.setBuffer(buffer);
  330.                 sound.play();
  331.             }
  332.  
  333.         }
  334.         break;
  335.  
  336.         default:
  337.             break;
  338.         }
  339.     }
  340. }
  341.  
  342. void gui::button::draw(sf::RenderTarget& target, sf::RenderStates states) const
  343. {
  344.     if (isVisible)
  345.     {
  346.         target.draw(buttonShape, states);
  347.         target.draw(shadow, states);
  348.         target.draw(content, states);
  349.     }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment