| 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/cmd_set_palette.h" |
| 13 | #include "app/commands/new_params.h" |
| 14 | #include "app/context.h" |
| 15 | #include "app/context_access.h" |
| 16 | #include "app/doc_api.h" |
| 17 | #include "app/pref/preferences.h" |
| 18 | #include "app/tx.h" |
| 19 | #include "app/util/pal_ops.h" |
| 20 | #include "doc/palette.h" |
| 21 | #include "doc/palette_picks.h" |
| 22 | #include "doc/remap.h" |
| 23 | |
| 24 | namespace app { |
| 25 | |
| 26 | using namespace ui; |
| 27 | |
| 28 | struct MoveColorsParams : public NewParams { |
| 29 | Param<int> before { this, 0, "before" }; |
| 30 | }; |
| 31 | |
| 32 | class MoveColorsCommand : public CommandWithNewParams<MoveColorsParams> { |
| 33 | public: |
| 34 | MoveColorsCommand(bool copy) |
| 35 | : CommandWithNewParams<MoveColorsParams>( |
| 36 | (copy ? CommandId::CopyColors(): |
| 37 | CommandId::MoveColors()), CmdRecordableFlag), |
| 38 | m_copy(copy) { } |
| 39 | |
| 40 | protected: |
| 41 | bool onEnabled(Context* ctx) override { |
| 42 | return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable | |
| 43 | ContextFlags::HasSelectedColors); |
| 44 | } |
| 45 | |
| 46 | void onExecute(Context* ctx) override { |
| 47 | ContextWriter writer(ctx); |
| 48 | Site site = ctx->activeSite(); |
| 49 | |
| 50 | PalettePicks picks = site.selectedColors(); |
| 51 | if (picks.picks() == 0) |
| 52 | return; // Do nothing |
| 53 | |
| 54 | ASSERT(writer.palette()); |
| 55 | if (!writer.palette()) |
| 56 | return; |
| 57 | |
| 58 | Tx tx(writer.context(), friendlyName(), ModifyDocument); |
| 59 | const int beforeIndex = params().before(); |
| 60 | int currentEntry = picks.firstPick(); |
| 61 | |
| 62 | #ifdef ENABLE_UI |
| 63 | if (ctx->isUIAvailable()) { |
| 64 | auto& fgColor = Preferences::instance().colorBar.fgColor; |
| 65 | if (fgColor().getType() == app::Color::IndexType) |
| 66 | currentEntry = fgColor().getIndex(); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | doc::Palette palette(*writer.palette()); |
| 71 | doc::Palette newPalette(palette); |
| 72 | move_or_copy_palette_colors(palette, newPalette, picks, |
| 73 | currentEntry, |
| 74 | beforeIndex, |
| 75 | m_copy); |
| 76 | |
| 77 | writer.document()->getApi(tx) |
| 78 | .setPalette(writer.sprite(), writer.frame(), &newPalette); |
| 79 | |
| 80 | ctx->setSelectedColors(picks); |
| 81 | |
| 82 | #ifdef ENABLE_UI |
| 83 | if (ctx->isUIAvailable()) { |
| 84 | auto& fgColor = Preferences::instance().colorBar.fgColor; |
| 85 | if (fgColor().getType() == app::Color::IndexType) |
| 86 | fgColor(Color::fromIndex(currentEntry)); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | tx.commit(); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | bool m_copy; |
| 95 | }; |
| 96 | |
| 97 | Command* CommandFactory::createMoveColorsCommand() |
| 98 | { |
| 99 | return new MoveColorsCommand(false); |
| 100 | } |
| 101 | |
| 102 | Command* CommandFactory::createCopyColorsCommand() |
| 103 | { |
| 104 | return new MoveColorsCommand(true); |
| 105 | } |
| 106 | |
| 107 | } // namespace app |
| 108 | |