Internals: InputScalarEx: Tweak internals so there is a fast path for the simple case.

(in particular because it makes it clear what the extra code is for, so people tempted to create their own InputText data transform/handler understand how little is actually needed)
docking
omar 7 years ago
parent ed756d474e
commit ef05141a06

@ -10578,13 +10578,6 @@ bool ImGui::InputScalarEx(const char* label, ImGuiDataType data_type, void* data
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const ImVec2 label_size = CalcTextSize(label, NULL, true);
const ImVec2 button_size = ImVec2(GetFrameHeight(), GetFrameHeight());
BeginGroup();
PushID(label);
if (step_ptr)
PushItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size.x + style.ItemInnerSpacing.x)*2));
char buf[64]; char buf[64];
DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, data_ptr, scalar_format); DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, data_ptr, scalar_format);
@ -10593,35 +10586,42 @@ bool ImGui::InputScalarEx(const char* label, ImGuiDataType data_type, void* data
if ((extra_flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0) if ((extra_flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0)
extra_flags |= ImGuiInputTextFlags_CharsDecimal; extra_flags |= ImGuiInputTextFlags_CharsDecimal;
extra_flags |= ImGuiInputTextFlags_AutoSelectAll; extra_flags |= ImGuiInputTextFlags_AutoSelectAll;
if (InputText("", buf, IM_ARRAYSIZE(buf), extra_flags)) // PushId(label) + "" gives us the expected ID from outside point of view
value_changed = DataTypeApplyOpFromText(buf, GImGui->InputTextState.InitialText.begin(), data_type, data_ptr, scalar_format);
// Step buttons
if (step_ptr) if (step_ptr)
{ {
const float button_size = GetFrameHeight();
BeginGroup(); // The only purpose of the group here is to allow the caller to query item data e.g. IsItemActive()
PushID(label);
PushItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));
if (InputText("", buf, IM_ARRAYSIZE(buf), extra_flags)) // PushId(label) + "" gives us the expected ID from outside point of view
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialText.Data, data_type, data_ptr, scalar_format);
PopItemWidth(); PopItemWidth();
// Step buttons
SameLine(0, style.ItemInnerSpacing.x); SameLine(0, style.ItemInnerSpacing.x);
if (ButtonEx("-", button_size, ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups)) if (ButtonEx("-", ImVec2(button_size, button_size), ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups))
{ {
DataTypeApplyOp(data_type, '-', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast_ptr ? step_fast_ptr : step_ptr); DataTypeApplyOp(data_type, '-', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast_ptr ? step_fast_ptr : step_ptr);
value_changed = true; value_changed = true;
} }
SameLine(0, style.ItemInnerSpacing.x); SameLine(0, style.ItemInnerSpacing.x);
if (ButtonEx("+", button_size, ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups)) if (ButtonEx("+", ImVec2(button_size, button_size), ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups))
{ {
DataTypeApplyOp(data_type, '+', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast_ptr ? step_fast_ptr : step_ptr); DataTypeApplyOp(data_type, '+', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast_ptr ? step_fast_ptr : step_ptr);
value_changed = true; value_changed = true;
} }
} SameLine(0, style.ItemInnerSpacing.x);
PopID(); TextUnformatted(label, FindRenderedTextEnd(label));
if (label_size.x > 0) PopID();
EndGroup();
}
else
{ {
SameLine(0, style.ItemInnerSpacing.x); if (InputText(label, buf, IM_ARRAYSIZE(buf), extra_flags))
RenderText(ImVec2(window->DC.CursorPos.x, window->DC.CursorPos.y + style.FramePadding.y), label); value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialText.Data, data_type, data_ptr, scalar_format);
ItemSize(label_size, style.FramePadding.y);
} }
EndGroup();
return value_changed; return value_changed;
} }

Loading…
Cancel
Save