Przeglądaj źródła

Allow passing -0.01f to PushItemWidth() for a perfect right-side alignment + fix CalcItemWidth()

ocornut 10 lat temu
rodzic
commit
801ed2c472
2 zmienionych plików z 5 dodań i 4 usunięć
  1. 4 3
      imgui.cpp
  2. 1 1
      imgui.h

+ 4 - 3
imgui.cpp

@@ -3073,7 +3073,6 @@ static void FocusWindow(ImGuiWindow* window)
 void ImGui::PushItemWidth(float item_width)
 {
     ImGuiWindow* window = GetCurrentWindow();
-    item_width = (float)(int)item_width;
     window->DC.ItemWidth.push_back(item_width == 0.0f ? window->ItemWidthDefault : item_width);
 }
 
@@ -3089,11 +3088,13 @@ float ImGui::CalcItemWidth()
     float w = window->DC.ItemWidth.back();
     if (w < 0.0f)
     {
-        // Align to a right-side limit
+        // Align to a right-side limit. We include 1 frame padding in the calculation because this is how the width is always used (we add 2 frame padding to it), but we could move that responsibility to the widget as well.
+        ImGuiState& g = *GImGui;
         w = -w;
         float width_to_right_edge = window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x;
-        w = ImMax(1.0f, width_to_right_edge - w);
+        w = ImMax(1.0f, width_to_right_edge - w - g.Style.FramePadding.x * 2.0f);
     }
+    w = (float)(int)w;
     return w;
 }
 

+ 1 - 1
imgui.h

@@ -195,7 +195,7 @@ namespace ImGui
     IMGUI_API void          PopStyleVar(int count = 1);
 
     // Parameters stacks (current window)
-    IMGUI_API void          PushItemWidth(float item_width);                                    // width of items for the common item+label case, pixels. 0.0f = default to ~2/3 of windows width, >0.0f: width in pixels, <0.0f align xx pixels to the right of window
+    IMGUI_API void          PushItemWidth(float item_width);                                    // width of items for the common item+label case, pixels. 0.0f = default to ~2/3 of windows width, >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -0.01f always align width to the right side)
     IMGUI_API void          PopItemWidth();
     IMGUI_API float         CalcItemWidth();                                                    // width of item given pushed settings and current cursor position
     IMGUI_API void          PushAllowKeyboardFocus(bool v);                                     // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets.