-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
252 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* @file popup_manager.hpp | ||
* @date 12/18/24 | ||
* @brief | ||
* | ||
* @copyright Copyright (c) 2024 Ruixiang Du (rdu) | ||
*/ | ||
|
||
#ifndef POPUP_MANAGER_HPP | ||
#define POPUP_MANAGER_HPP | ||
|
||
#include <mutex> | ||
|
||
#include "imview/popup.hpp" | ||
|
||
namespace quickviz { | ||
class PopupManager { | ||
struct PopupDescriptor { | ||
PopupType type; | ||
std::string title; | ||
std::string msg; | ||
float width; | ||
float height; | ||
OnConfirmationCallback callback; | ||
}; | ||
|
||
struct PopupState { | ||
bool triggered; | ||
PopupDescriptor descriptor; | ||
}; | ||
|
||
public: | ||
static PopupManager& GetInstance() { | ||
static PopupManager instance; | ||
return instance; | ||
} | ||
|
||
void RegisterNotificationPopup(std::string title, std::string msg, | ||
float width = 300, float height = 150); | ||
void RegisterConfirmationPopup(std::string title, std::string msg, | ||
OnConfirmationCallback callback = nullptr, | ||
float width = 300, float height = 150); | ||
|
||
void TriggerPopup(std::string title); | ||
void CancelPopup(std::string title); | ||
void UpdatePopups(); | ||
|
||
private: | ||
PopupManager() = default; | ||
~PopupManager() = default; | ||
|
||
std::mutex popup_mtx_; | ||
std::unordered_map<std::string, PopupState> popups_; | ||
}; | ||
} // namespace quickviz | ||
|
||
#endif // POPUP_MANAGER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* @file popup_manager.cpp | ||
* @date 12/18/24 | ||
* @brief | ||
* | ||
* @copyright Copyright (c) 2024 Ruixiang Du (rdu) | ||
*/ | ||
|
||
#include "imview/component/popup_manager.hpp" | ||
|
||
#include "imgui.h" | ||
|
||
namespace quickviz { | ||
void PopupManager::RegisterNotificationPopup(std::string title, std::string msg, | ||
float width, float height) { | ||
std::lock_guard<std::mutex> lock(popup_mtx_); | ||
popups_[title].triggered = false; | ||
popups_[title].descriptor = PopupDescriptor{ | ||
PopupType::kNotification, title, msg, width, height, nullptr}; | ||
} | ||
|
||
void PopupManager::RegisterConfirmationPopup(std::string title, std::string msg, | ||
OnConfirmationCallback callback, | ||
float width, float height) { | ||
std::lock_guard<std::mutex> lock(popup_mtx_); | ||
popups_[title].triggered = false; | ||
popups_[title].descriptor = PopupDescriptor{ | ||
PopupType::kConfirmation, title, msg, width, height, callback}; | ||
} | ||
|
||
void PopupManager::TriggerPopup(std::string title) { | ||
std::lock_guard<std::mutex> lock(popup_mtx_); | ||
if (popups_.find(title) != popups_.end()) { | ||
popups_[title].triggered = true; | ||
} | ||
} | ||
|
||
void PopupManager::CancelPopup(std::string title) { | ||
std::lock_guard<std::mutex> lock(popup_mtx_); | ||
if (popups_.find(title) != popups_.end()) { | ||
popups_[title].triggered = false; | ||
} | ||
} | ||
|
||
void PopupManager::UpdatePopups() { | ||
std::unordered_map<std::string, PopupState> popups; | ||
|
||
// update popup date | ||
{ | ||
std::lock_guard<std::mutex> lock(popup_mtx_); | ||
popups = popups_; | ||
for (auto& [title, state] : popups_) { | ||
if (state.triggered) { | ||
ImGui::OpenPopup(title.c_str()); | ||
state.triggered = false; | ||
} | ||
} | ||
} | ||
|
||
// show popups if triggered | ||
for (auto& [title, state] : popups) { | ||
switch (state.descriptor.type) { | ||
case PopupType::kNotification: | ||
ShowNotificationPopup(state.descriptor.title, state.descriptor.msg, | ||
state.descriptor.width, state.descriptor.height); | ||
break; | ||
case PopupType::kConfirmation: | ||
ShowConfirmationPopup(state.descriptor.title, state.descriptor.msg, | ||
state.descriptor.callback, state.descriptor.width, | ||
state.descriptor.height); | ||
break; | ||
} | ||
} | ||
} | ||
} // namespace quickviz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* test_viewer.cpp | ||
* | ||
* Created on: Jul 27, 2021 09:07 | ||
* Description: | ||
* | ||
* Copyright (c) 2021 Ruixiang Du (rdu) | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
#include "imview/viewer.hpp" | ||
#include "imview/box.hpp" | ||
|
||
#include "imview/panel.hpp" | ||
#include "imview/popup.hpp" | ||
|
||
#include "imview/component/popup_manager.hpp" | ||
|
||
using namespace quickviz; | ||
|
||
class MyPanel : public Panel { | ||
public: | ||
MyPanel(std::string name = "MyPanel") : Panel(name) { | ||
this->SetAutoLayout(false); | ||
|
||
PopupManager::GetInstance().RegisterNotificationPopup( | ||
"Notification", "This is a popup test"); | ||
PopupManager::GetInstance().RegisterConfirmationPopup( | ||
"Confirmation", "This is a confirmation test", [](bool confirm) { | ||
if (confirm) { | ||
std::cout << "Confirmed" << std::endl; | ||
} else { | ||
std::cout << "Not Confirmed" << std::endl; | ||
} | ||
}); | ||
} | ||
|
||
void Draw() override { | ||
Begin(); | ||
{ | ||
static bool show_popup = false; | ||
if (ImGui::Button("Show Notification")) { | ||
PopupManager::GetInstance().TriggerPopup("Notification"); | ||
} | ||
if (ImGui::Button("Show Confirmation")) { | ||
PopupManager::GetInstance().TriggerPopup("Confirmation"); | ||
} | ||
|
||
PopupManager::GetInstance().UpdatePopups(); | ||
|
||
ImGui::PushFont(Fonts::GetFont(FontSize::kFont18)); | ||
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0, 153, 153, 200)); | ||
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", | ||
1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); | ||
ImGui::PopStyleColor(); | ||
ImGui::PopFont(); | ||
} | ||
End(); | ||
} | ||
}; | ||
|
||
int main(int argc, char* argv[]) { | ||
Viewer viewer; | ||
|
||
auto obj1 = std::make_shared<MyPanel>("Panel1"); | ||
viewer.AddSceneObject(obj1); | ||
|
||
viewer.Show(); | ||
return 0; | ||
} |