mirror of
				https://github.com/Drezil/imgui.git
				synced 2025-11-04 07:01:04 +01:00 
			
		
		
		
	Selectable: add support for specifying text alignment on selectables (#2347)
Adds a style variable to Selectable that allows clients to specify the text alignment within Selectables, adds a section in the demo to demonstrate selectable text alignment, and a pair of sliders in the style editor to change selectable alignment on the fly. In terms of implementation, this one is extremely simple: Selectable was already calling an API that supports text alignment, but had hard-coded it to top-left. This changes that to just pass the style variable straight through to RenderTextClipped. Backwards-compatibility is preserved by defaulting the text_align parameter to (0, 0), i.e., top-left. This also fixes a bug with selectable text rendering that caused right-aligned text in a selectable to be clipped incorrectly, because the wrong clipping rectangle was being used.
This commit is contained in:
		
							
								
								
									
										46
									
								
								imgui.cpp
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								imgui.cpp
									
									
									
									
									
								
							@@ -1109,6 +1109,7 @@ ImGuiStyle::ImGuiStyle()
 | 
				
			|||||||
    AntiAliasedLines        = true;             // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU.
 | 
					    AntiAliasedLines        = true;             // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU.
 | 
				
			||||||
    AntiAliasedFill         = true;             // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)
 | 
					    AntiAliasedFill         = true;             // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)
 | 
				
			||||||
    CurveTessellationTol    = 1.25f;            // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.
 | 
					    CurveTessellationTol    = 1.25f;            // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.
 | 
				
			||||||
 | 
					    SelectableTextAlign     = ImVec2(0,0);      // Alignment of selectable text when button is larger than text.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Default theme
 | 
					    // Default theme
 | 
				
			||||||
    ImGui::StyleColorsDark(this);
 | 
					    ImGui::StyleColorsDark(this);
 | 
				
			||||||
@@ -5794,28 +5795,29 @@ struct ImGuiStyleVarInfo
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static const ImGuiStyleVarInfo GStyleVarInfo[] =
 | 
					static const ImGuiStyleVarInfo GStyleVarInfo[] =
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) },              // ImGuiStyleVar_Alpha
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) },               // ImGuiStyleVar_Alpha
 | 
				
			||||||
    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) },      // ImGuiStyleVar_WindowPadding
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) },       // ImGuiStyleVar_WindowPadding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) },     // ImGuiStyleVar_WindowRounding
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) },      // ImGuiStyleVar_WindowRounding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) },   // ImGuiStyleVar_WindowBorderSize
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) },    // ImGuiStyleVar_WindowBorderSize
 | 
				
			||||||
    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) },      // ImGuiStyleVar_WindowMinSize
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) },       // ImGuiStyleVar_WindowMinSize
 | 
				
			||||||
    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) },   // ImGuiStyleVar_WindowTitleAlign
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) },    // ImGuiStyleVar_WindowTitleAlign
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) },      // ImGuiStyleVar_ChildRounding
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) },       // ImGuiStyleVar_ChildRounding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) },    // ImGuiStyleVar_ChildBorderSize
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) },     // ImGuiStyleVar_ChildBorderSize
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) },      // ImGuiStyleVar_PopupRounding
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) },       // ImGuiStyleVar_PopupRounding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) },    // ImGuiStyleVar_PopupBorderSize
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) },     // ImGuiStyleVar_PopupBorderSize
 | 
				
			||||||
    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) },       // ImGuiStyleVar_FramePadding
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) },        // ImGuiStyleVar_FramePadding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) },      // ImGuiStyleVar_FrameRounding
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) },       // ImGuiStyleVar_FrameRounding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) },    // ImGuiStyleVar_FrameBorderSize
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) },     // ImGuiStyleVar_FrameBorderSize
 | 
				
			||||||
    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) },        // ImGuiStyleVar_ItemSpacing
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) },         // ImGuiStyleVar_ItemSpacing
 | 
				
			||||||
    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) },   // ImGuiStyleVar_ItemInnerSpacing
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) },    // ImGuiStyleVar_ItemInnerSpacing
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) },      // ImGuiStyleVar_IndentSpacing
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) },       // ImGuiStyleVar_IndentSpacing
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) },      // ImGuiStyleVar_ScrollbarSize
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) },       // ImGuiStyleVar_ScrollbarSize
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) },  // ImGuiStyleVar_ScrollbarRounding
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) },   // ImGuiStyleVar_ScrollbarRounding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) },        // ImGuiStyleVar_GrabMinSize
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) },         // ImGuiStyleVar_GrabMinSize
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) },       // ImGuiStyleVar_GrabRounding
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) },        // ImGuiStyleVar_GrabRounding
 | 
				
			||||||
    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) },        // ImGuiStyleVar_TabRounding
 | 
					    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) },         // ImGuiStyleVar_TabRounding
 | 
				
			||||||
    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) },    // ImGuiStyleVar_ButtonTextAlign
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) },     // ImGuiStyleVar_ButtonTextAlign
 | 
				
			||||||
 | 
					    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx)
 | 
					static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								imgui.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								imgui.h
									
									
									
									
									
								
							@@ -1082,6 +1082,7 @@ enum ImGuiStyleVar_
 | 
				
			|||||||
    ImGuiStyleVar_GrabRounding,        // float     GrabRounding
 | 
					    ImGuiStyleVar_GrabRounding,        // float     GrabRounding
 | 
				
			||||||
    ImGuiStyleVar_TabRounding,         // float     TabRounding
 | 
					    ImGuiStyleVar_TabRounding,         // float     TabRounding
 | 
				
			||||||
    ImGuiStyleVar_ButtonTextAlign,     // ImVec2    ButtonTextAlign
 | 
					    ImGuiStyleVar_ButtonTextAlign,     // ImVec2    ButtonTextAlign
 | 
				
			||||||
 | 
					    ImGuiStyleVar_SelectableTextAlign, // ImVec2    SelectableTextAlign
 | 
				
			||||||
    ImGuiStyleVar_COUNT
 | 
					    ImGuiStyleVar_COUNT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Obsolete names (will be removed)
 | 
					    // Obsolete names (will be removed)
 | 
				
			||||||
