LCOV - code coverage report
Current view: top level - client - Button.cpp (source / functions) Hit Total Coverage
Test: code-coverage.info.cleaned Lines: 0 60 0.0 %
Date: 2023-01-28 00:08:57 Functions: 0 10 0.0 %

          Line data    Source code
       1             : #include <client.hpp>
       2             : 
       3             : using namespace client;
       4             : 
       5             : /*!
       6             :  * @brief Constructor
       7             :  *
       8             :  * Constructor of Button class
       9             :  *
      10             :  * @param buttonSize size of the Button
      11             :  * @param buttonPos position of the Button
      12             :  * @param buttonColor color of the Button
      13             :  * @param border true if the border is red, false if the border is black
      14             :  */
      15           0 : Button::Button(sf::Vector2f buttonSize, sf::Vector2f buttonPos, sf::Color buttonColor, bool border)
      16             : {
      17           0 :     buttonRect = std::make_shared<sf::RectangleShape>();
      18           0 :     buttonRect->setSize(buttonSize);
      19           0 :     buttonRect->setPosition(buttonPos);
      20           0 :     buttonRect->setFillColor(buttonColor);
      21           0 :     if (border)
      22             :     {
      23           0 :         setActive();
      24             :     }
      25             :     else
      26             :     {
      27           0 :         setInactive();
      28             :     }
      29           0 : }
      30             : 
      31             : /*!
      32             :  * @brief operator()
      33             :  *
      34             :  * Return the button text
      35             :  */
      36           0 : std::string Button::operator()()
      37             : {
      38           0 :     return buttonText->getString();
      39             : }
      40             : 
      41             : /*!
      42             :  * @brief Set the text of the Button
      43             :  *
      44             :  * @param textSize size of the text
      45             :  * @param textOffset offset of the text
      46             :  * @param text text of the Button
      47             :  * @param font font of the text
      48             :  */
      49           0 : void Button::setText(int textSize, sf::Vector2f textOffset, std::string text, sf::Font &font, int size)
      50             : {
      51           0 :     maxTextSize = size;
      52           0 :     buttonText = std::make_shared<sf::Text>();
      53           0 :     buttonText->setFont(font);
      54           0 :     buttonText->setString(text);
      55           0 :     buttonText->setCharacterSize(textSize);
      56           0 :     centerText(true, textOffset);
      57           0 :     buttonText->setFillColor(sf::Color::Black);
      58           0 : }
      59             : 
      60             : /*!
      61             :  * @brief Add a char to the text
      62             :  *
      63             :  * @param ch char to be add
      64             :  */
      65           0 : void Button::addChar(std::string ch)
      66             : {
      67           0 :     buttonText->setString(buttonText->getString() + ch);
      68           0 :     if (buttonText->getLocalBounds().width >= buttonRect->getLocalBounds().width)
      69             :     {
      70           0 :         delChar();
      71             :     }
      72           0 :     centerText(false);
      73           0 : }
      74             : 
      75             : /*!
      76             :  * @brief Delete a char to the text
      77             :  */
      78           0 : void Button::delChar()
      79             : {
      80           0 :     std::string newString = buttonText->getString();
      81           0 :     newString.pop_back();
      82           0 :     buttonText->setString(newString);
      83           0 :     centerText(false);
      84           0 : }
      85             : 
      86             : /*!
      87             :  * @brief Center the text
      88             :  *
      89             :  * @param centerAllAxis true if you also want to center on y Axis
      90             :  * @param textOffset offset on x
      91             :  */
      92           0 : void Button::centerText(bool centerAllAxis, sf::Vector2f textOffset)
      93             : {
      94           0 :     sf::Vector2f buttonPos = buttonRect->getPosition();
      95           0 :     sf::Vector2f buttonSize = buttonRect->getSize();
      96             :     int yPosText;
      97           0 :     if (centerAllAxis)
      98             :     {
      99           0 :         yPosText = buttonPos.y + (buttonSize.y - buttonText->getGlobalBounds().height) / 2 - buttonText->getGlobalBounds().height / 2 + textOffset.y;
     100             :     }
     101             :     else
     102             :     {
     103           0 :         yPosText = buttonText->getPosition().y;
     104             :     }
     105           0 :     int xPosText = buttonPos.x + (buttonSize.x - buttonText->getGlobalBounds().width) / 2 + textOffset.x;
     106           0 :     buttonText->setPosition(sf::Vector2f(xPosText, yPosText));
     107           0 : }
     108             : 
     109             : /*!
     110             :  * @brief Put the border into red border
     111             :  */
     112           0 : void Button::setActive()
     113             : {
     114           0 :     redBorder = true;
     115           0 :     buttonRect->setOutlineColor(sf::Color::Red);
     116           0 :     buttonRect->setOutlineThickness(2.0f);
     117           0 : }
     118             : 
     119             : /*!
     120             :  * @brief Put the border into black border
     121             :  */
     122           0 : void Button::setInactive()
     123             : {
     124           0 :     redBorder = false;
     125           0 :     buttonRect->setOutlineColor(sf::Color::Black);
     126           0 :     buttonRect->setOutlineThickness(1.0f);
     127           0 : }
     128             : 
     129             : /*!
     130             :  * @brief Click on the button
     131             :  */
     132           0 : bool Button::clickButton()
     133             : {
     134           0 :     if (maxTextSize != 0)
     135             :     {
     136           0 :         setActive();
     137           0 :         return false;
     138             :     }
     139             :     else
     140             :     {
     141           0 :         return true;
     142             :     }
     143             : }
     144             : 
     145             : /*!
     146             :  * @brief Draw a button with its text on a window
     147             :  * @param window Window on which the button is drawn
     148             :  */
     149           0 : void Button::drawButton(std::shared_ptr<sf::RenderWindow> window) {
     150           0 :     window->draw(*buttonRect);
     151           0 :     window->draw(*buttonText);
     152           0 : }

Generated by: LCOV version 1.14