2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2018-08-21 03:00:59 +08:00
|
|
|
// Copyright (C) 2001-2018 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
|
|
|
|
2017-05-19 22:10:56 +08:00
|
|
|
#include "app/cmd/flatten_layers.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/context_access.h"
|
2018-09-18 11:19:24 +08:00
|
|
|
#include "app/doc_range.h"
|
|
|
|
#include "app/i18n/strings.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.h"
|
2017-05-19 22:10:56 +08:00
|
|
|
#include "app/ui/color_bar.h"
|
2018-09-18 11:19:24 +08:00
|
|
|
#include "app/ui/timeline/timeline.h"
|
|
|
|
#include "doc/layer.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class FlattenLayersCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
FlattenLayersCommand();
|
|
|
|
|
|
|
|
protected:
|
2018-09-18 11:19:24 +08:00
|
|
|
void onLoadParams(const Params& params) override;
|
2015-10-01 03:34:43 +08:00
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2018-09-18 11:19:24 +08:00
|
|
|
std::string onGetFriendlyName() const override;
|
|
|
|
|
|
|
|
bool m_visibleOnly;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
FlattenLayersCommand::FlattenLayersCommand()
|
2017-12-02 02:10:21 +08:00
|
|
|
: Command(CommandId::FlattenLayers(), CmdUIOnlyFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2018-09-18 11:19:24 +08:00
|
|
|
m_visibleOnly = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlattenLayersCommand::onLoadParams(const Params& params)
|
|
|
|
{
|
|
|
|
std::string visibleOnly = params.get("visibleOnly");
|
|
|
|
m_visibleOnly = (visibleOnly == "true");
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FlattenLayersCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlattenLayersCommand::onExecute(Context* context)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(context);
|
|
|
|
Sprite* sprite = writer.sprite();
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2018-08-21 03:00:59 +08:00
|
|
|
Tx tx(writer.context(), "Flatten Layers");
|
2018-09-18 11:19:24 +08:00
|
|
|
|
|
|
|
// TODO the range of selected layers should be in app::Site.
|
|
|
|
DocRange range;
|
|
|
|
|
|
|
|
if (m_visibleOnly) {
|
|
|
|
for (auto layer : sprite->root()->layers())
|
|
|
|
if (layer->isVisible())
|
|
|
|
range.selectLayer(layer);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#ifdef ENABLE_UI
|
|
|
|
if (context->isUIAvailable())
|
|
|
|
range = App::instance()->timeline()->range();
|
|
|
|
#endif
|
|
|
|
|
2018-09-18 20:24:52 +08:00
|
|
|
// If the range is not selected or we have only one image layer
|
|
|
|
// selected, we'll flatten all layers.
|
|
|
|
if (!range.enabled() ||
|
|
|
|
(range.selectedLayers().size() == 1 &&
|
|
|
|
(*range.selectedLayers().begin())->isImage())) {
|
2018-09-18 11:19:24 +08:00
|
|
|
for (auto layer : sprite->root()->layers())
|
|
|
|
range.selectLayer(layer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx(new cmd::FlattenLayers(sprite, range.selectedLayers()));
|
2018-08-21 03:00:59 +08:00
|
|
|
tx.commit();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2018-09-18 11:19:24 +08:00
|
|
|
|
|
|
|
#ifdef ENABLE_UI
|
2013-03-12 07:29:45 +08:00
|
|
|
update_screen_for_document(writer.document());
|
2018-09-18 11:19:24 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FlattenLayersCommand::onGetFriendlyName() const
|
|
|
|
{
|
|
|
|
if (m_visibleOnly)
|
|
|
|
return Strings::commands_FlattenLayers_Visible();
|
|
|
|
else
|
|
|
|
return Strings::commands_FlattenLayers();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createFlattenLayersCommand()
|
|
|
|
{
|
|
|
|
return new FlattenLayersCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|