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

Author Topic: [SOLVED] sf::Event::LostFocus  (Read 5709 times)

0 Members and 1 Guest are viewing this topic.

GabrielS

  • Newbie
  • *
  • Posts: 25
    • View Profile
[SOLVED] sf::Event::LostFocus
« on: June 21, 2013, 09:01:42 pm »
Hello community =)

I have a question about the event : sf::Event::LostFocus and sf::Event::GainFocus.
I have 2 sf::RenderWindow opened. Let's call them window1 and window2.
Window1 is permanently opened, and window2 can open itself from interactions with window1 ( if you click on a button from window1, you will see window2 appear, "partially on" window1).
That works well and I get focus on window2 when opening it.

However, if, from here, I click on window1, I do not get an event lost focus from window2. Clicking on an other application exactly the SAME way will give me the event 'lostfocus'.
I do not know if this is a problem in my program or a problem from SFML library. Please help me, I have no idea how to correct this bug,

Thanks in advance,

Gabriel
« Last Edit: June 24, 2013, 07:16:04 pm by GabrielS »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Event::LostFocus
« Reply #1 on: June 21, 2013, 09:08:48 pm »
Can you please provide a complete and minimal example that reproduces the problem?
Laurent Gomila - SFML developer

GabrielS

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: sf::Event::LostFocus
« Reply #2 on: June 21, 2013, 09:45:56 pm »
I'll try to give you a really simplify version of the issue I am encountering. This is just to show you where is my problem :

// windowMain and windowAux are two sf::renderwindow created
while (windowMain.isOpen())
{
    sf::Event event;
    while (windowMain.pollEvent(event))
    {
        switch (event.type)
        {
            case sf::Event::Closed: // closing using the cross button of the window
                windowMain.close(); // close the main window and gets out of this loop
                break;
            case sf::Event::MouseButtonPressed: // clicking
                createWindowAux(windowAux); // pops windowAux
                windowAux.display();
                break;
            default:
                break;
        }
    }
    while (windowAux.pollEvent(event)) // loop on editing/reading attributes
    {
        switch (event.type)
        {
            case sf::Event::Closed:
                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                windowAux.close();
                break;

            case sf::Event::LostFocus:
                cout << "LOSTFOCUS" << endl;
                break;

            case sf::Event::GainedFocus :
                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                // add objects here
                windowAux.display();
                cout << "GAINFOCUS" << endl;
                break;
            case sf::Event::Resized :
                cout << "RESIZED" << endl;
                break;
            default:
                break;
        }
    }
}
 

I just re-explain the problem I got (not so easy to explain sorry). By clicking on the mainWindow while the windowAux is opened, i do not get the event LOSTFOCUS in window2 (maybe I need to make 2 distincts event but I do not think so - events should be all equal at same time I suppose).
However, clicking on an other window - (not from SFML) as MicrosoftVisualStudios (for instance) - will give me the lostfocus event.

Also, this is the code for the creation of the window :
void createWindowForAttributes(sf::RenderWindow& window)
{
        if (!window.isOpen()) // if not already opened, creation of the window
        {
                window.create(sf::VideoMode(WIDTH_WINDOW_ATTRIBUTE,
                        HEIGHT_WINDOW_ATTRIBUTE),
                        "Editing", sf::Style::Titlebar | sf::Style::Close);
        }
        window.setTitle("Editing");
        window.setSize(sf::Vector2u(WIDTH_WINDOW_ATTRIBUTE,
                HEIGHT_WINDOW_ATTRIBUTE));
        window.clear(COLOR_BACKGROUND_WINDOW);
}
 

Thanks,
Tell me if you need anything else for the explanation,

Gabriel

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Event::LostFocus
« Reply #3 on: June 21, 2013, 10:36:02 pm »
I perfectly understood your problem, but I need a code to test, so that I can either confirm that it's a bug, or tell you what you did wrong ;)
Laurent Gomila - SFML developer

GabrielS

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: sf::Event::LostFocus
« Reply #4 on: June 21, 2013, 10:40:56 pm »
The example I give you should present the problem I am talking about. I have no idea where it comes from tho :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Event::LostFocus
« Reply #5 on: June 21, 2013, 10:44:11 pm »
If possible, post a complete and minimal example rather than incomplete parts of your original code.

The problem is about focus events with two windows: write a simple main which creates two windows and show the focus events ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Event::LostFocus
« Reply #6 on: June 21, 2013, 11:46:05 pm »
To know how the minimal example should look, check out this post.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GabrielS

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: sf::Event::LostFocus
« Reply #7 on: June 24, 2013, 03:19:39 pm »
Hello,

I managed to get exactly the same issue (wasn't so easy actually, I do believe there's a problem of mine somewhere - Well here is the minimal code :
#include <iostream>
#include <string>

using namespace std;

void createWindowAux(sf::RenderWindow& window)
{
        if (!window.isOpen())
        window.create(sf::VideoMode(400,400),
                "2nd window", sf::Style::Titlebar | sf::Style::Close);
}

 int main ()
{
        sf ::RenderWindow windowMain;
        windowMain.create(sf::VideoMode(700,800), "Simple Test");
        sf::RenderWindow windowAux;
        bool mIsComponentActive = false;

    while (windowMain.isOpen())
    {
        sf::Event event;
        while (windowMain.pollEvent(event))
        {
            switch (event.type)
                        {
                case sf::Event::Closed: // closing using the cross button of the window
                                        windowMain.close(); // close the main window and gets out of this loop
                                break;

                                case sf::Event::MouseButtonPressed:
                                        mIsComponentActive = false;
                                        sf::Vector2i localPosition = sf::Mouse::getPosition(windowMain);
                                        if (localPosition.y < 30)
                                        {
                                                mIsComponentActive = true;
                                                createWindowAux(windowAux);
                                        }
                                        windowAux.display();
                                break;
                        }
                }
                while (windowAux.pollEvent(event))
        {
                        if (mIsComponentActive)
                        {
                                switch (event.type)
                                {
                                        case sf::Event::Closed: // closing using the cross button of the window
                                                windowAux.close(); // close the main window and gets out of this loop
                                        break;

                                        case sf::Event::LostFocus:
                                                cout << "LOSTFOCUS" << endl;
                                                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                                        break;

                                        case sf::Event::GainedFocus :
                                                windowAux.clear(COLOR_BACKGROUND_WINDOW);
                                                // put objects here
                                                windowAux.display();
                                                cout << "GAINFOCUS" << endl;
                                        break;
                                }
                        }
                }
        }

    return 0;
}

The sequence of events I am doing to get the 'bug' is the following one :
  • first click on the top of the first window which displayed ("simple test" window)
    You should have windowAux opened now
  • Then click on windowMain : you don't get 'LOSTFOCUS' in cout.
    Moreover, if you try to open windowAux from the taskbar of window, you will only be able to minimize it (closing it will not work)

Now, do exactly the same things, but instead of clicking on windowMain to get back to this window, just click on another program opened : this time you get the even 'LOSTFOCUS'.

I hope it's clear enough. Again, the bug is not that easy to explain too, please ask me questions if you do not understand something I'm saying.

Thanks

Edit : I do see the bug... It comes from my boolean. Mmhh, I'll try to manage this ;-)
« Last Edit: June 24, 2013, 07:15:38 pm by GabrielS »

 

anything