|
| 1 | +#include "lpch.h" |
| 2 | +#include "PanelManager.h" |
| 3 | +#include "Lux/Utilities/FileSystem.h" |
| 4 | + |
| 5 | +#include <yaml-cpp/yaml.h> |
| 6 | + |
| 7 | +namespace Lux { |
| 8 | + |
| 9 | + PanelManager::~PanelManager() |
| 10 | + { |
| 11 | + for (auto& map : m_Panels) |
| 12 | + map.clear(); |
| 13 | + } |
| 14 | + |
| 15 | + PanelData* PanelManager::GetPanelData(uint32_t panelID) |
| 16 | + { |
| 17 | + for (auto& panelMap : m_Panels) |
| 18 | + { |
| 19 | + if (panelMap.find(panelID) == panelMap.end()) |
| 20 | + continue; |
| 21 | + |
| 22 | + return &panelMap.at(panelID); |
| 23 | + } |
| 24 | + |
| 25 | + return nullptr; |
| 26 | + } |
| 27 | + |
| 28 | + const PanelData* PanelManager::GetPanelData(uint32_t panelID) const |
| 29 | + { |
| 30 | + for (auto& panelMap : m_Panels) |
| 31 | + { |
| 32 | + if (panelMap.find(panelID) == panelMap.end()) |
| 33 | + continue; |
| 34 | + |
| 35 | + return &panelMap.at(panelID); |
| 36 | + } |
| 37 | + |
| 38 | + return nullptr; |
| 39 | + } |
| 40 | + |
| 41 | + void PanelManager::RemovePanel(const char* strID) |
| 42 | + { |
| 43 | + uint32_t id = Hash::GenerateFNVHash(strID); |
| 44 | + for (auto& panelMap : m_Panels) |
| 45 | + { |
| 46 | + if (panelMap.find(id) == panelMap.end()) |
| 47 | + continue; |
| 48 | + |
| 49 | + panelMap.erase(id); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + LUX_CORE_ERROR_TAG("PanelManager", "Couldn't find panel with id '{0}'", strID); |
| 54 | + } |
| 55 | + |
| 56 | + void PanelManager::OnImGuiRender() |
| 57 | + { |
| 58 | + for (auto& panelMap : m_Panels) |
| 59 | + { |
| 60 | + for (auto& [id, panelData] : panelMap) |
| 61 | + { |
| 62 | + bool closedThisFrame = false; |
| 63 | + |
| 64 | + if (panelData.IsOpen) |
| 65 | + { |
| 66 | + panelData.Panel->OnImGuiRender(panelData.IsOpen); |
| 67 | + closedThisFrame = !panelData.IsOpen; |
| 68 | + } |
| 69 | + |
| 70 | + if (closedThisFrame) |
| 71 | + { |
| 72 | + panelData.Panel->OnClose(); |
| 73 | + Serialize(); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + void PanelManager::OnEvent(Event& e) |
| 80 | + { |
| 81 | + for (auto& panelMap : m_Panels) |
| 82 | + { |
| 83 | + for (auto& [id, panelData] : panelMap) |
| 84 | + panelData.Panel->OnEvent(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void PanelManager::SetSceneContext(const Ref<Scene>& context) |
| 89 | + { |
| 90 | + for (auto& panelMap : m_Panels) |
| 91 | + { |
| 92 | + for (auto& [id, panelData] : panelMap) |
| 93 | + panelData.Panel->SetSceneContext(context); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + void PanelManager::OnProjectChanged(const Ref<Project>& project) |
| 98 | + { |
| 99 | + for (auto& panelMap : m_Panels) |
| 100 | + { |
| 101 | + for (auto& [id, panelData] : panelMap) |
| 102 | + panelData.Panel->OnProjectChanged(project); |
| 103 | + } |
| 104 | + |
| 105 | + Deserialize(); |
| 106 | + } |
| 107 | + |
| 108 | + void PanelManager::Serialize() const |
| 109 | + { |
| 110 | + YAML::Emitter out; |
| 111 | + out << YAML::BeginMap; |
| 112 | + |
| 113 | + out << YAML::Key << "Panels" << YAML::Value << YAML::BeginSeq; |
| 114 | + { |
| 115 | + for (size_t category = 0; category < m_Panels.size(); category++) |
| 116 | + { |
| 117 | + for (const auto& [panelID, panel] : m_Panels[category]) |
| 118 | + { |
| 119 | + out << YAML::BeginMap; |
| 120 | + out << YAML::Key << "ID" << YAML::Value << panelID; |
| 121 | + out << YAML::Key << "Name" << YAML::Value << panel.Name; |
| 122 | + out << YAML::Key << "IsOpen" << YAML::Value << panel.IsOpen; |
| 123 | + out << YAML::EndMap; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + out << YAML::EndSeq; |
| 128 | + |
| 129 | + out << YAML::EndMap; |
| 130 | + |
| 131 | + std::ofstream fout(FileSystem::GetPersistentStoragePath() / "EditorLayout.yaml"); |
| 132 | + fout << out.c_str(); |
| 133 | + fout.close(); |
| 134 | + } |
| 135 | + |
| 136 | + void PanelManager::Deserialize() |
| 137 | + { |
| 138 | + std::filesystem::path layoutPath = FileSystem::GetPersistentStoragePath() / "EditorLayout.yaml"; |
| 139 | + if (!FileSystem::Exists(layoutPath)) |
| 140 | + return; |
| 141 | + |
| 142 | + std::ifstream stream(layoutPath); |
| 143 | + LUX_CORE_VERIFY(stream); |
| 144 | + std::stringstream ss; |
| 145 | + ss << stream.rdbuf(); |
| 146 | + |
| 147 | + YAML::Node data = YAML::Load(ss.str()); |
| 148 | + if (!data["Panels"]) |
| 149 | + { |
| 150 | + LUX_CONSOLE_LOG_ERROR("Failed to load EditorLayout.yaml from {} because the file appears to be corrupted!", layoutPath.parent_path().string()); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + for (auto panelNode : data["Panels"]) |
| 155 | + { |
| 156 | + PanelData* panelData = GetPanelData(panelNode["ID"].as<uint32_t>(0)); |
| 157 | + |
| 158 | + if (panelData == nullptr) |
| 159 | + continue; |
| 160 | + |
| 161 | + panelData->IsOpen = panelNode["IsOpen"].as<bool>(panelData->IsOpen); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments