From b7f41b811a0146345c59c8b0b3669ff272a510ce Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 5 Dec 2019 12:05:18 -0300 Subject: [PATCH] Add Screenshot command --- data/gui.xml | 13 ++- data/strings/en.ini | 5 ++ src/app/CMakeLists.txt | 1 + src/app/commands/commands_list.h | 1 + src/app/commands/screenshot.cpp | 138 +++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/app/commands/screenshot.cpp diff --git a/data/gui.xml b/data/gui.xml index 4f6591e7c..c1e9a4287 100644 --- a/data/gui.xml +++ b/data/gui.xml @@ -497,7 +497,18 @@ - + + + + + + + + + + + + diff --git a/data/strings/en.ini b/data/strings/en.ini index 2c2ddf4d0..e75d44401 100644 --- a/data/strings/en.ini +++ b/data/strings/en.ini @@ -396,6 +396,11 @@ SaveFileAs = Save File As SaveFileCopyAs = Export SaveMask = Save Selection SavePalette = Save Palette +Screenshot = Screenshot +Screenshot_Open = Take & Open Screenshot +Screenshot_Save = Take & Save Screenshot +Screenshot_sRGB = (sRGB Color Profile) +Screenshot_DisplayCS = (Display Color Profile) Scroll = Scroll {0} ScrollCenter = Scroll to center of canvas SelectTile = Select Tile diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index b2a80fb79..2603378fd 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -294,6 +294,7 @@ if(ENABLE_UI) commands/filters/filter_preview.cpp commands/filters/filter_target_buttons.cpp commands/filters/filter_window.cpp + commands/screenshot.cpp file_selector.cpp modules/editors.cpp modules/gfx.cpp diff --git a/src/app/commands/commands_list.h b/src/app/commands/commands_list.h index f1aef97a6..062d0850f 100644 --- a/src/app/commands/commands_list.h +++ b/src/app/commands/commands_list.h @@ -127,6 +127,7 @@ FOR_EACH_COMMAND(ReverseFrames) FOR_EACH_COMMAND(Rotate) FOR_EACH_COMMAND(SaveMask) FOR_EACH_COMMAND(SavePalette) +FOR_EACH_COMMAND(Screenshot) FOR_EACH_COMMAND(Scroll) FOR_EACH_COMMAND(ScrollCenter) FOR_EACH_COMMAND(SelectTile) diff --git a/src/app/commands/screenshot.cpp b/src/app/commands/screenshot.cpp new file mode 100644 index 000000000..84f09f9e7 --- /dev/null +++ b/src/app/commands/screenshot.cpp @@ -0,0 +1,138 @@ +// Aseprite +// Copyright (C) 2019 Igara Studio S.A. +// +// 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/convert_color_profile.h" +#include "app/commands/command.h" +#include "app/commands/new_params.h" +#include "app/context.h" +#include "app/doc.h" +#include "app/file/file.h" +#include "app/i18n/strings.h" +#include "app/resource_finder.h" +#include "base/fs.h" +#include "doc/cel.h" +#include "doc/color.h" +#include "doc/layer.h" +#include "doc/sprite.h" +#include "fmt/format.h" +#include "os/display.h" +#include "os/surface.h" +#include "ui/manager.h" + +namespace app { + +using namespace ui; + +struct ScreenshotParams : public NewParams { + Param save { this, false, "save" }; + Param srgb { this, true, "srgb" }; +}; + +class ScreenshotCommand : public CommandWithNewParams { +public: + ScreenshotCommand(); + +protected: + void onExecute(Context* ctx) override; + std::string onGetFriendlyName() const override; +}; + +ScreenshotCommand::ScreenshotCommand() + : CommandWithNewParams(CommandId::Screenshot(), CmdUIOnlyFlag) +{ +} + +void ScreenshotCommand::onExecute(Context* ctx) +{ + if (!ctx->isUIAvailable()) + return; + + app::ResourceFinder rf(false); + rf.includeDesktopDir(""); + + os::Display* display = ui::Manager::getDefault()->getDisplay(); + os::Surface* surface = display->getSurface(); + std::string fn; + + if (params().save()) { + for (int i=0; i<10000; ++i) { + fn = base::join_path(rf.defaultFilename(), + fmt::format("screenshot-{}.png", i)); + if (!base::is_file(fn)) + break; + } + } + else { + fn = "screenshot.png"; + } + + doc::ImageSpec spec(doc::ColorMode::RGB, + surface->width(), + surface->height(), + 0, // Mask color + display->colorSpace()->gfxColorSpace()); + + doc::Sprite* spr = doc::Sprite::MakeStdSprite(spec); + static_cast(spr->firstLayer())->configureAsBackground(); + + doc::Cel* cel = spr->firstLayer()->cel(0); + doc::Image* img = cel->image(); + + for (int y=0; yheight(); ++y) { + for (int x=0; xwidth(); ++x) { + gfx::Color c = surface->getPixel(x, y); + + img->putPixel(x, y, doc::rgba(gfx::getr(c), + gfx::getg(c), + gfx::getb(c), 255)); + } + } + + std::unique_ptr doc(new Doc(spr)); + doc->setFilename(fn); + + // Convert color profile to sRGB + if (params().srgb()) + cmd::convert_color_profile(spr, gfx::ColorSpace::MakeSRGB()); + + if (params().save()) { + save_document(ctx, doc.get()); + } + else { + doc->setContext(ctx); + doc.release(); + } +} + +std::string ScreenshotCommand::onGetFriendlyName() const +{ + std::string name; + if (params().save()) + name = Strings::commands_Screenshot_Save(); + else + name = Strings::commands_Screenshot_Open(); + if (params().srgb()) { + name.push_back(' '); + name += Strings::commands_Screenshot_sRGB(); + } + else { + name.push_back(' '); + name += Strings::commands_Screenshot_DisplayCS(); + } + return name; +} + +Command* CommandFactory::createScreenshotCommand() +{ + return new ScreenshotCommand; +} + +} // namespace app