aboutsummaryrefslogtreecommitdiff
path: root/source/Game/EditorNotification.cpp
blob: e4a869e903173a97d391c848d2856f309be9bf81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Adapted from https://github.com/patrickcjk/imgui-notify
#include "EditorNotification.hpp"

#include "Macros.hpp"

#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui_internal.h>

#include <chrono>
#include <cstdarg>
#include <cstdio>
#include <utility>
#include <vector>

ImGuiToast::ImGuiToast(ImGuiToastType type, int dismissTime) {
	IM_ASSERT(type < ImGuiToastType_COUNT);

	mType = type;
	mDismissTime = dismissTime;

	using namespace std::chrono;
	auto timeStamp = system_clock::now().time_since_epoch();
	mCreationTime = duration_cast<milliseconds>(timeStamp).count();

	memset(mTitle, 0, sizeof(mTitle));
	memset(mContent, 0, sizeof(mContent));
}

ImGuiToast::ImGuiToast(ImGuiToastType type, const char* format, ...)
	: ImGuiToast(type) {
	if (format) {
		va_list args;
		va_start(args, format);
		SetContent(format, args);
		va_end(args);
	}
}

ImGuiToast::ImGuiToast(ImGuiToastType type, int dismissTime, const char* format, ...)
	: ImGuiToast(type, dismissTime) {
	if (format) {
		va_list args;
		va_start(args, format);
		SetContent(format, args);
		va_end(args);
	}
}

void ImGuiToast::SetTitle(const char* format, ...) {
	if (format) {
		va_list args;
		va_start(args, format);
		SetTitle(format, args);
		va_end(args);
	}
}

void ImGuiToast::SetContent(const char* format, ...) {
	if (format) {
		va_list args;
		va_start(args, format);
		SetContent(format, args);
		va_end(args);
	}
}

void ImGuiToast::SetType(const ImGuiToastType& type) {
	IM_ASSERT(type < ImGuiToastType_COUNT);
	mType = type;
}

const char* ImGuiToast::GetTitle() {
	return mTitle;
}

const char* ImGuiToast::GetDefaultTitle() {
	if (!strlen(mTitle)) {
		switch (mType) {
			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 ImGuiToastType_COUNT: UNREACHABLE;
		}
	}

	return mTitle;
}

ImGuiToastType ImGuiToast::GetType() {
	return mType;
}

ImVec4 ImGuiToast::GetColor() {
	switch (mType) {
		case ImGuiToastType_None: return ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // White
		case ImGuiToastType_Success: return ImVec4(0, 1.0f, 0, 1.0f); // Green
		case ImGuiToastType_Warning: return ImVec4(1.0f, 1.0f, 0, 1.0f); // Yellow
		case ImGuiToastType_Error: return ImVec4(1.0f, 0, 0, 1.0f); // Red
		case ImGuiToastType_Info: return ImVec4(0, 0.616, 1.0f, 1.0f); // Blue
		case ImGuiToastType_COUNT: UNREACHABLE;
	}
	return ImVec4();
}

const char* ImGuiToast::GetIcon() {
	switch (mType) {
		case ImGuiToastType_None: return nullptr;
#if 1
		// TODO add IconFontHeaders and replace with proper icons
		case ImGuiToastType_Success: return nullptr;
		case ImGuiToastType_Warning: return nullptr;
		case ImGuiToastType_Error: return nullptr;
		case ImGuiToastType_Info: return nullptr;
#else
		case ImGuiToastType_Success: return ICON_FA_CHECK_CIRCLE;
		case ImGuiToastType_Warning: return ICON_FA_EXCLAMATION_TRIANGLE;
		case ImGuiToastType_Error: return ICON_FA_TIMES_CIRCLE;
		case ImGuiToastType_Info: return ICON_FA_INFO_CIRCLE;
#endif
		case ImGuiToastType_COUNT: UNREACHABLE;
	}
	return nullptr;
}

const char* ImGuiToast::GetContent() {
	return this->mContent;
}

uint64_t ImGuiToast::GetElapsedTime() {
	using namespace std::chrono;
	auto timeStamp = system_clock::now().time_since_epoch();
	auto timeStampI = duration_cast<milliseconds>(timeStamp).count();
	return timeStampI - mCreationTime;
}

