How to right align text in ImGui Columns?
Asked Answered
T

3

9

Suppose I create a table with the following:

ImGui::Columns(3);

ImGui::Text("Header 1");
ImGui::NextColumn();
ImGui::Text("Header 2");
ImGui::NextColumn();
ImGui::Text("Header 3");
ImGui::NextColumn();

ImGui::Text("1");
ImGui::NextColumn();
ImGui::Text("2");
ImGui::NextColumn();
ImGui::Text("3");
ImGui::NextColumn();

ImGui::Columns(1);

How can I get the text in the second row (1, 2, and 3) to be right aligned in the column? I've seen CalcItemWidth and CalcTextSize, but I can't figure out how they work within a multi-column line.

Tennessee answered 21/9, 2019 at 23:0 Comment(0)
B
3

Nearly same code than iHowell answer but new x position should be checked against current position value in order to be well window-border aligned (text will then be right-clipped). In code:

ImGui::NextColumn();
std::string text = "1";
auto posX = (ImGui::GetCursorPosX() + ImGui::GetColumnWidth() - ImGui::CalcTextSize(text.c_str()).x 
    - ImGui::GetScrollX() - 2 * ImGui::GetStyle().ItemSpacing.x);
if(posX > ImGui::GetCursorPosX())
  ImGui::SetCursorPosX(posX);
ImGui::Text("%s", text);
Bandog answered 8/2, 2021 at 20:41 Comment(1)
To be even more clear you could rephrase "same answer than previously" because I have no idea what you are referring to. But the code is somewhat helpful, thanks.Bordiuk
T
14

I received help in the ImGui Discord channel and came up with this solution:

ImGui::NextColumn();
std::string text = "1";
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetColumnWidth() - ImGui::CalcTextSize(text.c_str()).x 
    - ImGui::GetScrollX() - 2 * ImGui::GetStyle().ItemSpacing.x);
ImGui::Text("%s", text);

Edit: @FabriceMollo's answer is better.

Tennessee answered 22/9, 2019 at 19:25 Comment(0)
B
3

Nearly same code than iHowell answer but new x position should be checked against current position value in order to be well window-border aligned (text will then be right-clipped). In code:

ImGui::NextColumn();
std::string text = "1";
auto posX = (ImGui::GetCursorPosX() + ImGui::GetColumnWidth() - ImGui::CalcTextSize(text.c_str()).x 
    - ImGui::GetScrollX() - 2 * ImGui::GetStyle().ItemSpacing.x);
if(posX > ImGui::GetCursorPosX())
  ImGui::SetCursorPosX(posX);
ImGui::Text("%s", text);
Bandog answered 8/2, 2021 at 20:41 Comment(1)
To be even more clear you could rephrase "same answer than previously" because I have no idea what you are referring to. But the code is somewhat helpful, thanks.Bordiuk
C
0

i think this is better

int your_column = 0;
std::string text = "ID";
auto column1_x = (ImGui::GetCursorPosX() + ImGui::GetColumnWidth(your_column) * 0.5 - ImGui::CalcTextSize(text.c_str()).x);
if (column1_x > ImGui::GetCursorPosX())
    ImGui::SetCursorPosX(column1_x);
ImGui::Text("%s", text.c_str());
Camshaft answered 10/2, 2023 at 10:32 Comment(1)
You should add some explanation and/or citations supporting your claim that, "this is better."Lapidate

© 2022 - 2024 — McMap. All rights reserved.