Skip to content

Commit f5a0164

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

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
//-------------------------------------------------------------------------
@@ -3886,9 +3893,137 @@ bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputT
38863893
return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data);
38873894
}
38883895

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

38944029
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)