mirror of
				https://github.com/Drezil/imgui.git
				synced 2025-11-03 22:51:06 +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:
		@@ -893,6 +893,25 @@ static void ShowDemoWindowWidgets()
 | 
			
		||||
            }
 | 
			
		||||
            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();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -2825,6 +2844,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
 | 
			
		||||
            ImGui::Text("Alignment");
 | 
			
		||||
            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("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::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f");
 | 
			
		||||
            ImGui::EndTabItem();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user