[nostalgia,studio] Cleanup Studio

This commit is contained in:
Gary Talent 2023-11-30 00:57:47 -06:00
parent c085c50b30
commit e543131f0d
9 changed files with 75 additions and 47 deletions

View File

@ -4,13 +4,15 @@
#pragma once
#include <ox/std/stringliteral.hpp>
namespace nostalgia::core {
constexpr auto TileWidth = 8;
constexpr auto TileHeight = 8;
constexpr auto PixelsPerTile = TileWidth * TileHeight;
constexpr auto FileExt_ng = "ng";
constexpr auto FileExt_npal = "npal";
constexpr ox::StringLiteral FileExt_ng("ng");
constexpr ox::StringLiteral FileExt_npal("npal");
}

View File

@ -140,7 +140,7 @@ void PaletteEditorImGui::draw(turbine::Context*) noexcept {
ox::Error PaletteEditorImGui::saveItem() noexcept {
const auto sctx = applicationData<studio::StudioContext>(*m_ctx);
return sctx->project->writeObj(m_itemPath, &m_pal);
return sctx->project->writeObj(m_itemPath, m_pal);
}
}

View File

@ -14,18 +14,8 @@ namespace nostalgia::core {
class StudioModule: public studio::Module {
ox::Vector<studio::EditorMaker> editors(turbine::Context *ctx) const noexcept final {
return {
{
{FileExt_ng},
[ctx](ox::CRStringView path) -> ox::Result<studio::BaseEditor*> {
return ox::makeCatch<TileSheetEditorImGui>(ctx, ox::String(path));
}
},
{
{FileExt_npal},
[ctx](ox::CRStringView path) -> ox::Result<studio::BaseEditor*> {
return ox::makeCatch<PaletteEditorImGui>(ctx, ox::String(path));
}
}
studio::editorMaker<TileSheetEditorImGui>(ctx, FileExt_ng),
studio::editorMaker<PaletteEditorImGui>(ctx, FileExt_npal),
};
}

View File

@ -773,7 +773,7 @@ void TileSheetEditorModel::ackUpdate() noexcept {
ox::Error TileSheetEditorModel::saveFile() noexcept {
const auto sctx = applicationData<studio::StudioContext>(*m_ctx);
return sctx->project->writeObj(m_path, &m_img);
return sctx->project->writeObj(m_path, m_img);
}
bool TileSheetEditorModel::pixelSelected(std::size_t idx) const noexcept {

View File

@ -49,7 +49,7 @@ void SceneEditorImGui::onActivated() noexcept {
ox::Error SceneEditorImGui::saveItem() noexcept {
const auto sctx = applicationData<studio::StudioContext>(m_ctx);
oxReturnError(sctx->project->writeObj(m_itemPath, &m_editor.scene()));
oxReturnError(sctx->project->writeObj(m_itemPath, m_editor.scene()));
oxReturnError(m_ctx.keelCtx.assetManager.setAsset(m_itemPath, m_editor.scene()));
return {};
}

View File

@ -18,10 +18,10 @@ class ItemMaker {
const ox::String name;
const ox::String parentDir;
const ox::String fileExt;
constexpr explicit ItemMaker(ox::String pName, ox::String pParentDir, ox::String pFileExt) noexcept:
constexpr explicit ItemMaker(ox::String pName, ox::String pParentDir, ox::CRStringView pFileExt) noexcept:
name(std::move(pName)),
parentDir(std::move(pParentDir)),
fileExt(std::move(pFileExt)) {
fileExt(pFileExt) {
}
virtual ~ItemMaker() noexcept = default;
virtual ox::Error write(turbine::Context *ctx, ox::CRStringView pName) const noexcept = 0;
@ -33,25 +33,39 @@ class ItemMakerT: public ItemMaker {
const T item;
const ox::ClawFormat fmt;
public:
constexpr explicit ItemMakerT(ox::String pDisplayName, ox::String pParentDir, ox::String fileExt, ox::ClawFormat pFmt = ox::ClawFormat::Metal) noexcept:
ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)),
constexpr explicit ItemMakerT(
ox::String pDisplayName,
ox::String pParentDir,
ox::CRStringView fileExt,
ox::ClawFormat pFmt = ox::ClawFormat::Metal) noexcept:
ItemMaker(std::move(pDisplayName), std::move(pParentDir), fileExt),
fmt(pFmt) {
}
constexpr ItemMakerT(ox::String pDisplayName, ox::String pParentDir, ox::String fileExt, const T &pItem, ox::ClawFormat pFmt) noexcept:
ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)),
constexpr ItemMakerT(
ox::String pDisplayName,
ox::String pParentDir,
ox::CRStringView fileExt,
T pItem,
ox::ClawFormat pFmt) noexcept:
ItemMaker(std::move(pDisplayName), std::move(pParentDir), fileExt),
item(pItem),
fmt(pFmt) {
}
constexpr ItemMakerT(ox::String pDisplayName, ox::String pParentDir, T &&pItem, ox::ClawFormat pFmt) noexcept:
ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)),
item(std::forward(pItem)),
constexpr ItemMakerT(
ox::String pDisplayName,
ox::String pParentDir,
ox::CRStringView fileExt,
T &&pItem,
ox::ClawFormat pFmt) noexcept:
ItemMaker(std::move(pDisplayName), std::move(pParentDir), fileExt),
item(std::move(pItem)),
fmt(pFmt) {
}
ox::Error write(turbine::Context *ctx, ox::CRStringView pName) const noexcept override {
const auto path = ox::sfmt("/{}/{}.{}", parentDir, pName, fileExt);
auto sctx = turbine::applicationData<studio::StudioContext>(*ctx);
keel::createUuidMapping(&ctx->keelCtx, path, ox::UUID::generate().unwrap());
return sctx->project->writeObj(path, &item, fmt);
return sctx->project->writeObj(path, item, fmt);
}
};

View File

@ -33,4 +33,26 @@ class Module {
};
template<typename Editor>
[[nodiscard]]
studio::EditorMaker editorMaker(turbine::Context *ctx, ox::CRStringView ext) noexcept {
return {
{ox::String(ext)},
[ctx](ox::CRStringView path) -> ox::Result<studio::BaseEditor*> {
return ox::makeCatch<Editor>(ctx, ox::String(path));
}
};
}
template<typename Editor>
[[nodiscard]]
studio::EditorMaker editorMaker(turbine::Context *ctx, ox::Vector<ox::String> exts) noexcept {
return {
std::move(exts),
[ctx](ox::CRStringView path) -> ox::Result<studio::BaseEditor*> {
return ox::makeCatch<Editor>(ctx, ox::String(path));
}
};
}
}

