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
6
7namespace yaze {
8namespace gui {
9
10void PopupRegistry::Open(const std::string& popup_id,
11 std::function<void()> render_callback) {
12 // Check if popup already exists
13 auto it = FindPopup(popup_id);
14
15 if (it != popups_.end()) {
16 // Update existing popup
17 it->is_open = true;
18 it->render_callback = std::move(render_callback);
19 ImGui::OpenPopup(popup_id.c_str());
20 return;
21 }
22
23 // Add new popup
24 PopupState new_popup;
25 new_popup.popup_id = popup_id;
26 new_popup.is_open = true;
27 new_popup.render_callback = std::move(render_callback);
28 new_popup.persist = true;
29
30 popups_.push_back(new_popup);
31
32 // Open the popup in ImGui
33 ImGui::OpenPopup(popup_id.c_str());
34}
35
36void PopupRegistry::Close(const std::string& popup_id) {
37 auto it = FindPopup(popup_id);
38
39 if (it != popups_.end()) {
40 it->is_open = false;
41
42 // Close in ImGui if it's the current popup
43 // Note: ImGui::CloseCurrentPopup() only works if this is the active popup
44 // In practice, the popup will be removed on next RenderAll() call
45 if (ImGui::IsPopupOpen(popup_id.c_str())) {
46 ImGui::CloseCurrentPopup();
47 }
48 }
49}
50
51bool PopupRegistry::IsOpen(const std::string& popup_id) const {
52 auto it = FindPopup(popup_id);
53 return it != popups_.end() && it->is_open;
54}
55
57 // Render all active popups
58 auto it = popups_.begin();
59 while (it != popups_.end()) {
60 if (it->is_open && it->render_callback) {
61 // Call the render callback which should handle BeginPopup/EndPopup.
62 // Canvases render these inside their zero-padding region.
63 {
64 PopupStyleScope popup_style;
65 it->render_callback();
66 }
67
68 // Check if popup was closed by user (clicking outside, pressing Escape,
69 // etc.)
70 if (!ImGui::IsPopupOpen(it->popup_id.c_str())) {
71 it->is_open = false;
72 }
73 }
74
75 // Remove closed popups from the registry
76 if (!it->is_open) {
77 it = popups_.erase(it);
78 } else {
79 ++it;
80 }
81 }
82}
83
85 return std::count_if(popups_.begin(), popups_.end(),
86 [](const PopupState& popup) { return popup.is_open; });
87}
88
90 // Close all popups
91 for (auto& popup : popups_) {
92 if (popup.is_open && ImGui::IsPopupOpen(popup.popup_id.c_str())) {
93 ImGui::CloseCurrentPopup();
94 }
95 popup.is_open = false;
96 }
97
98 // Clear the registry
99 popups_.clear();
100}
101
102std::vector<PopupState>::iterator PopupRegistry::FindPopup(
103 const std::string& popup_id) {
104 return std::find_if(popups_.begin(), popups_.end(),
105 [&popup_id](const PopupState& popup) {
106 return popup.popup_id == popup_id;
107 });
108}
109
110std::vector<PopupState>::const_iterator PopupRegistry::FindPopup(
111 const std::string& popup_id) const {
112 return std::find_if(popups_.begin(), popups_.end(),
113 [&popup_id](const PopupState& popup) {
114 return popup.popup_id == popup_id;
115 });
116}
117
118} // namespace gui
119} // 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.
Gives one popup the app's normal spacing.
State for a single persistent popup.
std::function< void()> render_callback