139 lines
4.3 KiB
C++
139 lines
4.3 KiB
C++
/*
|
|
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
|
*/
|
|
|
|
#include <iostream>
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <nostalgia/core/media.hpp>
|
|
#include <nostalgia/geo/point.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 = geo::Vec2(tileSheetParentSize.x - 16, tileSheetParentSize.y - 16);
|
|
ImGui::BeginChild("child1", tileSheetParentSize, true);
|
|
drawTileSheet(fbSize);
|
|
ImGui::EndChild();
|
|
ImGui::SameLine();
|
|
// draw palette/color picker
|
|
ImGui::BeginChild("child2", ImVec2(m_palViewWidth - 8, paneSize.y), true);
|
|
drawPalettePicker();
|
|
ImGui::EndChild();
|
|
}
|
|
|
|
studio::UndoStack *TileSheetEditorImGui::undoStack() noexcept {
|
|
return m_tileSheetEditor.undoStack();
|
|
}
|
|
|
|
void TileSheetEditorImGui::saveItem() {
|
|
}
|
|
|
|
void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept {
|
|
const auto fbSizei = geo::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.resizeView(fbSize);
|
|
} else if (m_tileSheetEditor.updated()) {
|
|
m_tileSheetEditor.resizeView(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
|
|
const auto &io = ImGui::GetIO();
|
|
const auto mousePos = geo::Vec2(io.MousePos);
|
|
if (ImGui::IsItemHovered()) {
|
|
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] && m_prevMouseDownPos != mousePos) {
|
|
m_prevMouseDownPos = mousePos;
|
|
auto clickPos = mousePos;
|
|
const auto &winPos = ImGui::GetWindowPos();
|
|
clickPos.x -= winPos.x + 10;
|
|
clickPos.y -= winPos.y + 10;
|
|
m_tileSheetEditor.click(fbSize, clickPos);
|
|
}
|
|
}
|
|
if (io.MouseReleased[0]) {
|
|
m_prevMouseDownPos = {-1, -1};
|
|
m_tileSheetEditor.releaseMouseButton();
|
|
}
|
|
}
|
|
|
|
void TileSheetEditorImGui::drawPalettePicker() noexcept {
|
|
// header
|
|
if (ImGui::BeginTable("PaletteTable", 3, ImGuiTableFlags_RowBg)) {
|
|
ImGui::TableSetupColumn("No.", 0, 0.45);
|
|
ImGui::TableSetupColumn("", 0, 0.22);
|
|
ImGui::TableSetupColumn("Color16", 0, 3);
|
|
ImGui::TableHeadersRow();
|
|
for (auto i = 0u; auto c: m_tileSheetEditor.pal().colors) {
|
|
ImGui::PushID(static_cast<int>(i));
|
|
// Column: color idx
|
|
ImGui::TableNextColumn();
|
|
const auto label = ox::BString<8>() + (i + 1);
|
|
const auto rowSelected = i == m_tileSheetEditor.palIdx();
|
|
if (ImGui::Selectable(label.c_str(), rowSelected, ImGuiSelectableFlags_SpanAllColumns)) {
|
|
m_tileSheetEditor.setPalIdx(i);
|
|
}
|
|
// Column: color RGB
|
|
ImGui::TableNextColumn();
|
|
auto ic = ImGui::GetColorU32(ImVec4(redf(c), greenf(c), bluef(c), 1));
|
|
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ic);
|
|
ImGui::TableNextColumn();
|
|
ImGui::Text("(%02d, %02d, %02d)", red16(c), green16(c), blue16(c));
|
|
ImGui::TableNextRow();
|
|
ImGui::PopID();
|
|
++i;
|
|
}
|
|
ImGui::EndTable();
|
|
}
|
|
}
|
|
|
|
} |