2022-02-09 18:30:49 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
2022-03-14 12:01:15 -04:00
|
|
|
#include <algorithm>
|
2022-02-09 18:30:49 -05:00
|
|
|
|
2022-03-14 12:01:15 -04:00
|
|
|
template <class T>
|
2022-02-09 18:30:49 -05:00
|
|
|
class SelectionManager
|
|
|
|
{
|
|
|
|
public:
|
2022-03-14 12:01:15 -04:00
|
|
|
SelectionManager() :
|
|
|
|
_multiselection_enabled(false),
|
|
|
|
deselection([](T* thing) { thing->deselect(); })
|
|
|
|
{}
|
2022-02-09 18:30:49 -05:00
|
|
|
|
|
|
|
// Remove whole selection completely
|
2022-03-14 12:01:15 -04:00
|
|
|
void discard()
|
|
|
|
{
|
|
|
|
apply(deselection);
|
|
|
|
_selected_things.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch(T * const thing)
|
|
|
|
{
|
|
|
|
bool already_there = std::any_of(_selected_things.begin(), _selected_things.end(),
|
|
|
|
[&thing](const auto& selected_thing)
|
|
|
|
{
|
|
|
|
return thing == selected_thing;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!already_there)
|
|
|
|
_selected_things.emplace_back(thing);
|
|
|
|
}
|
2022-02-09 18:30:49 -05:00
|
|
|
|
2022-03-14 12:01:15 -04:00
|
|
|
void remove(T * const thing)
|
|
|
|
{
|
|
|
|
for (std::size_t i = 0; i < _selected_things.size(); ++i)
|
|
|
|
{
|
|
|
|
if (thing == _selected_things.at(i))
|
|
|
|
{
|
|
|
|
_selected_things[i] = std::move(_selected_things.back());
|
|
|
|
_selected_things.pop_back();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 18:30:49 -05:00
|
|
|
|
2022-03-14 12:01:15 -04:00
|
|
|
void enableMultiselection(bool enable = true)
|
|
|
|
{
|
|
|
|
_multiselection_enabled = enable;
|
|
|
|
}
|
2022-02-09 18:30:49 -05:00
|
|
|
|
2022-03-14 12:01:15 -04:00
|
|
|
bool isMultiselectionEnabled() const
|
|
|
|
{
|
|
|
|
return _multiselection_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void apply(const std::function<void(T*)>& function)
|
|
|
|
{
|
|
|
|
for (auto& thing : _selected_things)
|
|
|
|
function(thing);
|
|
|
|
}
|
2022-02-17 14:20:30 -05:00
|
|
|
|
2022-02-09 18:30:49 -05:00
|
|
|
private:
|
2022-03-14 12:01:15 -04:00
|
|
|
std::vector<T*> _selected_things;
|
2022-02-09 18:30:49 -05:00
|
|
|
bool _multiselection_enabled;
|
2022-03-14 12:01:15 -04:00
|
|
|
|
|
|
|
const std::function<void(T*)> deselection;
|
2022-02-09 18:30:49 -05:00
|
|
|
};
|