Worldstone
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
SearchableListWidget.cpp
1 #include "SearchableListWidget.h"
2 
3 void SearchableListWidget::replaceElements(Vector<std::string> newElements)
4 {
5  elements = std::move(newElements);
6  filteredElements.clear();
7  filteredElements.reserve(elements.size());
8  for (const std::string& str : elements)
9  filteredElements.push_back(str.data());
10  currentSelectionIndex = 0;
11  textFilter.Clear();
12 }
13 void SearchableListWidget::updateFilteredElementsList()
14 {
15  filteredElements.clear();
16  for (const std::string& str : elements)
17  {
18  if (textFilter.PassFilter(str.data())) filteredElements.push_back(str.data());
19  }
20 }
21 
23 {
24  if (textFilter.Draw("##Filter")) {
25  updateFilteredElementsList();
26  }
27  if (!filteredElements.size()) {
28  ImGui::Text("No file found.");
29  return false;
30  }
31  return ImGui::ListBox(
32  "##List", &currentSelectionIndex, filteredElements.data(), int(filteredElements.size()),
33  int(ImGui::GetContentRegionAvail().y / ImGui::GetTextLineHeightWithSpacing()) - 1);
34 }
35 
37 {
38  if (currentSelectionIndex >= 0 && size_t(currentSelectionIndex) < filteredElements.size()) {
39  return filteredElements[size_t(currentSelectionIndex)];
40  }
41  return nullptr;
42 }
const char * getSelectedElement()
void replaceElements(Vector< std::string > newElements)
Replace all the elements of the list by a new one.
bool display()
Display the filter and the ListBox.