/****************************************************************************** * File - editor.cpp * Author - Joey Pollack * Date - 2021/11/01 (y/m/d) * Mod Date - 2021/11/01 (y/m/d) * Description - Entry point for the editor run mode. ******************************************************************************/ #include "editor.h" #include "panel_manager.h" #include #include #include #include #include #include // Panels #include "panels/worldTree.h" #include "panels/worldView.h" #include "panels/propertiesView.h" #include "panels/assetBrowser.h" // Tools #include "tools/map_editor/map_editor.h" using namespace lunarium::gui; // ERROR LOG MACRO #define LOG_ERR_AND_RETURN(result) { if (Failed(result)) { Logger::Log(mLogCat, LogLevel::ERROR, result.Description.c_str()); return result; } } namespace lunarium { namespace editor { Editor::Editor() : mLogCat(-1), mpFileBrowser(nullptr), mpPath(nullptr), mDoNewProject(false), mDoOpenProject(false), mDoSaveProject(false), mDoSaveAs(false), mNextWindowID(0), mpMapEditor(nullptr) { } OpRes Editor::Initialize() { mLogCat = Logger::RegisterCategory("EDITOR"); // Initialize internal data DataManager::Initialize(); // Init editor panels mAboutPanel.SetOpen(false); OpRes res = mPanelManager.Initialize(this, "Lunarium editor", true).LogIfFailed(mLogCat); if (Failed(res)) { return res; } mPanelManager.AddPanel(gui::PanelType::PT_ASSET_BROWSER, new AssetBrowser("")).LogIfFailed(mLogCat); mPanelManager.AddPanel(gui::PanelType::PT_WORLD_TREE, new WorldTree()).LogIfFailed(mLogCat); mPanelManager.AddPanel(gui::PanelType::PT_WORLD_VIEW, new WorldView()).LogIfFailed(mLogCat); mPanelManager.AddPanel(gui::PanelType::PT_PROPERTIES_VIEW, new PropertiesView()).LogIfFailed(mLogCat); return OpRes::OK(); } void Editor::Shutdown() { DataManager::Shutdown(); mPanelManager.Shutdown(); } void Editor::OnTick(double delta) { // Panels mPanelManager.OnTick(delta); // Tools if (mpMapEditor) { mpMapEditor->OnTick(delta); } HandleMenuEvents(); } void Editor::OnRender(lunarium::IGraphics* g) { DoMainMenu(); DoStatusBar(); mPanelManager.OnRender(g); if (mpMapEditor) { mpMapEditor->OnRender(g); } if (mAboutPanel.IsOpen()) { mAboutPanel.DoFrame(); } if (mpFileBrowser) { if (!mpFileBrowser->DoFrame()) { mpPath = mpFileBrowser->GetSelectedItem(); } } } uint32_t Editor::GetLogCat() const { return mLogCat; } unsigned int Editor::GetNextWindowID() { return ++mNextWindowID; } bool Editor::IsToolOpen(ToolType type) const { switch (type) { case ToolType::TT_MAP_EDITOR: { if (!mpMapEditor) return false; return mpMapEditor->IsOpen(); } } Logger::Log(mLogCat, LogLevel::WARNING, "And unknown ToolType was passed into Editor::IsToolOpen: %n", type); return false; } //////////////////////////////////////////////////////////// // HELPER METHODS //////////////////////////////////////////////////////////// void Editor::DestroyTools() { mpMapEditor->Shutdown(); delete mpMapEditor; mpMapEditor = nullptr; } void Editor::HandleMenuEvents() { /////////////////////////// // FILE if (mDoNewProject) { if (!mpFileBrowser) { mpFileBrowser = new FileBrowser; // mpFileBrowser->WarnOnExistingFileSelection(true); mpFileBrowser->SetSelectionMode(FileBrowser::SelectionMode::FILES_ONLY); mpFileBrowser->SetPrompt("Pick a location and name for the project"); if (!mpFileBrowser->OpenInDirectory("")) { delete mpFileBrowser; mpFileBrowser = nullptr; mDoNewProject = false; Logger::Log(mLogCat, LogLevel::ERROR, "Could not open the File Browser"); } } else { if (!mpFileBrowser->IsOpen()) { if (mpFileBrowser->GetResult() == FileBrowser::Result::OK) { Logger::Log(mLogCat, LogLevel::INFO, "Generating new project at %s", mpPath->string().c_str()); // Generate new project at mpPath OpRes result = mProject.GenerateProject(mpPath->filename().string(), mpPath->parent_path()); if (Failed(result)) { Logger::Log(mLogCat, LogLevel::ERROR, "Could not create a new project: %s", result.Description); } ((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(*mpPath / std::filesystem::path("contents/assets")); } else { Logger::Log(mLogCat, LogLevel::INFO, "New Project operation cancelled"); } mpPath = nullptr; delete mpFileBrowser; mpFileBrowser = nullptr; mDoNewProject = false; } } } if (mDoOpenProject) { if (!mpFileBrowser) { mpFileBrowser = new FileBrowser; // mpFileBrowser->WarnOnExistingFileSelection(true); mpFileBrowser->SetSelectionMode(FileBrowser::SelectionMode::FILES_ONLY); mpFileBrowser->SetPrompt("Locate the project to open"); if (!mpFileBrowser->OpenInDirectory("")) { delete mpFileBrowser; mpFileBrowser = nullptr; mDoOpenProject = false; Logger::Log(mLogCat, LogLevel::ERROR, "Could not open the File Browser"); } } else { if (!mpFileBrowser->IsOpen()) { if (mpFileBrowser->GetResult() == FileBrowser::Result::OK) { Logger::Log(mLogCat, LogLevel::INFO, "Generating new project at %s", mpPath->string().c_str()); // Generate new project at mpPath OpRes result = mProject.LoadProject(*mpPath); if (Failed(result)) { Logger::Log(mLogCat, LogLevel::ERROR, "Could not open project: %s -- reason: %s", mpPath->string().c_str(), result.Description); } ((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(mpPath->parent_path() / std::filesystem::path("contents/assets")); } else { Logger::Log(mLogCat, LogLevel::INFO, "New Project operation cancelled"); } mpPath = nullptr; delete mpFileBrowser; mpFileBrowser = nullptr; mDoOpenProject = false; } } } if (mDoSaveProject) { mDoSaveProject = false; } if (mDoSaveAs) { mDoSaveAs = false; } /////////////////////////// } //////////////////////////////////////////////////////////// // MENU BAR //////////////////////////////////////////////////////////// void Editor::DoMainMenu() { ImGui::BeginMainMenuBar(); // File if (ImGui::BeginMenu("File")) { if (ImGui::MenuItem("New Project")) { mDoNewProject = true; } if (ImGui::MenuItem("Open Project")) { mDoOpenProject = true; } if (ImGui::MenuItem("Save Project")) { mDoSaveProject = true; } if (ImGui::MenuItem("Save Project As")) { mDoSaveAs = true; } ImGui::Separator(); if (ImGui::MenuItem("Exit")) { Core::GetInstance().SignalShutdown(); } ImGui::EndMenu(); } if (ImGui::BeginMenu("Windows")) { if (ImGui::MenuItem("Reset Window Docking")) { mPanelManager.ResetDocking(); } ImGui::Separator(); HanelOpenPanel("Asset Browser", gui::PanelType::PT_ASSET_BROWSER); HanelOpenPanel("World Tree", gui::PanelType::PT_WORLD_TREE); HanelOpenPanel("World View", gui::PanelType::PT_WORLD_VIEW); HanelOpenPanel("Properties", gui::PanelType::PT_PROPERTIES_VIEW); ImGui::EndMenu(); } // Tools if (ImGui::BeginMenu("Tools")) { if (ImGui::MenuItem("Map Editor")) { if (!mpMapEditor) { mpMapEditor = new MapEditor; mpMapEditor->Initialize(this); } mpMapEditor->Open(); } ImGui::EndMenu(); } if (ImGui::BeginMenu("Help")) { if (ImGui::MenuItem("About")) { mAboutPanel.SetOpen(true); } ImGui::EndMenu(); } ImGui::EndMainMenuBar(); } void Editor::HanelOpenPanel(const char* name, gui::PanelType type) { if (ImGui::MenuItem(name)) { mPanelManager.OpenPanel(type); } } void Editor::DoStatusBar() { const ImGuiViewport* viewport = ImGui::GetMainViewport(); ImGui::SetNextWindowViewport(viewport->ID); float height = viewport->WorkSize.y * 0.035f; float ypos = (viewport->WorkPos.y + viewport->WorkSize.y) - height; ImGui::SetNextWindowSize(ImVec2(viewport->WorkSize.x, height), ImGuiCond_Always); ImGui::SetNextWindowPos(ImVec2(viewport->WorkPos.x, ypos), ImGuiCond_Always); ImGui::PushStyleVar( ImGuiStyleVar_WindowRounding, 0.0f ); ImGui::PushStyleVar( ImGuiStyleVar_WindowBorderSize, 0.1f ); ImGui::PushStyleVar( ImGuiStyleVar_WindowPadding, ImVec2( 0.0f, 0.0f ) ); ImGui::Begin("status bar", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoDocking ); if (ImGui::BeginTable("status", 3, ImGuiTableFlags_BordersInnerV)) { ImGui::AlignTextToFramePadding(); ImGui::TableNextColumn(); ImGui::Text("Lunarium (%s)", Version::GetVersion().ToString().c_str()); ImGui::TableNextColumn(); ImGui::Text("Testing"); ImGui::TableNextColumn(); ImGui::Text("Status Bar"); ImGui::EndTable(); } ImGui::PopStyleVar(3); ImGui::End(); } } }