[jasper] Fixes for Studio changes, make EditWorldSizeCommand merge
All checks were successful
Build / build (push) Successful in 3m13s

This commit is contained in:
Gary Talent 2024-05-22 02:12:46 -05:00
parent 3c593b8d58
commit f920748e19
6 changed files with 66 additions and 16 deletions

View File

@ -7,24 +7,60 @@
namespace jasper::world {
EditWorldSize::EditWorldSize(WorldDoc &doc, ox::Size const&size):
EditWorldSizeCommand::EditWorldSizeCommand(
ObjectCache const&objCache,
WorldDoc &doc,
WorldStatic &worldStatic,
ox::Size const&size):
m_objCache(objCache),
m_doc(doc),
m_worldStatic(worldStatic),
m_oldSize(m_doc.columns, m_doc.rows),
m_newVal(size) {
}
m_newSize(size) {}
void EditWorldSize::redo() noexcept {
void EditWorldSizeCommand::redo() noexcept {
m_oldMap = m_doc.tiles;
resize(m_doc, m_newVal);
resize(m_doc, m_newSize);
oxLogError(loadWorldStatic(m_objCache, m_doc).moveTo(m_worldStatic));
}
void EditWorldSize::undo() noexcept {
void EditWorldSizeCommand::undo() noexcept {
resize(m_doc, m_oldSize);
m_doc.tiles = std::move(m_oldMap);
oxLogError(loadWorldStatic(m_objCache, m_doc).moveTo(m_worldStatic));
}
int EditWorldSize::commandId() const noexcept {
bool EditWorldSizeCommand::mergeWith(studio::UndoCommand const&cmd) noexcept {
auto const es = dynamic_cast<EditWorldSizeCommand const*>(&cmd);
if (!es || dimensionChanged() != es->dimensionChanged()) {
return false;
}
m_newSize = es->m_newSize;
// undo
resize(m_doc, m_oldSize);
m_doc.tiles = std::move(m_oldMap);
// redo
m_oldMap = m_doc.tiles;
resize(m_doc, m_newSize);
oxLogError(loadWorldStatic(m_objCache, m_doc).moveTo(m_worldStatic));
return true;
}
int EditWorldSizeCommand::commandId() const noexcept {
return static_cast<int>(WorldEditorCommand::EditWorldSize);
}
EditWorldSizeCommand::Dimension EditWorldSizeCommand::dimensionChanged() const noexcept {
auto const xChanged = m_oldSize.width == m_newSize.width;
auto const yChanged = m_oldSize.height == m_newSize.height;
if (xChanged && !yChanged) {
return Dimension::X;
} else if (!xChanged && yChanged) {
return Dimension::Y;
} else if (xChanged && yChanged) {
return Dimension::Both;
}
return Dimension::Neither;
}
}

View File

@ -12,23 +12,36 @@
namespace jasper::world {
class EditWorldSize: public studio::UndoCommand {
class EditWorldSizeCommand: public studio::UndoCommand {
private:
ObjectCache const&m_objCache;
WorldDoc &m_doc;
WorldStatic &m_worldStatic;
size_t m_insertIdx{};
ox::Size const m_oldSize;
WorldDoc::TileMap m_oldMap;
ox::Size const m_newVal;
ox::Size m_newSize;
public:
EditWorldSize(WorldDoc &doc, ox::Size const&size);
EditWorldSizeCommand(
ObjectCache const&objCache,
WorldDoc &doc,
WorldStatic &worldStatic,
ox::Size const&size);
void redo() noexcept override;
void undo() noexcept override;
bool mergeWith(studio::UndoCommand const&cmd) noexcept override;
[[nodiscard]]
int commandId() const noexcept override;
[[nodiscard]]
inline size_t insertIdx() const noexcept {
return m_insertIdx;
}
private:
enum class Dimension {
X, Y, Both, Neither,
};
[[nodiscard]]
Dimension dimensionChanged() const noexcept;
};
}

View File

@ -159,10 +159,10 @@ void WorldEditorImGui::drawPropEditor() noexcept {
int width{m_doc.columns};
int height{m_doc.rows};
if (ImGui::InputInt("Map Width", &width, 1)) {
pushCommand<EditWorldSize>(m_doc, ox::Size{width, m_doc.rows});
pushCommand<EditWorldSizeCommand>(m_objCache, m_doc, m_worldStatic, ox::Size{width, m_doc.rows});
}
if (ImGui::InputInt("Map Height", &height, 1)) {
pushCommand<EditWorldSize>(m_doc, ox::Size{m_doc.columns, height});
pushCommand<EditWorldSizeCommand>(m_objCache, m_doc, m_worldStatic, ox::Size{m_doc.columns, height});
}
}

View File

@ -31,8 +31,8 @@ int EditObjectName::commandId() const noexcept {
return static_cast<int>(WorldObjCommand::EditObjectName);
}
bool EditObjectName::mergeWith(UndoCommand const*cmd) noexcept {
auto const en = dynamic_cast<EditObjectName const*>(cmd);
bool EditObjectName::mergeWith(UndoCommand const&cmd) noexcept {
auto const en = dynamic_cast<EditObjectName const*>(&cmd);
if (en && m_objIdx == en->m_objIdx) {
m_newVal = en->m_newVal;
return true;

View File

@ -24,7 +24,7 @@ class EditObjectName: public studio::UndoCommand {
void undo() noexcept override;
[[nodiscard]]
int commandId() const noexcept override;
bool mergeWith(UndoCommand const*cmd) noexcept override;
bool mergeWith(UndoCommand const&cmd) noexcept override;
};
class EditObjectSubSheet: public studio::UndoCommand {

View File

@ -33,6 +33,7 @@ void World::setupLayer(
ncore::Context &ctx,
uint_t lyr,
uint_t cbb) const noexcept {
ncore::clearBg(ctx, lyr);
ncore::setBgStatus(ctx, lyr, true);
ncore::setBgCbb(ctx, lyr, cbb);
auto const rows = ox::min<int>(m_worldStatic.rows, ncore::tileRows(ctx) / 2);