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

96 lines
2.8 KiB
C++

/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <iostream>
#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): m_model(ctx, path) {
// build shaders
oxThrowError(m_pixelsDrawer.buildShader());
oxThrowError(m_pixelGridDrawer.buildShader());
}
void TileSheetEditor::cut() {
}
void TileSheetEditor::copy() {
}
void TileSheetEditor::paste() {
}
void TileSheetEditor::draw() noexcept {
constexpr Color32 bgColor = 0x717d7e;
glClearColor(redf(bgColor), greenf(bgColor), bluef(bgColor), 1.f);
glClear(GL_COLOR_BUFFER_BIT);
m_pixelsDrawer.draw(m_updated, m_scrollOffset);
m_pixelGridDrawer.draw(m_updated, m_scrollOffset);
}
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>(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);
m_pixelGridDrawer.setPixelSizeMod(m_pixelSizeMod);
m_updated = true;
} else {
m_scrollOffset.y -= wheel * 0.1f;
}
// adjust scroll offset in both cases because the image can be zoomed
// or scrolled off screen
m_scrollOffset.y = ox::clamp(m_scrollOffset.y, 0.f, sheetSize.y / 2);
}
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>(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);
}
void TileSheetEditor::clickPixel(const geo::Vec2 &paneSize, const geo::Vec2 &clickPos) noexcept {
auto [x, y] = clickPos;
const auto pixDrawSz = m_pixelsDrawer.pixelSize(paneSize);
x /= paneSize.x;
y /= paneSize.y;
x += -m_scrollOffset.x / 2;
y += m_scrollOffset.y / 2;
x /= pixDrawSz.x;
y /= pixDrawSz.y;
const auto pt = geo::Point(static_cast<int>(x * 2), static_cast<int>(y * 2));
const uint8_t palIdx = 0;
m_model.img().setPixel(pt, palIdx);
m_updated = true;
}
void TileSheetEditor::resize(const geo::Vec2 &sz) noexcept {
m_pixelsDrawer.initBufferSet(sz, img(), pal());
m_pixelGridDrawer.initBufferSet(sz, img());
}
bool TileSheetEditor::updated() const noexcept {
return m_updated;
}
void TileSheetEditor::ackUpdate() noexcept {
m_updated = false;
}
void TileSheetEditor::saveItem() {
}
}