Added notification system

This commit is contained in:
KiritoDev 2022-05-11 18:07:35 -05:00 committed by louist103
parent 3013c18e13
commit 1719986a35
3 changed files with 52 additions and 21 deletions

View File

@ -21,7 +21,7 @@ void Ship::GameOverlay::LoadFont(const std::string& name, const std::string& pat
} }
} }
void Ship::GameOverlay::TextDraw(float x, float y, bool shadow, const char* fmt, ...) { void Ship::GameOverlay::TextDraw(float x, float y, bool shadow, ImVec4 color, const char* fmt, ...) {
char buf[1024]; char buf[1024];
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
@ -29,18 +29,32 @@ void Ship::GameOverlay::TextDraw(float x, float y, bool shadow, const char* fmt,
buf[IM_ARRAYSIZE(buf) - 1] = 0; buf[IM_ARRAYSIZE(buf) - 1] = 0;
va_end(args); va_end(args);
ImGui::PushStyleColor(ImGuiCol_Text, color);
ImGui::PushFont(Fonts[this->CurrentFont]); ImGui::PushFont(Fonts[this->CurrentFont]);
if (shadow) { if (shadow) {
ImGui::SetCursorPos(ImVec2(x + 1, y + 1)); ImGui::SetCursorPos(ImVec2(x + 1, y + 1));
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(.0f, .0f, .0f, 255)); ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(.0f, .0f, .0f, color.w));
ImGui::Text(buf, args); ImGui::Text(buf, args);
} }
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::SetCursorPos(ImVec2(x, y)); ImGui::SetCursorPos(ImVec2(x, y));
ImGui::Text(buf, args); ImGui::Text(buf, args);
ImGui::PopFont(); ImGui::PopFont();
ImGui::PopStyleColor();
} }
void Ship::GameOverlay::TextDrawNotification(float duration, bool shadow, const char* fmt, ...) {
char buf[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
buf[IM_ARRAYSIZE(buf) - 1] = 0;
va_end(args);
this->RegisteredOverlays[fmt] = new Overlay({ OverlayType::NOTIFICATION, ImStrdup(buf), duration, duration });
}
float Ship::GameOverlay::GetScreenWidth() { float Ship::GameOverlay::GetScreenWidth() {
const ImGuiViewport* viewport = ImGui::GetMainViewport(); const ImGuiViewport* viewport = ImGui::GetMainViewport();
return viewport->Size.x; return viewport->Size.x;
@ -124,27 +138,42 @@ void Ship::GameOverlay::Draw() {
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs); ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs);
float textY = 50; float textY = 50;
float notY = 0;
for (auto &[key, overlay] : this->RegisteredOverlays) { for (auto &[key, overlay] : this->RegisteredOverlays) {
if (overlay.type == OverlayType::TEXT) { if (overlay->type == OverlayType::TEXT) {
const char* text = ImStrdup(key.c_str()); const char* text = ImStrdup(overlay->value);
const CVar* var = CVar_Get(text); const CVar* var = CVar_Get(text);
ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
switch (var->type) { switch (var->type) {
case CVAR_TYPE_FLOAT: case CVAR_TYPE_FLOAT:
this->TextDraw(30, textY, true, "%s %.2f", text, var->value.valueFloat); this->TextDraw(30, textY, true, color, "%s %.2f", text, var->value.valueFloat);
break; break;
case CVAR_TYPE_S32: case CVAR_TYPE_S32:
this->TextDraw(30, textY, true, "%s %d", text, var->value.valueS32); this->TextDraw(30, textY, true, color, "%s %d", text, var->value.valueS32);
break; break;
case CVAR_TYPE_STRING: case CVAR_TYPE_STRING:
this->TextDraw(30, textY, true, "%s %s", text, var->value.valueStr); this->TextDraw(30, textY, true, color, "%s %s", text, var->value.valueStr);
break; break;
} }
free((void*) text); free((void*) text);
textY += 30; textY += 30;
} }
if (overlay->type == OverlayType::NOTIFICATION && overlay->duration > 0) {
const char* text = overlay->value;
const float duration = overlay->duration / overlay->fadeTime;
const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration);
const float textWidth = this->GetStringWidth(overlay->value);
this->TextDraw(GetScreenWidth() - textWidth - 40, GetScreenHeight() - 40 - notY, true, color, text);
notY += 30;
overlay->duration -= .05f;
}
} }
ImGui::End(); ImGui::End();
@ -160,19 +189,15 @@ bool Ship::OverlayCommand(const std::vector<std::string>& args) {
const char* key = args[2].c_str(); const char* key = args[2].c_str();
GameOverlay* overlay = SohImGui::overlay; GameOverlay* overlay = SohImGui::overlay;
if (args[1] == "add") { if (args[1] == "add") {
if (!overlay->RegisteredOverlays.contains(args[2])) { if (!overlay->RegisteredOverlays.contains(key)) {
overlay->RegisteredOverlays[args[2]] = { overlay->RegisteredOverlays[key] = new Overlay({ OverlayType::TEXT, ImStrdup(key), -1.0f });
OverlayType::TEXT,
key
};
INFO("Added overlay: %s ", key); INFO("Added overlay: %s ", key);
} else { } else {
ERROR("Overlay already exists: %s", key); ERROR("Overlay already exists: %s", key);
} }
} } else if (args[1] == "remove") {
else if (args[1] == "remove") { if (overlay->RegisteredOverlays.contains(key)) {
if (overlay->RegisteredOverlays.contains(args[2])) { overlay->RegisteredOverlays.erase(key);
overlay->RegisteredOverlays.erase(args[2]);
INFO("Removed overlay: %s ", key); INFO("Removed overlay: %s ", key);
} else { } else {
ERROR("Overlay not found: %s ", key); ERROR("Overlay not found: %s ", key);

View File

@ -7,18 +7,20 @@
#include <unordered_map> #include <unordered_map>
enum class OverlayType { enum class OverlayType {
TEXT, IMAGE TEXT, IMAGE, NOTIFICATION
}; };
struct Overlay { struct Overlay {
OverlayType type; OverlayType type;
const char* value; const char* value;
float fadeTime;
float duration;
}; };
namespace Ship { namespace Ship {
class GameOverlay { class GameOverlay {
public: public:
std::unordered_map<std::string, Overlay> RegisteredOverlays; std::unordered_map<std::string, Overlay*> RegisteredOverlays;
std::unordered_map<std::string, ImFont*> Fonts; std::unordered_map<std::string, ImFont*> Fonts;
std::string CurrentFont = "Default"; std::string CurrentFont = "Default";
void Init(); void Init();
@ -28,11 +30,11 @@ namespace Ship {
static float GetScreenHeight(); static float GetScreenHeight();
static float GetStringWidth(const char* text); static float GetStringWidth(const char* text);
static ImVec2 CalculateTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); static ImVec2 CalculateTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f);
void TextDraw(float x, float y, bool shadow, ImVec4 color, const char* text, ...);
void TextDrawNotification(float duration, bool shadow, const char* fmt, ...);
private: private:
void TextDraw(float x, float y, bool shadow, const char* text, ...);
void LoadFont(const std::string& name, const std::string& path, float fontSize); void LoadFont(const std::string& name, const std::string& path, float fontSize);
}; };
static bool OverlayCommand(const std::vector<std::string>& args); static bool OverlayCommand(const std::vector<std::string>& args);
} }

View File

@ -808,6 +808,10 @@ namespace SohImGui {
size = ImVec2(sw, size.y); size = ImVec2(sw, size.y);
} }
if (ImGui::Button("Test Notification")) {
overlay->TextDrawNotification(3.0f, true, ("Test Notification: " + std::to_string(rand())).c_str());
}
overlay->Draw(); overlay->Draw();
} }