2016-12-02 06:27:57 +08:00
|
|
|
// Aseprite
|
2017-03-27 00:33:12 +08:00
|
|
|
// Copyright (C) 2016-2017 David Capello
|
2016-12-02 06:27:57 +08:00
|
|
|
//
|
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/cmd/set_layer_opacity.h"
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
#include "app/context.h"
|
|
|
|
#include "app/context_access.h"
|
2017-12-01 10:41:45 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2016-12-02 06:27:57 +08:00
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/transaction.h"
|
2017-03-27 00:33:12 +08:00
|
|
|
#include "app/ui/timeline/timeline.h"
|
2016-12-02 06:27:57 +08:00
|
|
|
#include "doc/layer.h"
|
2017-12-01 10:41:45 +08:00
|
|
|
#include "fmt/format.h"
|
2016-12-02 06:27:57 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
class LayerOpacityCommand : public Command {
|
|
|
|
public:
|
|
|
|
LayerOpacityCommand();
|
|
|
|
|
|
|
|
protected:
|
2017-12-01 01:51:13 +08:00
|
|
|
bool onNeedsParams() const override { return true; }
|
2016-12-02 06:27:57 +08:00
|
|
|
void onLoadParams(const Params& params) override;
|
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
|
|
|
std::string onGetFriendlyName() const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_opacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
LayerOpacityCommand::LayerOpacityCommand()
|
2017-12-01 10:41:45 +08:00
|
|
|
: Command("LayerOpacity", CmdUIOnlyFlag)
|
2016-12-02 06:27:57 +08:00
|
|
|
{
|
|
|
|
m_opacity = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerOpacityCommand::onLoadParams(const Params& params)
|
|
|
|
{
|
|
|
|
m_opacity = params.get_as<int>("opacity");
|
|
|
|
m_opacity = MID(0, m_opacity, 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayerOpacityCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::HasActiveLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerOpacityCommand::onExecute(Context* context)
|
|
|
|
{
|
|
|
|
ContextWriter writer(context);
|
|
|
|
Layer* layer = writer.layer();
|
|
|
|
if (!layer ||
|
|
|
|
!layer->isImage() ||
|
|
|
|
static_cast<LayerImage*>(layer)->opacity() == m_opacity)
|
|
|
|
return;
|
|
|
|
|
|
|
|
{
|
|
|
|
Transaction transaction(writer.context(), "Set Layer Opacity");
|
|
|
|
|
|
|
|
// TODO the range of selected frames should be in doc::Site.
|
2016-12-03 06:12:46 +08:00
|
|
|
SelectedLayers selLayers;
|
2016-12-02 06:27:57 +08:00
|
|
|
auto range = App::instance()->timeline()->range();
|
|
|
|
if (range.enabled()) {
|
2016-12-03 06:12:46 +08:00
|
|
|
selLayers = range.selectedLayers();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selLayers.insert(writer.layer());
|
|
|
|
}
|
2016-12-02 06:27:57 +08:00
|
|
|
|
2016-12-03 06:12:46 +08:00
|
|
|
for (auto layer : selLayers) {
|
|
|
|
if (layer->isImage())
|
2016-12-02 06:27:57 +08:00
|
|
|
transaction.execute(
|
|
|
|
new cmd::SetLayerOpacity(static_cast<LayerImage*>(layer), m_opacity));
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
update_screen_for_document(writer.document());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LayerOpacityCommand::onGetFriendlyName() const
|
|
|
|
{
|
2017-12-01 10:41:45 +08:00
|
|
|
return fmt::format(getBaseFriendlyName(),
|
|
|
|
m_opacity,
|
|
|
|
int(100.0 * m_opacity / 255.0));
|
2016-12-02 06:27:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createLayerOpacityCommand()
|
|
|
|
{
|
|
|
|
return new LayerOpacityCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace app
|