[nostalgia/core/studio] Fix Subsheet width to update properly
All checks were successful
Build / build (push) Successful in 2m30s

This commit is contained in:
Gary Talent 2024-05-23 20:52:55 -05:00
parent 9105b1ece6
commit 37e65ab0a6
3 changed files with 61 additions and 25 deletions

View File

@ -222,6 +222,14 @@ ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) n
[[nodiscard]] [[nodiscard]]
unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept; unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept;
/**
*
* @param ss
* @param pBpp
* @param sz size of Subsheet in tiles (not pixels)
*/
ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const&sz) noexcept;
/** /**
* validateSubSheetIdx takes a SubSheetIdx and moves the index to the * validateSubSheetIdx takes a SubSheetIdx and moves the index to the
* preceding or parent sheet if the current corresponding sheet does * preceding or parent sheet if the current corresponding sheet does

View File

@ -23,9 +23,7 @@ core::UpdateSubSheetCommand::UpdateSubSheetCommand(
void UpdateSubSheetCommand::redo() noexcept { void UpdateSubSheetCommand::redo() noexcept {
auto &sheet = getSubSheet(m_img, m_idx); auto &sheet = getSubSheet(m_img, m_idx);
sheet.name = m_newName; sheet.name = m_newName;
sheet.columns = m_newCols; oxLogError(resizeSubsheet(sheet, m_img.bpp, {m_newCols, m_newRows}));
sheet.rows = m_newRows;
oxLogError(setPixelCount(sheet, m_img.bpp, static_cast<std::size_t>(PixelsPerTile * m_newCols * m_newRows)));
} }
void UpdateSubSheetCommand::undo() noexcept { void UpdateSubSheetCommand::undo() noexcept {

View File

@ -10,16 +10,16 @@
namespace nostalgia::core { namespace nostalgia::core {
std::size_t idx(TileSheet::SubSheet const&ss, ox::Point const&pt) noexcept { std::size_t idx(TileSheet::SubSheet const &ss, ox::Point const &pt) noexcept {
return ptToIdx(pt, ss.columns); return ptToIdx(pt, ss.columns);
} }
[[nodiscard]] [[nodiscard]]
static TileSheet::SubSheet const*getSubsheet(TileSheet::SubSheet const&ss, SubSheetId const id) noexcept { static TileSheet::SubSheet const *getSubsheet(TileSheet::SubSheet const &ss, SubSheetId const id) noexcept {
if (ss.id == id) { if (ss.id == id) {
return &ss; return &ss;
} }
for (auto const&child : ss.subsheets) { for (auto const &child: ss.subsheets) {
if (auto out = getSubsheet(child, id)) { if (auto out = getSubsheet(child, id)) {
return out; return out;
} }
@ -28,33 +28,33 @@ static TileSheet::SubSheet const*getSubsheet(TileSheet::SubSheet const&ss, SubSh
} }
[[nodiscard]] [[nodiscard]]
static size_t getTileCnt(TileSheet::SubSheet const&ss, int const bpp) noexcept { static size_t getTileCnt(TileSheet::SubSheet const &ss, int const bpp) noexcept {
if (ss.subsheets.empty()) { if (ss.subsheets.empty()) {
auto const bytesPerTile = bpp == 4 ? 32u : 64u; auto const bytesPerTile = bpp == 4 ? 32u : 64u;
return ss.pixels.size() / bytesPerTile; return ss.pixels.size() / bytesPerTile;
} else { } else {
size_t out{}; size_t out{};
for (auto const&child : ss.subsheets) { for (auto const &child: ss.subsheets) {
out += getTileCnt(child, bpp); out += getTileCnt(child, bpp);
} }
return out; return out;
} }
} }
size_t getTileCnt(TileSheet const&ts) noexcept { size_t getTileCnt(TileSheet const &ts) noexcept {
return getTileCnt(ts.subsheet, ts.bpp); return getTileCnt(ts.subsheet, ts.bpp);
} }
TileSheet::SubSheet const*getSubsheet(TileSheet const&ts, SubSheetId const id) noexcept { TileSheet::SubSheet const *getSubsheet(TileSheet const &ts, SubSheetId const id) noexcept {
return getSubsheet(ts.subsheet, id); return getSubsheet(ts.subsheet, id);
} }
static ox::Optional<size_t> getPixelIdx( static ox::Optional<size_t> getPixelIdx(
TileSheet::SubSheet const&ss, TileSheet::SubSheet const &ss,
SubSheetId const id, SubSheetId const id,
size_t idx, size_t idx,
int8_t const bpp) noexcept { int8_t const bpp) noexcept {
for (auto const&child : ss.subsheets) { for (auto const &child: ss.subsheets) {
if (child.id == id) { if (child.id == id) {
return ox::Optional<size_t>(ox::in_place, idx); return ox::Optional<size_t>(ox::in_place, idx);
} }
@ -66,12 +66,12 @@ static ox::Optional<size_t> getPixelIdx(
return {}; return {};
} }
size_t getTileIdx(TileSheet const&ts, SubSheetId const id) noexcept { size_t getTileIdx(TileSheet const &ts, SubSheetId const id) noexcept {
auto const out = getPixelIdx(ts.subsheet, id, 0, ts.bpp); auto const out = getPixelIdx(ts.subsheet, id, 0, ts.bpp);
return out.or_value(0) / PixelsPerTile; return out.or_value(0) / PixelsPerTile;
} }
uint8_t getPixel4Bpp(TileSheet::SubSheet const&ss, std::size_t idx) noexcept { uint8_t getPixel4Bpp(TileSheet::SubSheet const &ss, std::size_t idx) noexcept {
if (idx & 1) { if (idx & 1) {
return ss.pixels[idx / 2] >> 4; return ss.pixels[idx / 2] >> 4;
} else { } else {
@ -79,11 +79,11 @@ uint8_t getPixel4Bpp(TileSheet::SubSheet const&ss, std::size_t idx) noexcept {
} }
} }
uint8_t getPixel8Bpp(TileSheet::SubSheet const&ss, std::size_t idx) noexcept { uint8_t getPixel8Bpp(TileSheet::SubSheet const &ss, std::size_t idx) noexcept {
return ss.pixels[idx]; return ss.pixels[idx];
} }
uint8_t getPixel(TileSheet::SubSheet const&ss, int8_t pBpp, std::size_t idx) noexcept { uint8_t getPixel(TileSheet::SubSheet const &ss, int8_t pBpp, std::size_t idx) noexcept {
if (pBpp == 4) { if (pBpp == 4) {
return getPixel4Bpp(ss, idx); return getPixel4Bpp(ss, idx);
} else { } else {
@ -91,23 +91,23 @@ uint8_t getPixel(TileSheet::SubSheet const&ss, int8_t pBpp, std::size_t idx) noe
} }
} }
uint8_t getPixel4Bpp(TileSheet::SubSheet const&ss, ox::Point const&pt) noexcept { uint8_t getPixel4Bpp(TileSheet::SubSheet const &ss, ox::Point const &pt) noexcept {
const auto idx = ptToIdx(pt, ss.columns); const auto idx = ptToIdx(pt, ss.columns);
return getPixel4Bpp(ss, idx); return getPixel4Bpp(ss, idx);
} }
uint8_t getPixel8Bpp(TileSheet::SubSheet const&ss, ox::Point const&pt) noexcept { uint8_t getPixel8Bpp(TileSheet::SubSheet const &ss, ox::Point const &pt) noexcept {
const auto idx = ptToIdx(pt, ss.columns); const auto idx = ptToIdx(pt, ss.columns);
return getPixel8Bpp(ss, idx); return getPixel8Bpp(ss, idx);
} }
uint8_t getPixel(TileSheet::SubSheet const&ss, int8_t pBpp, ox::Point const&pt) noexcept { uint8_t getPixel(TileSheet::SubSheet const &ss, int8_t pBpp, ox::Point const &pt) noexcept {
const auto idx = ptToIdx(pt, ss.columns); const auto idx = ptToIdx(pt, ss.columns);
return getPixel(ss, pBpp, idx); return getPixel(ss, pBpp, idx);
} }
void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, uint64_t idx, uint8_t palIdx) noexcept { static void setPixel(ox::Vector<uint8_t> &pixels, int8_t pBpp, uint64_t idx, uint8_t palIdx) noexcept {
auto &pixel = ss.pixels[static_cast<std::size_t>(idx / 2)]; auto &pixel = pixels[static_cast<std::size_t>(idx / 2)];
if (pBpp == 4) { if (pBpp == 4) {
if (idx & 1) { if (idx & 1) {
pixel = static_cast<uint8_t>((pixel & 0b0000'1111) | (palIdx << 4)); pixel = static_cast<uint8_t>((pixel & 0b0000'1111) | (palIdx << 4));
@ -119,29 +119,59 @@ void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, uint64_t idx, uint8_t palIdx
} }
} }
void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, ox::Point const&pt, uint8_t palIdx) noexcept { void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, uint64_t idx, uint8_t palIdx) noexcept {
setPixel(ss.pixels, pBpp, idx, palIdx);
}
static void setPixel(ox::Vector<uint8_t> &pixels, int columns, int8_t pBpp, ox::Point const &pt, uint8_t palIdx) noexcept {
const auto idx = ptToIdx(pt, columns);
setPixel(pixels, pBpp, idx, palIdx);
}
void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, ox::Point const &pt, uint8_t palIdx) noexcept {
const auto idx = ptToIdx(pt, ss.columns); const auto idx = ptToIdx(pt, ss.columns);
setPixel(ss, pBpp, idx, palIdx); setPixel(ss, pBpp, idx, palIdx);
} }
ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) noexcept { static ox::Error setPixelCount(ox::Vector<uint8_t> &pixels, int8_t pBpp, std::size_t cnt) noexcept {
switch (pBpp) { switch (pBpp) {
case 4: case 4:
ss.pixels.resize(cnt / 2); pixels.resize(cnt / 2);
return OxError(0); return OxError(0);
case 8: case 8:
ss.pixels.resize(cnt); pixels.resize(cnt);
return OxError(0); return OxError(0);
default: default:
return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount"); return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount");
} }
} }
ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) noexcept {
return setPixelCount(ss.pixels, pBpp, cnt);
}
unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept { unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept {
const auto pixelsSize = static_cast<unsigned>(ss.pixels.size()); const auto pixelsSize = static_cast<unsigned>(ss.pixels.size());
return pBpp == 4 ? pixelsSize * 2 : pixelsSize; return pBpp == 4 ? pixelsSize * 2 : pixelsSize;
} }
ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const &sz) noexcept {
ox::Vector<uint8_t> out;
oxReturnError(setPixelCount(out, pBpp, static_cast<size_t>(sz.width * sz.height) * PixelsPerTile));
auto const w = sz.width * TileWidth;
auto const h = sz.height * TileHeight;
for (auto x = 0; x < w; ++x) {
for (auto y = 0; y < h; ++y) {
auto const palIdx = getPixel(ss, pBpp, {x, y});
setPixel(out, sz.width, pBpp, {x, y}, palIdx);
}
}
ss.columns = sz.width;
ss.rows = sz.height;
ss.pixels = std::move(out);
return {};
}
ox::Result<ox::StringView> getNameFor(TileSheet::SubSheet const&ss, SubSheetId pId) noexcept { ox::Result<ox::StringView> getNameFor(TileSheet::SubSheet const&ss, SubSheetId pId) noexcept {
if (ss.id == pId) { if (ss.id == pId) {
return ox::StringView(ss.name); return ox::StringView(ss.name);