[nostalgia/core/studio] Decouple core TileSheetEditor from ImGui
This commit is contained in:
@@ -4,8 +4,6 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <nostalgia/common/point.hpp>
|
||||
#include <nostalgia/core/consts.hpp>
|
||||
#include <nostalgia/core/media.hpp>
|
||||
@@ -16,28 +14,14 @@
|
||||
namespace nostalgia::core {
|
||||
|
||||
TileSheetEditor::TileSheetEditor(Context *ctx, const ox::String &path) {
|
||||
m_itemPath = path;
|
||||
const auto lastSlash = std::find(m_itemPath.rbegin(), m_itemPath.rend(), '/').offset();
|
||||
m_itemName = m_itemPath.substr(lastSlash + 1);
|
||||
// build shaders
|
||||
oxThrowError(m_pixelsDrawer.buildShader());
|
||||
oxThrowError(m_pixelGridDrawer.buildShader());
|
||||
oxRequireT(img, readObj<NostalgiaGraphic>(ctx, m_itemPath.c_str()));
|
||||
oxRequireT(img, readObj<NostalgiaGraphic>(ctx, path.c_str()));
|
||||
m_img = std::move(*img);
|
||||
oxThrowError(readObj<NostalgiaPalette>(ctx, m_img.defaultPalette).moveTo(&m_pal));
|
||||
}
|
||||
|
||||
ox::String TileSheetEditor::itemName() const noexcept {
|
||||
return m_itemPath;
|
||||
}
|
||||
|
||||
ox::String TileSheetEditor::itemDisplayName() const noexcept {
|
||||
return m_itemName;
|
||||
}
|
||||
|
||||
void TileSheetEditor::exportFile() {
|
||||
}
|
||||
|
||||
void TileSheetEditor::cut() {
|
||||
}
|
||||
|
||||
@@ -47,29 +31,74 @@ void TileSheetEditor::copy() {
|
||||
void TileSheetEditor::paste() {
|
||||
}
|
||||
|
||||
void TileSheetEditor::draw(core::Context*) noexcept {
|
||||
const auto paneSize = ImGui::GetContentRegionAvail();
|
||||
const auto tileSheetParentSize = ImVec2(paneSize.x - m_palViewWidth, paneSize.y);
|
||||
const auto fbSize = common::Vec2(tileSheetParentSize.x - 16, tileSheetParentSize.y - 16);
|
||||
ImGui::BeginChild("child1", ImVec2(tileSheetParentSize.x, tileSheetParentSize.y), true);
|
||||
drawTileSheet(fbSize);
|
||||
ImGui::EndChild();
|
||||
ImGui::SameLine();
|
||||
// draw palette/color picker
|
||||
ImGui::BeginChild("child2", ImVec2(m_palViewWidth - 8, paneSize.y), true);
|
||||
drawPalettePicker();
|
||||
ImGui::EndChild();
|
||||
}
|
||||
|
||||
void TileSheetEditor::glDraw() noexcept {
|
||||
// clear screen
|
||||
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);
|
||||
m_updated = false;
|
||||
//oxAssert(glGetError() == 0, "There was an OpenGL error");
|
||||
}
|
||||
|
||||
void TileSheetEditor::scrollV(const common::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);
|
||||
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 common::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);
|
||||
m_scrollOffset.x += wheelh * 0.1f;
|
||||
m_scrollOffset.x = ox::clamp(m_scrollOffset.x, -(sheetSize.x / 2), 0.f);
|
||||
}
|
||||
|
||||
void TileSheetEditor::clickPixel(const common::Vec2 &paneSize, const common::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 = common::Point(static_cast<int>(x * 2), static_cast<int>(y * 2));
|
||||
const uint8_t palIdx = 0;
|
||||
m_img.setPixel(pt, palIdx);
|
||||
m_updated = true;
|
||||
}
|
||||
|
||||
void TileSheetEditor::resize(const common::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;
|
||||
}
|
||||
|
||||
bool TileSheetEditor::updated() const noexcept {
|
||||
return m_updated;
|
||||
}
|
||||
|
||||
void TileSheetEditor::ackUpdate() noexcept {
|
||||
m_updated = false;
|
||||
}
|
||||
|
||||
void TileSheetEditor::saveItem() {
|
||||
@@ -107,89 +136,4 @@ void TileSheetEditor::getFillPixels(bool *pixels, common::Point pt, int oldColor
|
||||
}
|
||||
}
|
||||
|
||||
void TileSheetEditor::drawTileSheet(const common::Vec2 &fbSize) noexcept {
|
||||
const auto fbSizei = common::Size(static_cast<int>(fbSize.x), static_cast<int>(fbSize.y));
|
||||
if (m_framebuffer.width != fbSizei.width || m_framebuffer.height != fbSizei.height) {
|
||||
m_framebuffer = glutils::generateFrameBuffer(fbSizei.width, fbSizei.height);
|
||||
m_pixelsDrawer.initBufferSet(common::Vec2(fbSize), m_img, *m_pal);
|
||||
m_pixelGridDrawer.initBufferSet(common::Vec2(fbSize), m_img);
|
||||
} else if (m_updated) {
|
||||
m_pixelsDrawer.initBufferSet(common::Vec2(fbSize), m_img, *m_pal);
|
||||
m_pixelGridDrawer.initBufferSet(common::Vec2(fbSize), m_img);
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
||||
glViewport(0, 0, fbSizei.width, fbSizei.height);
|
||||
glDraw();
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
ImGui::Image(reinterpret_cast<void*>(m_framebuffer.color.id), static_cast<ImVec2>(fbSize), ImVec2(0, 1), ImVec2(1, 0));
|
||||
// handle input, this must come after drawing
|
||||
if (ImGui::IsItemHovered()) {
|
||||
const auto &io = ImGui::GetIO();
|
||||
const auto wheel = io.MouseWheel;
|
||||
const auto wheelh = io.MouseWheelH;
|
||||
if (wheel != 0) {
|
||||
const auto zoomMod = ox::defines::OS == ox::OS::Darwin ? io.KeySuper : io.KeyCtrl;
|
||||
const auto pixelSize = m_pixelsDrawer.pixelSize(common::Vec2(fbSize));
|
||||
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(m_img.columns) * TileWidth,
|
||||
pixelSize.y * static_cast<float>(m_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);
|
||||
}
|
||||
if (wheelh != 0) {
|
||||
const auto pixelSize = m_pixelsDrawer.pixelSize(common::Vec2(fbSize));
|
||||
const ImVec2 sheetSize(pixelSize.x * static_cast<float>(m_img.columns) * TileWidth,
|
||||
pixelSize.y * static_cast<float>(m_img.rows) * TileHeight);
|
||||
m_scrollOffset.x += wheelh * 0.1f;
|
||||
m_scrollOffset.x = ox::clamp(m_scrollOffset.x, -(sheetSize.x / 2), 0.f);
|
||||
}
|
||||
if (io.MouseDown[0]) {
|
||||
auto clickPos = common::Vec2(io.MousePos);
|
||||
const auto &winPos = ImGui::GetWindowPos();
|
||||
clickPos.x -= winPos.x + 10;
|
||||
clickPos.y -= winPos.y + 10;
|
||||
clickPixel(fbSize, clickPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TileSheetEditor::drawPalettePicker() noexcept {
|
||||
// header
|
||||
ImGui::BeginTable("PaletteTable", 2);
|
||||
ImGui::TableSetupColumn("No.", 0, 0.35);
|
||||
ImGui::TableSetupColumn("Color16", 0, 3);
|
||||
ImGui::TableHeadersRow();
|
||||
for (auto i = 1; auto c : m_pal->colors) {
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", i);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("(%d, %d, %d)", red16(c), green16(c), blue16(c));
|
||||
++i;
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
|
||||
void TileSheetEditor::clickPixel(const common::Vec2 &paneSize, const common::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 = common::Point(static_cast<int>(x * 2), static_cast<int>(y * 2));
|
||||
const uint8_t palIdx = 0;
|
||||
m_img.setPixel(pt, palIdx);
|
||||
m_updated = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user