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

Fixed SDL(3)_SetRenderScale handling #7178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renderer is able to be null in cases where SDL_Renderer isn't being used (IE: OpenGL, etc), so this needs to be able to handle that.

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; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are already math operators defined for ImVec2, they're just disabled by default, and none of the backends enable them. Not sure if there's a reason for that or if it was just out of habit.

It might be easier to just match the existing backends and do the math manually per-component for consistency.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are disabled by default as I think it caused issues for users of IM_VEC2_CLASS_EXTRA with own math class + own math operators, though honestly I am not sure of specifics off-hand that that might need a separate investigation.

But yeah at the moment it'd be good if backends didn't rely on those.

};

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