Skip to content

Commit

Permalink
Add options to InputTextResizable: Hint + Resizable
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed May 31, 2024
1 parent 571e728 commit 7041abc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/hello_imgui/hello_imgui_widgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,20 @@ namespace HelloImGui
// ```
struct InputTextData
{
// The text edited in the input field
std::string Text;

// An optional hint displayed when the input field is empty
// (only works for single-line text input)
std::string Hint;

// If true, the input field is multi-line
bool Multiline = false;

// If true, the input field is resizable
bool Resizable = true;

// The size of the input field in em units
ImVec2 SizeEm = ImVec2(0, 0);

InputTextData(const std::string& text = "", bool multiline = false, ImVec2 size_em = ImVec2(0, 0)) : Text(text), Multiline(multiline), SizeEm(size_em) {}
Expand Down
26 changes: 18 additions & 8 deletions src/hello_imgui/impl/hello_imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,28 @@ namespace HelloImGui
else
{
ImGui::SetNextItemWidth(HelloImGui::EmSize(textInput->SizeEm.x));
changed = ImGui::InputText(inputLabel.c_str(), &textInput->Text);
if (textInput->Hint.empty())
changed = ImGui::InputText(inputLabel.c_str(), &textInput->Text);
else
changed = ImGui::InputTextWithHint(inputLabel.c_str(), textInput->Hint.c_str(), &textInput->Text);
}
};

ImVec2 newSizePixels = HelloImGui::WidgetWithResizeHandle(label, gui_cb, 0.8f);
ImVec2 newSize = HelloImGui::PixelsToEm(newSizePixels);
if (textInput->Multiline)
textInput->SizeEm = newSize;
if (textInput->Resizable)
{
ImVec2 newSizePixels = HelloImGui::WidgetWithResizeHandle(label, gui_cb, 0.8f);
ImVec2 newSize = HelloImGui::PixelsToEm(newSizePixels);
if (textInput->Multiline)
textInput->SizeEm = newSize;
else
textInput->SizeEm.x = newSize.x;
if (textInput->SizeEm.x < 1.f)
textInput->SizeEm.x = 1.f;
}
else
textInput->SizeEm.x = newSize.x;
if (textInput->SizeEm.x < 1.f)
textInput->SizeEm.x = 1.f;
{
gui_cb();
}

if (!labelIsHidden)
{
Expand Down

0 comments on commit 7041abc

Please sign in to comment.