/*
 * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include <imgui.h>

#include <ox/std/point.hpp>
#include <keel/media.hpp>
#include <studio/studio.hpp>

#include <nostalgia/gfx/studio.hpp>

#include "../subcommands/export-tilesheet/export-tilesheet.hpp"
#include "tilesheeteditor-imgui.hpp"

namespace nostalgia::gfx {

namespace ig = studio::ig;

struct SubSheetRef {
	static constexpr auto TypeName = "nostalgia.gfx.studio.SubSheetRef";
	static constexpr auto TypeVersion = 1;
	TileSheet::SubSheetIdx subsheet{};
};

static OX_MODEL_BEGIN(SubSheetRef)
	OX_MODEL_FIELD(subsheet)
OX_MODEL_END()

struct TileSheetEditorConfig {
	static constexpr auto TypeName = "net.drinkingtea.nostalgia.gfx.studio.TileSheetEditorConfig";
	static constexpr auto TypeVersion = 1;
	TileSheet::SubSheetIdx activeSubsheet{};
};

static OX_MODEL_BEGIN(TileSheetEditorConfig)
	OX_MODEL_FIELD_RENAME(activeSubsheet, active_subsheet)
OX_MODEL_END()

TileSheetEditorImGui::TileSheetEditorImGui(studio::Context &sctx, ox::StringParam path):
		Editor{sctx, std::move(path)},
		m_sctx{sctx},
		m_tctx{m_sctx.tctx},
		m_palPicker{"Palette Chooser", keelCtx(sctx), FileExt_npal},
		m_view{m_sctx, itemPath(), *undoStack()},
		m_model{m_view.model()} {
	// connect signal/slots
	m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet);
	m_exportMenu.inputSubmitted.connect(this, &TileSheetEditorImGui::exportSubsheetToPng);
	// load config
	auto &kctx = keelCtx(m_sctx);
	auto const ip = itemPath();
	oxLogError(studio::headerizeConfigFile<TileSheetEditorConfig>(kctx, ip));
	auto const &config =
		studio::readConfig<TileSheetEditorConfig>(kctx, ip);
	if (config.ok()) {
		m_model.setActiveSubsheet(validateSubSheetIdx(m_model.img(), config.value.activeSubsheet));
	}
}

void TileSheetEditorImGui::exportFile() {
	m_exportMenu.show();
}

void TileSheetEditorImGui::cut() {
	m_model.cut();
}

void TileSheetEditorImGui::copy() {
	m_model.copy();
}

void TileSheetEditorImGui::paste() {
	m_model.paste();
}

bool TileSheetEditorImGui::acceptsClipboardPayload() const noexcept {
	return m_model.acceptsClipboardPayload();
}

void TileSheetEditorImGui::keyStateChanged(turbine::Key const key, bool const down) {
	if (!down) {
		return;
	}
	if (ig::mainWinHasFocus() && key == turbine::Key::Escape) {
		m_subsheetEditor.close();
		m_exportMenu.close();
		m_palPicker.close();
	}
	auto const pal = m_model.pal();
	if (ig::mainWinHasFocus() && !m_palPathFocused) {
		auto const colorCnt = gfx::colorCnt(pal, m_model.palettePage());
		if (key == turbine::Key::Alpha_D) {
			m_tool = TileSheetTool::Draw;
			setCopyEnabled(false);
			setCutEnabled(false);
			setPasteEnabled(false);
			m_model.clearSelection();
		} else if (key == turbine::Key::Alpha_E) {
			m_tool = TileSheetTool::Line;
			setCopyEnabled(false);
			setCutEnabled(false);
			setPasteEnabled(false);
			m_model.clearSelection();
		} else if (key == turbine::Key::Alpha_S) {
			m_tool = TileSheetTool::Select;
			setCopyEnabled(true);
			setCutEnabled(true);
			setPasteEnabled(true);
		} else if (key == turbine::Key::Alpha_F) {
			m_tool = TileSheetTool::Fill;
			setCopyEnabled(false);
			setCutEnabled(false);
			setPasteEnabled(false);
			m_model.clearSelection();
		} else if (key >= turbine::Key::Num_1 && key <= turbine::Key::Num_9) {
			if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) {
				auto const idx = ox::min<std::size_t>(
						static_cast<uint32_t>(key - turbine::Key::Num_1),
						m_model.pal().pages.size() - 1);
				m_model.setPalettePage(idx);
			} else if (key <= turbine::Key::Num_9) {
				auto const idx = ox::min<std::size_t>(
						static_cast<uint32_t>(key - turbine::Key::Num_1),
						colorCnt - 1);
				m_view.setPalIdx(idx);
			}
		} else if (key == turbine::Key::Num_0) {
			if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) {
				auto const idx = ox::min<std::size_t>(
						static_cast<uint32_t>(key - turbine::Key::Num_1 + 9),
						m_model.pal().pages.size() - 1);
				m_model.setPalettePage(idx);
			} else {
				auto const idx = ox::min<std::size_t>(
						static_cast<uint32_t>(key - turbine::Key::Num_1 + 9),
						colorCnt - 1);
				m_view.setPalIdx(idx);
			}
		}
	}
}

void TileSheetEditorImGui::draw(studio::Context&) noexcept {
	setCopyEnabled(m_model.hasSelection());
	setCutEnabled(m_model.hasSelection());
	setPasteEnabled(m_model.hasSelection());
	if (ig::mainWinHasFocus() && m_tool == TileSheetTool::Select) {
		if (ImGui::IsKeyDown(ImGuiKey_ModCtrl) && !m_palPathFocused) {
			if (ImGui::IsKeyPressed(ImGuiKey_A)) {
				auto const &img = m_model.activeSubSheet();
				m_model.setSelection({
					{},
					{img.columns * TileWidth - 1, img.rows * TileHeight - 1}});
			} else if (ImGui::IsKeyPressed(ImGuiKey_G)) {
				m_model.clearSelection();
			}
		}
	}
	auto const scale = ig::dpiScale();
	auto const paneSize = ImGui::GetContentRegionAvail();
	auto const tileSheetParentSize = ImVec2{paneSize.x - s_palViewWidth * scale, paneSize.y};
	auto const fbSize = ox::Vec2{tileSheetParentSize.x - 16 * scale, tileSheetParentSize.y - 16 * scale};
	ImGui::BeginChild("TileSheetView", tileSheetParentSize, true);
	{
		drawTileSheet(fbSize);
	}
	ImGui::EndChild();
	ImGui::SameLine();
	ImGui::BeginChild("Controls", {(s_palViewWidth - 8) * scale, paneSize.y}, true);
	{
		auto const controlsSize = ImGui::GetContentRegionAvail();
		ImGui::BeginChild("ToolBox", {0, 32 * scale}, true);
		{
			auto const btnSz = ImVec2{45 * scale, 14 * scale};
			if (ImGui::Selectable("Select", m_tool == TileSheetTool::Select, 0, btnSz)) {
				m_tool = TileSheetTool::Select;
			}
			ImGui::SameLine();
			if (ImGui::Selectable("Draw", m_tool == TileSheetTool::Draw, 0, btnSz)) {
				m_tool = TileSheetTool::Draw;
				m_model.clearSelection();
			}
			ImGui::SameLine();
			if (ImGui::Selectable("Fill", m_tool == TileSheetTool::Fill, 0, btnSz)) {
				m_tool = TileSheetTool::Fill;
				m_model.clearSelection();
			}
			ImGui::SameLine();
			if (ImGui::Selectable("Line", m_tool == TileSheetTool::Line, 0, btnSz)) {
				m_tool = TileSheetTool::Line;
				m_model.clearSelection();
			}
			//ImGui::SameLine();
			//size_t i{};
			//ig::ComboBox("##Operations", ox::Array<ox::CStringView, 1>{"Operations"}, i);
		}
		ImGui::EndChild();
		ImGui::BeginChild("OperationsBox", {0, 35 * scale}, ImGuiWindowFlags_NoTitleBar);
		{
			if (ImGui::BeginCombo("##Operations", "Operations", 0)) {
				if (ImGui::Selectable("Flip X", false)) {
					oxLogError(m_model.flipX());
				}
				if (ImGui::Selectable("Flip Y", false)) {
					oxLogError(m_model.flipY());
				}
				ImGui::BeginDisabled(!m_model.rotateEligible());
				if (ImGui::Selectable("Rotate Left", false)) {
					oxLogError(m_model.rotateLeft());
				}
				if (ImGui::Selectable("Rotate Right", false)) {
					oxLogError(m_model.rotateRight());
				}
				ImGui::EndDisabled();
				ImGui::EndCombo();
			}
		}
		ImGui::EndChild();
		auto const ySize = controlsSize.y - (38 + ig::BtnSz.y + 21) * scale;
		// draw palette/color picker
		ImGui::BeginChild("Palette", {(s_palViewWidth - 24) * scale, ySize / 2.f}, true);
		{
			drawPaletteMenu();
		}
		ImGui::EndChild();
		ImGui::BeginChild("SubSheets", {(s_palViewWidth - 24) * scale, ySize / 2.f}, true);
		{
			auto constexpr btnHeight = ig::BtnSz.y;
			auto const btnSize = ImVec2{btnHeight, btnHeight};
			if (ig::PushButton("+", btnSize)) {
				auto insertOnIdx = m_model.activeSubSheetIdx();
				auto const &parent = m_model.activeSubSheet();
				m_model.addSubsheet(insertOnIdx);
				insertOnIdx.emplace_back(static_cast<uint32_t>(parent.subsheets.size() - 1));
				setActiveSubsheet(insertOnIdx);
			}
			ImGui::SameLine();
			if (ig::PushButton("-", btnSize)) {
				auto const &activeSubsheetIdx = m_model.activeSubSheetIdx();
				if (!activeSubsheetIdx.empty()) {
					m_model.rmSubsheet(activeSubsheetIdx);
				}
			}
			ImGui::SameLine();
			if (ig::PushButton("Edit")) {
				showSubsheetEditor();
			}
			ImGui::SameLine();
			if (ig::PushButton("Export")) {
				m_exportMenu.show();
			}
			static constexpr auto flags =
				ImGuiTableFlags_RowBg |
                ImGuiTableFlags_NoBordersInBody |
                ImGuiTableFlags_ScrollY;
            if (ImGui::BeginTable("Subsheets", 4, flags)) {
                TileSheet::SubSheetIdx path;
                ImGui::TableSetupColumn("Subsheet", ImGuiTableColumnFlags_NoHide);
                ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_WidthFixed, 25 * scale);
                ImGui::TableSetupColumn("Columns", ImGuiTableColumnFlags_WidthFixed, 50 * scale);
                ImGui::TableSetupColumn("Rows", ImGuiTableColumnFlags_WidthFixed, 50 * scale);
                ImGui::TableHeadersRow();
                drawSubsheetSelector(m_view.img().subsheet, path);
                ImGui::EndTable();
            }
		}
		ImGui::EndChild();
	}
	ImGui::EndChild();
	m_subsheetEditor.draw();
	m_exportMenu.draw();
	if (auto pal = m_palPicker.draw(m_sctx)) {
		if (*pal != m_model.palPath()) {
			oxLogError(m_model.setPalette(*pal));
		}
	}
}

void TileSheetEditorImGui::drawSubsheetSelector(
		TileSheet::SubSheet &subsheet, TileSheet::SubSheetIdx &path) {
	constexpr auto indentReduce = 14;
	ImGui::TableNextRow(0, 5);
	using Str = ox::BasicString<100>;
	auto pathStr = ox::join<Str>("##", path).value;
	auto lbl = ox::sfmt<Str>("{}##{}", subsheet.name, pathStr);
	auto const rowSelected = path == m_model.activeSubSheetIdx();
	auto const flags = ImGuiTreeNodeFlags_SpanFullWidth
	                 | ImGuiTreeNodeFlags_OpenOnArrow
	                 | ImGuiTreeNodeFlags_DefaultOpen
	                 | (subsheet.subsheets.empty() ? ImGuiTreeNodeFlags_Leaf : 0)
	                 | (rowSelected ? ImGuiTreeNodeFlags_Selected : 0);
	ImGui::TableNextColumn();
	auto const open = ImGui::TreeNodeEx(lbl.c_str(), flags);
	std::ignore = ig::dragDropSource([&subsheet, &path] {
		ImGui::Text("%s", subsheet.name.c_str());
		return ig::setDragDropPayload(SubSheetRef{path});
	});
	if (ig::DragDropTarget const dragDropTarget; dragDropTarget) {
		auto const [ref, err] = ig::getDragDropPayload<SubSheetRef>();
		if (!err) {
			oxLogError(m_model.moveSubSheet(ref.subsheet, path));
		}
	}
	ImGui::SameLine();
	if (ImGui::IsItemClicked()) {
		setActiveSubsheet(path);
	}
	if (ImGui::IsMouseDoubleClicked(0) && ImGui::IsItemHovered()) {
		showSubsheetEditor();
	}
	ImGui::TableNextColumn();
	ImGui::Text("%d", subsheet.id);
	if (subsheet.subsheets.empty()) {
		ImGui::TableNextColumn();
		ImGui::Text("%d", subsheet.columns);
		ImGui::TableNextColumn();
		ImGui::Text("%d", subsheet.rows);
	} else {
		ImGui::TableNextColumn();
		ImGui::Text("--");
		ImGui::TableNextColumn();
		ImGui::Text("--");
	}
	if (open) {
		for (uint32_t i{}; auto &child : subsheet.subsheets) {
			path.push_back(i);
			ImGui::PushID(static_cast<int>(i));
			ig::IndentStackItem	const indentStackItem{-indentReduce};
			drawSubsheetSelector(child, path);
			ImGui::PopID();
			path.pop_back();
			++i;
		}
		ImGui::TreePop();
	}
}

[[nodiscard]]
ox::Vec2 TileSheetEditorImGui::clickPos(ImVec2 const &winPos, ox::Vec2 clickPos) noexcept {
	clickPos.x -= winPos.x + 10;
	clickPos.y -= winPos.y + 10;
	return clickPos;
}

ox::Error TileSheetEditorImGui::saveItem() noexcept {
	return m_model.saveFile();
}

void TileSheetEditorImGui::navigateTo(ox::StringViewCR arg) noexcept {
	auto const subSheetId = strToInt(arg);
	if (subSheetId.error) {
		return;
	}
	if (auto const [val, err] = getSubSheetIdx(m_model.img(), subSheetId.value);
	    !err) {
		setActiveSubsheet(val);
	}
}

void TileSheetEditorImGui::showSubsheetEditor() noexcept {
	auto const &sheet = m_model.activeSubSheet();
	if (!sheet.subsheets.empty()) {
		m_subsheetEditor.show(sheet.name, -1, -1);
	} else {
		m_subsheetEditor.show(sheet.name, sheet.columns, sheet.rows);
	}
}

ox::Error TileSheetEditorImGui::exportSubsheetToPng(int const scale) const noexcept {
	OX_REQUIRE(path, studio::saveFile({{"PNG", "png"}}));
	// subsheet to png
	auto const &s = m_model.activeSubSheet();
	auto const &pal = m_model.pal();
	return gfx::exportSubsheetToPng(s, pal, m_model.palettePage(), path, scale);
}

void TileSheetEditorImGui::drawTileSheet(ox::Vec2 const &fbSize) noexcept {
	auto const winPos = ImGui::GetWindowPos();
	auto const fbSizei = ox::Size{static_cast<int>(fbSize.x), static_cast<int>(fbSize.y)};
	if (m_framebuffer.width != fbSizei.width || m_framebuffer.height != fbSizei.height) {
		glutils::resizeInitFrameBuffer(m_framebuffer, fbSizei.width, fbSizei.height);
		m_view.resizeView(fbSize);
	} else if (m_view.updated()) {
		m_view.ackUpdate();
	}
	{
		glutils::FrameBufferBind const frameBufferBind(m_framebuffer);
		// clear screen and draw
		glViewport(0, 0, fbSizei.width, fbSizei.height);
		m_view.draw();
	}
	ImGui::Image(
		ig::toImTextureID(m_framebuffer.color.id),
		static_cast<ImVec2>(fbSize),
		{0, 1},
		{1, 0});
	// handle input, this must come after drawing
	auto const &io = ImGui::GetIO();
	auto const mousePos = ox::Vec2{ImGui::GetMousePos()};
	if (ImGui::IsItemHovered()) {
		auto const wheel = io.MouseWheel;
		auto const wheelh = io.MouseWheelH;
		if (wheel != 0) {
			m_view.scrollV(fbSize, wheel, ImGui::IsKeyDown(ImGuiKey_ModCtrl));
		}
		if (wheelh != 0) {
			m_view.scrollH(fbSize, wheelh);
		}
		using ImGui::IsMouseClicked;
		using ImGui::IsMouseDragging;
		if ((IsMouseDragging(ImGuiMouseButton_Left) || IsMouseClicked(ImGuiMouseButton_Left))
			&& m_prevMouseDownPos != mousePos) {
			m_prevMouseDownPos = mousePos;
			switch (m_tool) {
				case TileSheetTool::Draw:
					m_view.clickDraw(fbSize, clickPos(winPos, mousePos));
					break;
				case TileSheetTool::Fill:
					m_view.clickFill(fbSize, clickPos(winPos, mousePos));
					break;
				case TileSheetTool::Line:
					m_view.clickLine(fbSize, clickPos(winPos, mousePos));
					break;
				case TileSheetTool::Select:
					m_view.clickSelect(fbSize, clickPos(winPos, mousePos));
					break;
				case TileSheetTool::None:
					break;
			}
		}
	}
	if (ImGui::BeginPopupContextItem("TileMenu", ImGuiPopupFlags_MouseButtonRight)) {
		auto const popupPos = ox::Vec2{ImGui::GetWindowPos()};
		if (ImGui::MenuItem("Insert Tile")) {
			m_view.insertTile(fbSize, clickPos(winPos, popupPos));
		}
		if (ImGui::MenuItem("Delete Tile")) {
			m_view.deleteTile(fbSize, clickPos(winPos, popupPos));
		}
		ImGui::EndPopup();
	}
	if (io.MouseReleased[0]) {
		m_prevMouseDownPos = {-1, -1};
		m_view.releaseMouseButton(m_tool);
	}
}

void TileSheetEditorImGui::drawPaletteMenu() noexcept {
	ig::IDStackItem const idStackItem{"PaletteMenu"};
	auto const scale = ig::dpiScale();
	auto const comboWidthSub = 62 * scale;
	ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub);
	auto constexpr palFlags = ImGuiInputTextFlags_ReadOnly;
	if (ig::InputTextWithHint("##Palette", "Path to Palette", m_model.palPath(), palFlags)) {
		oxLogError(m_model.setPalette(m_model.palPath()));
	}
	m_palPathFocused = ImGui::IsItemFocused();
	if (ig::DragDropTarget const dragDropTarget; dragDropTarget) {
		auto const [ref, err] = ig::getDragDropPayload<studio::FileRef>("FileRef");
		if (!err && endsWith(ref.path, FileExt_npal)) {
			if (ref.path != m_model.palPath()) {
				oxLogError(m_model.setPalette(ref.path));
			}
		}
	}
	ImGui::SameLine();
	if (ImGui::Button("Browse")) {
		m_palPicker.open();
	}
	auto const pages = m_model.pal().pages.size();
	if (pages > 1) {
		ig::IndentStackItem	const indentStackItem{20};
		using Str = ox::IString<55>;
		auto numStr = ox::sfmt<Str>(
			"{} - {}", m_model.palettePage() + 1, m_model.pal().pages[m_model.palettePage()].name);
		ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub);
		if (ImGui::BeginCombo("Page", numStr.c_str(), 0)) {
			for (auto n = 0u; n < pages; ++n) {
				auto const selected = (m_model.palettePage() == n);
				numStr = ox::sfmt<Str>("{} - {}", n + 1, m_model.pal().pages[n].name);
				if (ImGui::Selectable(numStr.c_str(), selected) && m_model.palettePage() != n) {
					m_model.setPalettePage(n);
				}
				if (selected) {
					ImGui::SetItemDefaultFocus();
				}
			}
			ImGui::EndCombo();
		}
	}
	// header
	auto constexpr palTblFlags =
		ImGuiTableFlags_RowBg |
        ImGuiTableFlags_SizingStretchProp |
        ImGuiTableFlags_ScrollY;
	if (ImGui::BeginTable(
			"PaletteTable", 4, palTblFlags)) {
		ImGui::TableSetupColumn("Idx", 0, 0.6f);
		ImGui::TableSetupColumn("", 0, 0.22f);
		ImGui::TableSetupColumn("Name", 0, 3);
		ImGui::TableSetupColumn("Color16", 0, 3);
		ImGui::TableHeadersRow();
		{
			auto const &pal = m_model.pal();
			if (pal.pages.size() > m_model.palettePage()) {
				for (auto i = 0u; auto const &c: pal.pages[m_model.palettePage()].colors) {
					ImGui::TableNextRow();
					ImGui::PushID(static_cast<int>(i));
					// Column: color idx
					ImGui::TableNextColumn();
					auto const label = ox::intToStr(i + 1);
					auto const rowSelected = i == m_view.palIdx();
					if (ImGui::Selectable(
							label.c_str(), rowSelected, ImGuiSelectableFlags_SpanAllColumns)) {
						m_view.setPalIdx(i);
					}
					if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) {
						navigateToPalette(
							m_sctx,
							m_model.palPath(),
							i, m_model.palettePage());
					}
					// Column: color RGB
					ImGui::TableNextColumn();
					auto const ic = ImGui::GetColorU32({redf(c), greenf(c), bluef(c), 1});
					ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ic);
					ImGui::TableNextColumn();
					auto const &name =  i < pal.colorNames.size() ? pal.colorNames[i].c_str() : "";
					ImGui::Text("%s", name);
					ImGui::TableNextColumn();
					ImGui::Text("(%02d, %02d, %02d)", red16(c), green16(c), blue16(c));
					ImGui::PopID();
					++i;
				}
			}
		}
		ImGui::EndTable();
	}
}

ox::Error TileSheetEditorImGui::updateActiveSubsheet(
		ox::StringViewCR name, int const cols, int const rows) noexcept {
	return m_model.updateSubsheet(m_model.activeSubSheetIdx(), name, cols, rows);
}

void TileSheetEditorImGui::setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept {
	m_model.setActiveSubsheet(path);
	studio::editConfig<TileSheetEditorConfig>(keelCtx(m_sctx), itemPath(),
		[&path](TileSheetEditorConfig &config) {
			config.activeSubsheet = std::move(path);
		});
}


void TileSheetEditorImGui::SubSheetEditor::show(ox::StringViewCR name, int const cols, int const rows) noexcept {
	m_show = true;
	m_name = name;
	m_cols = cols;
	m_rows = rows;
}

void TileSheetEditorImGui::SubSheetEditor::draw() noexcept {
	constexpr auto popupName = "Edit Subsheet";
	if (!m_show) {
		return;
	}
	auto const modSize = m_cols > 0;
	auto constexpr popupWidth = 235.f;
	auto const popupHeight = modSize ? 130.f : 85.f;
	auto const popupSz = ImVec2{popupWidth, popupHeight};
	if (ig::BeginPopup(popupName, m_show, popupSz)) {
		ig::InputText("Name", m_name);
		if (modSize) {
			ImGui::InputInt("Columns", &m_cols);
			ImGui::InputInt("Rows", &m_rows);
		}
		if (ig::PopupControlsOkCancel(m_show) == ig::PopupResponse::OK) {
			inputSubmitted.emit(m_name, m_cols, m_rows);
		}
		ImGui::EndPopup();
	}
}

void TileSheetEditorImGui::SubSheetEditor::close() noexcept {
	m_show = false;
}


void TileSheetEditorImGui::ExportMenu::show() noexcept {
	m_show = true;
	m_scale = 5;
}

void TileSheetEditorImGui::ExportMenu::draw() noexcept {
	constexpr auto popupName = "Export Tile Sheet";
	if (!m_show) {
		return;
	}
	auto constexpr popupWidth = 235.f;
	auto constexpr popupHeight = 85.f;
	auto constexpr popupSz = ImVec2{popupWidth, popupHeight};
	if (ig::BeginPopup(popupName, m_show, popupSz)) {
		ImGui::InputInt("Scale", &m_scale);
		m_scale = ox::clamp(m_scale, 1, 135);
		if (ig::PopupControlsOkCancel(m_show) == ig::PopupResponse::OK) {
			inputSubmitted.emit(m_scale);
		}
		ImGui::EndPopup();
	}
}

void TileSheetEditorImGui::ExportMenu::close() noexcept {
	m_show = false;
}

}
