Skip to content

Commit

Permalink
Fixed SDL(3)_SetRenderScale handling
Browse files Browse the repository at this point in the history
Fixed #6065
  • Loading branch information
bog-dan-ro committed Dec 29, 2023
1 parent 4a24264 commit 293bc5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
20 changes: 12 additions & 8 deletions backends/imgui_impl_sdl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct ImGui_ImplSDL3_Data
int PendingMouseLeaveFrame;
char* ClipboardTextData;
bool MouseCanUseGlobalState;

ImVec2 RenderScale() const { ImVec2 res; SDL_GetRenderScale(Renderer, &res.x, &res.y); return res;}
ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); }
};

Expand Down Expand Up @@ -264,19 +264,20 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
{
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
mouse_pos /= bd->RenderScale();
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
return true;
}
case SDL_EVENT_MOUSE_WHEEL:
{
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
float wheel_x = -event->wheel.x;
float wheel_y = event->wheel.y;
ImVec2 wheel(-event->wheel.x, event->wheel.y);
#ifdef __EMSCRIPTEN__
wheel_x /= 100.0f;
wheel.x /= 100.0f;
#endif
io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
io.AddMouseWheelEvent(wheel_x, wheel_y);
wheel /= bd->RenderScale();
io.AddMouseWheelEvent(wheel.x, wheel.y);
return true;
}
case SDL_EVENT_MOUSE_BUTTON_DOWN:
Expand Down Expand Up @@ -488,11 +489,14 @@ static void ImGui_ImplSDL3_UpdateMouseData()
if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0)
{
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
float mouse_x_global, mouse_y_global;
ImVec2 mouse_global;
int window_x, window_y;
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
SDL_GetGlobalMouseState(&mouse_global.x, &mouse_global.y);
SDL_GetWindowPosition(focused_window, &window_x, &window_y);
io.AddMousePosEvent(mouse_x_global - window_x, mouse_y_global - window_y);
mouse_global.x -= window_x;
mouse_global.y -= window_y;
mouse_global /= bd->RenderScale();
io.AddMousePosEvent(mouse_global.x, mouse_global.y);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ struct ImVec2
#ifdef IM_VEC2_CLASS_EXTRA
IM_VEC2_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2.
#endif
inline ImVec2 & operator /=(ImVec2 other) {x /= other.x; y /= other.y; return *this; }
};

// ImVec4: 4D vector used to store clipping rectangles, colors etc. [Compile-time configurable type]
Expand Down

0 comments on commit 293bc5a

Please sign in to comment.