@@ -1263,6 +1264,7 @@ struct ImGuiStyle
 | 
				
			|||||||
    bool        AntiAliasedLines;           // Enable anti-aliasing on lines/borders. Disable if you are really tight on CPU/GPU.
 | 
					    bool        AntiAliasedLines;           // Enable anti-aliasing on lines/borders. Disable if you are really tight on CPU/GPU.
 | 
				
			||||||
    bool        AntiAliasedFill;            // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)
 | 
					    bool        AntiAliasedFill;            // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)
 | 
				
			||||||
    float       CurveTessellationTol;       // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.
 | 
					    float       CurveTessellationTol;       // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.
 | 
				
			||||||
 | 
					    ImVec2      SelectableTextAlign;        // Alignment of selectable text when selectable is larger than text. Defaults to (0,0) for top-left alignment.
 | 
				
			||||||
    ImVec4      Colors[ImGuiCol_COUNT];
 | 
					    ImVec4      Colors[ImGuiCol_COUNT];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    IMGUI_API ImGuiStyle();
 | 
					    IMGUI_API ImGuiStyle();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -893,6 +893,25 @@ static void ShowDemoWindowWidgets()
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            ImGui::TreePop();
 | 
					            ImGui::TreePop();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        if (ImGui::TreeNode("Alignment"))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            static bool selected[3*3] = { true, false, true, false, true, false, true, false, true };
 | 
				
			||||||
 | 
					            static char name[16];
 | 
				
			||||||
 | 
					            for (int i = 0; i < 3; i++)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                for (int j = 0; j < 3; j++)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    float x = (float) i / 2.f;
 | 
				
			||||||
 | 
					                    float y = (float) j / 2.f;
 | 
				
			||||||
 | 
					                    snprintf(name, IM_ARRAYSIZE(name), "(%.1f,%.1f)", x, y);
 | 
				
			||||||
 | 
					                    ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(x, y));
 | 
				
			||||||
 | 
					                    ImGui::Selectable(name, &selected[3*i+j], 0, ImVec2(70,70));
 | 
				
			||||||
 | 
					                    ImGui::PopStyleVar();
 | 
				
			||||||
 | 
					                    if (j != 2) ImGui::SameLine();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            ImGui::TreePop();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        ImGui::TreePop();
 | 
					        ImGui::TreePop();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -2825,6 +2844,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
 | 
				
			|||||||
            ImGui::Text("Alignment");
 | 
					            ImGui::Text("Alignment");
 | 
				
			||||||
            ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f");
 | 
					            ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f");
 | 
				
			||||||
            ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); ShowHelpMarker("Alignment applies when a button is larger than its text content.");
 | 
					            ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); ShowHelpMarker("Alignment applies when a button is larger than its text content.");
 | 
				
			||||||
 | 
					            ImGui::SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); ShowHelpMarker("Alignment applies when a selectable is larger than its text content.");
 | 
				
			||||||
            ImGui::Text("Safe Area Padding"); ImGui::SameLine(); ShowHelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured).");
 | 
					            ImGui::Text("Safe Area Padding"); ImGui::SameLine(); ShowHelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured).");
 | 
				
			||||||
            ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f");
 | 
					            ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f");
 | 
				
			||||||
            ImGui::EndTabItem();
 | 
					            ImGui::EndTabItem();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5100,7 +5100,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
 | 
					    if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
 | 
				
			||||||
    RenderTextClipped(bb_inner.Min, bb.Max, label, NULL, &label_size, ImVec2(0.0f,0.0f));
 | 
					    RenderTextClipped(bb_inner.Min, bb_inner.Max, label, NULL, &label_size, style.SelectableTextAlign, &bb);
 | 
				
			||||||
    if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor();
 | 
					    if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Automatically close popups
 | 
					    // Automatically close popups
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user