[nostalgia/studio] Add ClawViewer for unknown types

This commit is contained in:
2022-12-15 01:36:55 -06:00
parent 79d255b63f
commit 44f45e64e9
16 changed files with 505 additions and 75 deletions

View File

@ -3,6 +3,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_executable(
nostalgia-studio MACOSX_BUNDLE
aboutpopup.cpp
clawviewer.cpp
filedialogmanager.cpp
main.cpp
newmenu.cpp

View File

@ -0,0 +1,186 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <imgui.h>
#include "clawviewer.hpp"
namespace nostalgia {
ClawEditor::ClawEditor(ox::CRStringView path, ox::ModelObject obj) noexcept:
m_itemName(path),
m_itemDisplayName(pathToItemName(path)),
m_obj(std::move(obj)) {
}
ox::CRString ClawEditor::itemName() const noexcept {
return m_itemName;
}
ox::CRString ClawEditor::itemDisplayName() const noexcept {
return m_itemDisplayName;
}
void ClawEditor::draw(core::Context*) noexcept {
//const auto paneSize = ImGui::GetContentRegionAvail();
ImGui::BeginChild("PaletteEditor");
static constexpr auto flags = ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody;
if (ImGui::BeginTable("ObjTree", 3, flags)) {
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 100);
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, 250);
ImGui::TableSetupColumn("Value", ImGuiTableColumnFlags_NoHide);
ImGui::TableHeadersRow();
ObjPath objPath;
drawTree(&objPath, m_obj);
ImGui::EndTable();
}
ImGui::EndChild();
}
void ClawEditor::drawRow(const ox::ModelValue &value) noexcept {
using Str = ox::BasicString<100>;
Str val, type;
switch (value.type()) {
case ox::ModelValue::Type::Undefined:
val = "undefined";
type = "undefined";
break;
case ox::ModelValue::Type::Bool:
val = value.get<bool>() ? "true" : "false";
type = "bool";
break;
case ox::ModelValue::Type::UnsignedInteger8:
val = ox::sfmt<Str>("{}", value.get<uint8_t>());
type = "uint8";
break;
case ox::ModelValue::Type::UnsignedInteger16:
val = ox::sfmt<Str>("{}", value.get<uint16_t>());
type = "uint16";
break;
case ox::ModelValue::Type::UnsignedInteger32:
val = ox::sfmt<Str>("{}", value.get<uint32_t>());
type = "uint32";
break;
case ox::ModelValue::Type::UnsignedInteger64:
val = ox::sfmt<Str>("{}", value.get<uint64_t>());
type = "uint64";
break;
case ox::ModelValue::Type::SignedInteger8:
val = ox::sfmt<Str>("{}", value.get<int8_t>());
type = "int8";
break;
case ox::ModelValue::Type::SignedInteger16:
val = ox::sfmt<Str>("{}", value.get<int16_t>());
type = "int16";
break;
case ox::ModelValue::Type::SignedInteger32:
val = ox::sfmt<Str>("{}", value.get<int32_t>());
type = "int32";
break;
case ox::ModelValue::Type::SignedInteger64:
val = ox::sfmt<Str>("{}", value.get<int64_t>());
type = "int64";
break;
case ox::ModelValue::Type::String:
val = ox::sfmt<Str>("\"{}\"", value.get<ox::String>());
type = "string";
break;
case ox::ModelValue::Type::Object:
type = value.get<ox::ModelObject>().type()->typeName.c_str();
break;
case ox::ModelValue::Type::Union:
type = "union";
break;
case ox::ModelValue::Type::Vector:
type = "list";
break;
}
ImGui::TableNextColumn();
ImGui::Text("%s", type.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", val.c_str());
}
void ClawEditor::drawVar(ObjPath *path, ox::CRStringView name, const ox::ModelValue &value) noexcept {
using Str = ox::BasicString<100>;
path->push_back(name);
if (value.type() == ox::ModelValue::Type::Object) {
drawTree(path, value.get<ox::ModelObject>());
} else if (value.type() == ox::ModelValue::Type::Vector) {
const auto &vec = value.get<ox::ModelValueVector>();
const auto pathStr = ox::join<Str>("##", *path).unwrap();
const auto lbl = ox::sfmt<Str>("{}##{}", name, pathStr);
const auto flags = ImGuiTreeNodeFlags_SpanFullWidth
| ImGuiTreeNodeFlags_OpenOnArrow
| (vec.size() ? 0 : ImGuiTreeNodeFlags_Leaf)
| (false ? ImGuiTreeNodeFlags_Selected : 0);
const auto open = ImGui::TreeNodeEx(lbl.c_str(), flags);
ImGui::SameLine();
drawRow(value);
if (open) {
for (auto i = 0lu; const auto &e: vec) {
const auto iStr = ox::sfmt<Str>("{}", i);
path->push_back(iStr);
ImGui::TableNextRow(0, 5);
ImGui::TableNextColumn();
drawVar(path, ox::sfmt<Str>("[{}]", i), e);
path->pop_back();
++i;
}
ImGui::TreePop();
}
} else {
const auto pathStr = ox::join<Str>("##", *path).unwrap();
const auto lbl = ox::sfmt<Str>("{}##{}", name, pathStr);
const auto flags = ImGuiTreeNodeFlags_SpanFullWidth
| ImGuiTreeNodeFlags_OpenOnArrow
| ImGuiTreeNodeFlags_Leaf
| (false ? ImGuiTreeNodeFlags_Selected : 0);
const auto open = ImGui::TreeNodeEx(lbl.c_str(), flags);
ImGui::SameLine();
drawRow(value);
if (open) {
ImGui::TreePop();
}
}
path->pop_back();
}
void ClawEditor::drawTree(ObjPath *path, const ox::ModelObject &obj) noexcept {
using Str = ox::BasicString<100>;
for (const auto &c : obj) {
ImGui::TableNextRow(0, 5);
auto pathStr = ox::join<Str>("##", *path).unwrap();
auto lbl = ox::sfmt<Str>("{}##{}", c->name, pathStr);
const auto rowSelected = false;
const auto hasChildren = c->value.type() == ox::ModelValue::Type::Object
|| c->value.type() == ox::ModelValue::Type::Vector;
const auto flags = ImGuiTreeNodeFlags_SpanFullWidth
| ImGuiTreeNodeFlags_OpenOnArrow
| (hasChildren ? 0 : ImGuiTreeNodeFlags_Leaf)
| (rowSelected ? ImGuiTreeNodeFlags_Selected : 0);
ImGui::TableNextColumn();
if (ImGui::IsItemClicked()) {
//model()->setActiveSubsheet(*path);
}
if (ImGui::IsMouseDoubleClicked(0) && ImGui::IsItemHovered()) {
//showSubsheetEditor();
}
path->push_back(c->name);
if (c->value.type() == ox::ModelValue::Type::Object) {
const auto open = ImGui::TreeNodeEx(lbl.c_str(), flags);
ImGui::SameLine();
drawRow(c->value);
if (open) {
drawTree(path, c->value.get<ox::ModelObject>());
ImGui::TreePop();
}
} else {
drawVar(path, c->name, c->value);
}
path->pop_back();
}
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <ox/model/modelvalue.hpp>
#include <nostalgia/core/context.hpp>
#include "lib/editor.hpp"
namespace nostalgia {
class ClawEditor: public studio::Editor {
private:
using ObjPath = ox::Vector<ox::StringView, 8>;
ox::String m_itemName;
ox::String m_itemDisplayName;
ox::ModelObject m_obj;
public:
ClawEditor(ox::CRStringView path, ox::ModelObject obj) noexcept;
/**
* Returns the name of item being edited.
*/
ox::CRString itemName() const noexcept final;
ox::CRString itemDisplayName() const noexcept final;
void draw(core::Context*) noexcept final;
private:
static void drawRow(const ox::ModelValue &value) noexcept;
void drawVar(ObjPath *path, ox::CRStringView name, const ox::ModelValue &value) noexcept;
void drawTree(ObjPath *path, const ox::ModelObject &obj) noexcept;
};
}

View File

@ -100,6 +100,11 @@ class NOSTALGIASTUDIO_EXPORT BaseEditor: public Widget {
return nullptr;
}
static constexpr auto pathToItemName(ox::CRStringView path) noexcept {
const auto lastSlash = std::find(path.rbegin(), path.rend(), '/').offset();
return path.substr(lastSlash + 1);
}
// signals
public:
ox::Signal<ox::Error(bool)> unsavedChangesChanged;

View File

@ -61,7 +61,7 @@ class NOSTALGIASTUDIO_EXPORT Project {
ox::Error writeObj(const ox::String &path, const T *obj, ox::ClawFormat fmt = ox::ClawFormat::Metal) noexcept;
template<typename T>
ox::Result<ox::UniquePtr<T>> loadObj(const ox::String &path) const noexcept;
ox::Result<T> loadObj(const ox::String &path) const noexcept;
ox::Result<ox::FileStat> stat(const ox::String &path) const noexcept;
@ -126,10 +126,13 @@ ox::Error Project::writeObj(const ox::String &path, const T *obj, ox::ClawFormat
}
template<typename T>
ox::Result<ox::UniquePtr<T>> Project::loadObj(const ox::String &path) const noexcept {
auto obj = ox::make_unique<T>();
ox::Result<T> Project::loadObj(const ox::String &path) const noexcept {
oxRequire(buff, loadBuff(path));
return ox::readClaw<T>(buff);
if constexpr (ox::is_same_v<T, ox::ModelObject>) {
return ox::readClaw(&m_typeStore, buff);
} else {
return ox::readClaw<T>(buff);
}
}
template<typename Functor>

View File

@ -8,6 +8,7 @@
#include "lib/configio.hpp"
#include "builtinmodules.hpp"
#include "clawviewer.hpp"
#include "filedialogmanager.hpp"
#include "studioapp.hpp"
@ -300,11 +301,11 @@ ox::Error StudioUI::openProject(const ox::String &path) noexcept {
return m_projectExplorer->refreshProjectTreeModel();
}
ox::Error StudioUI::openFile(const ox::String &path) noexcept {
ox::Error StudioUI::openFile(ox::CRStringView path) noexcept {
return openFileActiveTab(path, true);
}
ox::Error StudioUI::openFileActiveTab(const ox::String &path, bool makeActiveTab) noexcept {
ox::Error StudioUI::openFileActiveTab(ox::CRStringView path, bool makeActiveTab) noexcept {
if (m_openFiles.contains(path)) {
for (auto &e : m_editors) {
if (makeActiveTab && e->itemName() == path) {
@ -317,17 +318,23 @@ ox::Error StudioUI::openFileActiveTab(const ox::String &path, bool makeActiveTab
}
oxRequire(ext, studio::fileExt(path));
// create Editor
studio::BaseEditor *editor = nullptr;
if (!m_editorMakers.contains(ext)) {
return OxError(1, "There is no editor for this file extension");
}
auto [editor, err] = m_editorMakers[ext](path);
if (err) {
if constexpr(!ox::defines::Debug) {
oxErrf("Could not open Editor: {}\n", toStr(err));
} else {
oxErrf("Could not open Editor: {} ({}:{})\n", err.errCode, err.file, err.line);
auto [obj, err] = m_project->loadObj<ox::ModelObject>(path);
if (err) {
return OxError(1, "There is no editor for this file extension");
}
editor = new ClawEditor(path, std::move(obj));
} else {
const auto err = m_editorMakers[ext](path).moveTo(&editor);
if (err) {
if constexpr(!ox::defines::Debug) {
oxErrf("Could not open Editor: {}\n", toStr(err));
} else {
oxErrf("Could not open Editor: {} ({}:{})\n", err.errCode, err.file, err.line);
}
return err;
}
return err;
}
editor->closed.connect(this, &StudioUI::closeFile);
m_editors.emplace_back(editor);

View File

@ -79,9 +79,9 @@ class StudioUI: public ox::SignalHandler {
ox::Error openProject(const ox::String &path) noexcept;
ox::Error openFile(const ox::String &path) noexcept;
ox::Error openFile(ox::CRStringView path) noexcept;
ox::Error openFileActiveTab(const ox::String &path, bool makeActiveTab) noexcept;
ox::Error openFileActiveTab(ox::CRStringView path, bool makeActiveTab) noexcept;
ox::Error closeFile(const ox::String &path) noexcept;
};