[nostalgia/core/studio] Break out TileSheetEditor application logic
This commit is contained in:
@@ -4,22 +4,19 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <nostalgia/geo/point.hpp>
|
||||
#include <nostalgia/core/consts.hpp>
|
||||
#include <nostalgia/core/media.hpp>
|
||||
#include <nostalgia/core/ptidxconv.hpp>
|
||||
#include <nostalgia/geo/point.hpp>
|
||||
|
||||
#include "tilesheeteditor.hpp"
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
TileSheetEditor::TileSheetEditor(Context *ctx, const ox::String &path) {
|
||||
TileSheetEditor::TileSheetEditor(Context *ctx, const ox::String &path): m_model(ctx, path) {
|
||||
// build shaders
|
||||
oxThrowError(m_pixelsDrawer.buildShader());
|
||||
oxThrowError(m_pixelGridDrawer.buildShader());
|
||||
oxRequireT(img, readObj<NostalgiaGraphic>(ctx, path.c_str()));
|
||||
m_img = std::move(*img);
|
||||
oxThrowError(readObj<NostalgiaPalette>(ctx, m_img.defaultPalette).moveTo(&m_pal));
|
||||
}
|
||||
|
||||
void TileSheetEditor::cut() {
|
||||
@@ -37,13 +34,12 @@ void TileSheetEditor::draw() noexcept {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
m_pixelsDrawer.draw(m_updated, m_scrollOffset);
|
||||
m_pixelGridDrawer.draw(m_updated, m_scrollOffset);
|
||||
m_updated = false;
|
||||
}
|
||||
|
||||
void TileSheetEditor::scrollV(const geo::Vec2 paneSz, float wheel, bool zoomMod) noexcept {
|
||||
void TileSheetEditor::scrollV(const geo::Vec2 &paneSz, float wheel, bool zoomMod) noexcept {
|
||||
const auto pixelSize = m_pixelsDrawer.pixelSize(paneSz);
|
||||
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(m_img.columns) * TileWidth,
|
||||
pixelSize.y * static_cast<float>(m_img.rows) * TileHeight);
|
||||
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(img().columns) * TileWidth,
|
||||
pixelSize.y * static_cast<float>(img().rows) * TileHeight);
|
||||
if (zoomMod) {
|
||||
m_pixelSizeMod = ox::clamp(m_pixelSizeMod + wheel * 0.02f, 0.55f, 2.f);
|
||||
m_pixelsDrawer.setPixelSizeMod(m_pixelSizeMod);
|
||||
@@ -57,10 +53,10 @@ void TileSheetEditor::scrollV(const geo::Vec2 paneSz, float wheel, bool zoomMod)
|
||||
m_scrollOffset.y = ox::clamp(m_scrollOffset.y, 0.f, sheetSize.y / 2);
|
||||
}
|
||||
|
||||
void TileSheetEditor::scrollH(const geo::Vec2 paneSz, float wheelh) noexcept {
|
||||
void TileSheetEditor::scrollH(const geo::Vec2 &paneSz, float wheelh) noexcept {
|
||||
const auto pixelSize = m_pixelsDrawer.pixelSize(paneSz);
|
||||
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(m_img.columns) * TileWidth,
|
||||
pixelSize.y * static_cast<float>(m_img.rows) * TileHeight);
|
||||
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(img().columns) * TileWidth,
|
||||
pixelSize.y * static_cast<float>(img().rows) * TileHeight);
|
||||
m_scrollOffset.x += wheelh * 0.1f;
|
||||
m_scrollOffset.x = ox::clamp(m_scrollOffset.x, -(sheetSize.x / 2), 0.f);
|
||||
}
|
||||
@@ -76,21 +72,13 @@ void TileSheetEditor::clickPixel(const geo::Vec2 &paneSize, const geo::Vec2 &cli
|
||||
y /= pixDrawSz.y;
|
||||
const auto pt = geo::Point(static_cast<int>(x * 2), static_cast<int>(y * 2));
|
||||
const uint8_t palIdx = 0;
|
||||
m_img.setPixel(pt, palIdx);
|
||||
m_model.img().setPixel(pt, palIdx);
|
||||
m_updated = true;
|
||||
}
|
||||
|
||||
void TileSheetEditor::resize(const geo::Vec2 &sz) noexcept {
|
||||
m_pixelsDrawer.initBufferSet(sz, m_img, *m_pal);
|
||||
m_pixelGridDrawer.initBufferSet(sz, m_img);
|
||||
}
|
||||
|
||||
const NostalgiaGraphic &TileSheetEditor::img() const {
|
||||
return m_img;
|
||||
}
|
||||
|
||||
const NostalgiaPalette &TileSheetEditor::pal() const {
|
||||
return *m_pal;
|
||||
m_pixelsDrawer.initBufferSet(sz, img(), pal());
|
||||
m_pixelGridDrawer.initBufferSet(sz, img());
|
||||
}
|
||||
|
||||
bool TileSheetEditor::updated() const noexcept {
|
||||
@@ -104,36 +92,4 @@ void TileSheetEditor::ackUpdate() noexcept {
|
||||
void TileSheetEditor::saveItem() {
|
||||
}
|
||||
|
||||
void TileSheetEditor::getFillPixels(bool *pixels, geo::Point pt, int oldColor) const {
|
||||
const auto tileIdx = [this](const geo::Point &pt) noexcept {
|
||||
return ptToIdx(pt, m_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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user