refactor(editor): integrate EditorCardRegistry for card management
- Introduced EditorCardRegistry class to centralize card registration and management, enhancing session awareness and visibility control. - Refactored EditorManager to delegate card-related operations to EditorCardRegistry, improving separation of concerns and maintainability. - Updated CMake configuration to include new source files for the EditorCardRegistry component. Benefits: - Streamlines card management within the editor, leading to a more organized and efficient user experience. - Enhances the overall architecture by clearly defining roles for card handling and editor operations.
This commit is contained in:
888
src/app/editor/system/editor_card_registry.cc
Normal file
888
src/app/editor/system/editor_card_registry.cc
Normal file
@@ -0,0 +1,888 @@
|
||||
#include "app/editor/system/editor_card_registry.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "app/gui/core/icons.h"
|
||||
#include "app/gui/core/theme_manager.h"
|
||||
#include "imgui/imgui.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace editor {
|
||||
|
||||
// ============================================================================
|
||||
// Session Lifecycle Management
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::RegisterSession(size_t session_id) {
|
||||
if (session_cards_.find(session_id) == session_cards_.end()) {
|
||||
session_cards_[session_id] = std::vector<std::string>();
|
||||
session_card_mapping_[session_id] = std::unordered_map<std::string, std::string>();
|
||||
UpdateSessionCount();
|
||||
printf("[EditorCardRegistry] Registered session %zu (total: %zu)\n",
|
||||
session_id, session_count_);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::UnregisterSession(size_t session_id) {
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
UnregisterSessionCards(session_id);
|
||||
session_cards_.erase(it);
|
||||
session_card_mapping_.erase(session_id);
|
||||
UpdateSessionCount();
|
||||
|
||||
// Reset active session if it was the one being removed
|
||||
if (active_session_ == session_id) {
|
||||
active_session_ = 0;
|
||||
if (!session_cards_.empty()) {
|
||||
active_session_ = session_cards_.begin()->first;
|
||||
}
|
||||
}
|
||||
|
||||
printf("[EditorCardRegistry] Unregistered session %zu (total: %zu)\n",
|
||||
session_id, session_count_);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::SetActiveSession(size_t session_id) {
|
||||
if (session_cards_.find(session_id) != session_cards_.end()) {
|
||||
active_session_ = session_id;
|
||||
printf("[EditorCardRegistry] Set active session to %zu\n", session_id);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Card Registration
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::RegisterCard(size_t session_id, const CardInfo& base_info) {
|
||||
RegisterSession(session_id); // Ensure session exists
|
||||
|
||||
std::string prefixed_id = MakeCardId(session_id, base_info.card_id);
|
||||
|
||||
// Check if already registered to avoid duplicates
|
||||
if (cards_.find(prefixed_id) != cards_.end()) {
|
||||
printf("[EditorCardRegistry] WARNING: Card '%s' already registered, skipping duplicate\n",
|
||||
prefixed_id.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
// Create new CardInfo with prefixed ID
|
||||
CardInfo prefixed_info = base_info;
|
||||
prefixed_info.card_id = prefixed_id;
|
||||
|
||||
// If no visibility_flag provided, create centralized one
|
||||
if (!prefixed_info.visibility_flag) {
|
||||
centralized_visibility_[prefixed_id] = false; // Hidden by default
|
||||
prefixed_info.visibility_flag = ¢ralized_visibility_[prefixed_id];
|
||||
}
|
||||
|
||||
// Register the card
|
||||
cards_[prefixed_id] = prefixed_info;
|
||||
|
||||
// Track in our session mapping
|
||||
session_cards_[session_id].push_back(prefixed_id);
|
||||
session_card_mapping_[session_id][base_info.card_id] = prefixed_id;
|
||||
|
||||
printf("[EditorCardRegistry] Registered card %s -> %s for session %zu\n",
|
||||
base_info.card_id.c_str(), prefixed_id.c_str(), session_id);
|
||||
}
|
||||
|
||||
void EditorCardRegistry::RegisterCard(size_t session_id,
|
||||
const std::string& card_id,
|
||||
const std::string& display_name,
|
||||
const std::string& icon,
|
||||
const std::string& category,
|
||||
const std::string& shortcut_hint,
|
||||
int priority,
|
||||
std::function<void()> on_show,
|
||||
std::function<void()> on_hide,
|
||||
bool visible_by_default) {
|
||||
CardInfo info;
|
||||
info.card_id = card_id;
|
||||
info.display_name = display_name;
|
||||
info.icon = icon;
|
||||
info.category = category;
|
||||
info.shortcut_hint = shortcut_hint;
|
||||
info.priority = priority;
|
||||
info.visibility_flag = nullptr; // Will be created in RegisterCard
|
||||
info.on_show = on_show;
|
||||
info.on_hide = on_hide;
|
||||
|
||||
RegisterCard(session_id, info);
|
||||
|
||||
// Set initial visibility if requested
|
||||
if (visible_by_default) {
|
||||
ShowCard(session_id, card_id);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::UnregisterCard(size_t session_id, const std::string& base_card_id) {
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto it = cards_.find(prefixed_id);
|
||||
if (it != cards_.end()) {
|
||||
printf("[EditorCardRegistry] Unregistered card: %s\n", prefixed_id.c_str());
|
||||
cards_.erase(it);
|
||||
centralized_visibility_.erase(prefixed_id);
|
||||
|
||||
// Remove from session tracking
|
||||
auto& session_card_list = session_cards_[session_id];
|
||||
session_card_list.erase(
|
||||
std::remove(session_card_list.begin(), session_card_list.end(), prefixed_id),
|
||||
session_card_list.end());
|
||||
|
||||
session_card_mapping_[session_id].erase(base_card_id);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::UnregisterCardsWithPrefix(const std::string& prefix) {
|
||||
std::vector<std::string> to_remove;
|
||||
|
||||
// Find all cards with the given prefix
|
||||
for (const auto& [card_id, card_info] : cards_) {
|
||||
if (card_id.find(prefix) == 0) { // Starts with prefix
|
||||
to_remove.push_back(card_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove them
|
||||
for (const auto& card_id : to_remove) {
|
||||
cards_.erase(card_id);
|
||||
centralized_visibility_.erase(card_id);
|
||||
printf("[EditorCardRegistry] Unregistered card with prefix '%s': %s\n",
|
||||
prefix.c_str(), card_id.c_str());
|
||||
}
|
||||
|
||||
// Also clean up session tracking
|
||||
for (auto& [session_id, card_list] : session_cards_) {
|
||||
card_list.erase(
|
||||
std::remove_if(card_list.begin(), card_list.end(),
|
||||
[&prefix](const std::string& id) {
|
||||
return id.find(prefix) == 0;
|
||||
}),
|
||||
card_list.end());
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::ClearAllCards() {
|
||||
cards_.clear();
|
||||
centralized_visibility_.clear();
|
||||
session_cards_.clear();
|
||||
session_card_mapping_.clear();
|
||||
session_count_ = 0;
|
||||
printf("[EditorCardRegistry] Cleared all cards\n");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Card Control (Programmatic, No GUI)
|
||||
// ============================================================================
|
||||
|
||||
bool EditorCardRegistry::ShowCard(size_t session_id, const std::string& base_card_id) {
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = cards_.find(prefixed_id);
|
||||
if (it != cards_.end()) {
|
||||
if (it->second.visibility_flag) {
|
||||
*it->second.visibility_flag = true;
|
||||
}
|
||||
if (it->second.on_show) {
|
||||
it->second.on_show();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EditorCardRegistry::HideCard(size_t session_id, const std::string& base_card_id) {
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = cards_.find(prefixed_id);
|
||||
if (it != cards_.end()) {
|
||||
if (it->second.visibility_flag) {
|
||||
*it->second.visibility_flag = false;
|
||||
}
|
||||
if (it->second.on_hide) {
|
||||
it->second.on_hide();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EditorCardRegistry::ToggleCard(size_t session_id, const std::string& base_card_id) {
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = cards_.find(prefixed_id);
|
||||
if (it != cards_.end() && it->second.visibility_flag) {
|
||||
bool new_state = !(*it->second.visibility_flag);
|
||||
*it->second.visibility_flag = new_state;
|
||||
|
||||
if (new_state && it->second.on_show) {
|
||||
it->second.on_show();
|
||||
} else if (!new_state && it->second.on_hide) {
|
||||
it->second.on_hide();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EditorCardRegistry::IsCardVisible(size_t session_id, const std::string& base_card_id) const {
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = cards_.find(prefixed_id);
|
||||
if (it != cards_.end() && it->second.visibility_flag) {
|
||||
return *it->second.visibility_flag;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool* EditorCardRegistry::GetVisibilityFlag(size_t session_id, const std::string& base_card_id) {
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto it = cards_.find(prefixed_id);
|
||||
if (it != cards_.end()) {
|
||||
return it->second.visibility_flag;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Batch Operations
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::ShowAllCardsInSession(size_t session_id) {
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
auto card_it = cards_.find(prefixed_card_id);
|
||||
if (card_it != cards_.end() && card_it->second.visibility_flag) {
|
||||
*card_it->second.visibility_flag = true;
|
||||
if (card_it->second.on_show) {
|
||||
card_it->second.on_show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::HideAllCardsInSession(size_t session_id) {
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
auto card_it = cards_.find(prefixed_card_id);
|
||||
if (card_it != cards_.end() && card_it->second.visibility_flag) {
|
||||
*card_it->second.visibility_flag = false;
|
||||
if (card_it->second.on_hide) {
|
||||
card_it->second.on_hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::ShowAllCardsInCategory(size_t session_id, const std::string& category) {
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
auto card_it = cards_.find(prefixed_card_id);
|
||||
if (card_it != cards_.end() && card_it->second.category == category) {
|
||||
if (card_it->second.visibility_flag) {
|
||||
*card_it->second.visibility_flag = true;
|
||||
}
|
||||
if (card_it->second.on_show) {
|
||||
card_it->second.on_show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::HideAllCardsInCategory(size_t session_id, const std::string& category) {
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
auto card_it = cards_.find(prefixed_card_id);
|
||||
if (card_it != cards_.end() && card_it->second.category == category) {
|
||||
if (card_it->second.visibility_flag) {
|
||||
*card_it->second.visibility_flag = false;
|
||||
}
|
||||
if (card_it->second.on_hide) {
|
||||
card_it->second.on_hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::ShowOnlyCard(size_t session_id, const std::string& base_card_id) {
|
||||
// First get the category of the target card
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto target_it = cards_.find(prefixed_id);
|
||||
if (target_it == cards_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string category = target_it->second.category;
|
||||
|
||||
// Hide all cards in the same category
|
||||
HideAllCardsInCategory(session_id, category);
|
||||
|
||||
// Show the target card
|
||||
ShowCard(session_id, base_card_id);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Query Methods
|
||||
// ============================================================================
|
||||
|
||||
std::vector<std::string> EditorCardRegistry::GetCardsInSession(size_t session_id) const {
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<CardInfo> EditorCardRegistry::GetCardsInCategory(size_t session_id,
|
||||
const std::string& category) const {
|
||||
std::vector<CardInfo> result;
|
||||
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
auto card_it = cards_.find(prefixed_card_id);
|
||||
if (card_it != cards_.end() && card_it->second.category == category) {
|
||||
result.push_back(card_it->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by priority
|
||||
std::sort(result.begin(), result.end(),
|
||||
[](const CardInfo& a, const CardInfo& b) {
|
||||
return a.priority < b.priority;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> EditorCardRegistry::GetAllCategories(size_t session_id) const {
|
||||
std::vector<std::string> categories;
|
||||
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
auto card_it = cards_.find(prefixed_card_id);
|
||||
if (card_it != cards_.end()) {
|
||||
if (std::find(categories.begin(), categories.end(),
|
||||
card_it->second.category) == categories.end()) {
|
||||
categories.push_back(card_it->second.category);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return categories;
|
||||
}
|
||||
|
||||
const CardInfo* EditorCardRegistry::GetCardInfo(size_t session_id,
|
||||
const std::string& base_card_id) const {
|
||||
std::string prefixed_id = GetPrefixedCardId(session_id, base_card_id);
|
||||
if (prefixed_id.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto it = cards_.find(prefixed_id);
|
||||
if (it != cards_.end()) {
|
||||
return &it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> EditorCardRegistry::GetAllCategories() const {
|
||||
std::vector<std::string> categories;
|
||||
for (const auto& [card_id, card_info] : cards_) {
|
||||
if (std::find(categories.begin(), categories.end(),
|
||||
card_info.category) == categories.end()) {
|
||||
categories.push_back(card_info.category);
|
||||
}
|
||||
}
|
||||
return categories;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// View Menu Integration
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::DrawViewMenuSection(size_t session_id, const std::string& category) {
|
||||
auto cards = GetCardsInCategory(session_id, category);
|
||||
|
||||
if (cards.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu(category.c_str())) {
|
||||
for (const auto& card : cards) {
|
||||
DrawCardMenuItem(card);
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::DrawViewMenuAll(size_t session_id) {
|
||||
auto categories = GetAllCategories(session_id);
|
||||
|
||||
for (const auto& category : categories) {
|
||||
DrawViewMenuSection(session_id, category);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// VSCode-Style Sidebar
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::DrawSidebar(size_t session_id,
|
||||
const std::string& category,
|
||||
const std::vector<std::string>& active_categories,
|
||||
std::function<void(const std::string&)> on_category_switch,
|
||||
std::function<void()> on_collapse) {
|
||||
// Get cards for this session and category
|
||||
auto cards = GetCardsInCategory(session_id, category);
|
||||
|
||||
if (cards.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Sidebar window
|
||||
ImGui::SetNextWindowSize(ImVec2(GetSidebarWidth() + 220, 0), ImGuiCond_Always);
|
||||
ImGui::SetNextWindowPos(ImVec2(0, ImGui::GetFrameHeightWithSpacing()), ImGuiCond_Always);
|
||||
|
||||
ImGui::Begin("##CardSidebar", nullptr,
|
||||
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse);
|
||||
|
||||
// Draw category tabs on the left
|
||||
ImGui::BeginChild("CategoryTabs", ImVec2(GetSidebarWidth(), 0), true);
|
||||
|
||||
for (const auto& cat : active_categories) {
|
||||
bool is_active = (cat == category);
|
||||
if (is_active) {
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, gui::GetPrimaryVec4());
|
||||
}
|
||||
|
||||
if (ImGui::Button(cat.substr(0, 1).c_str(), ImVec2(GetSidebarWidth() - 8, 40))) {
|
||||
if (on_category_switch) {
|
||||
on_category_switch(cat);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_active) {
|
||||
ImGui::PopStyleColor();
|
||||
}
|
||||
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::SetTooltip("%s", cat.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndChild();
|
||||
|
||||
ImGui::SameLine();
|
||||
|
||||
// Draw cards on the right
|
||||
ImGui::BeginChild("Cards", ImVec2(0, 0), true);
|
||||
|
||||
ImGui::Text("%s %s", ICON_MD_DASHBOARD, category.c_str());
|
||||
ImGui::Separator();
|
||||
|
||||
for (const auto& card : cards) {
|
||||
DrawCardInSidebar(card, IsCardVisible(session_id, card.card_id));
|
||||
}
|
||||
|
||||
ImGui::EndChild();
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Compact Controls for Menu Bar
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::DrawCompactCardControl(size_t session_id, const std::string& category) {
|
||||
auto cards = GetCardsInCategory(session_id, category);
|
||||
|
||||
if (cards.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Compact dropdown
|
||||
if (ImGui::BeginCombo("##CardControl", absl::StrFormat("%s Cards", ICON_MD_DASHBOARD).c_str())) {
|
||||
for (const auto& card : cards) {
|
||||
bool visible = card.visibility_flag ? *card.visibility_flag : false;
|
||||
if (ImGui::MenuItem(absl::StrFormat("%s %s", card.icon.c_str(),
|
||||
card.display_name.c_str()).c_str(),
|
||||
nullptr, visible)) {
|
||||
ToggleCard(session_id, card.card_id);
|
||||
}
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::DrawInlineCardToggles(size_t session_id, const std::string& category) {
|
||||
auto cards = GetCardsInCategory(session_id, category);
|
||||
|
||||
size_t visible_count = 0;
|
||||
for (const auto& card : cards) {
|
||||
if (card.visibility_flag && *card.visibility_flag) {
|
||||
visible_count++;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Text("(%zu/%zu)", visible_count, cards.size());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Card Browser UI
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::DrawCardBrowser(size_t session_id, bool* p_open) {
|
||||
ImGui::SetNextWindowSize(ImVec2(800, 600), ImGuiCond_FirstUseEver);
|
||||
|
||||
if (ImGui::Begin(absl::StrFormat("%s Card Browser", ICON_MD_DASHBOARD).c_str(),
|
||||
p_open)) {
|
||||
|
||||
static char search_filter[256] = "";
|
||||
static std::string category_filter = "All";
|
||||
|
||||
// Search bar
|
||||
ImGui::SetNextItemWidth(300);
|
||||
ImGui::InputTextWithHint("##Search", absl::StrFormat("%s Search cards...",
|
||||
ICON_MD_SEARCH).c_str(),
|
||||
search_filter, sizeof(search_filter));
|
||||
|
||||
ImGui::SameLine();
|
||||
|
||||
// Category filter
|
||||
if (ImGui::BeginCombo("##CategoryFilter", category_filter.c_str())) {
|
||||
if (ImGui::Selectable("All", category_filter == "All")) {
|
||||
category_filter = "All";
|
||||
}
|
||||
|
||||
auto categories = GetAllCategories(session_id);
|
||||
for (const auto& cat : categories) {
|
||||
if (ImGui::Selectable(cat.c_str(), category_filter == cat)) {
|
||||
category_filter = cat;
|
||||
}
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
// Card table
|
||||
if (ImGui::BeginTable("##CardTable", 4,
|
||||
ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg |
|
||||
ImGuiTableFlags_Borders)) {
|
||||
|
||||
ImGui::TableSetupColumn("Visible", ImGuiTableColumnFlags_WidthFixed, 60);
|
||||
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch);
|
||||
ImGui::TableSetupColumn("Category", ImGuiTableColumnFlags_WidthFixed, 120);
|
||||
ImGui::TableSetupColumn("Shortcut", ImGuiTableColumnFlags_WidthFixed, 100);
|
||||
ImGui::TableHeadersRow();
|
||||
|
||||
auto cards = (category_filter == "All")
|
||||
? GetCardsInSession(session_id)
|
||||
: std::vector<std::string>{};
|
||||
|
||||
if (category_filter != "All") {
|
||||
auto cat_cards = GetCardsInCategory(session_id, category_filter);
|
||||
for (const auto& card : cat_cards) {
|
||||
cards.push_back(card.card_id);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& card_id : cards) {
|
||||
auto card_it = cards_.find(card_id);
|
||||
if (card_it == cards_.end()) continue;
|
||||
|
||||
const auto& card = card_it->second;
|
||||
|
||||
// Apply search filter
|
||||
std::string search_str = search_filter;
|
||||
if (!search_str.empty()) {
|
||||
std::string card_lower = card.display_name;
|
||||
std::transform(card_lower.begin(), card_lower.end(), card_lower.begin(), ::tolower);
|
||||
std::transform(search_str.begin(), search_str.end(), search_str.begin(), ::tolower);
|
||||
if (card_lower.find(search_str) == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::TableNextRow();
|
||||
|
||||
// Visibility toggle
|
||||
ImGui::TableNextColumn();
|
||||
if (card.visibility_flag) {
|
||||
bool visible = *card.visibility_flag;
|
||||
if (ImGui::Checkbox(absl::StrFormat("##vis_%s", card.card_id.c_str()).c_str(),
|
||||
&visible)) {
|
||||
*card.visibility_flag = visible;
|
||||
if (visible && card.on_show) {
|
||||
card.on_show();
|
||||
} else if (!visible && card.on_hide) {
|
||||
card.on_hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Name with icon
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s %s", card.icon.c_str(), card.display_name.c_str());
|
||||
|
||||
// Category
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s", card.category.c_str());
|
||||
|
||||
// Shortcut
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextDisabled("%s", card.shortcut_hint.c_str());
|
||||
}
|
||||
|
||||
ImGui::EndTable();
|
||||
}
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Workspace Presets
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::SavePreset(const std::string& name, const std::string& description) {
|
||||
WorkspacePreset preset;
|
||||
preset.name = name;
|
||||
preset.description = description;
|
||||
|
||||
// Collect all visible cards across all sessions
|
||||
for (const auto& [card_id, card_info] : cards_) {
|
||||
if (card_info.visibility_flag && *card_info.visibility_flag) {
|
||||
preset.visible_cards.push_back(card_id);
|
||||
}
|
||||
}
|
||||
|
||||
presets_[name] = preset;
|
||||
SavePresetsToFile();
|
||||
printf("[EditorCardRegistry] Saved preset: %s (%zu cards)\n",
|
||||
name.c_str(), preset.visible_cards.size());
|
||||
}
|
||||
|
||||
bool EditorCardRegistry::LoadPreset(const std::string& name) {
|
||||
auto it = presets_.find(name);
|
||||
if (it == presets_.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// First hide all cards
|
||||
for (auto& [card_id, card_info] : cards_) {
|
||||
if (card_info.visibility_flag) {
|
||||
*card_info.visibility_flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Then show preset cards
|
||||
for (const auto& card_id : it->second.visible_cards) {
|
||||
auto card_it = cards_.find(card_id);
|
||||
if (card_it != cards_.end() && card_it->second.visibility_flag) {
|
||||
*card_it->second.visibility_flag = true;
|
||||
if (card_it->second.on_show) {
|
||||
card_it->second.on_show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("[EditorCardRegistry] Loaded preset: %s\n", name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void EditorCardRegistry::DeletePreset(const std::string& name) {
|
||||
presets_.erase(name);
|
||||
SavePresetsToFile();
|
||||
}
|
||||
|
||||
std::vector<EditorCardRegistry::WorkspacePreset> EditorCardRegistry::GetPresets() const {
|
||||
std::vector<WorkspacePreset> result;
|
||||
for (const auto& [name, preset] : presets_) {
|
||||
result.push_back(preset);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Quick Actions
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::ShowAll(size_t session_id) {
|
||||
ShowAllCardsInSession(session_id);
|
||||
}
|
||||
|
||||
void EditorCardRegistry::HideAll(size_t session_id) {
|
||||
HideAllCardsInSession(session_id);
|
||||
}
|
||||
|
||||
void EditorCardRegistry::ResetToDefaults(size_t session_id) {
|
||||
// Hide all cards first
|
||||
HideAllCardsInSession(session_id);
|
||||
|
||||
// TODO: Load default visibility from config file or hardcoded defaults
|
||||
printf("[EditorCardRegistry] Reset to defaults for session %zu\n", session_id);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Statistics
|
||||
// ============================================================================
|
||||
|
||||
size_t EditorCardRegistry::GetVisibleCardCount(size_t session_id) const {
|
||||
size_t count = 0;
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
auto card_it = cards_.find(prefixed_card_id);
|
||||
if (card_it != cards_.end() && card_it->second.visibility_flag) {
|
||||
if (*card_it->second.visibility_flag) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Session Prefixing Utilities
|
||||
// ============================================================================
|
||||
|
||||
std::string EditorCardRegistry::MakeCardId(size_t session_id, const std::string& base_id) const {
|
||||
if (ShouldPrefixCards()) {
|
||||
return absl::StrFormat("s%zu.%s", session_id, base_id);
|
||||
}
|
||||
return base_id;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Helper Methods (Private)
|
||||
// ============================================================================
|
||||
|
||||
void EditorCardRegistry::UpdateSessionCount() {
|
||||
session_count_ = session_cards_.size();
|
||||
}
|
||||
|
||||
std::string EditorCardRegistry::GetPrefixedCardId(size_t session_id,
|
||||
const std::string& base_id) const {
|
||||
auto session_it = session_card_mapping_.find(session_id);
|
||||
if (session_it != session_card_mapping_.end()) {
|
||||
auto card_it = session_it->second.find(base_id);
|
||||
if (card_it != session_it->second.end()) {
|
||||
return card_it->second;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: try unprefixed ID (for single session or direct access)
|
||||
if (cards_.find(base_id) != cards_.end()) {
|
||||
return base_id;
|
||||
}
|
||||
|
||||
return ""; // Card not found
|
||||
}
|
||||
|
||||
void EditorCardRegistry::UnregisterSessionCards(size_t session_id) {
|
||||
auto it = session_cards_.find(session_id);
|
||||
if (it != session_cards_.end()) {
|
||||
for (const auto& prefixed_card_id : it->second) {
|
||||
cards_.erase(prefixed_card_id);
|
||||
centralized_visibility_.erase(prefixed_card_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::SavePresetsToFile() {
|
||||
// TODO: Implement file I/O for presets
|
||||
printf("[EditorCardRegistry] SavePresetsToFile() - not yet implemented\n");
|
||||
}
|
||||
|
||||
void EditorCardRegistry::LoadPresetsFromFile() {
|
||||
// TODO: Implement file I/O for presets
|
||||
printf("[EditorCardRegistry] LoadPresetsFromFile() - not yet implemented\n");
|
||||
}
|
||||
|
||||
void EditorCardRegistry::DrawCardMenuItem(const CardInfo& info) {
|
||||
bool visible = info.visibility_flag ? *info.visibility_flag : false;
|
||||
|
||||
std::string label = absl::StrFormat("%s %s", info.icon.c_str(),
|
||||
info.display_name.c_str());
|
||||
|
||||
const char* shortcut = info.shortcut_hint.empty() ? nullptr : info.shortcut_hint.c_str();
|
||||
|
||||
if (ImGui::MenuItem(label.c_str(), shortcut, visible)) {
|
||||
if (info.visibility_flag) {
|
||||
*info.visibility_flag = !visible;
|
||||
if (*info.visibility_flag && info.on_show) {
|
||||
info.on_show();
|
||||
} else if (!*info.visibility_flag && info.on_hide) {
|
||||
info.on_hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorCardRegistry::DrawCardInSidebar(const CardInfo& info, bool is_active) {
|
||||
if (is_active) {
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, gui::GetPrimaryVec4());
|
||||
}
|
||||
|
||||
if (ImGui::Button(absl::StrFormat("%s %s", info.icon.c_str(),
|
||||
info.display_name.c_str()).c_str(),
|
||||
ImVec2(-1, 0))) {
|
||||
if (info.visibility_flag) {
|
||||
*info.visibility_flag = !*info.visibility_flag;
|
||||
if (*info.visibility_flag && info.on_show) {
|
||||
info.on_show();
|
||||
} else if (!*info.visibility_flag && info.on_hide) {
|
||||
info.on_hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_active) {
|
||||
ImGui::PopStyleColor();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace editor
|
||||
} // namespace yaze
|
||||
|
||||
406
src/app/editor/system/editor_card_registry.h
Normal file
406
src/app/editor/system/editor_card_registry.h
Normal file
@@ -0,0 +1,406 @@
|
||||
#ifndef YAZE_APP_EDITOR_SYSTEM_EDITOR_CARD_REGISTRY_H_
|
||||
#define YAZE_APP_EDITOR_SYSTEM_EDITOR_CARD_REGISTRY_H_
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "imgui/imgui.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace editor {
|
||||
|
||||
// Forward declaration
|
||||
class EditorCard;
|
||||
|
||||
/**
|
||||
* @struct CardInfo
|
||||
* @brief Metadata for an editor card
|
||||
*
|
||||
* Describes a registerable UI card that can be shown/hidden,
|
||||
* organized by category, and controlled programmatically.
|
||||
*/
|
||||
struct CardInfo {
|
||||
std::string card_id; // Unique identifier (e.g., "dungeon.room_selector")
|
||||
std::string display_name; // Human-readable name (e.g., "Room Selector")
|
||||
std::string icon; // Material icon
|
||||
std::string category; // Category (e.g., "Dungeon", "Graphics", "Palette")
|
||||
std::string shortcut_hint; // Display hint (e.g., "Ctrl+Shift+R")
|
||||
bool* visibility_flag; // Pointer to bool controlling visibility
|
||||
EditorCard* card_instance; // Pointer to actual card (optional)
|
||||
std::function<void()> on_show; // Callback when card is shown
|
||||
std::function<void()> on_hide; // Callback when card is hidden
|
||||
int priority; // Display priority for menus (lower = higher)
|
||||
};
|
||||
|
||||
/**
|
||||
* @class EditorCardRegistry
|
||||
* @brief Central registry for all editor cards with session awareness and dependency injection
|
||||
*
|
||||
* This class combines the functionality of EditorCardManager (global card management)
|
||||
* and SessionCardRegistry (session-aware prefixing) into a single, dependency-injected
|
||||
* component that can be passed to editors.
|
||||
*
|
||||
* Design Philosophy:
|
||||
* - Dependency injection (no singleton pattern)
|
||||
* - Session-aware card ID prefixing for multi-session support
|
||||
* - Centralized visibility management
|
||||
* - View menu integration
|
||||
* - Workspace preset system
|
||||
* - No direct GUI dependency in registration logic
|
||||
*
|
||||
* Session-Aware Card IDs:
|
||||
* - Single session: "dungeon.room_selector"
|
||||
* - Multiple sessions: "s0.dungeon.room_selector", "s1.dungeon.room_selector"
|
||||
*
|
||||
* Usage:
|
||||
* ```cpp
|
||||
* // In EditorManager:
|
||||
* EditorCardRegistry card_registry;
|
||||
* EditorDependencies deps;
|
||||
* deps.card_registry = &card_registry;
|
||||
*
|
||||
* // In Editor:
|
||||
* deps.card_registry->RegisterCard(deps.session_id, {
|
||||
* .card_id = "dungeon.room_selector",
|
||||
* .display_name = "Room Selector",
|
||||
* .icon = ICON_MD_LIST,
|
||||
* .category = "Dungeon",
|
||||
* .on_show = []() { }
|
||||
* });
|
||||
*
|
||||
* // Programmatic control:
|
||||
* deps.card_registry->ShowCard(deps.session_id, "dungeon.room_selector");
|
||||
* ```
|
||||
*/
|
||||
class EditorCardRegistry {
|
||||
public:
|
||||
EditorCardRegistry() = default;
|
||||
~EditorCardRegistry() = default;
|
||||
|
||||
// Non-copyable, non-movable (manages pointers and callbacks)
|
||||
EditorCardRegistry(const EditorCardRegistry&) = delete;
|
||||
EditorCardRegistry& operator=(const EditorCardRegistry&) = delete;
|
||||
EditorCardRegistry(EditorCardRegistry&&) = delete;
|
||||
EditorCardRegistry& operator=(EditorCardRegistry&&) = delete;
|
||||
|
||||
// ============================================================================
|
||||
// Session Lifecycle Management
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Register a new session in the registry
|
||||
* @param session_id Unique session identifier
|
||||
*
|
||||
* Creates internal tracking structures for the session.
|
||||
* Must be called before registering cards for a session.
|
||||
*/
|
||||
void RegisterSession(size_t session_id);
|
||||
|
||||
/**
|
||||
* @brief Unregister a session and all its cards
|
||||
* @param session_id Session identifier to remove
|
||||
*
|
||||
* Automatically unregisters all cards associated with the session.
|
||||
*/
|
||||
void UnregisterSession(size_t session_id);
|
||||
|
||||
/**
|
||||
* @brief Set the currently active session
|
||||
* @param session_id Session to make active
|
||||
*
|
||||
* Used for determining whether to apply card ID prefixing.
|
||||
*/
|
||||
void SetActiveSession(size_t session_id);
|
||||
|
||||
// ============================================================================
|
||||
// Card Registration
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Register a card for a specific session
|
||||
* @param session_id Session this card belongs to
|
||||
* @param base_info Card metadata (ID will be automatically prefixed if needed)
|
||||
*
|
||||
* The card_id in base_info should be the unprefixed ID. This method
|
||||
* automatically applies session prefixing when multiple sessions exist.
|
||||
*/
|
||||
void RegisterCard(size_t session_id, const CardInfo& base_info);
|
||||
|
||||
/**
|
||||
* @brief Register a card with inline parameters (convenience method)
|
||||
*/
|
||||
void RegisterCard(size_t session_id,
|
||||
const std::string& card_id,
|
||||
const std::string& display_name,
|
||||
const std::string& icon,
|
||||
const std::string& category,
|
||||
const std::string& shortcut_hint = "",
|
||||
int priority = 50,
|
||||
std::function<void()> on_show = nullptr,
|
||||
std::function<void()> on_hide = nullptr,
|
||||
bool visible_by_default = false);
|
||||
|
||||
/**
|
||||
* @brief Unregister a specific card
|
||||
* @param session_id Session the card belongs to
|
||||
* @param base_card_id Unprefixed card ID
|
||||
*/
|
||||
void UnregisterCard(size_t session_id, const std::string& base_card_id);
|
||||
|
||||
/**
|
||||
* @brief Unregister all cards with a given prefix
|
||||
* @param prefix Prefix to match (e.g., "s0" or "s1.dungeon")
|
||||
*
|
||||
* Useful for cleaning up session cards or category cards.
|
||||
*/
|
||||
void UnregisterCardsWithPrefix(const std::string& prefix);
|
||||
|
||||
/**
|
||||
* @brief Remove all registered cards (use with caution)
|
||||
*/
|
||||
void ClearAllCards();
|
||||
|
||||
// ============================================================================
|
||||
// Card Control (Programmatic, No GUI)
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Show a card programmatically
|
||||
* @param session_id Session the card belongs to
|
||||
* @param base_card_id Unprefixed card ID
|
||||
* @return true if card was found and shown
|
||||
*/
|
||||
bool ShowCard(size_t session_id, const std::string& base_card_id);
|
||||
|
||||
/**
|
||||
* @brief Hide a card programmatically
|
||||
*/
|
||||
bool HideCard(size_t session_id, const std::string& base_card_id);
|
||||
|
||||
/**
|
||||
* @brief Toggle a card's visibility
|
||||
*/
|
||||
bool ToggleCard(size_t session_id, const std::string& base_card_id);
|
||||
|
||||
/**
|
||||
* @brief Check if a card is currently visible
|
||||
*/
|
||||
bool IsCardVisible(size_t session_id, const std::string& base_card_id) const;
|
||||
|
||||
/**
|
||||
* @brief Get visibility flag pointer for a card
|
||||
* @return Pointer to bool controlling card visibility (for passing to EditorCard::Begin)
|
||||
*/
|
||||
bool* GetVisibilityFlag(size_t session_id, const std::string& base_card_id);
|
||||
|
||||
// ============================================================================
|
||||
// Batch Operations
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Show all cards in a specific session
|
||||
*/
|
||||
void ShowAllCardsInSession(size_t session_id);
|
||||
|
||||
/**
|
||||
* @brief Hide all cards in a specific session
|
||||
*/
|
||||
void HideAllCardsInSession(size_t session_id);
|
||||
|
||||
/**
|
||||
* @brief Show all cards in a category for a session
|
||||
*/
|
||||
void ShowAllCardsInCategory(size_t session_id, const std::string& category);
|
||||
|
||||
/**
|
||||
* @brief Hide all cards in a category for a session
|
||||
*/
|
||||
void HideAllCardsInCategory(size_t session_id, const std::string& category);
|
||||
|
||||
/**
|
||||
* @brief Show only one card, hiding all others in its category
|
||||
*/
|
||||
void ShowOnlyCard(size_t session_id, const std::string& base_card_id);
|
||||
|
||||
// ============================================================================
|
||||
// Query Methods
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Get all cards registered for a session
|
||||
* @return Vector of prefixed card IDs
|
||||
*/
|
||||
std::vector<std::string> GetCardsInSession(size_t session_id) const;
|
||||
|
||||
/**
|
||||
* @brief Get cards in a specific category for a session
|
||||
*/
|
||||
std::vector<CardInfo> GetCardsInCategory(size_t session_id, const std::string& category) const;
|
||||
|
||||
/**
|
||||
* @brief Get all categories for a session
|
||||
*/
|
||||
std::vector<std::string> GetAllCategories(size_t session_id) const;
|
||||
|
||||
/**
|
||||
* @brief Get card metadata
|
||||
* @param session_id Session the card belongs to
|
||||
* @param base_card_id Unprefixed card ID
|
||||
*/
|
||||
const CardInfo* GetCardInfo(size_t session_id, const std::string& base_card_id) const;
|
||||
|
||||
/**
|
||||
* @brief Get all registered categories across all sessions
|
||||
*/
|
||||
std::vector<std::string> GetAllCategories() const;
|
||||
|
||||
// ============================================================================
|
||||
// View Menu Integration
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Draw view menu section for a category
|
||||
*/
|
||||
void DrawViewMenuSection(size_t session_id, const std::string& category);
|
||||
|
||||
/**
|
||||
* @brief Draw all categories as view menu submenus
|
||||
*/
|
||||
void DrawViewMenuAll(size_t session_id);
|
||||
|
||||
// ============================================================================
|
||||
// VSCode-Style Sidebar
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Draw sidebar for a category with session filtering
|
||||
*/
|
||||
void DrawSidebar(size_t session_id,
|
||||
const std::string& category,
|
||||
const std::vector<std::string>& active_categories = {},
|
||||
std::function<void(const std::string&)> on_category_switch = nullptr,
|
||||
std::function<void()> on_collapse = nullptr);
|
||||
|
||||
static constexpr float GetSidebarWidth() { return 48.0f; }
|
||||
|
||||
// ============================================================================
|
||||
// Compact Controls for Menu Bar
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Draw compact card control for active editor's cards
|
||||
*/
|
||||
void DrawCompactCardControl(size_t session_id, const std::string& category);
|
||||
|
||||
/**
|
||||
* @brief Draw minimal inline card toggles
|
||||
*/
|
||||
void DrawInlineCardToggles(size_t session_id, const std::string& category);
|
||||
|
||||
// ============================================================================
|
||||
// Card Browser UI
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Draw visual card browser/toggler
|
||||
*/
|
||||
void DrawCardBrowser(size_t session_id, bool* p_open);
|
||||
|
||||
// ============================================================================
|
||||
// Workspace Presets
|
||||
// ============================================================================
|
||||
|
||||
struct WorkspacePreset {
|
||||
std::string name;
|
||||
std::vector<std::string> visible_cards; // Card IDs
|
||||
std::string description;
|
||||
};
|
||||
|
||||
void SavePreset(const std::string& name, const std::string& description = "");
|
||||
bool LoadPreset(const std::string& name);
|
||||
void DeletePreset(const std::string& name);
|
||||
std::vector<WorkspacePreset> GetPresets() const;
|
||||
|
||||
// ============================================================================
|
||||
// Quick Actions
|
||||
// ============================================================================
|
||||
|
||||
void ShowAll(size_t session_id);
|
||||
void HideAll(size_t session_id);
|
||||
void ResetToDefaults(size_t session_id);
|
||||
|
||||
// ============================================================================
|
||||
// Statistics
|
||||
// ============================================================================
|
||||
|
||||
size_t GetCardCount() const { return cards_.size(); }
|
||||
size_t GetVisibleCardCount(size_t session_id) const;
|
||||
size_t GetSessionCount() const { return session_count_; }
|
||||
|
||||
// ============================================================================
|
||||
// Session Prefixing Utilities
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Generate session-aware card ID
|
||||
* @param session_id Session identifier
|
||||
* @param base_id Unprefixed card ID
|
||||
* @return Prefixed ID if multiple sessions, otherwise base ID
|
||||
*
|
||||
* Examples:
|
||||
* - Single session: "dungeon.room_selector" → "dungeon.room_selector"
|
||||
* - Multi-session: "dungeon.room_selector" → "s0.dungeon.room_selector"
|
||||
*/
|
||||
std::string MakeCardId(size_t session_id, const std::string& base_id) const;
|
||||
|
||||
/**
|
||||
* @brief Check if card IDs should be prefixed
|
||||
* @return true if session_count > 1
|
||||
*/
|
||||
bool ShouldPrefixCards() const { return session_count_ > 1; }
|
||||
|
||||
private:
|
||||
// Core card storage (prefixed IDs → CardInfo)
|
||||
std::unordered_map<std::string, CardInfo> cards_;
|
||||
|
||||
// Centralized visibility flags for cards without external flags
|
||||
std::unordered_map<std::string, bool> centralized_visibility_;
|
||||
|
||||
// Session tracking
|
||||
size_t session_count_ = 0;
|
||||
size_t active_session_ = 0;
|
||||
|
||||
// Maps session_id → vector of prefixed card IDs registered for that session
|
||||
std::unordered_map<size_t, std::vector<std::string>> session_cards_;
|
||||
|
||||
// Maps session_id → (base_card_id → prefixed_card_id)
|
||||
std::unordered_map<size_t, std::unordered_map<std::string, std::string>> session_card_mapping_;
|
||||
|
||||
// Workspace presets
|
||||
std::unordered_map<std::string, WorkspacePreset> presets_;
|
||||
|
||||
// Active category tracking
|
||||
std::string active_category_;
|
||||
std::vector<std::string> recent_categories_;
|
||||
static constexpr size_t kMaxRecentCategories = 5;
|
||||
|
||||
// Helper methods
|
||||
void UpdateSessionCount();
|
||||
std::string GetPrefixedCardId(size_t session_id, const std::string& base_id) const;
|
||||
void UnregisterSessionCards(size_t session_id);
|
||||
void SavePresetsToFile();
|
||||
void LoadPresetsFromFile();
|
||||
|
||||
// UI drawing helpers (internal)
|
||||
void DrawCardMenuItem(const CardInfo& info);
|
||||
void DrawCardInSidebar(const CardInfo& info, bool is_active);
|
||||
};
|
||||
|
||||
} // namespace editor
|
||||
} // namespace yaze
|
||||
|
||||
#endif // YAZE_APP_EDITOR_SYSTEM_EDITOR_CARD_REGISTRY_H_
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "app/editor/editor.h"
|
||||
#include "app/editor/editor_manager.h"
|
||||
#include "app/editor/system/editor_registry.h"
|
||||
#include "app/editor/system/project_manager.h"
|
||||
#include "app/editor/system/rom_file_manager.h"
|
||||
#include "app/editor/system/session_coordinator.h"
|
||||
#include "app/editor/system/toast_manager.h"
|
||||
#include "app/editor/ui/menu_builder.h"
|
||||
#include "app/editor/system/session_coordinator.h"
|
||||
#include "app/gui/core/icons.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace editor {
|
||||
|
||||
MenuOrchestrator::MenuOrchestrator(
|
||||
EditorManager* editor_manager,
|
||||
MenuBuilder& menu_builder,
|
||||
RomFileManager& rom_manager,
|
||||
ProjectManager& project_manager,
|
||||
EditorRegistry& editor_registry,
|
||||
SessionCoordinator& session_coordinator,
|
||||
ToastManager& toast_manager)
|
||||
: menu_builder_(menu_builder),
|
||||
: editor_manager_(editor_manager),
|
||||
menu_builder_(menu_builder),
|
||||
rom_manager_(rom_manager),
|
||||
project_manager_(project_manager),
|
||||
editor_registry_(editor_registry),
|
||||
@@ -39,6 +42,9 @@ void MenuOrchestrator::BuildMainMenu() {
|
||||
BuildWindowMenu();
|
||||
BuildHelpMenu();
|
||||
|
||||
// Draw the constructed menu
|
||||
menu_builder_.Draw();
|
||||
|
||||
menu_needs_refresh_ = false;
|
||||
}
|
||||
|
||||
@@ -312,20 +318,28 @@ void MenuOrchestrator::RefreshMenu() {
|
||||
|
||||
// Menu item callbacks - delegate to appropriate managers
|
||||
void MenuOrchestrator::OnOpenRom() {
|
||||
auto status = rom_manager_.LoadRom();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to load ROM: %s", status.message()),
|
||||
ToastType::kError);
|
||||
// Delegate to EditorManager's LoadRom which handles session management
|
||||
if (editor_manager_) {
|
||||
auto status = editor_manager_->LoadRom();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to load ROM: %s", status.message()),
|
||||
ToastType::kError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnSaveRom() {
|
||||
auto status = rom_manager_.SaveRom();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to save ROM: %s", status.message()),
|
||||
ToastType::kError);
|
||||
// Delegate to EditorManager's SaveRom which handles editor data saving
|
||||
if (editor_manager_) {
|
||||
auto status = editor_manager_->SaveRom();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to save ROM: %s", status.message()),
|
||||
ToastType::kError);
|
||||
} else {
|
||||
toast_manager_.Show("ROM saved successfully", ToastType::kSuccess);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,58 +349,75 @@ void MenuOrchestrator::OnSaveRomAs() {
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnCreateProject() {
|
||||
auto status = project_manager_.CreateNewProject();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to create project: %s", status.message()),
|
||||
ToastType::kError);
|
||||
// Delegate to EditorManager which handles the full project creation flow
|
||||
if (editor_manager_) {
|
||||
auto status = editor_manager_->CreateNewProject();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to create project: %s", status.message()),
|
||||
ToastType::kError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnOpenProject() {
|
||||
auto status = project_manager_.OpenProject();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to open project: %s", status.message()),
|
||||
ToastType::kError);
|
||||
// Delegate to EditorManager which handles ROM loading and session creation
|
||||
if (editor_manager_) {
|
||||
auto status = editor_manager_->OpenProject();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to open project: %s", status.message()),
|
||||
ToastType::kError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnSaveProject() {
|
||||
auto status = project_manager_.SaveProject();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to save project: %s", status.message()),
|
||||
ToastType::kError);
|
||||
// Delegate to EditorManager which updates project with current state
|
||||
if (editor_manager_) {
|
||||
auto status = editor_manager_->SaveProject();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to save project: %s", status.message()),
|
||||
ToastType::kError);
|
||||
} else {
|
||||
toast_manager_.Show("Project saved successfully", ToastType::kSuccess);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnSaveProjectAs() {
|
||||
auto status = project_manager_.SaveProjectAs();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to save project as: %s", status.message()),
|
||||
ToastType::kError);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
auto status = editor_manager_->SaveProjectAs();
|
||||
if (!status.ok()) {
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Failed to save project as: %s", status.message()),
|
||||
ToastType::kError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Editor-specific menu actions
|
||||
void MenuOrchestrator::OnSwitchToEditor(EditorType editor_type) {
|
||||
editor_registry_.SwitchToEditor(editor_type);
|
||||
toast_manager_.Show(
|
||||
absl::StrFormat("Switched to %s",
|
||||
editor_registry_.GetEditorDisplayName(editor_type)),
|
||||
ToastType::kInfo);
|
||||
// Delegate to EditorManager which manages editor switching
|
||||
if (editor_manager_) {
|
||||
editor_manager_->SwitchToEditor(editor_type);
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnShowEditorSelection() {
|
||||
// TODO: Show editor selection dialog
|
||||
toast_manager_.Show("Editor Selection", ToastType::kInfo);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
editor_manager_->ShowEditorSelection();
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnShowDisplaySettings() {
|
||||
// TODO: Show display settings dialog
|
||||
toast_manager_.Show("Display Settings", ToastType::kInfo);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
editor_manager_->ShowDisplaySettings();
|
||||
}
|
||||
}
|
||||
|
||||
// Session management menu actions
|
||||
@@ -408,28 +439,38 @@ void MenuOrchestrator::OnSwitchToSession(size_t session_index) {
|
||||
|
||||
// Window management menu actions
|
||||
void MenuOrchestrator::OnShowAllWindows() {
|
||||
// TODO: Delegate to WindowDelegate
|
||||
toast_manager_.Show("Show All Windows", ToastType::kInfo);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
editor_manager_->ShowAllWindows();
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnHideAllWindows() {
|
||||
// TODO: Delegate to WindowDelegate
|
||||
toast_manager_.Show("Hide All Windows", ToastType::kInfo);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
editor_manager_->HideAllWindows();
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnResetWorkspaceLayout() {
|
||||
// TODO: Delegate to WindowDelegate
|
||||
toast_manager_.Show("Reset Workspace Layout", ToastType::kInfo);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
editor_manager_->ResetWorkspaceLayout();
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnSaveWorkspaceLayout() {
|
||||
// TODO: Delegate to WindowDelegate
|
||||
toast_manager_.Show("Save Workspace Layout", ToastType::kInfo);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
editor_manager_->SaveWorkspaceLayout();
|
||||
}
|
||||
}
|
||||
|
||||
void MenuOrchestrator::OnLoadWorkspaceLayout() {
|
||||
// TODO: Delegate to WindowDelegate
|
||||
toast_manager_.Show("Load Workspace Layout", ToastType::kInfo);
|
||||
// Delegate to EditorManager
|
||||
if (editor_manager_) {
|
||||
editor_manager_->LoadWorkspaceLayout();
|
||||
}
|
||||
}
|
||||
|
||||
// Tool menu actions
|
||||
|
||||
@@ -38,7 +38,8 @@ class ToastManager;
|
||||
class MenuOrchestrator {
|
||||
public:
|
||||
// Constructor takes references to the managers it coordinates with
|
||||
MenuOrchestrator(MenuBuilder& menu_builder,
|
||||
MenuOrchestrator(EditorManager* editor_manager,
|
||||
MenuBuilder& menu_builder,
|
||||
RomFileManager& rom_manager,
|
||||
ProjectManager& project_manager,
|
||||
EditorRegistry& editor_registry,
|
||||
@@ -103,6 +104,7 @@ class MenuOrchestrator {
|
||||
|
||||
private:
|
||||
// References to coordinated managers
|
||||
EditorManager* editor_manager_;
|
||||
MenuBuilder& menu_builder_;
|
||||
RomFileManager& rom_manager_;
|
||||
ProjectManager& project_manager_;
|
||||
|
||||
Reference in New Issue
Block a user