From 8237ab450e3825e0888892c23d657b2bfaf18e47 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 12 Dec 2024 18:48:42 +0100 Subject: [PATCH] Drags, Sliders: store initial value on activation, as a convenience for some mods. (#8223) --- imgui.cpp | 1 + imgui_internal.h | 1 + imgui_widgets.cpp | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/imgui.cpp b/imgui.cpp index 7325c1952..3aa3b1fbf 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3927,6 +3927,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas) ActiveIdPreviousFrameIsAlive = false; ActiveIdPreviousFrameHasBeenEditedBefore = false; ActiveIdPreviousFrameWindow = NULL; + memset(&ActiveIdValueOnActivation, 0, sizeof(ActiveIdValueOnActivation)); LastActiveId = 0; LastActiveIdTimer = 0.0f; diff --git a/imgui_internal.h b/imgui_internal.h index fed655e03..14fea3691 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -2109,6 +2109,7 @@ struct ImGuiContext bool ActiveIdPreviousFrameIsAlive; bool ActiveIdPreviousFrameHasBeenEditedBefore; ImGuiWindow* ActiveIdPreviousFrameWindow; + ImGuiDataTypeStorage ActiveIdValueOnActivation; // Backup of initial value at the time of activation. ONLY SET BY SPECIFIC WIDGETS: DragXXX and SliderXXX. ImGuiID LastActiveId; // Store the last non-zero ActiveId, useful for animation. float LastActiveIdTimer; // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation. diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 82b5714fc..4074a9d73 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -2632,6 +2632,10 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, temp_input_is_active = true; } + // Store initial value (not used by main lib but available as a convenience but some mods e.g. to revert) + if (make_active) + memcpy(&g.ActiveIdValueOnActivation, p_data, DataTypeGetInfo(data_type)->Size); + if (make_active && !temp_input_is_active) { SetActiveID(id, window); @@ -3222,6 +3226,10 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if ((clicked && g.IO.KeyCtrl) || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))) temp_input_is_active = true; + // Store initial value (not used by main lib but available as a convenience but some mods e.g. to revert) + if (make_active) + memcpy(&g.ActiveIdValueOnActivation, p_data, DataTypeGetInfo(data_type)->Size); + if (make_active && !temp_input_is_active) { SetActiveID(id, window);