Overlay: Refactor: allow removal of elements

main
Peter Repukat 3 years ago
parent c8d7f437a2
commit ce32be639e

@ -151,8 +151,8 @@ void Overlay::update()
if (enabled_ || force_enable_) {
window_.clear(sf::Color(0, 0, 0, 128)); // make window slightly dim screen with overlay
std::ranges::for_each(OVERLAY_ELEMS_, [this](const auto& fn) {
fn();
std::ranges::for_each(OVERLAY_ELEMS_, [this](const auto& elem) {
elem.second();
});
// ImGui::ShowDemoWindow();
@ -180,9 +180,19 @@ void Overlay::AddLog(const spdlog::details::log_msg& msg)
LOG_MSGS_.push_back({.time = msg.time, .level = msg.level, .payload = msg.payload.data()});
}
void Overlay::AddOverlayElem(const std::function<void()>& elem_fn)
int Overlay::AddOverlayElem(const std::function<void()>& elem_fn)
{
OVERLAY_ELEMS_.insert({overlay_element_id_, elem_fn});
// keep this non confusing, but longer...
const auto res = overlay_element_id_;
overlay_element_id_++;
return res;
}
void Overlay::RemoveOverlayElem(int id)
{
OVERLAY_ELEMS_.push_back(elem_fn);
OVERLAY_ELEMS_.erase(id);
}
void Overlay::showLogs() const

@ -38,7 +38,8 @@ class Overlay {
static void Shutdown();
static void AddLog(const spdlog::details::log_msg& msg);
static void AddOverlayElem(const std::function<void()>& elem_fn);
static int AddOverlayElem(const std::function<void()>& elem_fn);
static void RemoveOverlayElem(int id);
private:
sf::RenderWindow& window_;
@ -57,7 +58,8 @@ class Overlay {
static inline std::vector<Log> LOG_MSGS_;
static constexpr int LOG_RETENTION_TIME_ = 5;
static inline std::vector<std::function<void()>> OVERLAY_ELEMS_;
static inline int overlay_element_id_ = 0;
static inline std::map<int, std::function<void()>> OVERLAY_ELEMS_;
#ifdef _WIN32
std::string config_file_name_;

Loading…
Cancel
Save