19 namespace visualization {
23 static int g_next_text_edit_id = 1;
26 int InputTextCallback(ImGuiInputTextCallbackData *data) {
27 if (data && data->EventFlag == ImGuiInputTextFlags_CallbackResize) {
28 std::string *s =
reinterpret_cast<std::string *
>(data->UserData);
29 s->resize(data->BufTextLen);
30 data->Buf =
const_cast<char *
>(s->c_str());
46 impl_->id_ =
"##textedit_" +
std::to_string(g_next_text_edit_id++);
47 impl_->text_.reserve(1);
57 return impl_->placeholder_.c_str();
61 impl_->placeholder_ = text;
65 std::function<
void(
const char *)> on_text_changed) {
66 impl_->on_text_changed_ = on_text_changed;
70 std::function<
void(
const char *)> on_value_changed) {
71 impl_->on_value_changed_ = on_value_changed;
78 auto em =
std::ceil(ImGui::GetTextLineHeight());
79 auto padding = ImGui::GetStyle().FramePadding;
85 ImGui::SetCursorScreenPos(
86 ImVec2(
float(
frame.x),
float(
frame.y) + ImGui::GetScrollY()));
88 ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding,
91 ImGui::PushStyleColor(
94 ImGui::PushStyleColor(
95 ImGuiCol_FrameBgHovered,
97 ImGui::PushStyleColor(
98 ImGuiCol_FrameBgActive,
101 int text_flags = ImGuiInputTextFlags_CallbackResize;
103 text_flags = ImGuiInputTextFlags_ReadOnly;
108 if (ImGui::InputTextWithHint(impl_->id_.c_str(),
109 impl_->placeholder_.c_str(),
110 const_cast<char *
>(impl_->text_.c_str()),
111 impl_->text_.capacity(), text_flags,
112 InputTextCallback, &impl_->text_)) {
113 if (impl_->on_text_changed_) {
114 impl_->on_text_changed_(impl_->text_.c_str());
118 ImGui::PopItemWidth();
122 ImGui::PopStyleColor(3);
123 ImGui::PopStyleVar();
125 if (ImGui::IsItemDeactivatedAfterEdit()) {
127 if (impl_->on_value_changed_) {
128 impl_->on_value_changed_(impl_->text_.c_str());
const char * GetText() const
Returns the current text value displayed.
virtual bool ValidateNewText(const char *text)
void SetPlaceholderText(const char *text)
Sets the text to display if the text value is empty.
DrawResult Draw(const DrawContext &context) override
void SetOnValueChanged(std::function< void(const char *)> on_value_changed)
Size CalcPreferredSize(const LayoutContext &context, const Constraints &constraints) const override
const char * GetPlaceholderText() const
Returns the text displayed if the text value is empty.
void SetText(const char *text)
void SetOnTextChanged(std::function< void(const char *)> on_text_changed)
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
ImVec4 colorToImgui(const Color &color)
Generic file read and write utility for python interface.
std::string to_string(const T &n)
std::function< void(const char *)> on_value_changed_
std::function< void(const char *)> on_text_changed_