yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
toast_manager.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SYSTEM_TOAST_MANAGER_H
2#define YAZE_APP_EDITOR_SYSTEM_TOAST_MANAGER_H
3
4#include <deque>
5#include <string>
6
7// Must define before including imgui.h
8#ifndef IMGUI_DEFINE_MATH_OPERATORS
9#define IMGUI_DEFINE_MATH_OPERATORS
10#endif
11
12#include "imgui/imgui.h"
13
14namespace yaze {
15namespace editor {
16
18
19struct Toast {
20 std::string message;
22 float ttl_seconds = 3.0f;
23};
24
26 public:
27 void Show(const std::string &message, ToastType type = ToastType::kInfo,
28 float ttl_seconds = 3.0f) {
29 toasts_.push_back({message, type, ttl_seconds});
30 }
31
32 void Draw() {
33 if (toasts_.empty()) return;
34 ImGuiIO &io = ImGui::GetIO();
35 ImVec2 pos(io.DisplaySize.x - 10.f, 40.f);
36
37 // Iterate copy so we can mutate ttl while drawing ordered from newest.
38 for (auto it = toasts_.begin(); it != toasts_.end();) {
39 Toast &t = *it;
40 ImVec4 bg;
41 switch (t.type) {
42 case ToastType::kInfo: bg = ImVec4(0.10f, 0.10f, 0.10f, 0.85f); break;
43 case ToastType::kSuccess: bg = ImVec4(0.10f, 0.30f, 0.10f, 0.85f); break;
44 case ToastType::kWarning: bg = ImVec4(0.30f, 0.25f, 0.05f, 0.90f); break;
45 case ToastType::kError: bg = ImVec4(0.40f, 0.10f, 0.10f, 0.90f); break;
46 }
47 ImGui::SetNextWindowBgAlpha(bg.w);
48 ImGui::SetNextWindowPos(pos, ImGuiCond_Always, ImVec2(1.f, 0.f));
49 ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration |
50 ImGuiWindowFlags_AlwaysAutoResize |
51 ImGuiWindowFlags_NoSavedSettings |
52 ImGuiWindowFlags_NoNav;
53 ImGui::PushStyleColor(ImGuiCol_WindowBg, bg);
54 if (ImGui::Begin("##toast", nullptr, flags)) {
55 ImGui::TextUnformatted(t.message.c_str());
56 }
57 ImGui::End();
58 ImGui::PopStyleColor(1);
59
60 // Decrease TTL
61 t.ttl_seconds -= io.DeltaTime;
62 if (t.ttl_seconds <= 0.f) {
63 it = toasts_.erase(it);
64 } else {
65 // Next toast stacks below
66 pos.y += ImGui::GetItemRectSize().y + 6.f;
67 ++it;
68 }
69 }
70 }
71
72 private:
73 std::deque<Toast> toasts_;
74};
75
76} // namespace editor
77} // namespace yaze
78
79#endif // YAZE_APP_EDITOR_SYSTEM_TOAST_MANAGER_H
80
81
std::deque< Toast > toasts_
void Show(const std::string &message, ToastType type=ToastType::kInfo, float ttl_seconds=3.0f)
Main namespace for the application.