yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_popup.cc
Go to the documentation of this file.
1#include "canvas_popup.h"
2
3#include <algorithm>
4
5namespace yaze {
6namespace gui {
7
8void PopupRegistry::Open(const std::string& popup_id,
9 std::function<void()> render_callback) {
10 // Check if popup already exists
11 auto it = FindPopup(popup_id);
12
13 if (it != popups_.end()) {
14 // Update existing popup
15 it->is_open = true;
16 it->render_callback = std::move(render_callback);
17 ImGui::OpenPopup(popup_id.c_str());
18 return;
19 }
20
21 // Add new popup
22 PopupState new_popup;
23 new_popup.popup_id = popup_id;
24 new_popup.is_open = true;
25 new_popup.render_callback = std::move(render_callback);
26 new_popup.persist = true;
27
28 popups_.push_back(new_popup);
29
30 // Open the popup in ImGui
31 ImGui::OpenPopup(popup_id.c_str());
32}
33
34void PopupRegistry::Close(const std::string& popup_id) {
35 auto it = FindPopup(popup_id);
36
37 if (it != popups_.end()) {
38 it->is_open = false;
39
40 // Close in ImGui if it's the current popup
41 // Note: ImGui::CloseCurrentPopup() only works if this is the active popup
42 // In practice, the popup will be removed on next RenderAll() call
43 if (ImGui::IsPopupOpen(popup_id.c_str())) {
44 ImGui::CloseCurrentPopup();
45 }
46 }
47}
48
49bool PopupRegistry::IsOpen(const std::string& popup_id) const {
50 auto it = FindPopup(popup_id);
51 return it != popups_.end() && it->is_open;
52}
53
55 // Render all active popups
56 auto it = popups_.begin();
57 while (it != popups_.end()) {
58 if (it->is_open && it->render_callback) {
59 // Call the render callback which should handle BeginPopup/EndPopup
60 it->render_callback();
61
62 // Check if popup was closed by user (clicking outside, pressing Escape, etc.)
63 if (!ImGui::IsPopupOpen(it->popup_id.c_str())) {
64 it->is_open = false;
65 }
66 }
67
68 // Remove closed popups from the registry
69 if (!it->is_open) {
70 it = popups_.erase(it);
71 } else {
72 ++it;
73 }
74 }
75}
76
78 return std::count_if(popups_.begin(), popups_.end(),
79 [](const PopupState& popup) { return popup.is_open; });
80}
81
83 // Close all popups
84 for (auto& popup : popups_) {
85 if (popup.is_open && ImGui::IsPopupOpen(popup.popup_id.c_str())) {
86 ImGui::CloseCurrentPopup();
87 }
88 popup.is_open = false;
89 }
90
91 // Clear the registry
92 popups_.clear();
93}
94
95std::vector<PopupState>::iterator PopupRegistry::FindPopup(
96 const std::string& popup_id) {
97 return std::find_if(popups_.begin(), popups_.end(),
98 [&popup_id](const PopupState& popup) {
99 return popup.popup_id == popup_id;
100 });
101}
102
103std::vector<PopupState>::const_iterator PopupRegistry::FindPopup(
104 const std::string& popup_id) const {
105 return std::find_if(popups_.begin(), popups_.end(),
106 [&popup_id](const PopupState& popup) {
107 return popup.popup_id == popup_id;
108 });
109}
110
111} // namespace gui
112} // namespace yaze
113
void Close(const std::string &popup_id)
Close a persistent popup.
size_t GetActiveCount() const
Get the number of active popups.
void RenderAll()
Render all active popups.
void Clear()
Clear all popups from the registry.
std::vector< PopupState > popups_
bool IsOpen(const std::string &popup_id) const
Check if a popup is currently open.
std::vector< PopupState >::iterator FindPopup(const std::string &popup_id)
void Open(const std::string &popup_id, std::function< void()> render_callback)
Open a persistent popup.
Main namespace for the application.
Definition controller.cc:20
State for a single persistent popup.
std::function< void()> render_callback