[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
@@ -29,12 +29,16 @@ void TileSheetEditorModel::paste() {
}
void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept {
auto &activeSubSheet = m_img.getSubSheet(m_activeSubsSheetIdx);
const auto idx = ptToIdx(pt, activeSubSheet.columns);
if (idx >= activeSubSheet.pixelCnt(m_img.bpp)) {
return;
}
if (m_ongoingDrawCommand) {
m_updated = m_updated || m_ongoingDrawCommand->append(ptToIdx(pt, m_img.columns()));
m_updated = m_updated || m_ongoingDrawCommand->append(idx);
} else {
const auto idx = ptToIdx(pt, m_img.columns());
if (m_img.getPixel(idx) != palIdx) {
pushCommand(new DrawCommand(&m_img, idx, palIdx));
if (activeSubSheet.getPixel(m_img.bpp, idx) != palIdx) {
pushCommand(new DrawCommand(&m_img, m_activeSubsSheetIdx, idx, palIdx));
}
}
}
@@ -43,12 +47,32 @@ void TileSheetEditorModel::endDrawCommand() noexcept {
m_ongoingDrawCommand = nullptr;
}
void TileSheetEditorModel::addSubsheet(const TileSheet::SubSheetIdx &parentIdx) noexcept {
pushCommand(new AddSubSheetCommand(&m_img, parentIdx));
}
void TileSheetEditorModel::rmSubsheet(const TileSheet::SubSheetIdx &idx) noexcept {
pushCommand(new RmSubSheetCommand(&m_img, idx));
}
void TileSheetEditorModel::setActiveSubsheet(const TileSheet::SubSheetIdx &idx) noexcept {
m_activeSubsSheetIdx = idx;
this->activeSubsheetChanged.emit(m_activeSubsSheetIdx);
}
bool TileSheetEditorModel::updated() const noexcept {
return m_updated;
}
ox::Error TileSheetEditorModel::markUpdated() noexcept {
ox::Error TileSheetEditorModel::markUpdated(int cmdId) noexcept {
m_updated = true;
if (cmdId == CommandId::AddSubSheet || cmdId == CommandId::RmSubSheet) {
// make sure the current active SubSheet is still valid
auto idx = m_img.validateSubSheetIdx(m_activeSubsSheetIdx);
if (idx != m_activeSubsSheetIdx) {
setActiveSubsheet(idx);
}
}
return OxError(0);
}
@@ -63,8 +87,9 @@ ox::Error TileSheetEditorModel::saveFile() noexcept {
}
void TileSheetEditorModel::getFillPixels(bool *pixels, const geo::Point &pt, int oldColor) const noexcept {
const auto tileIdx = [this](const geo::Point &pt) noexcept {
return ptToIdx(pt, img().columns()) / PixelsPerTile;
auto &activeSubSheet = *this->activeSubSheet();
const auto tileIdx = [activeSubSheet](const geo::Point &pt) noexcept {
return ptToIdx(pt, activeSubSheet.columns) / PixelsPerTile;
};
// get points
const auto leftPt = pt + geo::Point(-1, 0);
@@ -72,24 +97,24 @@ void TileSheetEditorModel::getFillPixels(bool *pixels, const geo::Point &pt, int
const auto topPt = pt + geo::Point(0, -1);
const auto bottomPt = pt + geo::Point(0, 1);
// calculate indices
const auto idx = ptToIdx(pt, m_img.columns());
const auto leftIdx = ptToIdx(leftPt, m_img.columns());
const auto rightIdx = ptToIdx(rightPt, m_img.columns());
const auto topIdx = ptToIdx(topPt, m_img.columns());
const auto bottomIdx = ptToIdx(bottomPt, m_img.columns());
const auto idx = ptToIdx(pt, activeSubSheet.columns);
const auto leftIdx = ptToIdx(leftPt, activeSubSheet.columns);
const auto rightIdx = ptToIdx(rightPt, activeSubSheet.columns);
const auto topIdx = ptToIdx(topPt, activeSubSheet.columns);
const auto bottomIdx = ptToIdx(bottomPt, activeSubSheet.columns);
const auto tile = tileIdx(pt);
// mark pixels to update
pixels[idx % PixelsPerTile] = true;
if (!pixels[leftIdx % PixelsPerTile] && tile == tileIdx(leftPt) && m_img.pixels[leftIdx] == oldColor) {
if (!pixels[leftIdx % PixelsPerTile] && tile == tileIdx(leftPt) && activeSubSheet.pixels[leftIdx] == oldColor) {
getFillPixels(pixels, leftPt, oldColor);
}
if (!pixels[rightIdx % PixelsPerTile] && tile == tileIdx(rightPt) && m_img.pixels[rightIdx] == oldColor) {
if (!pixels[rightIdx % PixelsPerTile] && tile == tileIdx(rightPt) && activeSubSheet.pixels[rightIdx] == oldColor) {
getFillPixels(pixels, rightPt, oldColor);
}
if (!pixels[topIdx % PixelsPerTile] && tile == tileIdx(topPt) && m_img.pixels[topIdx] == oldColor) {
if (!pixels[topIdx % PixelsPerTile] && tile == tileIdx(topPt) && activeSubSheet.pixels[topIdx] == oldColor) {
getFillPixels(pixels, topPt, oldColor);
}
if (!pixels[bottomIdx % PixelsPerTile] && tile == tileIdx(bottomPt) && m_img.pixels[bottomIdx] == oldColor) {
if (!pixels[bottomIdx % PixelsPerTile] && tile == tileIdx(bottomPt) && activeSubSheet.pixels[bottomIdx] == oldColor) {
getFillPixels(pixels, bottomPt, oldColor);
}
}