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,
63 // etc.)
64 if (!ImGui::IsPopupOpen(it->popup_id.c_str())) {
65 it->is_open = false;
66 }
67 }
68
69 // Remove closed popups from the registry
70 if (!it->is_open) {
71 it = popups_.erase(it);
72 } else {
73 ++it;
74 }
75 }
76}
77
79 return std::count_if(popups_.begin(), popups_.end(),
80 [](const PopupState& popup) { return popup.is_open; });
81}
82
84 // Close all popups
85 for (auto& popup : popups_) {
86 if (popup.is_open && ImGui::IsPopupOpen(popup.popup_id.c_str())) {
87 ImGui::CloseCurrentPopup();
88 }
89 popup.is_open = false;
90 }
91
92 // Clear the registry
93 popups_.clear();
94}
95
96std::vector<PopupState>::iterator PopupRegistry::FindPopup(
97 const std::string& popup_id) {
98 return std::find_if(popups_.begin(), popups_.end(),
99 [&popup_id](const PopupState& popup) {
100 return popup.popup_id == popup_id;
101 });
102}
103
104std::vector<PopupState>::const_iterator PopupRegistry::FindPopup(
105 const std::string& popup_id) const {
106 return std::find_if(popups_.begin(), popups_.end(),
107 [&popup_id](const PopupState& popup) {
108 return popup.popup_id == popup_id;
109 });
110}
111
112} // namespace gui
113} // namespace yaze
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.
State for a single persistent popup.
std::function< void()> render_callback