ImGuiToastPhase ImGuiToast::GetPhase() {
	const auto elapsed = GetElapsedTime();

	if (elapsed > kNotifyFadeInOutTime + mDismissTime + kNotifyFadeInOutTime) {
		return ImGuiToastPhase_Expired;
	} else if (elapsed > kNotifyFadeInOutTime + mDismissTime) {
		return ImGuiToastPhase_FadeOut;
	} else if (elapsed > kNotifyFadeInOutTime) {
		return ImGuiToastPhase_Wait;
	} else {
		return ImGuiToastPhase_FadeIn;
	}
}

float ImGuiToast::GetFadePercent() {
	const auto phase = GetPhase();
	const auto elapsed = GetElapsedTime();

	if (phase == ImGuiToastPhase_FadeIn)
	{
		return ((float)elapsed / (float)kNotifyFadeInOutTime) * kNotifyOpacity;
	} else if (phase == ImGuiToastPhase_FadeOut)
	{
		return (1.0f - (((float)elapsed - (float)kNotifyFadeInOutTime - (float)mDismissTime) / (float)kNotifyFadeInOutTime)) * kNotifyOpacity;
	}

	return 1.0f * kNotifyOpacity;
}

void ImGuiToast::SetTitle(const char* format, va_list args) {
	vsnprintf(mTitle, sizeof(mTitle), format, args);
}

void ImGuiToast::SetContent(const char* format, va_list args) {
	vsnprintf(mContent, sizeof(mContent), format, args);
}

namespace ImGui {
static std::vector<ImGuiToast> notifications;
}

static bool IsNullOrEmpty(const char* str) {
	return !str || !strlen(str);
}

void ImGui::AddNotification(ImGuiToast toast) {
	notifications.push_back(std::move(toast));
}

void ImGui::RemoveNotification(int index) {
	notifications.erase(notifications.begin() + index);
}

void ImGui::ShowNotifications() {
	auto vpSize = GetMainViewport()->Size;

	float height = 0.0f;
	for (auto i = 0; i < notifications.size(); i++) {
		auto* currentToast = &notifications[i];

		// Remove toast if expired
		if (currentToast->GetPhase() == ImGuiToastPhase_Expired) {
			RemoveNotification(i);
			continue;
		}

		// Get icon, title and other data
		const auto icon = currentToast->GetIcon();
		const auto title = currentToast->GetTitle();
		const auto content = currentToast->GetContent();
		const auto defaultTitle = currentToast->GetDefaultTitle();
		const auto opacity = currentToast->GetFadePercent(); // Get opacity based of the current phase

		// Window rendering
		auto textColor = currentToast->GetColor();
		textColor.w = opacity;

		// Generate new unique name for this toast
		char windowName[50];
		snprintf(windowName, std::size(windowName), "##TOAST%d", i);

		SetNextWindowBgAlpha(opacity);
		SetNextWindowPos(ImVec2(vpSize.x - kNotifyPaddingX, vpSize.y - kNotifyPaddingY - height), ImGuiCond_Always, ImVec2(1.0f, 1.0f));
		Begin(windowName, nullptr, kNotifyToastFlags);
		BringWindowToDisplayFront(GetCurrentWindow());

		// Here we render the toast content
		{
			PushTextWrapPos(vpSize.x / 3.0f); // We want to support multi-line text, this will wrap the text after 1/3 of the screen width

			bool wasTitleRendered = false;

			// If an icon is set
			if (!::IsNullOrEmpty(icon)) {
				// Render icon text
				PushStyleColor(ImGuiCol_Text, textColor);
				TextUnformatted(icon);
				PopStyleColor();
				wasTitleRendered = true;
			}

			// If a title is set
			if (!::IsNullOrEmpty(title)) {
				// If a title and an icon is set, we want to render on same line
				if (!::IsNullOrEmpty(icon))
					SameLine();

				TextUnformatted(title); // Render title text
				wasTitleRendered = true;
			} else if (!::IsNullOrEmpty(defaultTitle)) {
				if (!::IsNullOrEmpty(icon))
					SameLine();

				TextUnformatted(defaultTitle); // Render default title text (ImGuiToastType_Success -> "Success", etc...)
				wasTitleRendered = true;
			}

			// In case ANYTHING was rendered in the top, we want to add a small padding so the text (or icon) looks centered vertically
			if (wasTitleRendered && !::IsNullOrEmpty(content)) {
				SetCursorPosY(GetCursorPosY() + 5.0f); // Must be a better way to do this!!!!
			}

			// If a content is set
			if (!::IsNullOrEmpty(content)) {
				if (wasTitleRendered) {
					Separator();
				}

				TextUnformatted(content); // Render content text
			}

			PopTextWrapPos();
		}

		// Save height for next toasts
		height += GetWindowHeight() + kNotifyPaddingMessageY;

		End();
	}
}