Skip to content

Commit

Permalink
Internals: InputScalarAsWidgetReplacement: Fixed seemingly unnecessar…
Browse files Browse the repository at this point in the history
…y calling of SetActiveID/SetHoveredID every frame, which in turns allow us to remove the g.ActiveIdAllow/Block settings duplicated.
  • Loading branch information
ocornut committed Jan 30, 2019
1 parent fb4f1ff commit 1fb57c9
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2617,18 +2617,15 @@ int ImParseFormatPrecision(const char* fmt, int default_precision)
}

// Create text input in place of an active drag/slider (used when doing a CTRL+Click on drag/slider widgets)
// FIXME: Logic is awkward and confusing. This should be reworked to facilitate using in other situations.
// FIXME: Facilitate using this in variety of other situations.
bool ImGui::InputScalarAsWidgetReplacement(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();

// Our replacement widget will override the focus ID (registered previously to allow for a TAB focus to happen)
// On the first frame, g.ScalarAsInputTextId == 0, then on subsequent frames it becomes == id
SetActiveID(g.ScalarAsInputTextId, window);
SetHoveredID(0);
g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down);
g.ActiveIdBlockNavInputFlags = (1 << ImGuiNavInput_Cancel);
// On the first frame, g.ScalarAsInputTextId == 0, then on subsequent frames it becomes == id.
// We clear ActiveID on the first frame to allow the InputText() taking it back.
if (g.ScalarAsInputTextId == 0)
ClearActiveID();

char fmt_buf[32];
char data_buf[32];
Expand All @@ -2637,11 +2634,11 @@ bool ImGui::InputScalarAsWidgetReplacement(const ImRect& bb, ImGuiID id, const c
ImStrTrimBlanks(data_buf);
ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal);
bool value_changed = InputTextEx(label, data_buf, IM_ARRAYSIZE(data_buf), bb.GetSize(), flags);
if (g.ScalarAsInputTextId == 0) // First frame we started displaying the InputText widget
if (g.ScalarAsInputTextId == 0)
{
IM_ASSERT(g.ActiveId == id); // InputText ID expected to match the Slider ID
// First frame we started displaying the InputText widget, we expect it to take the active id.
IM_ASSERT(g.ActiveId == id);
g.ScalarAsInputTextId = g.ActiveId;
SetHoveredID(id);
}
if (value_changed)
return DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialText.Data, data_type, data_ptr, NULL);
Expand Down

0 comments on commit 1fb57c9

Please sign in to comment.