[nostalgia/core/studio] Add palette picker combo box

This commit is contained in:
Gary Talent 2022-03-24 20:54:17 -05:00
parent ef3a28846c
commit 6eb4070d97
5 changed files with 82 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -12,9 +12,22 @@
namespace nostalgia::core {
TileSheetEditorImGui::TileSheetEditorImGui(Context *ctx, const ox::String &path): m_tileSheetEditor(ctx, path) {
m_ctx = ctx;
m_itemPath = path;
const auto lastSlash = std::find(m_itemPath.rbegin(), m_itemPath.rend(), '/').offset();
m_itemName = m_itemPath.substr(lastSlash + 1);
// init palette idx
const auto palPath = model()->palPath();
auto sctx = applicationData<studio::StudioContext>(m_ctx);
const auto &palList = sctx->project->fileList(core::FileExt_npal + 1);
for (std::size_t i = 0; const auto &pal : palList) {
if (palPath == pal) {
m_selectedPaletteIdx = i;
break;
}
++i;
}
// connect signal/slots
undoStack()->changeTriggered.connect(this, &TileSheetEditorImGui::markUnsavedChanges);
m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet);
}
@ -76,7 +89,7 @@ void TileSheetEditorImGui::draw(core::Context*) noexcept {
// draw palette/color picker
ImGui::BeginChild("Palette", ImVec2(m_palViewWidth - 24, ySize / 2.07f), true);
{
drawPalettePicker();
drawPaletteSelector();
}
ImGui::EndChild();
ImGui::BeginChild("SubSheets", ImVec2(m_palViewWidth - 24, ySize / 2.07f), true);
@ -200,7 +213,7 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept {
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 zoomMod = ox::defines::OS == ox::OS::Darwin ? io.KeySuper : core::buttonDown(m_ctx, core::Key::Mod_Ctrl);
m_tileSheetEditor.scrollV(fbSize, wheel, zoomMod);
}
if (wheelh != 0) {
@ -233,7 +246,23 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept {
}
}
void TileSheetEditorImGui::drawPalettePicker() noexcept {
void TileSheetEditorImGui::drawPaletteSelector() noexcept {
auto sctx = applicationData<studio::StudioContext>(m_ctx);
const auto &files = sctx->project->fileList(core::FileExt_npal + 1);
const auto first = m_selectedPaletteIdx < files.size() ? files[m_selectedPaletteIdx].c_str() : "";
if (ImGui::BeginCombo("Palette", first, 0)) {
for (auto n = 0u; n < files.size(); n++) {
const auto selected = (m_selectedPaletteIdx == n);
if (ImGui::Selectable(files[n].c_str(), selected) && m_selectedPaletteIdx != n) {
m_selectedPaletteIdx = n;
oxLogError(model()->setPalette(files[n]));
}
if (selected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
// header
if (ImGui::BeginTable("PaletteTable", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp)) {
ImGui::TableSetupColumn("No.", 0, 0.45);

View File

@ -6,9 +6,7 @@
#include <ox/model/def.hpp>
#include <nostalgia/geo/bounds.hpp>
#include <nostalgia/geo/vec.hpp>
#include <nostalgia/core/gfx.hpp>
#include <nostalgia/glutils/glutils.hpp>
#include <nostalgia/studio/studio.hpp>
@ -43,6 +41,9 @@ class TileSheetEditorImGui: public studio::Editor {
}
void draw() noexcept;
};
std::size_t m_selectedPaletteIdx = 0;
Context *m_ctx = nullptr;
ox::Vector<ox::String> m_paletteList;
SubSheetEditor m_subsheetEditor;
ox::String m_itemPath;
ox::String m_itemName;
@ -103,7 +104,7 @@ class TileSheetEditorImGui: public studio::Editor {
void drawTileSheet(const geo::Vec2 &fbSize) noexcept;
void drawPalettePicker() noexcept;
void drawPaletteSelector() noexcept;
ox::Error updateActiveSubsheet(const ox::String &name, int cols, int rows) noexcept;

View File

@ -57,6 +57,7 @@ enum class CommandId {
RmSubSheet = 3,
UpdateSubSheet = 4,
Paste = 5,
PaletteChange = 6,
};
constexpr bool operator==(CommandId c, int i) noexcept {
@ -322,6 +323,34 @@ class UpdateSubSheetCommand: public studio::UndoCommand {
};
class PaletteChangeCommand: public studio::UndoCommand {
private:
TileSheet *m_img = nullptr;
ox::FileAddress m_oldPalette;
ox::FileAddress m_newPalette;
public:
constexpr PaletteChangeCommand(TileSheet *img, const ox::String &newPalette) noexcept {
m_img = img;
m_oldPalette = m_img->defaultPalette;
m_newPalette = newPalette;
}
void redo() noexcept final {
m_img->defaultPalette = m_newPalette;
}
void undo() noexcept final {
m_img->defaultPalette = m_oldPalette;
}
[[nodiscard]]
int commandId() const noexcept final {
return static_cast<int>(CommandId::PaletteChange);
}
};
TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path) {
m_ctx = ctx;
@ -378,6 +407,15 @@ void TileSheetEditorModel::paste() {
pushCommand(new PasteCommand(&m_img, m_activeSubsSheetIdx, pt1, pt2, *cb));
}
const ox::FileAddress &TileSheetEditorModel::palPath() const noexcept {
return m_img.defaultPalette;
}
ox::Error TileSheetEditorModel::setPalette(const ox::String &path) noexcept {
pushCommand(new PaletteChangeCommand(&m_img, path));
return OxError(0);
}
void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept {
const auto &activeSubSheet = m_img.getSubSheet(m_activeSubsSheetIdx);
if (pt.x >= activeSubSheet.columns * TileWidth || pt.y >= activeSubSheet.rows * TileHeight) {
@ -480,6 +518,9 @@ ox::Error TileSheetEditorModel::markUpdated(int cmdId) noexcept {
case CommandId::Paste:
case CommandId::UpdateSubSheet:
break;
case CommandId::PaletteChange:
oxReturnError(readObj<Palette>(m_ctx, m_img.defaultPalette.getPath().value).moveTo(&m_pal));
break;
}
return OxError(0);
}

View File

@ -10,7 +10,6 @@
#include <nostalgia/core/gfx.hpp>
#include <nostalgia/geo/bounds.hpp>
#include <nostalgia/geo/point.hpp>
#include <nostalgia/geo/vec.hpp>
#include <nostalgia/studio/studio.hpp>
namespace nostalgia::core {
@ -53,6 +52,11 @@ class TileSheetEditorModel: public ox::SignalHandler {
[[nodiscard]]
constexpr const Palette &pal() const noexcept;
[[nodiscard]]
const ox::FileAddress &palPath() const noexcept;
ox::Error setPalette(const ox::String &path) noexcept;
void drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept;
void endDrawCommand() noexcept;