Skip to content

Commit 2e527d9

Browse files
committed
[Bundle] InputTextMultiline compatible with imgui-node-editor (cf thedmd/imgui-node-editor#242 (comment))
1 parent 837a679 commit 2e527d9

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

imgui_widgets.cpp

+136-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ Index of this file:
4747
// System includes
4848
#include <stdint.h> // intptr_t
4949

50+
// [ADAPT_IMGUI_BUNDLE]
51+
// for InputTextMultiline tooltip within node editor
52+
#include <string>
53+
#include <vector>
54+
#include <sstream>
55+
// [/ADAPT_IMGUI_BUNDLE]
56+
5057
//-------------------------------------------------------------------------
5158
// Warnings
5259
//-------------------------------------------------------------------------
@@ -3854,9 +3861,137 @@ bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputT
38543861
return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data);
38553862
}
38563863

3864+
// [ADAPT_IMGUI_BUNDLE] cf
3865+
bool Priv_ImGuiNodeEditor_IsInCanvas();
3866+
// [/ADAPT_IMGUI_BUNDLE]
3867+
3868+
38573869
bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
38583870
{
3859-
return InputTextEx(label, NULL, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data);
3871+
// [ADAPT_IMGUI_BUNDLE] cf
3872+
// When inside imgui-node-editor canvas, we cannot open child windows
3873+
// In this case, we present a one line version of the input text,
3874+
// and offer the possibility to open a popup to edit the text in a multiline widget
3875+
if (Priv_ImGuiNodeEditor_IsInCanvas())
3876+
{
3877+
// Helper function to split and format the text for a tooltip
3878+
// to show an extract of the full text when hovering the button,
3879+
auto fn_format_tooltip = [](
3880+
const char* text, size_t max_line_length, size_t max_lines) -> std::string
3881+
{
3882+
auto fn_cut_string_after_max_length = [](const std::string& text, size_t max_length) -> std::string
3883+
{
3884+
if (text.size() <= max_length)
3885+
return text;
3886+
std::string r = text.substr(0, max_length - 3) + "...";
3887+
return r;
3888+
};
3889+
auto fn_split_lines = [](const std::string& s) -> std::vector<std::string>
3890+
{
3891+
std::vector<std::string> lines;
3892+
std::istringstream f(s);
3893+
std::string line;
3894+
while (std::getline(f, line))
3895+
lines.push_back(line);
3896+
return lines;
3897+
};
3898+
std::string r = "";
3899+
auto lines = fn_split_lines(text);
3900+
size_t n = 0;
3901+
for (const auto& line : lines)
3902+
{
3903+
if (n >= max_lines)
3904+
{
3905+
r += "...\n";
3906+
break;
3907+
}
3908+
r += fn_cut_string_after_max_length(line, max_line_length) + "\n";
3909+
n++;
3910+
}
3911+
return r;
3912+
};
3913+
3914+
// Helper function to make the text color more red
3915+
auto fn_redify_color = [](const ImVec4& color) -> ImVec4
3916+
{
3917+
ImVec4 redified_color;
3918+
redified_color.x = color.x;
3919+
redified_color.y = 0.6f * color.y;
3920+
redified_color.z = 0.6f * color.z;
3921+
redified_color.w = color.w;
3922+
return redified_color;
3923+
};
3924+
3925+
// Intro
3926+
PushID(label); // make sure to use unique ids
3927+
bool changed = false;
3928+
ImGui::BeginGroup();
3929+
3930+
// A- One line version text, without the label
3931+
ImGuiStyle& style = ImGui::GetStyle();
3932+
ImVec2 pos = ImGui::GetCursorScreenPos();
3933+
float btn_additional_width = CalcTextSize("...").x + style.FramePadding.x * 2.0f;// + style.ItemInnerSpacing.x;
3934+
float one_line_widget_width = size.x > 0.f ? size.x : CalcItemWidth();
3935+
one_line_widget_width -= btn_additional_width;
3936+
ImGui::SetNextItemWidth(one_line_widget_width);
3937+
if (InputText("##hidden_label", buf, buf_size, flags, callback, user_data))
3938+
changed = true;
3939+
3940+
// B- Add a button to open a popup to edit the text
3941+
// i. First, move the cursor to the left, so that the button appears right next to the input text
3942+
//ImVec2 pos = ImGui::GetCursorScreenPos();
3943+
//pos.x = pos.x - style.ItemSpacing.x; // + style.ItemInnerSpacing.x;
3944+
pos.x += one_line_widget_width - 1.f;
3945+
ImGui::SetCursorScreenPos(pos);
3946+
// ii. Then add the button
3947+
bool shall_display_tooltip = strchr(buf, '\n') != NULL;
3948+
if (shall_display_tooltip)
3949+
{
3950+
ImVec4 color = ImGui::GetStyleColorVec4(ImGuiCol_Text);
3951+
ImGui::PushStyleColor(ImGuiCol_Text, fn_redify_color(color));
3952+
}
3953+
bool was_button_pressed = false;
3954+
if (Button("..."))
3955+
{
3956+
was_button_pressed = true;
3957+
OpenPopup("InputTextMultilinePopup");
3958+
}
3959+
if (shall_display_tooltip)
3960+
ImGui::PopStyleColor();
3961+
3962+
if (shall_display_tooltip && ! was_button_pressed)
3963+
ImGui::SetItemTooltip("%s", fn_format_tooltip(buf, 60, 3).c_str());
3964+
3965+
// C. Finally, add the label (up until "##")
3966+
const char* label_end = FindRenderedTextEnd(label);
3967+
pos.x += style.ItemInnerSpacing.x + btn_additional_width;
3968+
ImGui::SetCursorScreenPos(pos);
3969+
TextUnformatted(label, label_end);
3970+
3971+
ImGui::EndGroup();
3972+
3973+
// D. Handle the popup
3974+
if (ImGui::BeginPopup("InputTextMultilinePopup"))
3975+
{
3976+
// Note: there is no infinite recursion here, since we are not inside the canvas anymore
3977+
// (as soon as BeginPopup return true, we are outside the canvas)
3978+
// (iif the patches https://github.com/thedmd/imgui-node-editor/issues/242#issuecomment-1681806764
3979+
// and https://github.com/thedmd/imgui-node-editor/issues/242#issuecomment-2404714757 are applied)
3980+
ImVec2 size_multiline = size;
3981+
size_multiline.x = one_line_widget_width;
3982+
if (InputTextMultiline("##edit", buf, buf_size, size_multiline, flags, callback, user_data))
3983+
changed = true;
3984+
EndPopup();
3985+
}
3986+
PopID();
3987+
return changed;
3988+
}
3989+
// [/ADAPT_IMGUI_BUNDLE]
3990+
else
3991+
{
3992+
// Standard behavior outside of imgui-node-editor canvas
3993+
return InputTextEx(label, NULL, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data);
3994+
}
38603995
}
38613996

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

0 commit comments

Comments
 (0)