[nostalgia] Add basic support for subsheets

This commit is contained in:
2022-02-26 22:48:18 -06:00
parent 329ecb3266
commit e8a046c2dc
20 changed files with 630 additions and 238 deletions

View File

@@ -2,8 +2,6 @@
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <iostream>
#include <imgui.h>
#include <nostalgia/core/media.hpp>
@@ -32,37 +30,114 @@ void TileSheetEditorImGui::exportFile() {
}
void TileSheetEditorImGui::cut() {
m_tileSheetEditor.cut();
model()->cut();
}
void TileSheetEditorImGui::copy() {
m_tileSheetEditor.copy();
model()->copy();
}
void TileSheetEditorImGui::paste() {
m_tileSheetEditor.paste();
model()->paste();
}
void TileSheetEditorImGui::draw(core::Context*) noexcept {
const auto paneSize = ImGui::GetContentRegionAvail();
const auto tileSheetParentSize = ImVec2(paneSize.x - m_palViewWidth, paneSize.y);
const auto fbSize = geo::Vec2(tileSheetParentSize.x - 16, tileSheetParentSize.y - 16);
ImGui::BeginChild("child1", tileSheetParentSize, true);
drawTileSheet(fbSize);
ImGui::BeginChild("TileSheetView", tileSheetParentSize, true);
{
drawTileSheet(fbSize);
}
ImGui::EndChild();
ImGui::SameLine();
// draw palette/color picker
ImGui::BeginChild("child2", ImVec2(m_palViewWidth - 8, paneSize.y), true);
drawPalettePicker();
ImGui::BeginChild("Controls", ImVec2(m_palViewWidth - 8, paneSize.y), true);
{
// draw palette/color picker
ImGui::BeginChild("child1", ImVec2(m_palViewWidth - 24, paneSize.y / 2.07f), true);
{
drawPalettePicker();
}
ImGui::EndChild();
ImGui::BeginChild("child2", ImVec2(m_palViewWidth - 24, paneSize.y / 2.07f), true);
{
const auto btnSize = ImVec2(18, 18);
if (ImGui::Button("+", btnSize)) {
auto insertOnIdx = model()->activeSubSheetIdx();
const auto &parent = *model()->activeSubSheet();
model()->addSubsheet(insertOnIdx);
insertOnIdx.emplace_back(parent.subsheets.size() - 1);
model()->setActiveSubsheet(insertOnIdx);
}
ImGui::SameLine();
if (ImGui::Button("-", btnSize)) {
const auto &activeSubsheetIdx = model()->activeSubSheetIdx();
if (activeSubsheetIdx.size() > 0) {
model()->rmSubsheet(activeSubsheetIdx);
}
}
TileSheet::SubSheetIdx path;
constexpr auto flags = ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody;
if (ImGui::BeginTable("Subsheets", 3, flags)) {
ImGui::TableSetupColumn("Subsheet", ImGuiTableColumnFlags_NoHide);
ImGui::TableSetupColumn("Columns", ImGuiTableColumnFlags_WidthFixed, 50);
ImGui::TableSetupColumn("Rows", ImGuiTableColumnFlags_WidthFixed, 50);
ImGui::TableHeadersRow();
drawSubsheetSelector(&m_tileSheetEditor.img().subsheet, &path);
ImGui::EndTable();
}
}
ImGui::EndChild();
}
ImGui::EndChild();
}
void TileSheetEditorImGui::drawSubsheetSelector(TileSheet::SubSheet *subsheet, TileSheet::SubSheetIdx *path) noexcept {
ImGui::TableNextRow(0, 5);
using Str = ox::BasicString<100>;
auto pathStr = ox::join<Str>("##", *path).value;
auto lbl = ox::sfmt<Str>("{}##{}", subsheet->name, pathStr);
const auto rowSelected = *path == model()->activeSubSheetIdx();
const auto flags = ImGuiTreeNodeFlags_SpanFullWidth
| ImGuiTreeNodeFlags_OpenOnArrow
| ImGuiTreeNodeFlags_DefaultOpen
| (subsheet->subsheets.size() ? 0 : ImGuiTreeNodeFlags_Leaf)
| (rowSelected ? ImGuiTreeNodeFlags_Selected : 0);
ImGui::TableNextColumn();
const auto open = ImGui::TreeNodeEx(lbl.c_str(), flags);
if (ImGui::IsItemClicked()) {
model()->setActiveSubsheet(*path);
}
if (subsheet->subsheets.size()) {
ImGui::TableNextColumn();
ImGui::Text("--");
ImGui::TableNextColumn();
ImGui::Text("--");
} else {
ImGui::TableNextColumn();
ImGui::Text("%d", subsheet->columns);
ImGui::TableNextColumn();
ImGui::Text("%d", subsheet->rows);
}
if (open) {
for (auto i = 0ul; auto &child : subsheet->subsheets) {
path->push_back(i);
ImGui::PushID(static_cast<int>(i));
drawSubsheetSelector(&child, path);
ImGui::PopID();
path->pop_back();
++i;
}
ImGui::TreePop();
}
}
studio::UndoStack *TileSheetEditorImGui::undoStack() noexcept {
return m_tileSheetEditor.undoStack();
return model()->undoStack();
}
void TileSheetEditorImGui::saveItem() {
const auto err = m_tileSheetEditor.model()->saveFile();
const auto err = model()->saveFile();
if (!err) {
this->setUnsavedChanges(false);
} else {
@@ -143,8 +218,7 @@ void TileSheetEditorImGui::drawPalettePicker() noexcept {
}
}
ox::Error TileSheetEditorImGui::markUnsavedChanges() noexcept {
oxDebug("markUnsavedChanges");
ox::Error TileSheetEditorImGui::markUnsavedChanges(int) noexcept {
setUnsavedChanges(true);
return OxError(0);
}