From dd15b44230c7b9cad7609cedf3884aec579860dc Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 25 Apr 2019 16:04:45 +0200 Subject: [PATCH] Internals: TempInputText: Tidying up DragScalar / SliderScalar / TempInputTextScalar. --- imgui_widgets.cpp | 73 +++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 46eaf7f34..9b2125455 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -1989,8 +1989,6 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* v, floa if (!ItemAdd(total_bb, id, &frame_bb)) return false; - const bool hovered = ItemHoverable(frame_bb, id); - // Default format string when passing NULL // Patch old "%.0f" format string to use "%d", read function comments for more details. IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); @@ -2000,38 +1998,38 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* v, floa format = PatchFormatStringFloatToInt(format); // 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_start = false; - 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 (!temp_input_is_active) { - SetActiveID(id, window); - SetFocusID(id, window); - FocusWindow(window); - g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); - if (focus_requested || g.IO.KeyCtrl || g.IO.MouseDoubleClicked[0] || g.NavInputId == 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_start = true; - g.TempInputTextId = 0; + SetActiveID(id, window); + SetFocusID(id, window); + FocusWindow(window); + g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); + if (focus_requested || g.IO.KeyCtrl || g.IO.MouseDoubleClicked[0] || g.NavInputId == id) + { + temp_input_start = true; + FocusableItemUnregister(window); + } } } 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); - } - - // 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 const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); RenderNavHighlight(frame_bb, id); 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. char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format); @@ -2443,28 +2441,27 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* v, co format = PatchFormatStringFloatToInt(format); // 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_start = false; - 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 && !temp_input_is_active)) + if (!temp_input_is_active) { - SetActiveID(id, window); - SetFocusID(id, window); - FocusWindow(window); - g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); - if (focus_requested || g.IO.KeyCtrl || g.NavInputId == id) + const bool focus_requested = FocusableItemRegister(window, id); + if (focus_requested || (hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavInputId == id) { - temp_input_start = true; - g.TempInputTextId = 0; + SetActiveID(id, window); + SetFocusID(id, window); + FocusWindow(window); + g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); + if (focus_requested || g.IO.KeyCtrl || g.NavInputId == id) + { + temp_input_start = true; + FocusableItemUnregister(window); + } } } 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); - } // Draw frame 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. 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; // 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. - if (g.TempInputTextId == 0) + const bool init = (g.TempInputTextId != id); + if (init) ClearActiveID(); 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)); DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, data_ptr, format); 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); 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. IM_ASSERT(g.ActiveId == id);