Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text in "InputInt" is not centered. #8397

Open
gilpazintel opened this issue Feb 13, 2025 · 0 comments
Open

Text in "InputInt" is not centered. #8397

gilpazintel opened this issue Feb 13, 2025 · 0 comments

Comments

@gilpazintel
Copy link

Version/Branch of Dear ImGui:

Version 1.90.9

Back-ends:

imgui_impl_XXX.cpp + imgui_impl_XXX.cpp

Compiler, OS:

Windows 11

Full config/build information:

No response

Details:

this issue is as a follow-up of issue: https://github.com/ocornut/imgui/issues/7344
as commented in the above issue, the center of the text is not implemented yet.
I want to make the text center and use a minimum extra code as possible.
could you please advise if the below is the best way to do it?

note: although modifying the ImGui source at me side would be cleaner, I want to avoid it so further update of ImGui will be easy to merge.

thank you

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

bool RsImGui::InputIntCentered(const char* label, int* v, int step)
{
const char* format = "%d";
int step_fast = 100;
ImGuiWindow* window = ImGui::GetCurrentWindow();
const void* p_step = (const void*)(step ? &step : NULL);
const void* p_step_fast = &step_fast;
if (window->SkipItems)
return false;

ImGuiDataType data_type = ImGuiDataType_S32;

ImGuiContext& g = *GImGui;
ImGuiStyle& style = g.Style;

void* p_data_default = (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasRefVal) ? &g.NextItemData.RefVal : &g.DataTypeZeroValue;

char buf[64];
ImGui::DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, v, format);

ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string.
flags |= (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint;

bool value_changed = false;
bool pushed_style_var = false;


const float button_size = ImGui::GetFrameHeight();

ImGui::BeginGroup(); // The only purpose of the group here is to allow the caller to query item data e.g. IsItemActive()
ImGui::PushID(label);
ImGui::SetNextItemWidth(ImMax(1.0f, ImGui::CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));

pushed_style_var = handleTextCentering(buf, style);

if (ImGui::InputText("", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + "" gives us the expected ID from outside point of view
    value_changed = ImGui::DataTypeApplyFromText(buf, data_type, v, format, (flags & ImGuiInputTextFlags_ParseEmptyRefVal) ? p_data_default : NULL);

if (pushed_style_var)
{
    ImGui::PopStyleVar();
}

// Step buttons
const ImVec2 backup_frame_padding = style.FramePadding;
style.FramePadding.x = style.FramePadding.y;
ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups;

ImGui::SameLine(0, style.ItemInnerSpacing.x);
if (ImGui::ButtonEx("-", ImVec2(button_size, button_size), button_flags))
{
    ImGui::DataTypeApplyOp(data_type, '-', v, v, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
    value_changed = true;
}
ImGui::SameLine(0, style.ItemInnerSpacing.x);
if (ImGui::ButtonEx("+", ImVec2(button_size, button_size), button_flags))
{
    ImGui::DataTypeApplyOp(data_type, '+', v, v, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
    value_changed = true;
}

const char* label_end = ImGui::FindRenderedTextEnd(label);
if (label != label_end)
{
    ImGui::SameLine(0, style.ItemInnerSpacing.x);
    ImGui::TextEx(label, label_end);
}
style.FramePadding = backup_frame_padding;

ImGui::PopID();
ImGui::EndGroup();

if (value_changed)
    ImGui::MarkItemEdited(g.LastItemData.ID);

return value_changed;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant