Skip to content

Commit

Permalink
move centered edit box from 3rd party to RsImGui
Browse files Browse the repository at this point in the history
  • Loading branch information
gilpazintel committed Feb 10, 2025
1 parent a741ab0 commit 6e06457
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 49 deletions.
14 changes: 6 additions & 8 deletions third-party/imgui/imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,6 @@ namespace ImGui

// Text Utilities
IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f);
IMGUI_API bool handleTextCentering(const char* buf, ImGuiStyle& style); // add pedding to center text in a column, return true if pedding was done.

// Color Utilities
IMGUI_API ImVec4 ColorConvertU32ToFloat4(ImU32 in);
Expand Down Expand Up @@ -1110,15 +1109,14 @@ enum ImGuiInputTextFlags_
ImGuiInputTextFlags_DisplayEmptyRefVal = 1 << 14, // InputFloat(), InputInt(), InputScalar() etc. only: when value is zero, do not display it. Generally used with ImGuiInputTextFlags_ParseEmptyRefVal.
ImGuiInputTextFlags_NoHorizontalScroll = 1 << 15, // Disable following the cursor horizontally
ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID().
ImGuiInputTextFlags_CenterText = 1 << 17, // centering text

// Callback features
ImGuiInputTextFlags_CallbackCompletion = 1 << 18, // Callback on pressing TAB (for completion handling)
ImGuiInputTextFlags_CallbackHistory = 1 << 19, // Callback on pressing Up/Down arrows (for history handling)
ImGuiInputTextFlags_CallbackAlways = 1 << 20, // Callback on each iteration. User code may query cursor position, modify text buffer.
ImGuiInputTextFlags_CallbackCharFilter = 1 << 21, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.
ImGuiInputTextFlags_CallbackResize = 1 << 22, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this)
ImGuiInputTextFlags_CallbackEdit = 1 << 23, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active)
ImGuiInputTextFlags_CallbackCompletion = 1 << 17, // Callback on pressing TAB (for completion handling)
ImGuiInputTextFlags_CallbackHistory = 1 << 18, // Callback on pressing Up/Down arrows (for history handling)
ImGuiInputTextFlags_CallbackAlways = 1 << 19, // Callback on each iteration. User code may query cursor position, modify text buffer.
ImGuiInputTextFlags_CallbackCharFilter = 1 << 20, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.
ImGuiInputTextFlags_CallbackResize = 1 << 21, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this)
ImGuiInputTextFlags_CallbackEdit = 1 << 22, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active)

// Obsolete names
//ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior
Expand Down
39 changes: 0 additions & 39 deletions third-party/imgui/imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3521,22 +3521,6 @@ void ImGui::SetNextItemRefVal(ImGuiDataType data_type, void* p_data)
memcpy(&g.NextItemData.RefVal, p_data, DataTypeGetInfo(data_type)->Size);
}

bool ImGui::handleTextCentering(const char* buf, ImGuiStyle& style)
{
bool pushed_style_var = false;
float text_width = CalcTextSize(buf).x;
float input_box_width = CalcItemWidth();

float padding = (input_box_width - text_width) / 2.0f;
if (padding > 0)
{
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(padding, style.FramePadding.y));
pushed_style_var = true;
}

return pushed_style_var;
}

// Note: p_data, p_step, p_step_fast are _pointers_ to a memory address holding the data. For an Input widget, p_step and p_step_fast are optional.
// Read code of e.g. InputFloat(), InputInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly.
bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step, const void* p_step_fast, const char* format, ImGuiInputTextFlags flags)
Expand All @@ -3563,21 +3547,10 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data
flags |= (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint;

bool value_changed = false;
bool pushed_style_var = false;

if (p_step == NULL)
{
if (flags & ImGuiInputTextFlags_CenterText)
{
pushed_style_var = handleTextCentering(buf, style);
}
if (InputText(label, buf, IM_ARRAYSIZE(buf), flags))
value_changed = DataTypeApplyFromText(buf, data_type, p_data, format, (flags & ImGuiInputTextFlags_ParseEmptyRefVal) ? p_data_default : NULL);

if (pushed_style_var)
{
ImGui::PopStyleVar();
}
}
else
{
Expand All @@ -3586,20 +3559,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data
BeginGroup(); // The only purpose of the group here is to allow the caller to query item data e.g. IsItemActive()
PushID(label);
SetNextItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));

if (flags & ImGuiInputTextFlags_CenterText)
{
pushed_style_var = handleTextCentering(buf, style);
}

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

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

IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable);

// Step buttons
Expand Down
118 changes: 118 additions & 0 deletions third-party/imgui/realsense_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,124 @@ float RsImGui::RoundScalar(float value, int decimal_precision)
return negative ? -value : value;
}

bool handleTextCentering(const char* buf, ImGuiStyle& style)
{
bool pushed_style_var = false;
float text_width = ImGui::CalcTextSize(buf).x;
float input_box_width = ImGui::CalcItemWidth();

float padding = (input_box_width - text_width) / 2.0f;
if (padding > 0)
{
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(padding, style.FramePadding.y));
pushed_style_var = true;
}

return pushed_style_var;
}

bool RsImGui::InputIntCentered(const char* label, int* v, int step, int step_fast, ImGuiInputTextFlags flags)
{
// Hexadecimal input provided as a convenience but the flag name is awkward. Typically you'd use InputText() to parse your own data, if you want to handle prefixes.
const char* format = (flags & ImGuiInputTextFlags_CharsHexadecimal) ? "%08X" : "%d";
ImGuiWindow* window = ImGui::GetCurrentWindow();
const void* p_step = (const void*)(step ? &step : NULL);
const void* p_step_fast = (const void*)(step_fast ? &step_fast : NULL);
if (window->SkipItems)
return false;

ImGuiDataType data_type = ImGuiDataType_S32;

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

if (format == NULL)
format = ImGui::DataTypeGetInfo(data_type)->PrintFmt;

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

char buf[64];
if ((flags & ImGuiInputTextFlags_DisplayEmptyRefVal) && ImGui::DataTypeCompare(data_type, v, p_data_default) == 0)
buf[0] = 0;
else
ImGui::DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, v, format);

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;

if (p_step == NULL)
{
pushed_style_var = handleTextCentering(buf, style);

if (ImGui::InputText(label, buf, IM_ARRAYSIZE(buf), flags))
value_changed = ImGui::DataTypeApplyFromText(buf, data_type, v, format, (flags & ImGuiInputTextFlags_ParseEmptyRefVal) ? p_data_default : NULL);

if (pushed_style_var)
{
ImGui::PopStyleVar();
}
}
else
{
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();
}

IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable);

// Step buttons
const ImVec2 backup_frame_padding = style.FramePadding;
style.FramePadding.x = style.FramePadding.y;
ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups;
if (flags & ImGuiInputTextFlags_ReadOnly)
ImGui::BeginDisabled();
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;
}
if (flags & ImGuiInputTextFlags_ReadOnly)
ImGui::EndDisabled();

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;
}

bool RsImGui::CustomComboBox(const char* label, int* current_item, const char* const items[], int items_count)
{
bool value_changed = false;
Expand Down
1 change: 1 addition & 0 deletions third-party/imgui/realsense_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace RsImGui
};
bool SliderIntWithSteps(const char* label, int* v, int v_min, int v_max, int v_step = 1);
float RoundScalar(float value, int decimal_precision);
bool InputIntCentered(const char* label, int* v, int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0);
bool CustomComboBox(const char* label, int* current_item, const char* const items[], int items_count);
bool SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags, bool render_bg);
bool VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* display_format, float power, bool render_bg);
Expand Down
4 changes: 2 additions & 2 deletions tools/depth-quality/depth-quality-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ namespace rs2
if (_use_ground_truth)
{
ImGui::PushItemWidth(120);
if (ImGui::InputInt("##GT", &_ground_truth, 1, 100, ImGuiInputTextFlags_CenterText))
if (RsImGui::InputIntCentered("##GT", &_ground_truth, 1))
{
_metrics_model.set_ground_truth(_ground_truth);
}
Expand Down Expand Up @@ -751,7 +751,7 @@ namespace rs2
{
ImGui::SameLine(); ImGui::SetCursorPosX(col1);
ImGui::PushItemWidth(120);
if (ImGui::InputInt("##LC", &_limit_capture, 1, 100, ImGuiInputTextFlags_CenterText))
if (RsImGui::InputIntCentered("##LC", &_limit_capture, 1))
{
// Ensure the value is positive
if (_limit_capture <= 0)
Expand Down

0 comments on commit 6e06457

Please sign in to comment.