Skip to content

Commit ae7c708

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

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
//-------------------------------------------------------------------------
@@ -3845,9 +3852,137 @@ bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputT
38453852
return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data);
38463853
}
38473854

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

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