[nostalgia/core/studio] Decouple core TileSheetEditor from ImGui
This commit is contained in:
115
src/nostalgia/core/studio/tilesheeteditor-imgui.cpp
Normal file
115
src/nostalgia/core/studio/tilesheeteditor-imgui.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <nostalgia/common/point.hpp>
|
||||
#include <nostalgia/core/media.hpp>
|
||||
|
||||
#include "tilesheeteditor-imgui.hpp"
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
TileSheetEditorImGui::TileSheetEditorImGui(Context *ctx, const ox::String &path): m_tileSheetEditor(ctx, path) {
|
||||
m_itemPath = path;
|
||||
const auto lastSlash = std::find(m_itemPath.rbegin(), m_itemPath.rend(), '/').offset();
|
||||
m_itemName = m_itemPath.substr(lastSlash + 1);
|
||||
}
|
||||
|
||||
ox::String TileSheetEditorImGui::itemName() const noexcept {
|
||||
return m_itemPath;
|
||||
}
|
||||
|
||||
ox::String TileSheetEditorImGui::itemDisplayName() const noexcept {
|
||||
return m_itemName;
|
||||
}
|
||||
|
||||
void TileSheetEditorImGui::exportFile() {
|
||||
}
|
||||
|
||||
void TileSheetEditorImGui::cut() {
|
||||
m_tileSheetEditor.cut();
|
||||
}
|
||||
|
||||
void TileSheetEditorImGui::copy() {
|
||||
m_tileSheetEditor.copy();
|
||||
}
|
||||
|
||||
void TileSheetEditorImGui::paste() {
|
||||
m_tileSheetEditor.paste();
|
||||
}
|
||||
|
||||
void TileSheetEditorImGui::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 TileSheetEditorImGui::saveItem() {
|
||||
}
|
||||
|
||||
void TileSheetEditorImGui::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_tileSheetEditor.resize(fbSize);
|
||||
} else if (m_tileSheetEditor.updated()) {
|
||||
m_tileSheetEditor.resize(fbSize);
|
||||
m_tileSheetEditor.ackUpdate();
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
||||
// clear screen and draw
|
||||
glViewport(0, 0, fbSizei.width, fbSizei.height);
|
||||
m_tileSheetEditor.draw();
|
||||
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;
|
||||
m_tileSheetEditor.scrollV(fbSize, wheel, zoomMod);
|
||||
}
|
||||
if (wheelh != 0) {
|
||||
m_tileSheetEditor.scrollH(fbSize, wheelh);
|
||||
}
|
||||
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;
|
||||
m_tileSheetEditor.clickPixel(fbSize, clickPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TileSheetEditorImGui::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_tileSheetEditor.pal().colors) {
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", i);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("(%d, %d, %d)", red16(c), green16(c), blue16(c));
|
||||
++i;
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user