23 #include <imgui_internal.h>
29 itsInputBuf[0] =
'\0';
50 std::lock_guard<std::mutex> _(itsDataMtx);
57 std::lock_guard<std::mutex> _(itsDataMtx);
58 if (itsLastInput.empty())
return false;
63 itsData.push_back(std::make_pair(
true, str));
71 std::lock_guard<std::mutex> _(itsDataMtx);
72 itsData.push_back(std::make_pair(
false, str));
73 while (itsData.size() > 10000) itsData.pop_front();
77 static int TextEditCallbackStub(ImGuiInputTextCallbackData * data)
87 const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing();
88 ImGui::BeginChild(
"ScrollingRegion", ImVec2(0, -footer_height_to_reserve),
false,
89 ImGuiWindowFlags_HorizontalScrollbar);
92 if (ImGui::BeginPopupContextWindow())
94 if (ImGui::Selectable(
"Clear")) clear();
99 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1));
102 std::lock_guard<std::mutex> _(itsDataMtx);
103 for (
auto const & p : itsData)
105 ImVec4 color;
bool has_color =
false;
106 auto const & s = p.second;
109 { color = ImVec4(1.0f, 0.8f, 0.6f, 1.0f); has_color =
true; }
112 if (s ==
"OK") { color = ImVec4(0.2f, 1.0f, 0.2f, 1.0f); has_color =
true; }
118 if (has_color) ImGui::PushStyleColor(ImGuiCol_Text, color);
119 ImGui::TextUnformatted(s.c_str());
120 if (has_color) ImGui::PopStyleColor();
123 static bool autoScroll =
true;
124 static bool scrollToBottom =
true;
126 if (scrollToBottom || (autoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())) ImGui::SetScrollHereY(1.0f);
127 scrollToBottom =
false;
129 ImGui::PopStyleVar();
134 bool reclaim_focus =
false;
135 ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackHistory;
139 if (ImGui::InputTextWithHint(
"Input",
"Type JeVois commands here...",
140 itsInputBuf, IM_ARRAYSIZE(itsInputBuf), input_text_flags,
141 &TextEditCallbackStub,
this))
143 itsLastInput = itsInputBuf;
144 itsInputBuf[0] =
'\0';
145 reclaim_focus =
true;
148 scrollToBottom =
true;
152 if (itsHistory.empty() ==
false)
153 for (
int i =
int(itsHistory.size()) - 1; i >= 0; --i)
154 if (itsHistory[i] == itsLastInput) { itsHistory.erase(itsHistory.begin() + i);
break; }
157 itsHistory.push_back(itsLastInput);
158 while (itsHistory.size() > 100) itsHistory.erase(itsHistory.begin());
162 ImGui::SetItemDefaultFocus();
163 if (reclaim_focus) ImGui::SetKeyboardFocusHere(-1);
169 switch (data->EventFlag)
239 case ImGuiInputTextFlags_CallbackHistory:
242 const int prev_history_pos = itsHistoryPos;
243 if (data->EventKey == ImGuiKey_UpArrow)
245 if (itsHistoryPos == -1) itsHistoryPos = int(itsHistory.size()) - 1;
246 else if (itsHistoryPos > 0) --itsHistoryPos;
248 else if (data->EventKey == ImGuiKey_DownArrow)
250 if (itsHistoryPos != -1 && ++itsHistoryPos >=
int(itsHistory.size())) itsHistoryPos = -1;
254 if (prev_history_pos != itsHistoryPos)
256 std::string
const & history_str = (itsHistoryPos >= 0) ? itsHistory[itsHistoryPos] :
"";
257 data->DeleteChars(0, data->BufTextLen);
258 data->InsertChars(0, history_str.c_str());