Skip to content

Commit

Permalink
Rename Toast Types and move to ImGui table
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonzkiller committed Jan 20, 2025
1 parent 78aecb6 commit bb9f935
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 120 deletions.
164 changes: 75 additions & 89 deletions src/common/ImGuiNotify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
* @date 07.07.2024
*/

#ifndef IMGUI_NOTIFY
#define IMGUI_NOTIFY

#pragma once

#include <vector> // Vector for storing notifications list
Expand All @@ -33,7 +30,7 @@
#define NOTIFY_DEFAULT_DISMISS 5000 // Auto dismiss after X ms (default, applied only of no data provided in constructors)
#define NOTIFY_OPACITY 0.8f // 0-1 Toast opacity
#define NOTIFY_USE_SEPARATOR false // If true, a separator will be rendered between the title and the content
#define NOTIFY_USE_DISMISS_BUTTON false // If true, a dismiss button will be rendered in the top right corner of the toast
#define NOTIFY_USE_DISMISS_BUTTON true // If true, a dismiss button will be rendered in the top right corner of the toast
#define NOTIFY_RENDER_LIMIT 5 // Max number of toasts rendered at the same time. Set to 0 for unlimited

// Warning: Requires ImGui docking with multi-viewport enabled
Expand All @@ -56,7 +53,7 @@ static const ImGuiWindowFlags NOTIFY_DEFAULT_TOAST_FLAGS =
va_end(args); \
}

enum class ImGuiToastType : uint8_t
enum class ToastNotificationType : uint8_t
{
None,
Success,
Expand All @@ -66,7 +63,7 @@ enum class ImGuiToastType : uint8_t
COUNT
};

enum class ImGuiToastPhase : uint8_t
enum class ImNotifyToastPhase : uint8_t
{
FadeIn,
Wait,
Expand All @@ -75,7 +72,7 @@ enum class ImGuiToastPhase : uint8_t
COUNT
};

