2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2017-12-01 10:41:45 +08:00
|
|
|
// Copyright (C) 2001-2017 David Capello
|
2015-02-12 23:16:25 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/commands/command.h"
|
2024-11-06 12:23:00 +08:00
|
|
|
#include "app/commands/new_params.h"
|
|
|
|
#include "app/commands/params.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/context_access.h"
|
2025-05-14 21:03:20 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ini_file.h"
|
|
|
|
#include "app/ui_context.h"
|
2016-11-02 06:14:05 +08:00
|
|
|
#include "base/fs.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-09-23 03:33:49 +08:00
|
|
|
#include "duplicate_sprite.xml.h"
|
2014-08-11 17:55:51 +08:00
|
|
|
|
2012-07-10 04:36:45 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-06-18 09:02:54 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
using namespace ui;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2024-11-06 12:23:00 +08:00
|
|
|
struct DuplicateSpriteParams : public NewParams {
|
|
|
|
Param<bool> ui{ this, true, "ui" };
|
|
|
|
Param<std::string> filename{ this, std::string(), "filename" };
|
|
|
|
Param<bool> flatten{ this, false, "flatten" };
|
|
|
|
};
|
|
|
|
|
|
|
|
class DuplicateSpriteCommand : public CommandWithNewParams<DuplicateSpriteParams> {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
DuplicateSpriteCommand();
|
|
|
|
|
|
|
|
protected:
|
2015-10-01 03:34:43 +08:00
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DuplicateSpriteCommand::DuplicateSpriteCommand()
|
2024-11-06 12:23:00 +08:00
|
|
|
: CommandWithNewParams<DuplicateSpriteParams>(CommandId::DuplicateSprite(), CmdRecordableFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DuplicateSpriteCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsReadable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DuplicateSpriteCommand::onExecute(Context* context)
|
|
|
|
{
|
2024-11-06 12:23:00 +08:00
|
|
|
const bool ui = (params().ui() && context->isUIAvailable());
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
const ContextReader reader(context);
|
2018-07-07 22:54:44 +08:00
|
|
|
const Doc* document = reader.document();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2024-11-06 12:23:00 +08:00
|
|
|
const std::string fn = document->filename();
|
|
|
|
const std::string ext = base::get_file_extension(fn);
|
2024-12-17 01:52:19 +08:00
|
|
|
|
2024-11-06 12:23:00 +08:00
|
|
|
std::string duplicateFn = params().filename.isSet() ?
|
|
|
|
params().filename() :
|
2025-05-14 21:03:20 +08:00
|
|
|
Strings::general_copy_of(base::get_file_title(fn)) +
|
|
|
|
(!ext.empty() ? "." + ext : "");
|
2024-11-06 12:23:00 +08:00
|
|
|
|
|
|
|
bool flatten = params().flatten.isSet() ? params().flatten() :
|
|
|
|
get_config_bool("DuplicateSprite", "Flatten", false);
|
|
|
|
|
|
|
|
if (ui) {
|
|
|
|
// Load the window widget
|
|
|
|
app::gen::DuplicateSprite window;
|
|
|
|
window.srcName()->setText(base::get_file_name(fn));
|
|
|
|
window.dstName()->setText(duplicateFn);
|
|
|
|
window.flatten()->setSelected(flatten);
|
|
|
|
|
|
|
|
// Open the window
|
|
|
|
window.openWindowInForeground();
|
|
|
|
|
|
|
|
if (window.closer() == window.ok()) {
|
|
|
|
flatten = window.flatten()->isSelected();
|
|
|
|
duplicateFn = window.dstName()->text();
|
|
|
|
|
|
|
|
// Only set the config when we do it from the UI, to avoid automation messing with user
|
|
|
|
// expectations.
|
|
|
|
set_config_bool("DuplicateSprite", "Flatten", flatten);
|
|
|
|
}
|
|
|
|
else // Abort if we close/cancel the window
|
|
|
|
return;
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2024-11-06 12:23:00 +08:00
|
|
|
// Make a copy of the document
|
|
|
|
Doc* docCopy;
|
|
|
|
if (flatten)
|
|
|
|
docCopy = document->duplicate(DuplicateWithFlattenLayers);
|
|
|
|
else
|
|
|
|
docCopy = document->duplicate(DuplicateExactCopy);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2024-11-06 12:23:00 +08:00
|
|
|
docCopy->setFilename(duplicateFn);
|
|
|
|
docCopy->setContext(context);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createDuplicateSpriteCommand()
|
|
|
|
{
|
|
|
|
return new DuplicateSpriteCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|