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

This commit is contained in:
2022-03-24 20:54:17 -05:00
parent ef3a28846c
commit 6eb4070d97
5 changed files with 82 additions and 7 deletions
@@ -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);
}