enum class ImGuiToastPos : uint8_t
enum class ImNotifyToastPos : uint8_t
{
TopLeft,
TopCenter,
Expand All @@ -90,12 +87,12 @@ enum class ImGuiToastPos : uint8_t
/**
* @brief A class for creating toast notifications with ImGui.
*/
class ImGuiToast
class ToastNotification
{
private:
ImGuiWindowFlags flags = NOTIFY_DEFAULT_TOAST_FLAGS;

ImGuiToastType type = ImGuiToastType::None;
ToastNotificationType type = ToastNotificationType::None;
char title[NOTIFY_MAX_MSG_LENGTH];
char content[NOTIFY_MAX_MSG_LENGTH];

Expand Down Expand Up @@ -140,9 +137,9 @@ class ImGuiToast
*
* @param type The type of the toast notification.
*/
inline void setType(const ImGuiToastType& type)
inline void setType(const ToastNotificationType& type)
{
IM_ASSERT(type < ImGuiToastType::COUNT);
IM_ASSERT(type < ToastNotificationType::COUNT);
this->type = type;
};

Expand Down Expand Up @@ -189,11 +186,11 @@ class ImGuiToast
{
switch (this->type)
{
case ImGuiToastType::None: return nullptr;
case ImGuiToastType::Success: return "Success";
case ImGuiToastType::Warning: return "Warning";
case ImGuiToastType::Error: return "Error";
case ImGuiToastType::Info: return "Info";
case ToastNotificationType::None: return nullptr;
case ToastNotificationType::Success: return "Success";
case ToastNotificationType::Warning: return "Warning";
case ToastNotificationType::Error: return "Error";
case ToastNotificationType::Info: return "Info";
default: return nullptr;
}
}
Expand All @@ -206,7 +203,7 @@ class ImGuiToast
*
* @return ImGuiToastType The type of the toast notification.
*/
inline ImGuiToastType getType() { return this->type; };
inline ToastNotificationType getType() { return this->type; };

/**
* @brief Get the color of the toast notification based on its type.
Expand All @@ -217,11 +214,11 @@ class ImGuiToast
{
switch (this->type)
{
case ImGuiToastType::None: return {255, 255, 255, 255}; // White
case ImGuiToastType::Success: return {0, 255, 0, 255}; // Green
case ImGuiToastType::Warning: return {255, 255, 0, 255}; // Yellow
case ImGuiToastType::Error: return {255, 0, 0, 255}; // Error
case ImGuiToastType::Info: return {0, 157, 255, 255}; // Blue
case ToastNotificationType::None: return {255, 255, 255, 255}; // White
case ToastNotificationType::Success: return {0, 255, 0, 255}; // Green
case ToastNotificationType::Warning: return {255, 255, 0, 255}; // Yellow
case ToastNotificationType::Error: return {255, 0, 0, 255}; // Error
case ToastNotificationType::Info: return {0, 157, 255, 255}; // Blue
default: return {255, 255, 255, 255}; // White
}
}
Expand All @@ -235,11 +232,11 @@ class ImGuiToast
{
switch (this->type)
{
case ImGuiToastType::None: return nullptr;
case ImGuiToastType::Success: return ICON_MD_CHECK_CIRCLE; // Font Awesome 6
case ImGuiToastType::Warning: return ICON_MD_ALERT; // Font Awesome 6
case ImGuiToastType::Error: return ICON_MD_ALERT_DECAGRAM; // Font Awesome 6
case ImGuiToastType::Info: return ICON_MD_INFORMATION; // Font Awesome 6
case ToastNotificationType::None: return nullptr;
case ToastNotificationType::Success: return ICON_MD_CHECK_CIRCLE; // Font Awesome 6
case ToastNotificationType::Warning: return ICON_MD_ALERT; // Font Awesome 6
case ToastNotificationType::Error: return ICON_MD_ALERT_DECAGRAM; // Font Awesome 6
case ToastNotificationType::Info: return ICON_MD_INFORMATION; // Font Awesome 6
default: return nullptr;
}
}
Expand All @@ -262,31 +259,31 @@ class ImGuiToast
/**
* @brief Get the current phase of the toast notification based on the elapsed time since its creation.
*
* @return ImGuiToastPhase The current phase of the toast notification.
* - ImGuiToastPhase::FadeIn: The notification is fading in.
* - ImGuiToastPhase::Wait: The notification is waiting to be dismissed.
* - ImGuiToastPhase::FadeOut: The notification is fading out.
* - ImGuiToastPhase::Expired: The notification has expired and should be removed.
* @return ImNotifyToastPhase The current phase of the toast notification.
* - ImNotifyToastPhase::FadeIn: The notification is fading in.
* - ImNotifyToastPhase::Wait: The notification is waiting to be dismissed.
* - ImNotifyToastPhase::FadeOut: The notification is fading out.
* - ImNotifyToastPhase::Expired: The notification has expired and should be removed.
*/
inline ImGuiToastPhase getPhase()
inline ImNotifyToastPhase getPhase()
{
const int64_t elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(getElapsedTime()).count();

if (elapsed > NOTIFY_FADE_IN_OUT_TIME + this->dismissTime + NOTIFY_FADE_IN_OUT_TIME)
{
return ImGuiToastPhase::Expired;
return ImNotifyToastPhase::Expired;
}
else if (elapsed > NOTIFY_FADE_IN_OUT_TIME + this->dismissTime)
{
return ImGuiToastPhase::FadeOut;
return ImNotifyToastPhase::FadeOut;
}
else if (elapsed > NOTIFY_FADE_IN_OUT_TIME)
{
return ImGuiToastPhase::Wait;
return ImNotifyToastPhase::Wait;
}
else
{
return ImGuiToastPhase::FadeIn;
return ImNotifyToastPhase::FadeIn;
}
}

Expand All @@ -296,14 +293,14 @@ class ImGuiToast
*/
inline float getFadePercent()
{
const ImGuiToastPhase phase = getPhase();
const ImNotifyToastPhase phase = getPhase();
const int64_t elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(getElapsedTime()).count();

if (phase == ImGuiToastPhase::FadeIn)
if (phase == ImNotifyToastPhase::FadeIn)
{
return ((float)elapsed / (float)NOTIFY_FADE_IN_OUT_TIME) * NOTIFY_OPACITY;
}
else if (phase == ImGuiToastPhase::FadeOut)
else if (phase == ImNotifyToastPhase::FadeOut)
{
return (1.f - (((float)elapsed - (float)NOTIFY_FADE_IN_OUT_TIME - (float)this->dismissTime) / (float)NOTIFY_FADE_IN_OUT_TIME)) * NOTIFY_OPACITY;
}
Expand All @@ -330,14 +327,14 @@ class ImGuiToast
// Constructors

/**
* @brief Creates a new ImGuiToast object with the specified type and dismiss time.
* @brief Creates a new ToastNotification object with the specified type and dismiss time.
*
* @param type The type of the toast.
* @param dismissTime The time in milliseconds after which the toast should be dismissed. Default is NOTIFY_DEFAULT_DISMISS.
*/
ImGuiToast(ImGuiToastType type, int dismissTime = NOTIFY_DEFAULT_DISMISS)
ToastNotification(ToastNotificationType type, int dismissTime = NOTIFY_DEFAULT_DISMISS)
{
IM_ASSERT(type < ImGuiToastType::COUNT);
IM_ASSERT(type < ToastNotificationType::COUNT);

this->type = type;
this->dismissTime = dismissTime;
Expand All @@ -349,55 +346,55 @@ class ImGuiToast
}

/**
* @brief Constructor for creating an ImGuiToast object with a specified type and message format.
* @brief Constructor for creating an ToastNotification object with a specified type and message format.
*
* @param type The type of the toast message.
* @param format The format string for the message.
* @param ... The variable arguments to be formatted according to the format string.
*/
ImGuiToast(ImGuiToastType type, const char* format, ...)
: ImGuiToast(type)
ToastNotification(ToastNotificationType type, const char* format, ...)
: ToastNotification(type)
{
NOTIFY_FORMAT(this->setContent, format);
}

/* @brief Constructor for creating an ImGuiToast object with a specified type and std::string message.
/* @brief Constructor for creating an ToastNotification object with a specified type and std::string message.
* @param dismissTime The time in milliseconds before the toast message is dismissed.
* @param message The message to be displayed in the toast.
*/
ImGuiToast(ImGuiToastType type, const std::string& message)
: ImGuiToast(type)
ToastNotification(ToastNotificationType type, const std::string& message)
: ToastNotification(type)
{
this->setContent(message);
}

/**
* @brief Constructor for creating a new ImGuiToast object with a specified type, dismiss time, and content format.
* @brief Constructor for creating a new ToastNotification object with a specified type, dismiss time, and content format.
*
* @param type The type of the toast message.
* @param dismissTime The time in milliseconds before the toast message is dismissed.
* @param format The format string for the content of the toast message.
* @param ... The variable arguments to be formatted according to the format string.
*/
ImGuiToast(ImGuiToastType type, int dismissTime, const char* format, ...)
: ImGuiToast(type, dismissTime)
ToastNotification(ToastNotificationType type, int dismissTime, const char* format, ...)
: ToastNotification(type, dismissTime)
{
NOTIFY_FORMAT(this->setContent, format);
}

/* @brief Constructor for creating an ImGuiToast object with a specified type, dismiss time, and std::string message.
/* @brief Constructor for creating an ToastNotification object with a specified type, dismiss time, and std::string message.
* @param type The type of the toast message.
* @param dismissTime The time in milliseconds before the toast message is dismissed.
* @param message The message to be displayed in the toast.
*/
ImGuiToast(ImGuiToastType type, int dismissTime, const std::string& message)
: ImGuiToast(type, dismissTime)
ToastNotification(ToastNotificationType type, int dismissTime, const std::string& message)
: ToastNotification(type, dismissTime)
{
this->setContent(message);
}

/**
* @brief Constructor for creating a new ImGuiToast object with a specified type, dismiss time, title format, content format and a button.
* @brief Constructor for creating a new ToastNotification object with a specified type, dismiss time, title format, content format and a button.
*
* @param type The type of the toast message.
* @param dismissTime The time in milliseconds before the toast message is dismissed.
Expand All @@ -406,8 +403,8 @@ class ImGuiToast
* @param format The format string for the content of the toast message.
* @param ... The variable arguments to be formatted according to the format string.
*/
ImGuiToast(ImGuiToastType type, int dismissTime, const char* buttonLabel, const std::function<void()>& onButtonPress, const char* format, ...)
: ImGuiToast(type, dismissTime)
ToastNotification(ToastNotificationType type, int dismissTime, const char* buttonLabel, const std::function<void()>& onButtonPress, const char* format, ...)
: ToastNotification(type, dismissTime)
{
NOTIFY_FORMAT(this->setContent, format);

Expand All @@ -418,13 +415,13 @@ class ImGuiToast

namespace ImGui
{
inline std::vector<ImGuiToast> notifications;
inline std::vector<ToastNotification> notifications;

/**
* Inserts a new notification into the notification queue.
* @param toast The notification to be inserted.
*/
inline void InsertNotification(const ImGuiToast& toast)
inline void InsertNotification(const ToastNotification& toast)
{
notifications.push_back(toast);
}
Expand Down Expand Up @@ -452,10 +449,10 @@ inline void RenderNotifications()

for (size_t i = 0; i < notifications.size(); ++i)
{
ImGuiToast* currentToast = &notifications[i];
ToastNotification* currentToast = &notifications[i];

// Remove toast if expired
if (currentToast->getPhase() == ImGuiToastPhase::Expired)
if (currentToast->getPhase() == ImNotifyToastPhase::Expired)
{
RemoveNotification(i);
continue;
Expand Down Expand Up @@ -623,38 +620,27 @@ inline void RenderNotifications()
}
} // namespace ImGui

namespace sol_ImGuiNotify
namespace sol_ToastNotification
{
// rename this to be more user friendly
inline void ShowToast(const ImGuiToast& toast)
inline void ShowToast(const ToastNotification& toast)
{
ImGui::InsertNotification(toast);
}

inline void InitUserTypeAndEnums(sol::table luaGlobals)
inline void BindImNotifyToast(sol::table aTable)
{
luaGlobals.new_usertype<ImGuiToast>(
"ImGuiToast",
sol::constructors<ImGuiToast(ImGuiToastType),ImGuiToast(ImGuiToastType, int),ImGuiToast(ImGuiToastType,const std::string&),ImGuiToast(ImGuiToastType,int,const std::string&)>(),
"SetTitle", sol::resolve<void(const std::string&)>(&ImGuiToast::setTitle),
"SetContent", sol::resolve<void(const std::string&)>(&ImGuiToast::setContent),
"SetType", &ImGuiToast::setType,
"SetWindowFlags", &ImGuiToast::setWindowFlags);

luaGlobals.new_enum(
"ImGuiToastType", "None", ImGuiToastType::None, "Success", ImGuiToastType::Success, "Warning", ImGuiToastType::Warning, "Error", ImGuiToastType::Error, "Info", ImGuiToastType::Info);
aTable.new_usertype<ToastNotification>(
"ToastNotification",
sol::constructors<ToastNotification(ToastNotificationType),ToastNotification(ToastNotificationType, int),ToastNotification(ToastNotificationType,const std::string&),ToastNotification(ToastNotificationType,int,const std::string&)>(),
"SetTitle", sol::resolve<void(const std::string&)>(&ToastNotification::setTitle),
"SetContent", sol::resolve<void(const std::string&)>(&ToastNotification::setContent),
"SetType", &ToastNotification::setType,
"SetWindowFlags", &ToastNotification::setWindowFlags);

aTable.new_enum(
"ToastNotificationType", "None", ToastNotificationType::None, "Success", ToastNotificationType::Success, "Warning", ToastNotificationType::Warning, "Error", ToastNotificationType::Error, "Info", ToastNotificationType::Info);

aTable.set_function("ShowToast", ShowToast);
}

inline void InitBindings(sol::state& lua, sol::table luaGlobals)
{
InitUserTypeAndEnums(luaGlobals);

sol::table ImGuiNotify(lua, sol::create);

ImGuiNotify.set_function("ShowToast", ShowToast);

luaGlobals["ImGuiNotify"] = ImGuiNotify;
}
} // namespace sol_ImGuiNotify

#endif
} // namespace sol_ToastNotification
2 changes: 1 addition & 1 deletion src/overlay/Overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void Overlay::PostInitialize()
else
{
const auto cOverlayBindCode = CET::Get().GetBindings().GetBindCodeForModBind(Bindings::GetOverlayToggleModBind());
ImGui::InsertNotification({ImGuiToastType::Info, NOTIFY_DEFAULT_DISMISS, "CET Overlay Bind: %s", VKBindings::GetBindString(cOverlayBindCode).c_str()});
ImGui::InsertNotification({ToastNotificationType::Info, NOTIFY_DEFAULT_DISMISS, "CET Overlay Bind: %s", VKBindings::GetBindString(cOverlayBindCode).c_str()});
}

m_initialized = true;
Expand Down
Loading

0 comments on commit bb9f935

Please sign in to comment.