[nostalgia/core/studio] Add zoom support to TileSheetEditor

This commit is contained in:
Gary Talent 2022-02-03 00:24:40 -06:00
parent 78942ce21d
commit d4e198ecc3
6 changed files with 49 additions and 12 deletions

View File

@ -2,6 +2,8 @@
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <iostream>
#include <imgui.h>
#include <nostalgia/common/point.hpp>
@ -47,8 +49,10 @@ void TileSheetEditor::paste() {
void TileSheetEditor::draw(core::Context*) noexcept {
const auto size = ImGui::GetContentRegionAvail();
const auto sizei = common::Size(static_cast<int>(size.x), static_cast<int>(size.y));
if (m_framebuffer.width != sizei.width || m_framebuffer.height != sizei.height) {
m_framebuffer = glutils::generateFrameBuffer(sizei.width, sizei.height);
if (m_updated || m_framebuffer.width != sizei.width || m_framebuffer.height != sizei.height) {
if (!m_framebuffer) {
m_framebuffer = glutils::generateFrameBuffer(sizei.width, sizei.height);
}
m_pixels.initBufferSet(m_img, *m_pal);
m_pixelGrid.initBufferSet(m_img);
}
@ -57,6 +61,24 @@ void TileSheetEditor::draw(core::Context*) noexcept {
glDraw();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
ImGui::Image(reinterpret_cast<void*>(m_framebuffer.color.id), size, ImVec2(0, 1), ImVec2(1, 0));
if (ImGui::IsItemHovered()) {
const auto &io = ImGui::GetIO();
const auto wheel = io.MouseWheel * 0.01f;
const auto zoomMod = ox::defines::OS == ox::defines::OS::Darwin ? io.KeySuper : io.KeyCtrl;
if (zoomMod && wheel != 0) {
m_pixelSizeMod = ox::clamp(m_pixelSizeMod + wheel, 0.55f, 2.f);
m_pixels.setPixelSizeMod(m_pixelSizeMod);
m_pixelGrid.setPixelSizeMod(m_pixelSizeMod);
m_updated = true;
}
if (io.MouseClicked[0]) {
auto clickPos = io.MouseClickedPos[0];
const auto &winPos = ImGui::GetWindowPos();
clickPos.x -= winPos.x;
clickPos.y -= winPos.y;
std::cout << clickPos.x - 8 << ", " << clickPos.y - 31 << '\n';
}
}
}
void TileSheetEditor::glDraw() noexcept {

View File

@ -73,9 +73,9 @@ struct TileSheetClipboard {
[[nodiscard]]
bool empty() const;
void pastePixels(common::Point pt, ox::Vector<int> *tgt, int tgtColumns) const;
void pastePixels(const common::Point &pt, ox::Vector<int> *tgt, int tgtColumns) const;
void setPoints(common::Point p1, common::Point p2);
void setPoints(const common::Point &p1, const common::Point &p2);
[[nodiscard]]
common::Point point1() const;
@ -106,6 +106,7 @@ class TileSheetEditor: public studio::Editor {
NostalgiaGraphic m_img;
AssetRef<NostalgiaPalette> m_pal;
Context *m_ctx = nullptr;
float m_pixelSizeMod = 1;
public:
TileSheetEditor(Context *ctx, const ox::String &path);

View File

@ -7,6 +7,10 @@
namespace nostalgia::core {
void TileSheetGrid::setPixelSizeMod(float sm) noexcept {
m_pixelSizeMod = sm;
}
ox::Error TileSheetGrid::buildShader() noexcept {
const auto pixelLineVshad = ox::sfmt(VShad, glutils::GlslVersion);
const auto pixelLineFshad = ox::sfmt(FShad, glutils::GlslVersion);
@ -92,11 +96,11 @@ void TileSheetGrid::setBufferObjects(const NostalgiaGraphic &img, glutils::Buffe
}
}
ImVec2 TileSheetGrid::pixelSize() noexcept {
ImVec2 TileSheetGrid::pixelSize() const noexcept {
const auto [sw, sh] = ImGui::GetContentRegionAvail();
constexpr float ymod = 0.35f / 10.0f;
const auto xmod = ymod * sh / sw;
return {xmod, ymod};
return {xmod * m_pixelSizeMod, ymod * m_pixelSizeMod};
}
}

View File

@ -60,8 +60,11 @@ class TileSheetGrid {
glutils::GLProgram m_shader;
glutils::BufferSet m_bufferSet;
float m_pixelSizeMod = 1;
public:
void setPixelSizeMod(float sm) noexcept;
ox::Error buildShader() noexcept;
void draw(bool update) noexcept;
@ -73,7 +76,7 @@ class TileSheetGrid {
void setBufferObjects(const NostalgiaGraphic &img, glutils::BufferSet *bg) noexcept;
static class ImVec2 pixelSize() noexcept;
class ImVec2 pixelSize() const noexcept;
};

View File

@ -8,6 +8,10 @@
namespace nostalgia::core {
void TileSheetPixels::setPixelSizeMod(float sm) noexcept {
m_pixelSizeMod = sm;
}
ox::Error TileSheetPixels::buildShader() noexcept {
const auto Vshad = ox::sfmt(VShad, glutils::GlslVersion);
const auto Fshad = ox::sfmt(FShad, glutils::GlslVersion);
@ -67,7 +71,7 @@ void TileSheetPixels::setPixelBufferObject(unsigned vertexRow, float x, float y,
}
void TileSheetPixels::setBufferObjects(const NostalgiaGraphic &img, const NostalgiaPalette &pal, glutils::BufferSet *bg) noexcept {
const auto setPixel = [bg, img, pal](std::size_t i, uint8_t p) {
const auto setPixel = [this, bg, img, pal](std::size_t i, uint8_t p) {
const auto color = pal.colors[p];
const auto pt = idxToPt(i, img.columns);
const auto fx = static_cast<float>(pt.x);
@ -98,11 +102,11 @@ void TileSheetPixels::setBufferObjects(const NostalgiaGraphic &img, const Nostal
}
}
ImVec2 TileSheetPixels::pixelSize() noexcept {
ImVec2 TileSheetPixels::pixelSize() const noexcept {
const auto [sw, sh] = ImGui::GetContentRegionAvail();
constexpr float ymod = 0.35f / 10.0f;
const auto xmod = ymod * sh / sw;
return {xmod, ymod};
return {xmod * m_pixelSizeMod, ymod * m_pixelSizeMod};
}
}

View File

@ -37,10 +37,13 @@ class TileSheetPixels {
outColor = vec4(fColor, 1.0);
})";
float m_pixelSizeMod = 1;
glutils::GLProgram m_shader;
glutils::BufferSet m_bufferSet;
public:
void setPixelSizeMod(float sm) noexcept;
ox::Error buildShader() noexcept;
void draw(bool update) noexcept;
@ -48,11 +51,11 @@ class TileSheetPixels {
void initBufferSet(const NostalgiaGraphic &img, const NostalgiaPalette &pal) noexcept;
private:
static void setPixelBufferObject(unsigned vertexRow, float x, float y, Color16 color, float *vbo, GLuint *ebo) noexcept;
void setPixelBufferObject(unsigned vertexRow, float x, float y, Color16 color, float *vbo, GLuint *ebo) noexcept;
void setBufferObjects(const NostalgiaGraphic &img, const NostalgiaPalette &pal, glutils::BufferSet *bg) noexcept;
static class ImVec2 pixelSize() noexcept;
class ImVec2 pixelSize() const noexcept;
};