diff --git a/imgui-SFML.cpp b/imgui-SFML.cpp index b3a59bf..ba097dd 100644 --- a/imgui-SFML.cpp +++ b/imgui-SFML.cpp @@ -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()}; @@ -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; } @@ -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); } @@ -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(event.size.width), static_cast(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(event.mouseMove.x), + static_cast(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(event.size.width), static_cast(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(event.mouseMove.x), - static_cast(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); @@ -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); @@ -422,7 +437,7 @@ 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(io.MousePos.x), static_cast(io.MousePos.y)); @@ -430,13 +445,6 @@ void Update(const sf::Vector2i& mousePos, const sf::Vector2f& displaySize, sf::T } else { io.MousePos = ImVec2(static_cast(mousePos.x), static_cast(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