| 1 | // Aseprite |
| 2 | // Copyright (C) 2018-2019 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2018 David Capello |
| 4 | // |
| 5 | // This program is distributed under the terms of |
| 6 | // the End-User License Agreement for Aseprite. |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include "app/commands/command.h" |
| 13 | #include "app/context.h" |
| 14 | #include "app/doc.h" |
| 15 | #include "app/site.h" |
| 16 | #include "app/util/new_image_from_mask.h" |
| 17 | #include "base/fs.h" |
| 18 | #include "doc/cel.h" |
| 19 | #include "doc/document.h" |
| 20 | #include "doc/layer.h" |
| 21 | #include "doc/mask.h" |
| 22 | #include "doc/palette.h" |
| 23 | #include "doc/sprite.h" |
| 24 | |
| 25 | #include <cstdio> |
| 26 | |
| 27 | namespace app { |
| 28 | |
| 29 | using namespace doc; |
| 30 | |
| 31 | class NewSpriteFromSelectionCommand : public Command { |
| 32 | public: |
| 33 | NewSpriteFromSelectionCommand(); |
| 34 | |
| 35 | protected: |
| 36 | bool onEnabled(Context* context) override; |
| 37 | void onExecute(Context* context) override; |
| 38 | }; |
| 39 | |
| 40 | NewSpriteFromSelectionCommand::NewSpriteFromSelectionCommand() |
| 41 | : Command(CommandId::NewSpriteFromSelection(), CmdUIOnlyFlag) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | bool NewSpriteFromSelectionCommand::onEnabled(Context* context) |
| 46 | { |
| 47 | return context->checkFlags(ContextFlags::ActiveDocumentIsReadable | |
| 48 | ContextFlags::HasVisibleMask); |
| 49 | } |
| 50 | |
| 51 | void NewSpriteFromSelectionCommand::onExecute(Context* context) |
| 52 | { |
| 53 | const Site site = context->activeSite(); |
| 54 | const Doc* doc = site.document(); |
| 55 | const Sprite* sprite = site.sprite(); |
| 56 | const Mask* mask = doc->mask(); |
| 57 | ImageRef image( |
| 58 | new_image_from_mask(site, mask, true)); |
| 59 | if (!image) |
| 60 | return; |
| 61 | |
| 62 | Palette* palette = sprite->palette(site.frame()); |
| 63 | |
| 64 | std::unique_ptr<Sprite> dstSprite( |
| 65 | Sprite::MakeStdSprite( |
| 66 | ImageSpec((ColorMode)image->pixelFormat(), |
| 67 | image->width(), |
| 68 | image->height(), |
| 69 | sprite->transparentColor(), |
| 70 | sprite->colorSpace()), |
| 71 | palette->size())); |
| 72 | |
| 73 | palette->copyColorsTo(dstSprite->palette(frame_t(0))); |
| 74 | |
| 75 | LayerImage* dstLayer = static_cast<LayerImage*>(dstSprite->root()->firstLayer()); |
| 76 | if (site.layer()->isBackground()) |
| 77 | dstLayer->configureAsBackground(); // Configure layer name as background |
| 78 | dstLayer->setFlags(site.layer()->flags()); // Copy all flags |
| 79 | copy_image(dstLayer->cel(frame_t(0))->image(), image.get()); |
| 80 | |
| 81 | std::unique_ptr<Doc> dstDoc(new Doc(dstSprite.get())); |
| 82 | dstSprite.release(); |
| 83 | char buf[1024]; |
| 84 | std::sprintf(buf, "%s-%dx%d-%dx%d" , |
| 85 | base::get_file_title(doc->filename()).c_str(), |
| 86 | mask->bounds().x, mask->bounds().y, |
| 87 | mask->bounds().w, mask->bounds().h); |
| 88 | dstDoc->setFilename(buf); |
| 89 | dstDoc->setContext(context); |
| 90 | dstDoc.release(); |
| 91 | } |
| 92 | |
| 93 | Command* CommandFactory::createNewSpriteFromSelectionCommand() |
| 94 | { |
| 95 | return new NewSpriteFromSelectionCommand(); |
| 96 | } |
| 97 | |
| 98 | } // namespace app |
| 99 | |