Skip to content

Commit

Permalink
[Bundle] make InputTextMultiline with imgui-node-editor (cf thedmd/im…
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Oct 10, 2024
1 parent 52c093f commit 18aec18
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3817,9 +3817,66 @@ bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputT
return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data);
}

// [ADAPT_IMGUI_BUNDLE] cf
bool Priv_ImGuiNodeEditor_IsInCanvas();
// [/ADAPT_IMGUI_BUNDLE]


bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
{
return InputTextEx(label, NULL, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data);
// [ADAPT_IMGUI_BUNDLE] cf
// When inside imgui-node-editor canvas, we cannot open child windows
// In this case, we present a one line version of the input text,
// and offer the possibility to open a popup to edit the text in a multiline widget
if (Priv_ImGuiNodeEditor_IsInCanvas())
{
PushID(label); // make sure to use unique ids
bool changed = false;

// A- One line version text, without the label
if (size.x > 0.f)
SetNextItemWidth(size.x);
if (InputText("##hidden_label", buf, buf_size, flags, callback, user_data))
changed = true;
float input_text_actual_width = GetItemRectSize().x; // we will reuse this width for the widget in the popup
SameLine();

// B- Add a button to open a popup to edit the text
// i. First, move the cursor to the left, so that the button appears right next to the input text
ImVec2 pos = ImGui::GetCursorScreenPos();
pos.x -= (ImGui::GetStyle().ItemSpacing.x);
ImGui::SetCursorScreenPos(pos);
// ii. Then add the button
if (SmallButton("..."))
OpenPopup("InputTextMultilinePopup");

// C. Finally, add the label (up until "##")
const char* label_end = FindRenderedTextEnd(label);
SameLine();
TextUnformatted(label, label_end);

// D. Handle the popup
if (ImGui::BeginPopup("InputTextMultilinePopup"))
{
// Note: there is no infinite recursion here, since we are not inside the canvas anymore
// (as soon as BeginPopup return true, we are outside the canvas)
// (iif the patches https://github.com/thedmd/imgui-node-editor/issues/242#issuecomment-1681806764
// and https://github.com/thedmd/imgui-node-editor/issues/242#issuecomment-2404714757 are applied)
ImVec2 size_multiline = size;
size_multiline.x = input_text_actual_width;
if (InputTextMultiline("##edit", buf, buf_size, size_multiline, flags, callback, user_data))
changed = true;
EndPopup();
}
PopID();
return changed;
}
// [/ADAPT_IMGUI_BUNDLE]
else
{
// Standard behavior outside of imgui-node-editor canvas
return InputTextEx(label, NULL, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data);
}
}

bool ImGui::InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
Expand Down

0 comments on commit 18aec18

Please sign in to comment.