Unravel Engine C++ Reference
Loading...
Searching...
No Matches
imgui_messagebox.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <imgui_includes.h>
4#include <string>
5#include <vector>
6#include <memory>
7#include <chrono>
8
9namespace ImBox
10{
11
14{
15 None = 0x0,
16 Ok = 1 << 0,
17 Cancel = 1 << 1,
18 Yes = 1 << 2,
19 No = 1 << 3,
20 Abort = 1 << 4,
21 Retry = 1 << 5,
22 Ignore = 1 << 6,
23 YesToAll = 1 << 7,
24 NoToAll = 1 << 8,
25 Apply = 1 << 9,
26 Discard = 1 << 10,
27 Help = 1 << 11,
28 Reset = 1 << 12,
29 Close = 1 << 13,
30 Save = 1 << 14,
31 Delete = 1 << 15,
32 DontSave = 1 << 16,
33 CancelDelete = 1 << 17
34};
35
36inline auto IsConfirmation(ModalResult result) -> bool
37{
38 return result == ModalResult::Ok || result == ModalResult::Yes || result == ModalResult::YesToAll || result == ModalResult::Apply || result == ModalResult::Save;
39}
40
42enum class MessageType
43{
44 Info,
45 Warning,
46 Error,
47 Success,
49 Custom
50};
51
54{
55 Opening,
56 Open,
57 Closing,
58 Closed
59};
60
63{
65 ImVec2 min_size = {300, 150};
66 ImVec2 max_size = {600, 400};
67 float animation_duration = 0.1f;
68 bool center_on_screen = true;
69 bool allow_resize = false;
70 bool show_close_button = true;
71 ImVec4 background_color = {0.0f, 0.0f, 0.0f, 0.0f}; // Use default if alpha is 0
72};
73
75class MsgBox
76{
77public:
79 MsgBox(const std::string& title, const std::string& message, int buttons,
80 const MsgBoxConfig& config = MsgBoxConfig{});
81
84
86 auto OpenPopup(std::function<void(ModalResult)> callback) -> void;
87
89 auto Draw() -> bool;
90
92 auto IsOpen() const -> bool { return animation_state_ != AnimationState::Closed; }
93
94
96 auto SetCustomIcon(const char* icon) -> void { custom_icon_ = icon; }
97
98private:
99 auto CalculateButtonLayout() -> void;
100 auto DrawIcon() -> void;
101 auto DrawMessage() -> void;
102 auto DrawButtons() -> void;
103 auto UpdateAnimation() -> void;
104 auto GetTypeIcon() const -> const char*;
105 auto GetTypeColor() const -> ImVec4;
106 auto GetAnimationAlpha() const -> float;
107
108 std::string title_;
109 std::string message_;
110 int buttons_;
111 MsgBoxConfig config_;
112
113 std::function<void(ModalResult)> callback_;
114
115 // Animation
116 AnimationState animation_state_;
117 std::chrono::steady_clock::time_point animation_start_;
118 float animation_progress_;
119
120 // Layout
121 std::vector<std::pair<ModalResult, std::string>> button_layout_;
122 ImVec2 content_size_;
123
124 // Custom styling
125 const char* custom_icon_;
126
127 bool open_requested_{false};
128 int id_counter_{}; // Store the unique ID counter for this instance
129
130 static int next_id_counter_;
131};
132
135{
136public:
138 static auto GetInstance() -> MsgBoxManager&;
139
141 auto ShowMessageBox(
142 const std::string& title,
143 const std::string& message,
144 int buttons = ModalResult::Ok,
145 const MsgBoxConfig& config = MsgBoxConfig{},
146 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
147
149 auto RenderAll() -> void;
150
152 auto CloseAll() -> void;
153
155 auto GetActiveCount() const -> size_t { return active_boxes_.size(); }
156
157private:
158 MsgBoxManager() = default;
159 auto CleanupClosedBoxes() -> void;
160
161 std::vector<std::shared_ptr<MsgBox>> active_boxes_;
162};
163
164// Convenience functions for common message box types
165
167auto ShowInfo(const std::string& title, const std::string& message,
168 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
169
171auto ShowWarning(const std::string& title, const std::string& message,
172 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
173
175auto ShowError(const std::string& title, const std::string& message,
176 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
177
179auto ShowSuccess(const std::string& title, const std::string& message,
180 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
181
183auto ShowQuestion(const std::string& title, const std::string& message,
184 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
185
187auto ShowConfirmation(const std::string& title, const std::string& message,
188 std::function<void(ModalResult)> callback = nullptr, MessageType type = MessageType::Question) -> std::shared_ptr<MsgBox>;
189
191auto ShowSaveConfirmation(const std::string& title, const std::string& message,
192 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
193
195auto ShowDeleteConfirmation(const std::string& title, const std::string& message,
196 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
197
199auto RenderMessageBoxes() -> void;
200
201} // namespace ImBox
Individual message box instance.
auto SetCustomIcon(const char *icon) -> void
Set custom icon (overrides type-based icon)
auto IsOpen() const -> bool
Check if the message box is open.
auto OpenPopup(std::function< void(ModalResult)> callback) -> void
Open the popup with callback.
MsgBox(const std::string &title, const std::string &message, int buttons, const MsgBoxConfig &config=MsgBoxConfig{})
Constructor with configuration.
auto Draw() -> bool
Draw the message box (returns true if still open)
~MsgBox()
Destructor.
Message box manager for handling multiple popups.
auto RenderAll() -> void
Render all active message boxes.
auto ShowMessageBox(const std::string &title, const std::string &message, int buttons=ModalResult::Ok, const MsgBoxConfig &config=MsgBoxConfig{}, std::function< void(ModalResult)> callback=nullptr) -> std::shared_ptr< MsgBox >
Create and show a message box.
static auto GetInstance() -> MsgBoxManager &
Get the singleton instance.
auto GetActiveCount() const -> size_t
Get count of active message boxes.
auto CloseAll() -> void
Close all message boxes.
const char * icon
const char * title
texture_job_type type
ModalResult
Modal result flags for message box buttons.
auto ShowDeleteConfirmation(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show a delete confirmation dialog with Delete/Cancel buttons.
MessageType
Message box types that determine appearance and icon.
auto ShowInfo(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show an information message box.
auto ShowConfirmation(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback, MessageType type) -> std::shared_ptr< MsgBox >
Show a confirmation dialog with OK/Cancel buttons.
auto ShowSaveConfirmation(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show a save confirmation dialog with Save/Don't Save/Cancel buttons.
auto ShowSuccess(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show a success message box.
auto IsConfirmation(ModalResult result) -> bool
auto ShowWarning(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show a warning message box.
auto ShowQuestion(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show a question message box with Yes/No buttons.
auto ShowError(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show an error message box.
AnimationState
Animation state for smooth transitions.
auto RenderMessageBoxes() -> void
Render all message boxes (call this in your main render loop)
Hash specialization for batch_key to enable use in std::unordered_map.
Configuration for message box appearance and behavior.