View File

@ -38,7 +38,7 @@ constexpr ox::Result<ox::StringView> fileExt(ox::CRStringView path) noexcept {
class Project {
private:
keel::Context *m_ctx = nullptr;
keel::Context &m_ctx;
ox::String m_path;
ox::String m_projectDataDir;
mutable keel::TypeStore m_typeStore;
@ -60,12 +60,12 @@ class Project {
*/
template<typename T>
ox::Error writeObj(
const ox::StringView &path,
const T *obj,
ox::CRStringView path,
T const&obj,
ox::ClawFormat fmt = ox::ClawFormat::Metal) noexcept;
template<typename T>
ox::Result<T> loadObj(const ox::StringView &path) const noexcept;
ox::Result<T> loadObj(ox::CRStringView path) const noexcept;
ox::Result<ox::FileStat> stat(ox::CRStringView path) const noexcept;
@ -83,9 +83,9 @@ class Project {
void indexFile(ox::CRStringView path) noexcept;
ox::Error writeBuff(const ox::StringView &path, const ox::Buffer &buff) noexcept;
ox::Error writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexcept;
ox::Result<ox::Buffer> loadBuff(const ox::StringView &path) const noexcept;
ox::Result<ox::Buffer> loadBuff(ox::CRStringView path) const noexcept;
ox::Error lsProcDir(ox::Vector<ox::String> *paths, ox::CRStringView path) const noexcept;
@ -105,14 +105,14 @@ class Project {
};
template<typename T>
ox::Error Project::writeObj(const ox::StringView &path, const T *obj, ox::ClawFormat fmt) noexcept {
ox::Error Project::writeObj(ox::CRStringView path, T const&obj, ox::ClawFormat fmt) noexcept {
// write MetalClaw
oxRequireM(buff, ox::writeClaw(*obj, fmt));
oxRequireM(buff, ox::writeClaw(obj, fmt));
// write to FS
oxReturnError(writeBuff(path, buff));
// write type descriptor
if (m_typeStore.get<T>().error) {
oxReturnError(ox::buildTypeDef(&m_typeStore, obj));
oxReturnError(ox::buildTypeDef(&m_typeStore, &obj));
}
// write out type store
const auto descPath = ox::sfmt("/.{}/type_descriptors", m_projectDataDir);
@ -125,13 +125,13 @@ ox::Error Project::writeObj(const ox::StringView &path, const T *obj, ox::ClawFo
const auto typePath = ox::sfmt("/{}/{}", descPath, buildTypeId(*t));
oxReturnError(writeBuff(typePath, typeOut));
}
oxReturnError(keel::setAsset(m_ctx, path, *obj));
oxReturnError(keel::setAsset(&m_ctx, path, obj));
fileUpdated.emit(path);
return {};
}
template<typename T>
ox::Result<T> Project::loadObj(const ox::StringView &path) const noexcept {
ox::Result<T> Project::loadObj(ox::CRStringView path) const noexcept {
oxRequire(buff, loadBuff(path));
if constexpr (ox::is_same_v<T, ox::ModelObject>) {
return keel::readAsset(&m_typeStore, buff);
@ -151,7 +151,7 @@ ox::Error Project::subscribe(ProjectEvent e, ox::SignalHandler *tgt, Functor &&s
case ProjectEvent::FileRecognized:
{
oxRequire(files, listFiles());
for (const auto &f : files) {
for (auto const&f : files) {
slot(f);
}
connect(this, &Project::fileRecognized, tgt, slot);
@ -164,7 +164,7 @@ ox::Error Project::subscribe(ProjectEvent e, ox::SignalHandler *tgt, Functor &&s
connect(this, &Project::fileUpdated, tgt, slot);
break;
}
return OxError(0);
return {};
}
}

View File

@ -22,7 +22,7 @@ static void generateTypes(ox::TypeStore *ts) noexcept {
}
Project::Project(keel::Context *ctx, ox::String path, ox::CRStringView projectDataDir) noexcept:
m_ctx(ctx),
m_ctx(*ctx),
m_path(std::move(path)),
m_projectDataDir(projectDataDir),
m_typeStore(ctx->rom.get(), ox::sfmt("/.{}/type_descriptors", projectDataDir)),
@ -45,7 +45,7 @@ ox::FileSystem *Project::romFs() noexcept {
ox::Error Project::mkdir(ox::CRStringView path) const noexcept {
oxReturnError(m_fs->mkdir(path, true));
fileUpdated.emit(path);
return OxError(0);
return {};
}
ox::Result<ox::FileStat> Project::stat(ox::CRStringView path) const noexcept {
@ -83,12 +83,12 @@ void Project::indexFile(ox::CRStringView path) noexcept {
m_fileExtFileMap[ext].emplace_back(path);
}
ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff) noexcept {
ox::Error Project::writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexcept {
constexpr auto HdrSz = 40;
ox::Buffer outBuff;
outBuff.reserve(buff.size() + HdrSz);
ox::BufferWriter writer(&outBuff);
const auto [uuid, err] = m_ctx->pathToUuid.at(path);
const auto [uuid, err] = m_ctx.pathToUuid.at(path);
if (!err) {
oxReturnError(keel::writeUuidHeader(writer, *uuid));
}
@ -104,7 +104,7 @@ ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff)
return {};
}
ox::Result<ox::Buffer> Project::loadBuff(const ox::StringView &path) const noexcept {
ox::Result<ox::Buffer> Project::loadBuff(ox::CRStringView path) const noexcept {
return m_fs->read(path);
}
@ -124,7 +124,7 @@ ox::Error Project::lsProcDir(ox::Vector<ox::String> *paths, ox::CRStringView pat
break;
}
}
return OxError(0);
return {};
}
ox::Result<ox::Vector<ox::String>> Project::listFiles(ox::CRStringView path) const noexcept {