From caa0e7393513dbc292868b645fe20d9991a7e76e Mon Sep 17 00:00:00 2001 From: Anthony Rabine Date: Wed, 27 Dec 2023 22:06:11 +0100 Subject: [PATCH] Simpler way to create new project, project path in title bar --- software/library/story_project.h | 1 + story-editor/src/gui.cpp | 5 +++ story-editor/src/gui.h | 1 + story-editor/src/main_window.cpp | 60 +++++++++++++++++++++++++++----- story-editor/src/main_window.h | 3 +- 5 files changed, 61 insertions(+), 9 deletions(-) diff --git a/software/library/story_project.h b/software/library/story_project.h index a353c24..4c5689e 100644 --- a/software/library/story_project.h +++ b/software/library/story_project.h @@ -69,6 +69,7 @@ struct StoryProject { m_uuid = ""; m_working_dir = ""; + m_story_file_path = ""; m_initialized = false; } diff --git a/story-editor/src/gui.cpp b/story-editor/src/gui.cpp index e454ad2..474bafe 100644 --- a/story-editor/src/gui.cpp +++ b/story-editor/src/gui.cpp @@ -299,6 +299,11 @@ void Gui::Destroy() SDL_Quit(); } +void Gui::SetWindowTitle(const std::string &title) +{ + SDL_SetWindowTitle(window, title.c_str()); +} + bool Gui::LoadRawImage(const std::string &filename, Image &image) { bool success = true; diff --git a/story-editor/src/gui.h b/story-editor/src/gui.h index 01fbb20..604283f 100644 --- a/story-editor/src/gui.h +++ b/story-editor/src/gui.h @@ -44,6 +44,7 @@ public: void StartFrame(); void EndFrame(); void Destroy(); + void SetWindowTitle(const std::string &title); static bool LoadRawImage(const std::string &filename, Image &image); static Size GetWindowSize(); diff --git a/story-editor/src/main_window.cpp b/story-editor/src/main_window.cpp index 9b80d07..d461898 100644 --- a/story-editor/src/main_window.cpp +++ b/story-editor/src/main_window.cpp @@ -323,7 +323,7 @@ void MainWindow::DrawMainMenuBar() if (showNewProject) { - ImGui::OpenPopup("NewProjectPopup"); + ImGuiFileDialog::Instance()->OpenDialog("ChooseDirDialog", "Choose a parent directory for your project", nullptr, ".", 1, nullptr, ImGuiFileDialogFlags_Modal); } if (showOpenProject) @@ -363,7 +363,7 @@ void MainWindow::DrawMainMenuBar() void MainWindow::Initialize() { // GUI Init - gui.Initialize(); + m_gui.Initialize(); // gui.ApplyTheme(); m_editorWindow.Initialize(); @@ -420,7 +420,6 @@ void MainWindow::OpenProjectDialog() std::string filePath = ImGuiFileDialog::Instance()->GetCurrentPath(); OpenProject(filePathName); - // RefreshProjectInformation(); // FIXME } // close @@ -430,6 +429,39 @@ void MainWindow::OpenProjectDialog() void MainWindow::NewProjectPopup() { + // Always center this window when appearing + ImVec2 center = ImGui::GetMainViewport()->GetCenter(); + ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); + + if (ImGuiFileDialog::Instance()->Display("ChooseDirDialog")) + { + // action if OK + if (ImGuiFileDialog::Instance()->IsOk()) + { + std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName(); + std::string projdir = ImGuiFileDialog::Instance()->GetCurrentPath(); + + std::string uuid = UUID().String(); + auto p = std::filesystem::path(projdir) / uuid / std::filesystem::path("project.json"); + m_story.Initialize(p.generic_string()); + + m_story.SetDisplayFormat(320, 240); + m_story.SetImageFormat(StoryProject::IMG_FORMAT_QOIF); + m_story.SetSoundFormat(StoryProject::SND_FORMAT_WAV); + m_story.SetName("New project"); + m_story.SetUuid(uuid); + + SaveProject(); + OpenProject(p.generic_string()); + } + + // close + ImGuiFileDialog::Instance()->Close(); + } + + /* + + static std::string projdir; // Always center this window when appearing ImVec2 center = ImGui::GetMainViewport()->GetCenter(); @@ -443,11 +475,13 @@ void MainWindow::NewProjectPopup() ImGui::Text("Directory: "); ImGui::SameLine(); static char project_dir[256] = ""; ImGui::InputTextWithHint("##project_path", "Project path", project_dir, IM_ARRAYSIZE(project_dir)); + ImGui::SameLine(); if (ImGui::Button( ICON_MDI_FOLDER " ...")) { ImGuiFileDialog::Instance()->OpenDialog("ChooseDirDialog", "Choose File", nullptr, ".", 1, nullptr, ImGuiFileDialogFlags_Modal); } + // display if (ImGuiFileDialog::Instance()->Display("ChooseDirDialog")) { @@ -616,6 +650,7 @@ void MainWindow::NewProjectPopup() { projdir = ""; } +*/ } void MainWindow::SaveProject() @@ -627,7 +662,6 @@ void MainWindow::SaveProject() void MainWindow::OpenProject(const std::string &filename) { - m_story.Initialize(filename); nlohmann::json model; @@ -662,7 +696,15 @@ void MainWindow::OpenProject(const std::string &filename) Log("Open project error"); } + RefreshProjectInformation(); } + +void MainWindow::RefreshProjectInformation() +{ + m_gui.SetWindowTitle("Story Editor - " + m_story.GetProjectFilePath()); +} + + void MainWindow::CloseProject() { m_story.Clear(); @@ -680,6 +722,8 @@ void MainWindow::CloseProject() m_editorWindow.Disable(); m_resourcesWindow.Disable(); m_PropertiesWindow.Disable(); + + RefreshProjectInformation(); } @@ -691,9 +735,9 @@ void MainWindow::Loop() while (!done) { - bool aboutToClose = gui.PollEvent(); + bool aboutToClose = m_gui.PollEvent(); - gui.StartFrame(); + m_gui.StartFrame(); ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()); DrawMainMenuBar(); @@ -725,12 +769,12 @@ void MainWindow::Loop() done = true; } - gui.EndFrame(); + m_gui.EndFrame(); } - gui.Destroy(); + m_gui.Destroy(); } void MainWindow::Log(const std::string &txt, bool critical) diff --git a/story-editor/src/main_window.h b/story-editor/src/main_window.h index d530b33..6f85b93 100644 --- a/story-editor/src/main_window.h +++ b/story-editor/src/main_window.h @@ -109,7 +109,7 @@ private: ResourceManager m_resources; - Gui gui; + Gui m_gui; EmulatorWindow m_emulatorWindow; ConsoleWindow m_consoleWindow; CodeEditor m_editorWindow; @@ -173,6 +173,7 @@ private: std::string GetFileNameFromMemory(uint32_t addr); void ProcessStory(); void StepInstruction(); + void RefreshProjectInformation(); }; #endif // MAINWINDOW_H