Skip to content

Commit b3d19ee

Browse files
KiritoDvKiritoDv
andauthored
Fixed id and cleanup on finish of notifications (#304)
Co-authored-by: KiritoDv <nohomoboi01@gmail.com>
1 parent 3bc0564 commit b3d19ee

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

libultraship/libultraship/GameOverlay.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "SohImGuiImpl.h"
99
#include "TextureMod.h"
1010
#include "Lib/ImGui/imgui_internal.h"
11+
#include "Utils/StringHelper.h"
1112

1213
void Ship::GameOverlay::LoadFont(const std::string& name, const std::string& path, float fontSize) {
1314
ImGuiIO& io = ImGui::GetIO();
@@ -50,10 +51,21 @@ void Ship::GameOverlay::TextDrawNotification(float duration, bool shadow, const
5051
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
5152
buf[IM_ARRAYSIZE(buf) - 1] = 0;
5253
va_end(args);
53-
54-
this->RegisteredOverlays[fmt] = new Overlay({ OverlayType::NOTIFICATION, ImStrdup(buf), duration, duration });
54+
this->RegisteredOverlays[StringHelper::Sprintf("NotificationID:%d%d", rand(), this->RegisteredOverlays.size())] = new Overlay({ OverlayType::NOTIFICATION, ImStrdup(buf), duration, duration });
55+
NeedsCleanup = true;
5556
}
5657

58+
void Ship::GameOverlay::CleanupNotifications() {
59+
if(!NeedsCleanup) return;
60+
for (auto it = this->RegisteredOverlays.begin(); it != this->RegisteredOverlays.end(); ) {
61+
if (it->second->type == OverlayType::NOTIFICATION && it->second->duration <= 0.0f) {
62+
it = this->RegisteredOverlays.erase(it);
63+
} else {
64+
++it;
65+
}
66+
}
67+
NeedsCleanup = false;
68+
}
5769

5870
float Ship::GameOverlay::GetScreenWidth() {
5971
const ImGuiViewport* viewport = ImGui::GetMainViewport();
@@ -137,6 +149,8 @@ void Ship::GameOverlay::Draw() {
137149
ImGui::Begin("SoHOverlay", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground |
138150
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs);
139151

152+
this->CleanupNotifications();
153+
140154
float textY = 50;
141155
float notY = 0;
142156

libultraship/libultraship/GameOverlay.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ namespace Ship {
3333
void TextDraw(float x, float y, bool shadow, ImVec4 color, const char* text, ...);
3434
void TextDrawNotification(float duration, bool shadow, const char* fmt, ...);
3535
private:
36+
bool NeedsCleanup = false;
37+
void CleanupNotifications();
3638
void LoadFont(const std::string& name, const std::string& path, float fontSize);
3739
};
3840

0 commit comments

Comments
 (0)