| 1 | // Aseprite |
| 2 | // Copyright (c) 2019 Igara Studio S.A. |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "app/app.h" |
| 12 | #include "app/commands/new_params.h" |
| 13 | #include "app/context.h" |
| 14 | #include "app/context_access.h" |
| 15 | #include "app/tx.h" |
| 16 | #include "app/util/cel_ops.h" |
| 17 | #include "doc/layer.h" |
| 18 | #include "doc/layer_tilemap.h" |
| 19 | #include "doc/remap.h" |
| 20 | #include "doc/tileset.h" |
| 21 | |
| 22 | namespace app { |
| 23 | |
| 24 | using namespace ui; |
| 25 | |
| 26 | struct MoveTilesParams : public NewParams { |
| 27 | Param<int> before { this, 0, "before" }; |
| 28 | }; |
| 29 | |
| 30 | class MoveTilesCommand : public CommandWithNewParams<MoveTilesParams> { |
| 31 | public: |
| 32 | MoveTilesCommand(const bool copy) |
| 33 | : CommandWithNewParams<MoveTilesParams>( |
| 34 | (copy ? CommandId::CopyTiles(): |
| 35 | CommandId::MoveTiles()), CmdRecordableFlag) |
| 36 | , m_copy(copy) { } |
| 37 | |
| 38 | protected: |
| 39 | bool onEnabled(Context* ctx) override { |
| 40 | return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable | |
| 41 | ContextFlags::HasActiveLayer | |
| 42 | ContextFlags::ActiveLayerIsTilemap); |
| 43 | } |
| 44 | |
| 45 | void onExecute(Context* ctx) override { |
| 46 | ContextWriter writer(ctx); |
| 47 | doc::Layer* layer = writer.layer(); |
| 48 | if (!layer || !layer->isTilemap()) |
| 49 | return; |
| 50 | |
| 51 | doc::Tileset* tileset = static_cast<LayerTilemap*>(layer)->tileset(); |
| 52 | ASSERT(tileset); |
| 53 | if (!tileset) |
| 54 | return; |
| 55 | |
| 56 | PalettePicks picks = writer.site()->selectedTiles(); |
| 57 | if (picks.picks() == 0) |
| 58 | return; |
| 59 | |
| 60 | Tx tx(writer.context(), onGetFriendlyName(), ModifyDocument); |
| 61 | const int beforeIndex = params().before(); |
| 62 | int currentEntry = picks.firstPick(); |
| 63 | |
| 64 | if (m_copy) |
| 65 | copy_tiles_in_tileset(tx, tileset, picks, currentEntry, beforeIndex); |
| 66 | else |
| 67 | move_tiles_in_tileset(tx, tileset, picks, currentEntry, beforeIndex); |
| 68 | |
| 69 | tx.commit(); |
| 70 | |
| 71 | ctx->setSelectedTiles(picks); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | bool m_copy; |
| 76 | }; |
| 77 | |
| 78 | Command* CommandFactory::createMoveTilesCommand() |
| 79 | { |
| 80 | return new MoveTilesCommand(false); |
| 81 | } |
| 82 | |
| 83 | Command* CommandFactory::createCopyTilesCommand() |
| 84 | { |
| 85 | return new MoveTilesCommand(true); |
| 86 | } |
| 87 | |
| 88 | } // namespace app |
| 89 | |