Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented hovered state / use window events instead of global functions to query mouse state #261

Closed
wants to merge 13 commits into from
70 changes: 39 additions & 31 deletions imgui-SFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ struct WindowContext {

bool windowHasFocus;
bool mouseMoved{false};
bool mousePressed[3] = {false};
ImGuiMouseCursor lastCursor{ImGuiMouseCursor_COUNT};
bool windowIsHovered = {false};

bool touchDown[3] = {false};
sf::Vector2i touchPos;

unsigned int joystickId{getConnectedJoystickId()};
Expand Down Expand Up @@ -263,6 +262,8 @@ bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultF
return UpdateFontTexture();
}

s_currWindowCtx->windowHasFocus = window.hasFocus();
// TODO : initialize s_currWindowCtx->windowIsHovered
return true;
}

Expand All @@ -274,6 +275,7 @@ void SetCurrentWindow(const sf::Window& window) {
assert(found != s_windowContexts.end() &&
"Failed to find the window. Forgot to call ImGui::SFML::Init for the window?");
s_currWindowCtx = found->get();
s_currWindowCtx->windowHasFocus = window.hasFocus();
ImGui::SetCurrentContext(s_currWindowCtx->imContext);
}

Expand All @@ -293,23 +295,48 @@ void ProcessEvent(const sf::Event& event) {
assert(s_currWindowCtx && "No current window is set - forgot to call ImGui::SFML::Init?");
ImGuiIO& io = ImGui::GetIO();

if (s_currWindowCtx->windowHasFocus) {
switch (event.type) {
case sf::Event::Resized:
io.DisplaySize =
ImVec2(static_cast<float>(event.size.width), static_cast<float>(event.size.height));
break;
case sf::Event::MouseEntered:
s_currWindowCtx->windowIsHovered = true;
break;
case sf::Event::MouseLeft:
s_currWindowCtx->windowIsHovered = false;
break;
case sf::Event::MouseMoved:
io.AddMousePosEvent(static_cast<float>(event.mouseMove.x),
static_cast<float>(event.mouseMove.y));
s_currWindowCtx->mouseMoved = true;
break;
default:
break;
}

if (s_currWindowCtx->windowIsHovered) {
switch (event.type) {
case sf::Event::Resized:
io.DisplaySize =
ImVec2(static_cast<float>(event.size.width), static_cast<float>(event.size.height));
case sf::Event::MouseWheelScrolled:
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel ||
(event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel && io.KeyShift)) {
io.MouseWheel += event.mouseWheelScroll.delta;
} else if (event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel) {
io.MouseWheelH += event.mouseWheelScroll.delta;
}
break;
case sf::Event::MouseMoved:
io.AddMousePosEvent(static_cast<float>(event.mouseMove.x),
static_cast<float>(event.mouseMove.y));
s_currWindowCtx->mouseMoved = true;
default:
break;
}
}

if (s_currWindowCtx->windowHasFocus) {
switch (event.type) {
case sf::Event::MouseButtonPressed: // fall-through
case sf::Event::MouseButtonReleased: {
const int button = event.mouseButton.button;
if (button >= 0 && button < 3) {
if (event.type == sf::Event::MouseButtonPressed) {
s_currWindowCtx->mousePressed[event.mouseButton.button] = true;
io.AddMouseButtonEvent(button, true);
} else {
io.AddMouseButtonEvent(button, false);
Expand All @@ -319,19 +346,7 @@ void ProcessEvent(const sf::Event& event) {
case sf::Event::TouchBegan: // fall-through
case sf::Event::TouchEnded: {
s_currWindowCtx->mouseMoved = false;
const unsigned int button = event.touch.finger;
if (event.type == sf::Event::TouchBegan && button < 3) {
s_currWindowCtx->touchDown[event.touch.finger] = true;
}
} break;
case sf::Event::MouseWheelScrolled:
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel ||
(event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel && io.KeyShift)) {
io.AddMouseWheelEvent(0, event.mouseWheelScroll.delta);
} else if (event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel) {
io.AddMouseWheelEvent(event.mouseWheelScroll.delta, 0);
}
break;
case sf::Event::KeyPressed: // fall-through
case sf::Event::KeyReleased: {
const bool down = (event.type == sf::Event::KeyPressed);
Expand Down Expand Up @@ -422,21 +437,14 @@ void Update(const sf::Vector2i& mousePos, const sf::Vector2f& displaySize, sf::T
io.DisplaySize = ImVec2(displaySize.x, displaySize.y);
io.DeltaTime = dt.asSeconds();

if (s_currWindowCtx->windowHasFocus) {
if (s_currWindowCtx->windowIsHovered || s_currWindowCtx->windowHasFocus) {
if (io.WantSetMousePos) {
const sf::Vector2i newMousePos(static_cast<int>(io.MousePos.x),
static_cast<int>(io.MousePos.y));
sf::Mouse::setPosition(newMousePos);
} else {
io.MousePos = ImVec2(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
}
for (unsigned int i = 0; i < 3; i++) {
io.MouseDown[i] = s_currWindowCtx->touchDown[i] || sf::Touch::isDown(i) ||
s_currWindowCtx->mousePressed[i] ||
sf::Mouse::isButtonPressed((sf::Mouse::Button)i);
s_currWindowCtx->mousePressed[i] = false;
s_currWindowCtx->touchDown[i] = false;
}
}

#ifdef ANDROID
Expand Down