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

This commit is contained in:
ocornut 2015-02-10 21:42:21 +00:00
parent e7f51172c1
commit 801ed2c472
2 changed files with 5 additions and 4 deletions

View File

@ -3073,7 +3073,6 @@ static void FocusWindow(ImGuiWindow* window)
void ImGui::PushItemWidth(float item_width) void ImGui::PushItemWidth(float item_width)
{ {
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
item_width = (float)(int)item_width;
window->DC.ItemWidth.push_back(item_width == 0.0f ? window->ItemWidthDefault : 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(); float w = window->DC.ItemWidth.back();
if (w < 0.0f) 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; w = -w;
float width_to_right_edge = window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x; 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; return w;
} }

View File

@ -195,7 +195,7 @@ namespace ImGui
IMGUI_API void PopStyleVar(int count = 1); IMGUI_API void PopStyleVar(int count = 1);
// Parameters stacks (current window) // 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 void PopItemWidth();
IMGUI_API float CalcItemWidth(); // width of item given pushed settings and current cursor position 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. IMGUI_API void PushAllowKeyboardFocus(bool v); // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets.