[nostalgia/core/studio] Break out TileSheetEditor application logic

This commit is contained in:
2022-02-13 11:17:57 -06:00
parent 320df614a9
commit 8774f1c062
10 changed files with 246 additions and 149 deletions
@@ -0,0 +1,50 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <nostalgia/core/media.hpp>
#include "tilesheeteditormodel.hpp"
namespace nostalgia::core {
TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path) {
// build shaders
oxRequireT(img, readObj<NostalgiaGraphic>(ctx, path.c_str()));
m_img = std::move(*img);
oxThrowError(readObj<NostalgiaPalette>(ctx, m_img.defaultPalette).moveTo(&m_pal));
}
void TileSheetEditorModel::getFillPixels(bool *pixels, geo::Point pt, int oldColor) const noexcept {
const auto tileIdx = [this](const geo::Point &pt) noexcept {
return ptToIdx(pt, img().columns) / PixelsPerTile;
};
// get points
const auto leftPt = pt + geo::Point(-1, 0);
const auto rightPt = pt + geo::Point(1, 0);
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 tile = tileIdx(pt);
// mark pixels to update
pixels[idx % PixelsPerTile] = true;
if (!pixels[leftIdx % PixelsPerTile] && tile == tileIdx(leftPt) && m_img.pixels[leftIdx] == oldColor) {
getFillPixels(pixels, leftPt, oldColor);
}
if (!pixels[rightIdx % PixelsPerTile] && tile == tileIdx(rightPt) && m_img.pixels[rightIdx] == oldColor) {
getFillPixels(pixels, rightPt, oldColor);
}
if (!pixels[topIdx % PixelsPerTile] && tile == tileIdx(topPt) && m_img.pixels[topIdx] == oldColor) {
getFillPixels(pixels, topPt, oldColor);
}
if (!pixels[bottomIdx % PixelsPerTile] && tile == tileIdx(bottomPt) && m_img.pixels[bottomIdx] == oldColor) {
getFillPixels(pixels, bottomPt, oldColor);
}
}
}