[olympic/studio] Add new project menu, make file creation open file
Some checks failed
Build / build (push) Failing after 2m7s

This commit is contained in:
2023-12-28 23:44:25 -06:00
parent ffbdb09c31
commit 1df4e78084
9 changed files with 228 additions and 27 deletions

View File

@ -5,6 +5,7 @@ add_library(
filedialogmanager.cpp
main.cpp
newmenu.cpp
newproject.cpp
projectexplorer.cpp
projecttreemodel.cpp
studioapp.cpp

View File

@ -115,12 +115,12 @@ void NewMenu::drawLastPageButtons(turbine::Context &ctx) noexcept {
}
void NewMenu::finish(turbine::Context &ctx) noexcept {
auto const err = m_types[static_cast<std::size_t>(m_selectedType)]->write(ctx, m_itemName);
auto const [path, err] = m_types[static_cast<std::size_t>(m_selectedType)]->write(ctx, m_itemName);
if (err) {
oxLogError(err);
return;
}
finished.emit("");
finished.emit(path);
m_stage = Stage::Closed;
}

View File

@ -0,0 +1,95 @@
/*
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <imgui.h>
#include <studio/imguiuitl.hpp>
#include <utility>
#include "filedialogmanager.hpp"
#include "newproject.hpp"
namespace studio {
NewProject::NewProject(ox::String projectDatadir) noexcept: m_projectDataDir(std::move(projectDatadir)) {
setTitle(ox::String("New Project"));
setSize({230, 140});
}
void NewProject::open() noexcept {
m_stage = Stage::Opening;
m_projectPath = "";
m_projectName = "";
}
void NewProject::close() noexcept {
m_stage = Stage::Closed;
m_open = false;
}
bool NewProject::isOpen() const noexcept {
return m_open;
}
void NewProject::draw(turbine::Context &ctx) noexcept {
switch (m_stage) {
case Stage::Opening:
ImGui::OpenPopup(title().c_str());
m_stage = Stage::NewItemName;
m_open = true;
[[fallthrough]];
case Stage::NewItemName:
drawNewProjectName(ctx);
break;
case Stage::Closed:
m_open = false;
break;
}
}
void NewProject::drawNewProjectName(turbine::Context &ctx) noexcept {
drawWindow(ctx, &m_open, [this, &ctx] {
ImGui::InputText("Name", m_projectName.data(), m_projectName.cap());
ImGui::Text("Path: %s", m_projectPath.c_str());
if (ImGui::Button("Browse")) {
oxLogError(studio::chooseDirectory().moveTo(m_projectPath));
}
drawLastPageButtons(ctx);
});
}
void NewProject::drawFirstPageButtons() noexcept {
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 130);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - 20);
auto const btnSz = ImVec2(60, 20);
if (ImGui::Button("Next", btnSz)) {
m_stage = Stage::NewItemName;
}
ImGui::SameLine();
if (ImGui::Button("Cancel", btnSz)) {
ImGui::CloseCurrentPopup();
m_stage = Stage::Closed;
}
}
void NewProject::drawLastPageButtons(turbine::Context&) noexcept {
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 95);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetContentRegionAvail().y - 20);
if (ImGui::Button("Finish")) {
finish();
}
ImGui::SameLine();
if (ImGui::Button("Quit")) {
ImGui::CloseCurrentPopup();
m_stage = Stage::Closed;
}
}
void NewProject::finish() noexcept {
auto projectPath = ox::sfmt("{}/{}", m_projectPath, m_projectName);
finished.emit(projectPath);
m_stage = Stage::Closed;
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <ox/claw/claw.hpp>
#include <ox/event/signal.hpp>
#include <ox/std/string.hpp>
#include <studio/itemmaker.hpp>
#include <studio/popup.hpp>
namespace studio {
class NewProject: public studio::Popup {
public:
enum class Stage {
Closed,
Opening,
NewItemName,
};
// emits path parameter
ox::Signal<ox::Error(ox::StringView)> finished;
private:
Stage m_stage = Stage::Closed;
ox::String const m_projectDataDir;
ox::String m_projectPath;
ox::BString<255> m_projectName;
ox::Vector<ox::UniquePtr<studio::ItemMaker>> m_types;
bool m_open = false;
public:
NewProject(ox::String projectDatadir) noexcept;
void open() noexcept override;
void close() noexcept override;
[[nodiscard]]
bool isOpen() const noexcept override;
void draw(turbine::Context &ctx) noexcept override;
private:
void drawNewProjectName(turbine::Context &ctx) noexcept;
void drawFirstPageButtons() noexcept;
void drawLastPageButtons(turbine::Context &ctx) noexcept;
void finish() noexcept;
};
}

View File

@ -2,6 +2,8 @@
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <filesystem>
#include <imgui.h>
#include <keel/media.hpp>
@ -42,15 +44,18 @@ StudioUI::StudioUI(turbine::Context &ctx, ox::StringView projectDataDir) noexcep
m_ctx(ctx),
m_projectDataDir(projectDataDir),
m_projectExplorer(m_ctx),
m_newProject(ox::String(projectDataDir)),
m_aboutPopup(m_ctx) {
m_projectExplorer.fileChosen.connect(this, &StudioUI::openFile);
m_newProject.finished.connect(this, &StudioUI::createOpenProject);
m_newMenu.finished.connect(this, &StudioUI::openFile);
ImGui::GetIO().IniFilename = nullptr;
loadModules();
// open project and files
auto const [config, err] = studio::readConfig<StudioConfig>(keelCtx(m_ctx));
m_showProjectExplorer = config.showProjectExplorer;
if (!err) {
auto const openProjErr = openProject(config.projectPath);
auto const openProjErr = openProjectPath(config.projectPath);
if (!openProjErr) {
for (auto const&f: config.openFiles) {
auto openFileErr = openFileActiveTab(f, config.activeTabItemName == f);
@ -95,10 +100,14 @@ void StudioUI::handleKeyEvent(turbine::Key key, bool down) noexcept {
}
break;
case turbine::Key::Alpha_N:
m_newMenu.open();
if (turbine::buttonDown(m_ctx, turbine::Key::Mod_Shift)) {
m_newProject.open();
} else {
m_newMenu.open();
}
break;
case turbine::Key::Alpha_O:
m_taskRunner.add(*ox::make<FileDialogManager>(this, &StudioUI::openProject));
m_taskRunner.add(*ox::make<FileDialogManager>(this, &StudioUI::openProjectPath));
break;
case turbine::Key::Alpha_Q:
turbine::requestShutdown(m_ctx);
@ -168,8 +177,11 @@ void StudioUI::drawMenu() noexcept {
if (ImGui::MenuItem("New...", "Ctrl+N")) {
m_newMenu.open();
}
if (ImGui::MenuItem("New Project...", "Ctrl+Shift+N")) {
m_newProject.open();
}
if (ImGui::MenuItem("Open Project...", "Ctrl+O")) {
m_taskRunner.add(*ox::make<FileDialogManager>(this, &StudioUI::openProject));
m_taskRunner.add(*ox::make<FileDialogManager>(this, &StudioUI::openProjectPath));
}
if (ImGui::MenuItem("Save", "Ctrl+S", false, m_activeEditor && m_activeEditor->unsavedChanges())) {
m_activeEditor->save();
@ -317,19 +329,27 @@ void StudioUI::save() noexcept {
}
}
ox::Error StudioUI::openProject(ox::CRStringView path) noexcept {
ox::Error StudioUI::createOpenProject(ox::CRStringView path) noexcept {
std::error_code ec;
std::filesystem::create_directories(toStdStringView(path), ec);
oxReturnError(OxError(ec.value() != 0, "Could not create project directory"));
oxReturnError(openProjectPath(path));
return m_project->writeAllTypeDescriptors();
}
ox::Error StudioUI::openProjectPath(ox::CRStringView path) noexcept {
oxRequireM(fs, keel::loadRomFs(path));
oxReturnError(keel::setRomFs(keelCtx(m_ctx), std::move(fs)));
turbine::setWindowTitle(m_ctx, ox::sfmt("{} - {}", keelCtx(m_ctx).appName, path));
m_project = ox::make_unique<studio::Project>(keelCtx(m_ctx), ox::String(path), m_projectDataDir);
auto sctx = applicationData<studio::StudioContext>(m_ctx);
auto const sctx = applicationData<studio::StudioContext>(m_ctx);
sctx->project = m_project.get();
turbine::setWindowTitle(m_ctx, ox::sfmt("{} - {}", keelCtx(m_ctx).appName, m_project->projectPath()));
m_project->fileAdded.connect(&m_projectExplorer, &ProjectExplorer::refreshProjectTreeModel);
m_project->fileDeleted.connect(&m_projectExplorer, &ProjectExplorer::refreshProjectTreeModel);
m_openFiles.clear();
m_editors.clear();
studio::editConfig<StudioConfig>(keelCtx(m_ctx), [&](StudioConfig *config) {
config->projectPath = ox::String(path);
config->projectPath = ox::String(m_project->projectPath());
config->openFiles.clear();
});
return m_projectExplorer.refreshProjectTreeModel();

View File

@ -14,6 +14,7 @@
#include <studio/task.hpp>
#include "aboutpopup.hpp"
#include "newmenu.hpp"
#include "newproject.hpp"
#include "projectexplorer.hpp"
#include "projecttreemodel.hpp"
@ -36,9 +37,11 @@ class StudioUI: public ox::SignalHandler {
studio::BaseEditor *m_activeEditor = nullptr;
studio::BaseEditor *m_activeEditorUpdatePending = nullptr;
NewMenu m_newMenu;
NewProject m_newProject;
AboutPopup m_aboutPopup;
ox::Array<studio::Popup*, 2> const m_popups = {
&m_newMenu,
&m_newProject,
&m_aboutPopup
};
bool m_showProjectExplorer = true;
@ -79,7 +82,9 @@ class StudioUI: public ox::SignalHandler {
void save() noexcept;
ox::Error openProject(ox::CRStringView path) noexcept;
ox::Error createOpenProject(ox::CRStringView path) noexcept;
ox::Error openProjectPath(ox::CRStringView path) noexcept;
ox::Error openFile(ox::CRStringView path) noexcept;