Skip to content

Commit

Permalink
ImGui: Zooming with mouse wheel now always scales the whole window, i…
Browse files Browse the repository at this point in the history
…nstead of each element individually
  • Loading branch information
wofsauge committed Nov 6, 2024
1 parent 6f164f1 commit dfd8575
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 0 additions & 2 deletions repentogon/ImGuiFeatures/ConsoleMega.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ struct ConsoleMega : ImGuiWindowObject {
AddWindowContextMenu();
std::deque<Console_HistoryEntry>* history = &g_Game->GetConsole()->_history;

ImGui::SetWindowFontScale(1.0f);

// fill remaining window space minus the current font size (+ padding). fixes issue where the input is outside the window frame
bool textInputScrollbarVisible = imFontUnifont->CalcTextSizeA(imFontUnifont->FontSize, FLT_MAX, 0.0f, inputBuf, inputBuf + strlen(inputBuf)).x * imFontUnifont->Scale > ImGui::GetContentRegionAvail().x;
float textboxHeight = -4 - (ImGui::GetStyle().FramePadding.y * 2) - (imFontUnifont->Scale * imFontUnifont->FontSize) - (textInputScrollbarVisible ? 14 : 0);
Expand Down
29 changes: 28 additions & 1 deletion repentogon/ImGuiFeatures/ImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,32 @@ LRESULT CALLBACK windowProc_hook(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa
return CallWindowProc(windowProc, hWnd, uMsg, wParam, lParam);
}

// Manual handling of Window scaling via CTRL + Scroll, to only scale a window as a whole, and not every element seperately
void HandleZoomWithMouseWheel() {
ImGuiContext& g = *GImGui;
if (g.HoveredWindow && g.IO.MouseWheel != 0.0f && !g.HoveredWindow->Collapsed)
{
// change scale operation to be executed on window root
ImGuiWindow* window = g.HoveredWindow;
while (window->ParentWindow != nullptr) {
window = window->ParentWindow;
}

if (g.IO.KeyCtrl)
{
// Zoom / Scale window. Based on imgui v1.45 implementation because new one calls a windows function to set the new window positions
// new imgui function impl in file: ..\REPENTOGON\libs\imgui\imgui.cpp - Line 9137 (Zoom / Scale window)
float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f);
float scale = new_font_scale / window->FontWindowScale;
const ImVec2 offset = ImVec2(window->Size.x * (1.0f - scale) * (g.IO.MousePos.x - window->Pos.x) / window->Size.x,
window->Size.y * (1.0f - scale) * (g.IO.MousePos.y - window->Pos.y) / window->Size.y);
window->Pos = ImVec2(window->Pos.x + offset.x, window->Pos.y + offset.y);
window->Size = ImVec2(window->Size.x * scale, window->Size.y * scale);
window->SizeFull = ImVec2(window->SizeFull.x * scale, window->SizeFull.y * scale);
window->FontWindowScale = new_font_scale;
}
}
}


//luamod error popup
Expand Down Expand Up @@ -509,6 +534,7 @@ void __stdcall RunImGui(HDC hdc) {
// mouse, keyboard and gamepad support
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
io.FontAllowUserScaling = false; // disable mouse wheel zoom. We handle it ourselfs
ImGui::CaptureMouseFromApp();
ImGui::CaptureKeyboardFromApp();
ImFontConfig cfg;
Expand Down Expand Up @@ -558,7 +584,6 @@ void __stdcall RunImGui(HDC hdc) {
io.Fonts->AddFontFromFileTTF("resources-repentogon\\fonts\\Font Awesome 6 Free-Solid-900.otf", font_base_size, &cfg, icon_ranges);

imguiInitialized = true;
ImGui::GetIO().FontAllowUserScaling = true;
logViewer.AddLog("[REPENTOGON]", "Initialized Dear ImGui v%s\n", IMGUI_VERSION);
printf("[REPENTOGON] Dear ImGui v%s initialized! Any further logs can be seen in the in-game log viewer.\n", IMGUI_VERSION);
}
Expand Down Expand Up @@ -629,6 +654,8 @@ void __stdcall RunImGui(HDC hdc) {
// notifications last, to force them to overlap everything
notificationHandler.Draw(menuShown);

HandleZoomWithMouseWheel();

ImGui::Render();


Expand Down

0 comments on commit dfd8575

Please sign in to comment.