Check All now leave checkmarks only on Search if used together

This commit is contained in:
HarrySilan
2022-06-20 23:04:49 +08:00
parent fce6ed4a44
commit a2f656302c
2 changed files with 23 additions and 6 deletions

View File

@ -172,17 +172,27 @@ namespace cheat::feature
std::sort(teleports.begin(), teleports.end(), [](const auto& a, const auto& b) std::sort(teleports.begin(), teleports.end(), [](const auto& a, const auto& b)
{return doj::alphanum_less<std::string>()(a.first, b.first); }); {return doj::alphanum_less<std::string>()(a.first, b.first); });
bool allChecked = checkedIndices.size() == teleports.size() && !teleports.empty(); bool allSearchChecked = std::includes(checkedIndices.begin(), checkedIndices.end() ,searchIndices.begin(), searchIndices.end()) && !searchIndices.empty();
ImGui::Checkbox("Check All", &allChecked); bool allChecked = (checkedIndices.size() == teleports.size() && !teleports.empty()) || allSearchChecked;
ImGui::Checkbox("All", &allChecked);
if (ImGui::IsItemClicked()) { if (ImGui::IsItemClicked()) {
if (!teleports.empty()) { if (!teleports.empty()) {
if (allChecked) { if (allChecked) {
selectedIndex = -1; selectedIndex = -1;
checkedIndices.clear(); if (!searchIndices.empty()) {
checkedIndices.erase(searchIndices.begin(), searchIndices.end());
}
else {
checkedIndices.clear();
}
} }
else { else {
for (int i = 0; i < teleports.size(); i++) { if (!searchIndices.empty()) {
checkedIndices.insert(i); checkedIndices.insert(searchIndices.begin(), searchIndices.end());
}
else {
for (int i = 0; i < teleports.size(); i++)
checkedIndices.insert(i);
} }
} }
UpdateIndexName(); UpdateIndexName();
@ -191,12 +201,19 @@ namespace cheat::feature
ImGui::SameLine(); ImGui::SameLine();
ImGui::InputText("Search", &search); ImGui::InputText("Search", &search);
unsigned int index = 0; unsigned int index = 0;
searchIndices.clear();
for (const auto& [teleportName, position] : teleports) for (const auto& [teleportName, position] : teleports)
{ {
// find without case sensitivity // find without case sensitivity
if (search.empty() || std::search(teleportName.begin(), teleportName.end(), search.begin(), search.end(), [](char a, char b) if (search.empty() || std::search(teleportName.begin(), teleportName.end(), search.begin(), search.end(), [](char a, char b)
{ return std::tolower(a) == std::tolower(b); }) != teleportName.end()) { return std::tolower(a) == std::tolower(b); }) != teleportName.end())
{ {
// sets are sorted by default and does not allow duplicates
// which works in favor here.
if (!search.empty()) {
searchIndices.insert(index);
}
bool checked = std::any_of(checkedIndices.begin(), checkedIndices.end(), [&index](const auto& i) { return i == index; }); bool checked = std::any_of(checkedIndices.begin(), checkedIndices.end(), [&index](const auto& i) { return i == index; });
bool selected = index == selectedIndex; bool selected = index == selectedIndex;
@ -207,7 +224,6 @@ namespace cheat::feature
checkedIndices.erase(index); checkedIndices.erase(index);
} }
else { else {
// sets are sorted by default
checkedIndices.insert(index); checkedIndices.insert(index);
} }
} }

View File

@ -25,6 +25,7 @@ namespace cheat::feature
private: private:
std::vector<std::pair<std::string, app::Vector3>> teleports; std::vector<std::pair<std::string, app::Vector3>> teleports;
std::set<unsigned int> checkedIndices; std::set<unsigned int> checkedIndices;
std::set<unsigned int> searchIndices;
bool selectedByClick = false; bool selectedByClick = false;
int selectedIndex = -1; int selectedIndex = -1;
std::string selectedIndexName; std::string selectedIndexName;