The window doesn't seem to receive a GainedFocus event if the window is recreated after losing focus.
Here's an example:
#include <SFML/Graphics.hpp>
void createWindow(sf::RenderWindow& window)
{
window.create(sf::VideoMode(800, 600), "Gained Focus Event Failing", sf::Style::Default);
}
int main()
{
sf::RenderWindow window;
createWindow(window);
sf::Color color{ sf::Color::Green };
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.type == sf::Event::LostFocus)
{
createWindow(window); // this stops GainedFocus from working
color = sf::Color::Red;
}
else if (event.type == sf::Event::GainedFocus)
{
//createWindow(window); // this is fine if commented out or not
color = sf::Color::Green;
}
}
window.clear(color);
window.display();
}
return EXIT_SUCCESS;
}
The code changes the window to red when it's not in focus and green when it is in focus.
When the window is recreated after losing focus, when returning focus to the window, it doesn't receive the GainedFocus event and therefore no longer turns green.
In the code, if you comment out the createWindow line (before turning color to red), the event works fine. In addition, if you uncomment the createWindow line (before turning color to green), it makes no difference (lost focus still works immediately). In fact, even after regaining focus but not receiving the GainedFocus event, the window will still receive LostFocus events.
It's obvious that event is still for the correct window as you can still close the window that can't regain focus.
EDIT: Win7, VS2013, SFML2.3 debug and release (32-bit).
EDIT 2: modified thread subject title.
The title is misleading, because the event keeps working fine, it's just that the first gain event is not triggered. ;)
hasFocus() will also report the focus correctly.
#include <SFML/Graphics.hpp>
#include <iostream>
void createWindow(sf::RenderWindow& window)
{
window.create(sf::VideoMode(800, 600), "Gained Focus Event Failing", sf::Style::Default);
}
int main()
{
sf::RenderWindow window;
createWindow(window);
bool recreated = false;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.type == sf::Event::LostFocus)
{
std::cout << "LostFocus" << std::endl;
if (!recreated)
{
recreated = true;
createWindow(window); // this stops GainedFocus from working
}
}
else if (event.type == sf::Event::GainedFocus)
{
//createWindow(window); // this is fine if commented out or not
std::cout << "GainedFocus" << std::endl;
}
else if (event.type == sf::Event::KeyReleased)
{
std::cout << "Has focus: " << window.hasFocus() << std::endl;
if (event.key.code == sf::Keyboard::Space)
createWindow(window); // this stops GainedFocus from working
}
}
window.clear();
window.display();
}
}
It's quite a corner case and someone will have to look at the underlying code whether this intentional or not. ;)