Files
ox/src/nostalgia/core/studio/tilesheeteditormodel.cpp
T

71 lines
2.4 KiB
C++

/*
* 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) {
oxRequireT(img, readObj<NostalgiaGraphic>(ctx, path.c_str()));
m_img = *img;
oxThrowError(readObj<NostalgiaPalette>(ctx, m_img.defaultPalette).moveTo(&m_pal));
}
void TileSheetEditorModel::draw(const geo::Point &pt, std::size_t palIdx) noexcept {
if (!m_ongoingDrawCommand) {
m_ongoingDrawCommand = new DrawCommand(&m_img, ptToIdx(pt, m_img.columns), palIdx);
m_undoStack.push(m_ongoingDrawCommand);
m_updated = true;
} else {
m_updated = m_ongoingDrawCommand->append(ptToIdx(pt, m_img.columns));
}
}
void TileSheetEditorModel::endDraw() noexcept {
m_ongoingDrawCommand = nullptr;
}
bool TileSheetEditorModel::updated() const noexcept {
return m_updated;
}
void TileSheetEditorModel::ackUpdate() noexcept {
m_updated = false;
}
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);
}
}
}