1// Aseprite
2// Copyright (C) 2001-2017 David Capello
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/command.h"
13#include "app/context_access.h"
14#include "app/ini_file.h"
15#include "app/modules/editors.h"
16#include "app/ui_context.h"
17#include "base/fs.h"
18#include "doc/sprite.h"
19#include "ui/ui.h"
20
21#include "duplicate_sprite.xml.h"
22
23#include <cstdio>
24
25namespace app {
26
27using namespace ui;
28
29class DuplicateSpriteCommand : public Command {
30public:
31 DuplicateSpriteCommand();
32
33protected:
34 bool onEnabled(Context* context) override;
35 void onExecute(Context* context) override;
36};
37
38DuplicateSpriteCommand::DuplicateSpriteCommand()
39 : Command(CommandId::DuplicateSprite(), CmdUIOnlyFlag)
40{
41}
42
43bool DuplicateSpriteCommand::onEnabled(Context* context)
44{
45 return context->checkFlags(ContextFlags::ActiveDocumentIsReadable);
46}
47
48void DuplicateSpriteCommand::onExecute(Context* context)
49{
50 const ContextReader reader(context);
51 const Doc* document = reader.document();
52
53 // Load the window widget
54 app::gen::DuplicateSprite window;
55 std::string fn = document->filename();
56 std::string ext = base::get_file_extension(fn);
57 window.srcName()->setText(base::get_file_name(fn));
58 window.dstName()->setText(base::get_file_title(fn) +
59 " Copy" + (!ext.empty() ? "." + ext: ""));
60
61 if (get_config_bool("DuplicateSprite", "Flatten", false))
62 window.flatten()->setSelected(true);
63
64 // Open the window
65 window.openWindowInForeground();
66
67 if (window.closer() == window.ok()) {
68 set_config_bool("DuplicateSprite", "Flatten", window.flatten()->isSelected());
69
70 // Make a copy of the document
71 Doc* docCopy;
72 if (window.flatten()->isSelected())
73 docCopy = document->duplicate(DuplicateWithFlattenLayers);
74 else
75 docCopy = document->duplicate(DuplicateExactCopy);
76
77 docCopy->setFilename(window.dstName()->text().c_str());
78 docCopy->setContext(context);
79 }
80}
81
82Command* CommandFactory::createDuplicateSpriteCommand()
83{
84 return new DuplicateSpriteCommand;
85}
86
87} // namespace app
88