From bedc3c94f8339e7acde8307737d86bf2b5ddbed8 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Mon, 20 Dec 2021 18:58:23 +0100 Subject: [PATCH] Error notifications --- src/client/component/gui.cpp | 73 +++++++++++++----------- src/client/component/gui_entity_list.cpp | 3 +- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/client/component/gui.cpp b/src/client/component/gui.cpp index 36875ca1..b20aa0cb 100644 --- a/src/client/component/gui.cpp +++ b/src/client/component/gui.cpp @@ -20,14 +20,13 @@ namespace gui namespace { utils::concurrency::container>> on_frame_callbacks; + utils::concurrency::container> notifications; ID3D11Device* device; ID3D11DeviceContext* device_context; bool initialized = false; bool toggled = false; - std::vector notifications; - void initialize_gui_context() { ImGui::CreateContext(); @@ -62,41 +61,44 @@ namespace gui void show_notifications() { - static auto window_flags = ImGuiWindowFlags_NoDecoration | - ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | - ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoMove; + static auto window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav | + ImGuiWindowFlags_NoMove; - auto index = 0; - for (auto i = notifications.begin(); i != notifications.end(); ++i) + notifications.access([](std::vector& notifications_) { - const auto now = std::chrono::high_resolution_clock::now(); - if (now - i->creation_time >= i->duration) + auto index = 0; + for (auto i = notifications_.begin(); i != notifications_.end(); ++i) { - notifications.erase(i--); - continue; + const auto now = std::chrono::high_resolution_clock::now(); + if (now - i->creation_time >= i->duration) + { + notifications_.erase(i--); + continue; + } + + const auto title = i->title.size() <= 34 + ? i->title + : i->title.substr(0, 31) + "..."; + + const auto text = i->text.size() <= 34 + ? i->text + : i->text.substr(0, 31) + "..."; + + ImGui::SetNextWindowSizeConstraints(ImVec2(250, 50), ImVec2(250, 50)); + ImGui::SetNextWindowBgAlpha(0.6f); + ImGui::Begin(utils::string::va("Notification #%i", index), nullptr, window_flags); + + ImGui::SetWindowPos(ImVec2(10, 30.f + static_cast(index) * 60.f)); + ImGui::SetWindowSize(ImVec2(250, 0)); + ImGui::Text(title.data()); + ImGui::Text(text.data()); + + ImGui::End(); + + index++; } - - const auto title = i->title.size() <= 34 - ? i->title - : i->title.substr(0, 31) + "..."; - - const auto text = i->text.size() <= 34 - ? i->text - : i->text.substr(0, 31) + "..."; - - ImGui::SetNextWindowSizeConstraints(ImVec2(250, 50), ImVec2(250, 50)); - ImGui::SetNextWindowBgAlpha(0.6f); - ImGui::Begin(utils::string::va("Notification #%i", index), nullptr, window_flags); - - ImGui::SetWindowPos(ImVec2(10, 30.f + static_cast(index) * 60.f)); - ImGui::SetWindowSize(ImVec2(250, 0)); - ImGui::Text(title.data()); - ImGui::Text(text.data()); - - ImGui::End(); - - index++; - } + }); } void gui_draw() @@ -231,7 +233,10 @@ namespace gui notification.duration = duration; notification.creation_time = std::chrono::high_resolution_clock::now(); - notifications.insert(notifications.begin(), notification); + notifications.access([notification](std::vector& notifications_) + { + notifications_.insert(notifications_.begin(), notification); + }); } class component final : public component_interface diff --git a/src/client/component/gui_entity_list.cpp b/src/client/component/gui_entity_list.cpp index e452d726..246a2fe9 100644 --- a/src/client/component/gui_entity_list.cpp +++ b/src/client/component/gui_entity_list.cpp @@ -466,7 +466,6 @@ namespace entity_list } catch (...) { - } } @@ -568,6 +567,7 @@ namespace entity_list } catch (...) { + gui::notification("Error", utils::string::va("^1error setting field '%s'!", name.data())); } }); } @@ -589,6 +589,7 @@ namespace entity_list } catch (...) { + gui::notification("Error", utils::string::va("^1error setting field '%s'!", name.data())); } }); }