[nostalgia/core] Cleanup TileSheetEditor with new ImGui util functions
All checks were successful
Build / build (push) Successful in 2m21s

This commit is contained in:
Gary Talent 2024-01-20 14:59:43 -06:00
parent c2e34b6456
commit 5d1f680a51
4 changed files with 71 additions and 69 deletions

View File

@ -12,6 +12,8 @@
namespace nostalgia::core { namespace nostalgia::core {
namespace ig = studio::ig;
static ox::Vector<uint32_t> normalizePixelSizes( static ox::Vector<uint32_t> normalizePixelSizes(
ox::Vector<uint8_t> const&inPixels, ox::Vector<uint8_t> const&inPixels,
int const bpp) noexcept { int const bpp) noexcept {
@ -194,9 +196,9 @@ void TileSheetEditorImGui::draw(turbine::Context&) noexcept {
ImGui::EndChild(); ImGui::EndChild();
ImGui::BeginChild("SubSheets", ImVec2(m_palViewWidth - 24, ySize / 2.f), true); ImGui::BeginChild("SubSheets", ImVec2(m_palViewWidth - 24, ySize / 2.f), true);
{ {
static constexpr auto btnHeight = 18; static constexpr auto btnHeight = ig::BtnSz.y;
auto const btnSize = ImVec2(18, btnHeight); auto const btnSize = ImVec2(btnHeight, btnHeight);
if (ImGui::Button("+", btnSize)) { if (ig::PushButton("+", btnSize)) {
auto insertOnIdx = m_model.activeSubSheetIdx(); auto insertOnIdx = m_model.activeSubSheetIdx();
auto const&parent = m_model.activeSubSheet(); auto const&parent = m_model.activeSubSheet();
m_model.addSubsheet(insertOnIdx); m_model.addSubsheet(insertOnIdx);
@ -204,18 +206,18 @@ void TileSheetEditorImGui::draw(turbine::Context&) noexcept {
m_model.setActiveSubsheet(insertOnIdx); m_model.setActiveSubsheet(insertOnIdx);
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("-", btnSize)) { if (ig::PushButton("-", btnSize)) {
auto const&activeSubsheetIdx = m_model.activeSubSheetIdx(); auto const&activeSubsheetIdx = m_model.activeSubSheetIdx();
if (!activeSubsheetIdx.empty()) { if (!activeSubsheetIdx.empty()) {
m_model.rmSubsheet(activeSubsheetIdx); m_model.rmSubsheet(activeSubsheetIdx);
} }
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Edit", ImVec2(51, btnHeight))) { if (ig::PushButton("Edit")) {
showSubsheetEditor(); showSubsheetEditor();
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Export", ImVec2(51, btnHeight))) { if (ig::PushButton("Export")) {
m_exportMenu.show(); m_exportMenu.show();
} }
TileSheet::SubSheetIdx path; TileSheet::SubSheetIdx path;
@ -233,11 +235,12 @@ void TileSheetEditorImGui::draw(turbine::Context&) noexcept {
ImGui::EndChild(); ImGui::EndChild();
} }
ImGui::EndChild(); ImGui::EndChild();
m_subsheetEditor.draw(); m_subsheetEditor.draw(m_ctx);
m_exportMenu.draw(); m_exportMenu.draw(m_ctx);
} }
void TileSheetEditorImGui::drawSubsheetSelector(TileSheet::SubSheet &subsheet, TileSheet::SubSheetIdx &path) { void TileSheetEditorImGui::drawSubsheetSelector(
TileSheet::SubSheet &subsheet, TileSheet::SubSheetIdx &path) {
constexpr auto indentReduce = 14; constexpr auto indentReduce = 14;
ImGui::TableNextRow(0, 5); ImGui::TableNextRow(0, 5);
using Str = ox::BasicString<100>; using Str = ox::BasicString<100>;
@ -398,22 +401,10 @@ void TileSheetEditorImGui::drawTileSheet(ox::Vec2 const&fbSize) noexcept {
void TileSheetEditorImGui::drawPaletteSelector() noexcept { void TileSheetEditorImGui::drawPaletteSelector() noexcept {
auto &sctx = *applicationData<studio::StudioContext>(m_ctx); auto &sctx = *applicationData<studio::StudioContext>(m_ctx);
auto const&files = sctx.project->fileList(core::FileExt_npal); auto const&files = sctx.project->fileList(core::FileExt_npal);
auto const first = m_selectedPaletteIdx < files.size() ?
files[m_selectedPaletteIdx].c_str() : "";
auto const comboWidthSub = 62; auto const comboWidthSub = 62;
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub); ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub);
if (ImGui::BeginCombo("Palette", first, 0)) { if (ig::ComboBox("Palette", files, m_selectedPaletteIdx)) {
for (auto n = 0u; n < files.size(); ++n) { oxLogError(m_model.setPalette(files[m_selectedPaletteIdx]));
const auto selected = (m_selectedPaletteIdx == n);
if (ImGui::Selectable(files[n].c_str(), selected) && m_selectedPaletteIdx != n) {
m_selectedPaletteIdx = n;
oxLogError(m_model.setPalette(files[n]));
}
if (selected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
} }
auto const pages = m_model.pal().pages.size(); auto const pages = m_model.pal().pages.size();
if (pages > 1) { if (pages > 1) {
@ -491,34 +482,24 @@ ox::Error TileSheetEditorImGui::markUnsavedChanges(studio::UndoCommand const*) n
return {}; return {};
} }
void TileSheetEditorImGui::SubSheetEditor::draw() noexcept { void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &ctx) noexcept {
constexpr auto modalFlags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
constexpr auto popupName = "Edit Subsheet"; constexpr auto popupName = "Edit Subsheet";
if (!m_show) { if (!m_show) {
return; return;
} }
ImGui::OpenPopup(popupName);
auto const modSize = m_cols > 0; auto const modSize = m_cols > 0;
auto const popupWidth = 235.f; auto constexpr popupWidth = 235.f;
auto const popupHeight = modSize ? 125.f : 80.f; auto const popupHeight = modSize ? 130.f : 85.f;
ImGui::SetNextWindowSize(ImVec2(popupWidth, popupHeight)); auto const popupSz = ImVec2(popupWidth, popupHeight);
if (ImGui::BeginPopupModal(popupName, &m_show, modalFlags)) { if (ig::BeginPopup(ctx, popupName, m_show, popupSz)) {
ImGui::InputText("Name", m_name.data(), m_name.cap()); ImGui::InputText("Name", m_name.data(), m_name.cap());
if (modSize) { if (modSize) {
ImGui::InputInt("Columns", &m_cols); ImGui::InputInt("Columns", &m_cols);
ImGui::InputInt("Rows", &m_rows); ImGui::InputInt("Rows", &m_rows);
} }
ImGui::SetCursorPosX(popupWidth - 116); if (ig::PopupControlsOkCancel(popupWidth, m_show) == ig::PopupResponse::OK) {
if (ImGui::Button("OK", ImVec2{50, 20})) {
ImGui::CloseCurrentPopup();
m_show = false;
inputSubmitted.emit(m_name, m_cols, m_rows); inputSubmitted.emit(m_name, m_cols, m_rows);
} }
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2{50, 20})) {
ImGui::CloseCurrentPopup();
m_show = false;
}
ImGui::EndPopup(); ImGui::EndPopup();
} }
} }
@ -527,28 +508,20 @@ void TileSheetEditorImGui::SubSheetEditor::close() noexcept {
m_show = false; m_show = false;
} }
void TileSheetEditorImGui::ExportMenu::draw() noexcept { void TileSheetEditorImGui::ExportMenu::draw(turbine::Context &ctx) noexcept {
constexpr auto modalFlags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
constexpr auto popupName = "Export Tile Sheet"; constexpr auto popupName = "Export Tile Sheet";
if (!m_show) { if (!m_show) {
return; return;
} }
ImGui::OpenPopup(popupName); constexpr auto popupWidth = 235.f;
constexpr auto popupHeight = 80.f; constexpr auto popupHeight = 85.f;
ImGui::SetNextWindowSize(ImVec2(235, popupHeight)); constexpr auto popupSz = ImVec2(popupWidth, popupHeight);
if (ImGui::BeginPopupModal(popupName, &m_show, modalFlags)) { if (ig::BeginPopup(ctx, popupName, m_show, popupSz)) {
ImGui::InputInt("Scale", &m_scale); ImGui::InputInt("Scale", &m_scale);
m_scale = ox::clamp(m_scale, 1, 50); m_scale = ox::clamp(m_scale, 1, 50);
if (ImGui::Button("OK", ImVec2{50, 20})) { if (ig::PopupControlsOkCancel(popupWidth, m_show) == ig::PopupResponse::OK) {
ImGui::CloseCurrentPopup();
m_show = false;
inputSubmitted.emit(m_scale); inputSubmitted.emit(m_scale);
} }
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2{50, 20})) {
ImGui::CloseCurrentPopup();
m_show = false;
}
ImGui::EndPopup(); ImGui::EndPopup();
} }
} }

View File

@ -40,7 +40,7 @@ class TileSheetEditorImGui: public studio::Editor {
m_cols = cols; m_cols = cols;
m_rows = rows; m_rows = rows;
} }
void draw() noexcept; void draw(turbine::Context &ctx) noexcept;
void close() noexcept; void close() noexcept;
[[nodiscard]] [[nodiscard]]
inline bool isOpen() const noexcept { return m_show; } inline bool isOpen() const noexcept { return m_show; }
@ -55,7 +55,7 @@ class TileSheetEditorImGui: public studio::Editor {
m_show = true; m_show = true;
m_scale = 5; m_scale = 5;
} }
void draw() noexcept; void draw(turbine::Context &ctx) noexcept;
void close() noexcept; void close() noexcept;
[[nodiscard]] [[nodiscard]]
inline bool isOpen() const noexcept { return m_show; } inline bool isOpen() const noexcept { return m_show; }

View File

@ -10,26 +10,30 @@
namespace studio::ig { namespace studio::ig {
inline constexpr auto BtnSz = ImVec2{50, 22}; inline constexpr auto BtnSz = ImVec2{52, 22};
template<typename T>
class IDStackItem { class IDStackItem {
private:
T m_id;
public: public:
explicit IDStackItem(T id) noexcept: m_id(id) { explicit IDStackItem(int id) noexcept;
ImGui::PushID(m_id); explicit IDStackItem(const char *id) noexcept;
} explicit IDStackItem(ox::CStringView id) noexcept;
~IDStackItem() noexcept { ~IDStackItem() noexcept;
ImGui::PopID();
}
}; };
void centerNextWindow(turbine::Context &ctx) noexcept; void centerNextWindow(turbine::Context &ctx) noexcept;
bool PushButton(ox::CStringView lbl, ImVec2 const&btnSz = BtnSz) noexcept; bool PushButton(ox::CStringView lbl, ImVec2 const&btnSz = BtnSz) noexcept;
void PopupBtns(float popupWidth, bool &popupOpen); enum class PopupResponse {
None,
OK,
Cancel,
};
PopupResponse PopupControlsOkCancel(float popupWidth, bool &popupOpen);
[[nodiscard]]
bool BeginPopup(turbine::Context &ctx, ox::CStringView popupName, bool &show, ImVec2 const&sz = {285, 0});
/** /**
* *

View File

@ -10,6 +10,20 @@
namespace studio::ig { namespace studio::ig {
IDStackItem::IDStackItem(int id) noexcept {
ImGui::PushID(id);
}
IDStackItem::IDStackItem(const char *id) noexcept {
ImGui::PushID(id);
}
IDStackItem::IDStackItem(ox::CStringView id) noexcept: IDStackItem(id.c_str()) {}
IDStackItem::~IDStackItem() noexcept {
ImGui::PopID();
}
void centerNextWindow(turbine::Context &ctx) noexcept { void centerNextWindow(turbine::Context &ctx) noexcept {
auto const sz = turbine::getScreenSize(ctx); auto const sz = turbine::getScreenSize(ctx);
auto const screenW = static_cast<float>(sz.width); auto const screenW = static_cast<float>(sz.width);
@ -22,19 +36,31 @@ bool PushButton(ox::CStringView lbl, ImVec2 const&btnSz) noexcept {
return ImGui::Button(lbl.c_str(), btnSz); return ImGui::Button(lbl.c_str(), btnSz);
} }
void PopupBtns(float popupWidth, bool &popupOpen) { PopupResponse PopupControlsOkCancel(float popupWidth, bool &popupOpen) {
auto out = PopupResponse::None;
constexpr auto btnSz = ImVec2{50, BtnSz.y}; constexpr auto btnSz = ImVec2{50, BtnSz.y};
ImGui::Separator(); ImGui::Separator();
ImGui::SetCursorPosX(popupWidth - 118); ImGui::SetCursorPosX(popupWidth - 118);
if (ImGui::Button("OK", btnSz)) { if (ImGui::Button("OK", btnSz)) {
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
popupOpen = false; popupOpen = false;
out = PopupResponse::OK;
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Cancel", btnSz)) { if (ImGui::IsKeyDown(ImGuiKey_Escape) || ImGui::Button("Cancel", btnSz)) {
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
popupOpen = false; popupOpen = false;
out = PopupResponse::Cancel;
} }
return out;
}
bool BeginPopup(turbine::Context &ctx, ox::CStringView popupName, bool &show, ImVec2 const&sz) {
constexpr auto modalFlags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
ig::centerNextWindow(ctx);
ImGui::OpenPopup(popupName.c_str());
ImGui::SetNextWindowSize(sz);
return ImGui::BeginPopupModal(popupName.c_str(), &show, modalFlags);
} }
bool ComboBox( bool ComboBox(
@ -48,7 +74,6 @@ bool ComboBox(
const auto selected = (selectedIdx == i); const auto selected = (selectedIdx == i);
if (ImGui::Selectable(list[i].c_str(), selected) && selectedIdx != i) { if (ImGui::Selectable(list[i].c_str(), selected) && selectedIdx != i) {
selectedIdx = i; selectedIdx = i;
//oxLogError(m_model.setPalette(list[n]));
out = true; out = true;
} }
} }