Bullet(), BulletText(): vertical and horizontal alignment fixes (namely for widget sized line height). (followup to #414, #282)

This commit is contained in:
ocornut 2015-12-13 23:41:02 +00:00
parent 558430ca8f
commit 2e5352786a
2 changed files with 23 additions and 12 deletions

View File

@ -5583,7 +5583,7 @@ bool ImGui::CollapsingHeader(const char* label, const char* str_id, bool display
const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImVec2 label_size = CalcTextSize(label, NULL, true);
// We vertically grow up to current line height up the typical widget height. // We vertically grow up to current line height up the typical widget height.
const float text_base_offset_y = ImMax(0.0f, window->DC.CurrentLineTextBaseOffset - padding.y); const float text_base_offset_y = ImMax(0.0f, window->DC.CurrentLineTextBaseOffset - padding.y); // Latch before ItemSize changes it
const float frame_height = ImMax(ImMin(window->DC.CurrentLineHeight, g.FontSize + g.Style.FramePadding.y*2), label_size.y + padding.y*2); const float frame_height = ImMax(ImMin(window->DC.CurrentLineHeight, g.FontSize + g.Style.FramePadding.y*2), label_size.y + padding.y*2);
ImRect bb = ImRect(window->DC.CursorPos, ImVec2(window->Pos.x + GetContentRegionMax().x, window->DC.CursorPos.y + frame_height)); ImRect bb = ImRect(window->DC.CursorPos, ImVec2(window->Pos.x + GetContentRegionMax().x, window->DC.CursorPos.y + frame_height));
if (display_frame) if (display_frame)
@ -5657,21 +5657,21 @@ void ImGui::Bullet()
ImGuiState& g = *GImGui; ImGuiState& g = *GImGui;
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const float line_height = g.FontSize; const float line_height = ImMax(ImMin(window->DC.CurrentLineHeight, g.FontSize + g.Style.FramePadding.y*2), g.FontSize);
const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(line_height, line_height)); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize, line_height));
ItemSize(bb); ItemSize(bb);
if (!ItemAdd(bb, NULL)) if (!ItemAdd(bb, NULL))
{ {
ImGui::SameLine(0, -1); ImGui::SameLine(0, style.FramePadding.x*2);
return; return;
} }
// Render // Render
const float bullet_size = line_height*0.15f; const float bullet_size = g.FontSize*0.15f;
window->DrawList->AddCircleFilled(bb.Min + ImVec2(style.FramePadding.x + line_height*0.5f, line_height*0.5f), bullet_size, GetColorU32(ImGuiCol_Text)); window->DrawList->AddCircleFilled(bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f), bullet_size, GetColorU32(ImGuiCol_Text));
// Stay on same line // Stay on same line
ImGui::SameLine(0, -1); ImGui::SameLine(0, style.FramePadding.x*2);
} }
// Text with a little bullet aligned to the typical tree node. // Text with a little bullet aligned to the typical tree node.
@ -5686,17 +5686,18 @@ void ImGui::BulletTextV(const char* fmt, va_list args)
const char* text_begin = g.TempBuffer; const char* text_begin = g.TempBuffer;
const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);
const float line_height = g.FontSize;
const ImVec2 label_size = CalcTextSize(text_begin, text_end, true); const ImVec2 label_size = CalcTextSize(text_begin, text_end, true);
const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(line_height + (label_size.x > 0.0f ? (style.FramePadding.x*2) : 0.0f),0) + label_size); // Empty text doesn't add padding const float text_base_offset_y = ImMax(0.0f, window->DC.CurrentLineTextBaseOffset); // Latch before ItemSize changes it
const float line_height = ImMax(ImMin(window->DC.CurrentLineHeight, g.FontSize + g.Style.FramePadding.y*2), g.FontSize);
const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x*2) : 0.0f), ImMax(line_height, label_size.y))); // Empty text doesn't add padding
ItemSize(bb); ItemSize(bb);
if (!ItemAdd(bb, NULL)) if (!ItemAdd(bb, NULL))
return; return;
// Render // Render
const float bullet_size = line_height*0.15f; const float bullet_size = g.FontSize*0.15f;
window->DrawList->AddCircleFilled(bb.Min + ImVec2(style.FramePadding.x + line_height*0.5f, line_height*0.5f), bullet_size, GetColorU32(ImGuiCol_Text)); window->DrawList->AddCircleFilled(bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f), bullet_size, GetColorU32(ImGuiCol_Text));
RenderText(bb.Min+ImVec2(g.FontSize + style.FramePadding.x*2,0), text_begin, text_end); RenderText(bb.Min+ImVec2(g.FontSize + style.FramePadding.x*2, text_base_offset_y), text_begin, text_end);
} }
void ImGui::BulletText(const char* fmt, ...) void ImGui::BulletText(const char* fmt, ...)

View File

@ -991,6 +991,15 @@ void ImGui::ShowTestWindow(bool* p_opened)
ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##2"); ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##2");
if (tree_opened) { for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); } // Dummy tree data if (tree_opened) { for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); } // Dummy tree data
// Bullet
ImGui::Button("Button##3");
ImGui::SameLine(0.0f, spacing);
ImGui::BulletText("Bullet text");
ImGui::AlignFirstTextHeightToWidgets();
ImGui::BulletText("Node");
ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##4");
ImGui::TreePop(); ImGui::TreePop();
} }
@ -2261,6 +2270,7 @@ static void ShowExampleAppPropertyEditor(bool* opened)
//ImGui::Text("Field_%d", i); //ImGui::Text("Field_%d", i);
char label[32]; char label[32];
sprintf(label, "Field_%d", i); sprintf(label, "Field_%d", i);
ImGui::Bullet();
ImGui::Selectable(label); ImGui::Selectable(label);
ImGui::NextColumn(); ImGui::NextColumn();
ImGui::PushItemWidth(-1); ImGui::PushItemWidth(-1);