Internals: TempInputText: Tidying up DragScalar / SliderScalar / TempInputTextScalar.

This commit is contained in:
omar 2019-04-25 16:04:45 +02:00
parent 0ca1675ff9
commit dd15b44230

View File

@ -1989,8 +1989,6 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* v, floa
if (!ItemAdd(total_bb, id, &frame_bb)) if (!ItemAdd(total_bb, id, &frame_bb))
return false; return false;
const bool hovered = ItemHoverable(frame_bb, id);
// Default format string when passing NULL // Default format string when passing NULL
// Patch old "%.0f" format string to use "%d", read function comments for more details. // Patch old "%.0f" format string to use "%d", read function comments for more details.
IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT);
@ -2000,10 +1998,13 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* v, floa
format = PatchFormatStringFloatToInt(format); format = PatchFormatStringFloatToInt(format);
// Tabbing or CTRL-clicking on Drag turns it into an input box // Tabbing or CTRL-clicking on Drag turns it into an input box
const bool hovered = ItemHoverable(frame_bb, id);
bool temp_input_is_active = TempInputTextIsActive(id); bool temp_input_is_active = TempInputTextIsActive(id);
bool temp_input_start = false; bool temp_input_start = false;
if (!temp_input_is_active)
{
const bool focus_requested = FocusableItemRegister(window, id); const bool focus_requested = FocusableItemRegister(window, id);
if (focus_requested || (hovered && (g.IO.MouseClicked[0] || g.IO.MouseDoubleClicked[0])) || g.NavActivateId == id || (g.NavInputId == id && !temp_input_is_active)) if (focus_requested || (hovered && (g.IO.MouseClicked[0] || g.IO.MouseDoubleClicked[0])) || g.NavActivateId == id || g.NavInputId == id)
{ {
SetActiveID(id, window); SetActiveID(id, window);
SetFocusID(id, window); SetFocusID(id, window);
@ -2012,26 +2013,23 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* v, floa
if (focus_requested || g.IO.KeyCtrl || g.IO.MouseDoubleClicked[0] || g.NavInputId == id) if (focus_requested || g.IO.KeyCtrl || g.IO.MouseDoubleClicked[0] || g.NavInputId == id)
{ {
temp_input_start = true; temp_input_start = true;
g.TempInputTextId = 0; FocusableItemUnregister(window);
}
} }
} }
if (temp_input_is_active || temp_input_start) if (temp_input_is_active || temp_input_start)
{
window->DC.CursorPos = frame_bb.Min;
FocusableItemUnregister(window);
return TempInputTextScalar(frame_bb, id, label, data_type, v, format); return TempInputTextScalar(frame_bb, id, label, data_type, v, format);
}
// Actual drag behavior
const bool value_changed = DragBehavior(id, data_type, v, v_speed, v_min, v_max, format, power, ImGuiDragFlags_None);
if (value_changed)
MarkItemEdited(id);
// Draw frame // Draw frame
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
RenderNavHighlight(frame_bb, id); RenderNavHighlight(frame_bb, id);
RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding);
// Drag behavior
const bool value_changed = DragBehavior(id, data_type, v, v_speed, v_min, v_max, format, power, ImGuiDragFlags_None);
if (value_changed)
MarkItemEdited(id);
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value. // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
char value_buf[64]; char value_buf[64];
const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format); const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format);
@ -2443,11 +2441,13 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* v, co
format = PatchFormatStringFloatToInt(format); format = PatchFormatStringFloatToInt(format);
// Tabbing or CTRL-clicking on Slider turns it into an input box // Tabbing or CTRL-clicking on Slider turns it into an input box
const bool hovered = ItemHoverable(frame_bb, id);
bool temp_input_is_active = TempInputTextIsActive(id); bool temp_input_is_active = TempInputTextIsActive(id);
bool temp_input_start = false; bool temp_input_start = false;
if (!temp_input_is_active)
{
const bool focus_requested = FocusableItemRegister(window, id); const bool focus_requested = FocusableItemRegister(window, id);
const bool hovered = ItemHoverable(frame_bb, id); if (focus_requested || (hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavInputId == id)
if (focus_requested || (hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || (g.NavInputId == id && !temp_input_is_active))
{ {
SetActiveID(id, window); SetActiveID(id, window);
SetFocusID(id, window); SetFocusID(id, window);
@ -2456,15 +2456,12 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* v, co
if (focus_requested || g.IO.KeyCtrl || g.NavInputId == id) if (focus_requested || g.IO.KeyCtrl || g.NavInputId == id)
{ {
temp_input_start = true; temp_input_start = true;
g.TempInputTextId = 0; FocusableItemUnregister(window);
}
} }
} }
if (temp_input_is_active || temp_input_start) if (temp_input_is_active || temp_input_start)
{
window->DC.CursorPos = frame_bb.Min;
FocusableItemUnregister(window);
return TempInputTextScalar(frame_bb, id, label, data_type, v, format); return TempInputTextScalar(frame_bb, id, label, data_type, v, format);
}
// Draw frame // Draw frame
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
@ -2740,12 +2737,12 @@ int ImParseFormatPrecision(const char* fmt, int default_precision)
// FIXME: Facilitate using this in variety of other situations. // FIXME: Facilitate using this in variety of other situations.
bool ImGui::TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format) bool ImGui::TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format)
{ {
IM_UNUSED(id);
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
// On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id. // On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id.
// We clear ActiveID on the first frame to allow the InputText() taking it back. // We clear ActiveID on the first frame to allow the InputText() taking it back.
if (g.TempInputTextId == 0) const bool init = (g.TempInputTextId != id);
if (init)
ClearActiveID(); ClearActiveID();
char fmt_buf[32]; char fmt_buf[32];
@ -2753,9 +2750,11 @@ bool ImGui::TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label,
format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf)); format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf));
DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, data_ptr, format); DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, data_ptr, format);
ImStrTrimBlanks(data_buf); ImStrTrimBlanks(data_buf);
g.CurrentWindow->DC.CursorPos = bb.Min;
ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal); ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal);
bool value_changed = InputTextEx(label, NULL, data_buf, IM_ARRAYSIZE(data_buf), bb.GetSize(), flags); bool value_changed = InputTextEx(label, NULL, data_buf, IM_ARRAYSIZE(data_buf), bb.GetSize(), flags);
if (g.TempInputTextId == 0) if (init)
{ {
// First frame we started displaying the InputText widget, we expect it to take the active id. // First frame we started displaying the InputText widget, we expect it to take the active id.
IM_ASSERT(g.ActiveId == id); IM_ASSERT(g.ActiveId == id);