From 0bf9ec1108b8113ee7338e5d260a206c89a76252 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Fri, 22 Oct 2021 16:35:35 +0200 Subject: [PATCH] Fnct. to add overlay elems --- GlosSITarget/Overlay.cpp | 13 ++++++++++--- GlosSITarget/Overlay.h | 12 ++++++++---- GlosSITarget/OverlayLogSink.h | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/GlosSITarget/Overlay.cpp b/GlosSITarget/Overlay.cpp index 4f8127c..8a1558a 100644 --- a/GlosSITarget/Overlay.cpp +++ b/GlosSITarget/Overlay.cpp @@ -1,10 +1,12 @@ #include "Overlay.h" +#include + #define IMGUI_USER_CONFIG "imconfig.h" #include "imgui-SFML.h" #include "imgui.h" -Overlay::Overlay(sf::RenderWindow& window, std::function on_close) : window_(window), on_close_(on_close) +Overlay::Overlay(sf::RenderWindow& window, const std::function& on_close) : window_(window), on_close_(on_close) { ImGui::SFML::Init(window_); ImGuiIO& io = ImGui::GetIO(); @@ -104,7 +106,7 @@ void Overlay::update() if (enabled_) { window_.clear(sf::Color(0, 0, 0, 64)); // make window slightly dim screen with overlay - + std::ranges::for_each(OVERLAY_ELEMS_, [](const auto& fn) { fn(); }); if (closeButton()) { return; @@ -124,11 +126,16 @@ void Overlay::Shutdown() ImGui::SFML::Shutdown(); } -void Overlay::ShowNotification(const spdlog::details::log_msg& msg) +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& elem_fn) +{ + OVERLAY_ELEMS_.push_back(elem_fn); +} + void Overlay::showLogs() const { std::vector logs; diff --git a/GlosSITarget/Overlay.h b/GlosSITarget/Overlay.h index 788f66c..44778e2 100644 --- a/GlosSITarget/Overlay.h +++ b/GlosSITarget/Overlay.h @@ -7,7 +7,7 @@ class Overlay { public: - Overlay(sf::RenderWindow& window, std::function on_close); + Overlay(sf::RenderWindow& window, const std::function& on_close); void setEnabled(bool enabled); bool isEnabled() const; @@ -15,15 +15,17 @@ class Overlay { void update(); static void ProcessEvent(sf::Event evnt); static void Shutdown(); - static void ShowNotification(const spdlog::details::log_msg& msg); + static void AddLog(const spdlog::details::log_msg& msg); + + static void AddOverlayElem(const std::function& elem_fn); + private: sf::RenderWindow& window_; sf::Clock update_clock_; bool enabled_ = true; std::function on_close_; void showLogs() const; - - bool closeButton() const; + [[nodiscard]] bool closeButton() const; struct Log { std::chrono::system_clock::time_point time; @@ -33,4 +35,6 @@ class Overlay { static inline std::vector LOG_MSGS_; static constexpr int LOG_RETENTION_TIME_ = 5; + static inline std::vector> OVERLAY_ELEMS_; + }; diff --git a/GlosSITarget/OverlayLogSink.h b/GlosSITarget/OverlayLogSink.h index 48c876d..234441d 100644 --- a/GlosSITarget/OverlayLogSink.h +++ b/GlosSITarget/OverlayLogSink.h @@ -14,7 +14,7 @@ class overlay_sink : public spdlog::sinks::base_sink { protected: void sink_it_(const spdlog::details::log_msg& msg) override { - Overlay::ShowNotification(msg); + Overlay::AddLog(msg); } void flush_() override