[nostalgia] Split studio::Editor into Editor and BaseEditor

This commit is contained in:
Gary Talent 2022-04-09 16:20:39 -05:00
parent 56964e197a
commit edf4571ff7
8 changed files with 64 additions and 55 deletions

View File

@ -13,7 +13,7 @@ ox::Vector<studio::EditorMaker> Module::editors(core::Context *ctx) noexcept {
return { return {
{ {
{"ng"}, {"ng"},
[ctx](const ox::String &path) -> ox::Result<studio::Editor*> { [ctx](const ox::String &path) -> ox::Result<studio::BaseEditor*> {
try { try {
return new TileSheetEditorImGui(ctx, path); return new TileSheetEditorImGui(ctx, path);
} catch (const ox::Exception &ex) { } catch (const ox::Exception &ex) {
@ -23,7 +23,7 @@ ox::Vector<studio::EditorMaker> Module::editors(core::Context *ctx) noexcept {
}, },
{ {
{"npal"}, {"npal"},
[ctx](const ox::String &path) -> ox::Result<studio::Editor*> { [ctx](const ox::String &path) -> ox::Result<studio::BaseEditor*> {
return PaletteEditorImGui::make(ctx, path); return PaletteEditorImGui::make(ctx, path);
} }
} }

View File

@ -21,11 +21,10 @@ ox::Result<PaletteEditorImGui*> PaletteEditorImGui::make(Context *ctx, const ox:
out->m_itemName = out->m_itemPath.substr(lastSlash + 1); out->m_itemName = out->m_itemPath.substr(lastSlash + 1);
oxRequire(pal, core::readObj<Palette>(out->m_ctx, out->m_itemPath)); oxRequire(pal, core::readObj<Palette>(out->m_ctx, out->m_itemPath));
out->m_pal = *pal.get(); out->m_pal = *pal.get();
out->undoStack()->changeTriggered.connect(out.get(), &PaletteEditorImGui::markUnsavedChanges);
return out.release(); return out.release();
} }
const ox::String &PaletteEditorImGui::itemName() const { const ox::String &PaletteEditorImGui::itemName() const noexcept {
return m_itemPath; return m_itemPath;
} }
@ -33,10 +32,6 @@ const ox::String &PaletteEditorImGui::itemDisplayName() const noexcept {
return m_itemName; return m_itemName;
} }
studio::UndoStack *PaletteEditorImGui::undoStack() noexcept {
return &m_undoStack;
}
void PaletteEditorImGui::draw(core::Context*) noexcept { void PaletteEditorImGui::draw(core::Context*) noexcept {
static constexpr auto flags = ImGuiTableFlags_RowBg; static constexpr auto flags = ImGuiTableFlags_RowBg;
const auto paneSize = ImGui::GetContentRegionAvail(); const auto paneSize = ImGui::GetContentRegionAvail();
@ -50,20 +45,20 @@ void PaletteEditorImGui::draw(core::Context*) noexcept {
{ {
const auto sz = ImVec2(70, 24); const auto sz = ImVec2(70, 24);
if (ImGui::Button("Add", sz)) { if (ImGui::Button("Add", sz)) {
m_undoStack.push(new AddColorCommand(&m_pal, 0, m_pal.colors.size())); undoStack()->push(new AddColorCommand(&m_pal, 0, m_pal.colors.size()));
} }
ImGui::SameLine(); ImGui::SameLine();
ImGui::BeginDisabled(m_selectedRow >= m_pal.colors.size()); ImGui::BeginDisabled(m_selectedRow >= m_pal.colors.size());
{ {
if (ImGui::Button("Remove", sz)) { if (ImGui::Button("Remove", sz)) {
m_undoStack.push(new RemoveColorCommand(&m_pal, m_pal.colors[static_cast<std::size_t>(m_selectedRow)], m_selectedRow)); undoStack()->push(new RemoveColorCommand(&m_pal, m_pal.colors[static_cast<std::size_t>(m_selectedRow)], m_selectedRow));
m_selectedRow = ox::min(m_pal.colors.size() - 1, m_selectedRow); m_selectedRow = ox::min(m_pal.colors.size() - 1, m_selectedRow);
} }
ImGui::SameLine(); ImGui::SameLine();
ImGui::BeginDisabled(m_selectedRow <= 0); ImGui::BeginDisabled(m_selectedRow <= 0);
{ {
if (ImGui::Button("Move Up", sz)) { if (ImGui::Button("Move Up", sz)) {
m_undoStack.push(new MoveColorCommand(&m_pal, m_selectedRow, -1)); undoStack()->push(new MoveColorCommand(&m_pal, m_selectedRow, -1));
--m_selectedRow; --m_selectedRow;
} }
} }
@ -72,7 +67,7 @@ void PaletteEditorImGui::draw(core::Context*) noexcept {
ImGui::BeginDisabled(m_selectedRow >= m_pal.colors.size() - 1); ImGui::BeginDisabled(m_selectedRow >= m_pal.colors.size() - 1);
{ {
if (ImGui::Button("Move Down", sz)) { if (ImGui::Button("Move Down", sz)) {
m_undoStack.push(new MoveColorCommand(&m_pal, m_selectedRow, 1)); undoStack()->push(new MoveColorCommand(&m_pal, m_selectedRow, 1));
++m_selectedRow; ++m_selectedRow;
} }
} }
@ -132,7 +127,7 @@ void PaletteEditorImGui::draw(core::Context*) noexcept {
ImGui::InputInt("Blue", &b, 1, 5); ImGui::InputInt("Blue", &b, 1, 5);
const auto newColor = color16(r, g, b, a); const auto newColor = color16(r, g, b, a);
if (c != newColor) { if (c != newColor) {
m_undoStack.push(new UpdateColorCommand(&m_pal, m_selectedRow, c, newColor)); undoStack()->push(new UpdateColorCommand(&m_pal, m_selectedRow, c, newColor));
} }
} }
ImGui::EndChild(); ImGui::EndChild();
@ -148,9 +143,4 @@ ox::Error PaletteEditorImGui::saveItem() noexcept {
return OxError(0); return OxError(0);
} }
ox::Error PaletteEditorImGui::markUnsavedChanges(int) noexcept {
setUnsavedChanges(true);
return OxError(0);
}
} }

View File

@ -17,7 +17,6 @@ class PaletteEditorImGui: public studio::Editor {
ox::String m_itemPath; ox::String m_itemPath;
Palette m_pal; Palette m_pal;
std::size_t m_selectedRow = 0; std::size_t m_selectedRow = 0;
studio::UndoStack m_undoStack;
PaletteEditorImGui() noexcept = default; PaletteEditorImGui() noexcept = default;
@ -27,19 +26,14 @@ class PaletteEditorImGui: public studio::Editor {
/** /**
* Returns the name of item being edited. * Returns the name of item being edited.
*/ */
const ox::String &itemName() const override; const ox::String &itemName() const noexcept final;
const ox::String &itemDisplayName() const noexcept override; const ox::String &itemDisplayName() const noexcept final;
void draw(core::Context*) noexcept override; void draw(core::Context*) noexcept final;
protected: protected:
studio::UndoStack *undoStack() noexcept final; ox::Error saveItem() noexcept final;
ox::Error saveItem() noexcept override;
private:
ox::Error markUnsavedChanges(int) noexcept;
}; };

View File

@ -23,7 +23,7 @@ enum class Tool {
Select, Select,
}; };
class TileSheetEditorImGui: public studio::Editor { class TileSheetEditorImGui: public studio::BaseEditor {
private: private:
class SubSheetEditor { class SubSheetEditor {

View File

@ -8,27 +8,27 @@
namespace nostalgia::studio { namespace nostalgia::studio {
const ox::String &Editor::itemDisplayName() const { const ox::String &BaseEditor::itemDisplayName() const noexcept {
return itemName(); return itemName();
} }
void Editor::cut() { void BaseEditor::cut() {
} }
void Editor::copy() { void BaseEditor::copy() {
} }
void Editor::paste() { void BaseEditor::paste() {
} }
void Editor::exportFile() { void BaseEditor::exportFile() {
} }
void Editor::close() { void BaseEditor::close() {
this->closed.emit(itemName()); this->closed.emit(itemName());
} }
void Editor::save() noexcept { void BaseEditor::save() noexcept {
const auto err = saveItem(); const auto err = saveItem();
if (!err) { if (!err) {
setUnsavedChanges(false); setUnsavedChanges(false);
@ -37,52 +37,61 @@ void Editor::save() noexcept {
} }
} }
void Editor::setUnsavedChanges(bool uc) { void BaseEditor::setUnsavedChanges(bool uc) {
m_unsavedChanges = uc; m_unsavedChanges = uc;
unsavedChangesChanged.emit(uc); unsavedChangesChanged.emit(uc);
} }
bool Editor::unsavedChanges() const noexcept { bool BaseEditor::unsavedChanges() const noexcept {
return m_unsavedChanges; return m_unsavedChanges;
} }
void Editor::setExportable(bool exportable) { void BaseEditor::setExportable(bool exportable) {
m_exportable = exportable; m_exportable = exportable;
exportableChanged.emit(exportable); exportableChanged.emit(exportable);
} }
bool Editor::exportable() const { bool BaseEditor::exportable() const {
return m_exportable; return m_exportable;
} }
void Editor::setCutEnabled(bool v) { void BaseEditor::setCutEnabled(bool v) {
m_cutEnabled = v; m_cutEnabled = v;
cutEnabledChanged.emit(v); cutEnabledChanged.emit(v);
} }
bool Editor::cutEnabled() const { bool BaseEditor::cutEnabled() const {
return m_cutEnabled; return m_cutEnabled;
} }
void Editor::setCopyEnabled(bool v) { void BaseEditor::setCopyEnabled(bool v) {
m_copyEnabled = v; m_copyEnabled = v;
copyEnabledChanged.emit(v); copyEnabledChanged.emit(v);
} }
bool Editor::copyEnabled() const { bool BaseEditor::copyEnabled() const {
return m_copyEnabled; return m_copyEnabled;
} }
void Editor::setPasteEnabled(bool v) { void BaseEditor::setPasteEnabled(bool v) {
m_pasteEnabled = v; m_pasteEnabled = v;
pasteEnabledChanged.emit(v); pasteEnabledChanged.emit(v);
} }
bool Editor::pasteEnabled() const { bool BaseEditor::pasteEnabled() const {
return m_pasteEnabled; return m_pasteEnabled;
} }
ox::Error Editor::saveItem() noexcept { ox::Error BaseEditor::saveItem() noexcept {
return OxError(0);
}
Editor::Editor() noexcept {
m_undoStack.changeTriggered.connect(this, &Editor::markUnsavedChanges);
}
ox::Error Editor::markUnsavedChanges(int) noexcept {
setUnsavedChanges(true);
return OxError(0); return OxError(0);
} }

View File

@ -17,7 +17,7 @@ class StudioUI;
namespace nostalgia::studio { namespace nostalgia::studio {
class NOSTALGIASTUDIO_EXPORT Editor: public Widget { class NOSTALGIASTUDIO_EXPORT BaseEditor: public Widget {
friend StudioUI; friend StudioUI;
@ -29,16 +29,16 @@ class NOSTALGIASTUDIO_EXPORT Editor: public Widget {
bool m_pasteEnabled = false; bool m_pasteEnabled = false;
public: public:
~Editor() override = default; ~BaseEditor() override = default;
/** /**
* Returns the name of item being edited. * Returns the name of item being edited.
*/ */
[[nodiscard]] [[nodiscard]]
virtual const ox::String &itemName() const = 0; virtual const ox::String &itemName() const noexcept = 0;
[[nodiscard]] [[nodiscard]]
virtual const ox::String &itemDisplayName() const; virtual const ox::String &itemDisplayName() const noexcept;
virtual void cut(); virtual void cut();
@ -109,4 +109,20 @@ class NOSTALGIASTUDIO_EXPORT Editor: public Widget {
}; };
class Editor: public studio::BaseEditor {
private:
studio::UndoStack m_undoStack;
public:
Editor() noexcept;
UndoStack *undoStack() noexcept final {
return &m_undoStack;
}
private:
ox::Error markUnsavedChanges(int) noexcept;
};
} }

View File

@ -14,7 +14,7 @@
namespace nostalgia::studio { namespace nostalgia::studio {
struct EditorMaker { struct EditorMaker {
using Func = std::function<ox::Result<class Editor*>(const ox::String&)>; using Func = std::function<ox::Result<class BaseEditor*>(const ox::String&)>;
ox::Vector<ox::String> fileTypes; ox::Vector<ox::String> fileTypes;
Func make; Func make;
}; };

View File

@ -24,13 +24,13 @@ class StudioUI: public ox::SignalHandler {
ox::UniquePtr<studio::Project> m_project; ox::UniquePtr<studio::Project> m_project;
studio::TaskRunner m_taskRunner; studio::TaskRunner m_taskRunner;
core::Context *m_ctx = nullptr; core::Context *m_ctx = nullptr;
ox::Vector<ox::UniquePtr<studio::Editor>> m_editors; ox::Vector<ox::UniquePtr<studio::BaseEditor>> m_editors;
ox::Vector<ox::UniquePtr<studio::Widget>> m_widgets; ox::Vector<ox::UniquePtr<studio::Widget>> m_widgets;
ox::HashMap<ox::String, studio::EditorMaker::Func> m_editorMakers; ox::HashMap<ox::String, studio::EditorMaker::Func> m_editorMakers;
ProjectExplorer *m_projectExplorer = nullptr; ProjectExplorer *m_projectExplorer = nullptr;
ox::Vector<ox::String> m_openFiles; ox::Vector<ox::String> m_openFiles;
studio::Editor *m_activeEditor = nullptr; studio::BaseEditor *m_activeEditor = nullptr;
studio::Editor *m_activeEditorUpdatePending = nullptr; studio::BaseEditor *m_activeEditorUpdatePending = nullptr;
bool m_saveEnabled = false; bool m_saveEnabled = false;
bool m_aboutEnabled = false; bool m_aboutEnabled = false;
bool m_showProjectExplorer = true; bool m_showProjectExplorer = true;