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
37enum class MessageType
38{
39 Info,
40 Warning,
41 Error,
42 Success,
44 Custom
45};
46
49{
50 Opening,
51 Open,
52 Closing,
53 Closed
54};
55
58{
60 ImVec2 min_size = {300, 150};
61 ImVec2 max_size = {600, 400};
62 float animation_duration = 0.1f;
63 bool center_on_screen = true;
64 bool allow_resize = false;
65 bool show_close_button = true;
66 ImVec4 background_color = {0.0f, 0.0f, 0.0f, 0.0f}; // Use default if alpha is 0
67};
68
70class MsgBox
71{
72public:
74 MsgBox(const std::string& title, const std::string& message, int buttons,
75 const MsgBoxConfig& config = MsgBoxConfig{});
76
79
81 auto OpenPopup(std::function<void(ModalResult)> callback) -> void;
82
84 auto Draw() -> bool;
85
87 auto IsOpen() const -> bool { return animation_state_ != AnimationState::Closed; }
88
89
91 auto SetCustomIcon(const char* icon) -> void { custom_icon_ = icon; }
92
93private:
94 auto CalculateButtonLayout() -> void;
95 auto DrawIcon() -> void;
96 auto DrawMessage() -> void;
97 auto DrawButtons() -> void;
98 auto UpdateAnimation() -> void;
99 auto GetTypeIcon() const -> const char*;
100 auto GetTypeColor() const -> ImVec4;
101 auto GetAnimationAlpha() const -> float;
102
103 std::string title_;
104 std::string message_;
105 int buttons_;
106 MsgBoxConfig config_;
107
108 std::function<void(ModalResult)> callback_;
109
110 // Animation
111 AnimationState animation_state_;
112 std::chrono::steady_clock::time_point animation_start_;
113 float animation_progress_;
114
115 // Layout
116 std::vector<std::pair<ModalResult, std::string>> button_layout_;
117 ImVec2 content_size_;
118
119 // Custom styling
120 const char* custom_icon_;
121
122 bool open_requested_{false};
123 int id_counter_{}; // Store the unique ID counter for this instance
124
125 static int next_id_counter_;
126};
127
130{
131public:
133 static auto GetInstance() -> MsgBoxManager&;
134
136 auto ShowMessageBox(
137 const std::string& title,
138 const std::string& message,
139 int buttons = ModalResult::Ok,
140 const MsgBoxConfig& config = MsgBoxConfig{},
141 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
142
144 auto RenderAll() -> void;
145
147 auto CloseAll() -> void;
148
150 auto GetActiveCount() const -> size_t { return active_boxes_.size(); }
151
152private:
153 MsgBoxManager() = default;
154 auto CleanupClosedBoxes() -> void;
155
156 std::vector<std::shared_ptr<MsgBox>> active_boxes_;
157};
158
159// Convenience functions for common message box types
160
162auto ShowInfo(const std::string& title, const std::string& message,
163 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
164
166auto ShowWarning(const std::string& title, const std::string& message,
167 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
168
170auto ShowError(const std::string& title, const std::string& message,
171 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
172
174auto ShowSuccess(const std::string& title, const std::string& message,
175 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
176
178auto ShowQuestion(const std::string& title, const std::string& message,
179 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
180
182auto ShowConfirmation(const std::string& title, const std::string& message,
183 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
184
186auto ShowSaveConfirmation(const std::string& title, const std::string& message,
187 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
188
190auto ShowDeleteConfirmation(const std::string& title, const std::string& message,
191 std::function<void(ModalResult)> callback = nullptr) -> std::shared_ptr<MsgBox>;
192
194auto RenderMessageBoxes() -> void;
195
196} // 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
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 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 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 ShowConfirmation(const std::string &title, const std::string &message, std::function< void(ModalResult)> callback) -> std::shared_ptr< MsgBox >
Show a confirmation dialog with OK/Cancel 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)
Configuration for message box appearance and behavior.