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