Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to draw a button2 by clicking on button1?  (Read 1466 times)

0 Members and 1 Guest are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
How to draw a button2 by clicking on button1?
« on: February 09, 2015, 08:35:10 pm »
Hi,

I would like to open a menu by clicking on a button.
Theoretically it works so far, but it does open only for a fraction of a second and then goes away. How to prevent it from disappearing.
Thx in advance.


#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow mMainWindow(sf::VideoMode(400, 400), "Window");
    mMainWindow.setFramerateLimit(40);

        sf::RectangleShape rect1;
        rect1.setSize(sf::Vector2f(200, 200));

        sf::RectangleShape rect2;
        rect2.setSize(sf::Vector2f(200, 200));
        rect2.setPosition(100, 100);
       
    while (mMainWindow.isOpen())
    {
        sf::Event event;

                bool leftclick = false;
                bool rect1clicked = false;

                sf::Vector2i mousePos;

        while (mMainWindow.pollEvent(event))
        {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                mMainWindow.close();
                                break;
                        case sf::Event::MouseButtonPressed:
                                if (event.mouseButton.button == sf::Mouse::Left)
                                {
                                        leftclick = true;
                                        mousePos = sf::Vector2i (event.mouseButton.x, event.mouseButton.y);
                                        break;
                                }
                        }
                }
                if (leftclick)
                {
                        sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));
            if (rect1.getGlobalBounds().contains(mousecoords))
                        {
                                rect1clicked = true;
                        }
                }

    mMainWindow.clear();
        mMainWindow.draw(rect1);
        if(rect1clicked)
        {
                mMainWindow.draw(rect2);
        }
    mMainWindow.display();
    }
    return 0;
}
« Last Edit: February 09, 2015, 09:14:58 pm by Bogdan »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to draw a button2 by clicking on button1?
« Reply #1 on: February 09, 2015, 08:39:50 pm »
Don't reset rect1clicked to false every time.

excentio

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: How to draw a button2 by clicking on button1?
« Reply #2 on: February 15, 2015, 09:18:12 pm »
I think, you should replace
leftclick = true;
to
leftclick = !leftclick;
or
leftclick ^= true;

and move these variables from the cycle, because it's recursive, leftclick and rect1clicked will be false each frame, move them to the beginning of the main()
like :
 
int main() {
  bool leftclick = false;
  bool rect1clicked = false;

   sf::RenderWindow mMainWindow(sf::VideoMode(400, 400), "Window");
   mMainWindow.setFramerateLimit(40);

//all other code
 
« Last Edit: February 15, 2015, 09:20:43 pm by excentio »

 

anything