mirror of https://github.com/aseprite/aseprite.git
Added parameters to PasteTextCommand
This commit is contained in:
parent
260d9fbd52
commit
f0f9e34b94
|
@ -10,11 +10,18 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "app/app.h"
|
#include "app/app.h"
|
||||||
|
#include "app/cmd/copy_region.h"
|
||||||
|
#include "app/cmd/patch_cel.h"
|
||||||
#include "app/color_utils.h"
|
#include "app/color_utils.h"
|
||||||
#include "app/commands/command.h"
|
#include "app/commands/command.h"
|
||||||
|
#include "app/commands/commands.h"
|
||||||
|
#include "app/commands/new_params.h"
|
||||||
#include "app/console.h"
|
#include "app/console.h"
|
||||||
#include "app/context.h"
|
#include "app/context.h"
|
||||||
|
#include "app/context_access.h"
|
||||||
#include "app/pref/preferences.h"
|
#include "app/pref/preferences.h"
|
||||||
|
#include "app/site.h"
|
||||||
|
#include "app/tx.h"
|
||||||
#include "app/ui/editor/editor.h"
|
#include "app/ui/editor/editor.h"
|
||||||
#include "app/ui/timeline/timeline.h"
|
#include "app/ui/timeline/timeline.h"
|
||||||
#include "app/util/render_text.h"
|
#include "app/util/render_text.h"
|
||||||
|
@ -22,6 +29,8 @@
|
||||||
#include "doc/image_ref.h"
|
#include "doc/image_ref.h"
|
||||||
#include "render/dithering.h"
|
#include "render/dithering.h"
|
||||||
#include "render/quantization.h"
|
#include "render/quantization.h"
|
||||||
|
#include "render/rasterize.h"
|
||||||
|
#include "render/render.h"
|
||||||
#include "ui/manager.h"
|
#include "ui/manager.h"
|
||||||
|
|
||||||
#include "paste_text.xml.h"
|
#include "paste_text.xml.h"
|
||||||
|
@ -30,7 +39,17 @@ namespace app {
|
||||||
|
|
||||||
static std::string last_text_used;
|
static std::string last_text_used;
|
||||||
|
|
||||||
class PasteTextCommand : public Command {
|
struct PasteTextParams : public NewParams {
|
||||||
|
Param<bool> ui{ this, true, "ui" };
|
||||||
|
Param<app::Color> color{ this, app::Color::fromMask(), "color" };
|
||||||
|
Param<std::string> text{ this, "", "text" };
|
||||||
|
Param<std::string> fontName{ this, "Aseprite", "fontName" };
|
||||||
|
Param<double> fontSize{ this, 6, "fontSize" };
|
||||||
|
Param<int> x{ this, 0, "x" };
|
||||||
|
Param<int> y{ this, 0, "y" };
|
||||||
|
};
|
||||||
|
|
||||||
|
class PasteTextCommand : public CommandWithNewParams<PasteTextParams> {
|
||||||
public:
|
public:
|
||||||
PasteTextCommand();
|
PasteTextCommand();
|
||||||
|
|
||||||
|
@ -39,14 +58,14 @@ protected:
|
||||||
void onExecute(Context* ctx) override;
|
void onExecute(Context* ctx) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
PasteTextCommand::PasteTextCommand() : Command(CommandId::PasteText())
|
PasteTextCommand::PasteTextCommand() : CommandWithNewParams(CommandId::PasteText())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PasteTextCommand::onEnabled(Context* ctx)
|
bool PasteTextCommand::onEnabled(Context* ctx)
|
||||||
{
|
{
|
||||||
return ctx->isUIAvailable() && ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
||||||
ContextFlags::ActiveLayerIsEditable);
|
ContextFlags::ActiveLayerIsEditable);
|
||||||
}
|
}
|
||||||
|
|
||||||
class PasteTextWindow : public app::gen::PasteText {
|
class PasteTextWindow : public app::gen::PasteText {
|
||||||
|
@ -62,55 +81,104 @@ public:
|
||||||
|
|
||||||
void PasteTextCommand::onExecute(Context* ctx)
|
void PasteTextCommand::onExecute(Context* ctx)
|
||||||
{
|
{
|
||||||
auto editor = Editor::activeEditor();
|
const bool ui = params().ui() && ctx->isUIAvailable();
|
||||||
if (editor == nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Preferences& pref = Preferences::instance();
|
|
||||||
FontInfo fontInfo = FontInfo::getFromPreferences();
|
FontInfo fontInfo = FontInfo::getFromPreferences();
|
||||||
PasteTextWindow window(fontInfo, pref.colorBar.fgColor());
|
Preferences& pref = Preferences::instance();
|
||||||
|
|
||||||
window.userText()->setText(last_text_used);
|
std::string text;
|
||||||
|
app::Color color;
|
||||||
|
ui::Paint paint;
|
||||||
|
|
||||||
window.openWindowInForeground();
|
if (ui) {
|
||||||
if (window.closer() != window.ok())
|
PasteTextWindow window(fontInfo, pref.colorBar.fgColor());
|
||||||
return;
|
|
||||||
|
|
||||||
last_text_used = window.userText()->text();
|
window.userText()->setText(params().text().empty() ? last_text_used : params().text());
|
||||||
|
|
||||||
fontInfo = window.fontInfo();
|
window.openWindowInForeground();
|
||||||
fontInfo.updatePreferences();
|
if (window.closer() != window.ok())
|
||||||
|
return;
|
||||||
|
|
||||||
|
text = window.userText()->text();
|
||||||
|
last_text_used = text;
|
||||||
|
color = window.fontColor()->getColor();
|
||||||
|
paint = window.fontFace()->paint();
|
||||||
|
|
||||||
|
fontInfo = window.fontInfo();
|
||||||
|
fontInfo.updatePreferences();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
text = params().text();
|
||||||
|
color = params().color.isSet() ? params().color() : pref.colorBar.fgColor();
|
||||||
|
|
||||||
|
FontInfo info(FontInfo::Type::Unknown, params().fontName(), params().fontSize());
|
||||||
|
fontInfo = info;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::string text = window.userText()->text();
|
|
||||||
app::Color color = window.fontColor()->getColor();
|
|
||||||
|
|
||||||
ui::Paint paint = window.fontFace()->paint();
|
|
||||||
paint.color(color_utils::color_for_ui(color));
|
paint.color(color_utils::color_for_ui(color));
|
||||||
|
|
||||||
doc::ImageRef image = render_text(fontInfo, text, paint);
|
doc::ImageRef image = render_text(fontInfo, text, paint);
|
||||||
if (image) {
|
if (!image)
|
||||||
Sprite* sprite = editor->sprite();
|
return;
|
||||||
if (image->pixelFormat() != sprite->pixelFormat()) {
|
|
||||||
RgbMap* rgbmap = sprite->rgbMap(editor->frame());
|
|
||||||
image.reset(render::convert_pixel_format(image.get(),
|
|
||||||
NULL,
|
|
||||||
sprite->pixelFormat(),
|
|
||||||
render::Dithering(),
|
|
||||||
rgbmap,
|
|
||||||
sprite->palette(editor->frame()),
|
|
||||||
false,
|
|
||||||
sprite->transparentColor()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO we don't support pasting text in multiple cels at the
|
auto site = ctx->activeSite();
|
||||||
// moment, so we clear the range here (same as in
|
Sprite* sprite = site.sprite();
|
||||||
// clipboard::paste())
|
if (image->pixelFormat() != sprite->pixelFormat()) {
|
||||||
if (auto timeline = App::instance()->timeline())
|
RgbMap* rgbmap = sprite->rgbMap(site.frame());
|
||||||
timeline->clearAndInvalidateRange();
|
image.reset(render::convert_pixel_format(image.get(),
|
||||||
|
NULL,
|
||||||
editor->pasteImage(image.get());
|
sprite->pixelFormat(),
|
||||||
|
render::Dithering(),
|
||||||
|
rgbmap,
|
||||||
|
sprite->palette(site.frame()),
|
||||||
|
false,
|
||||||
|
sprite->transparentColor()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO we don't support pasting text in multiple cels at the
|
||||||
|
// moment, so we clear the range here (same as in
|
||||||
|
// clipboard::paste())
|
||||||
|
if (auto timeline = App::instance()->timeline())
|
||||||
|
timeline->clearAndInvalidateRange();
|
||||||
|
|
||||||
|
auto point = sprite->bounds().center() - gfx::Point(image->size().w / 2, image->size().h / 2);
|
||||||
|
if (params().x.isSet())
|
||||||
|
point.x = params().x();
|
||||||
|
if (params().y.isSet())
|
||||||
|
point.y = params().y();
|
||||||
|
|
||||||
|
if (ui) {
|
||||||
|
// TODO: Do we want to make this selectable result available when not using UI?
|
||||||
|
Editor::activeEditor()->pasteImage(image.get(), nullptr, &point);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContextWriter writer(ctx);
|
||||||
|
Tx tx(writer, "Paste Text");
|
||||||
|
ImageRef finalImage = image;
|
||||||
|
if (writer.cel()->image()) {
|
||||||
|
gfx::Rect celRect(point, image->size());
|
||||||
|
ASSERT(!celRect.isEmpty() && celRect.x >= 0 && celRect.y >= 0);
|
||||||
|
finalImage.reset(
|
||||||
|
doc::crop_image(writer.cel()->image(), celRect, writer.cel()->image()->maskColor()));
|
||||||
|
render::Render render;
|
||||||
|
render.setNewBlend(pref.experimental.newBlend());
|
||||||
|
render.setBgOptions(render::BgOptions::MakeTransparent());
|
||||||
|
render.renderImage(finalImage.get(),
|
||||||
|
image.get(),
|
||||||
|
writer.palette(),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
255,
|
||||||
|
doc::BlendMode::NORMAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
tx(new cmd::CopyRegion(writer.cel()->image(),
|
||||||
|
finalImage.get(),
|
||||||
|
gfx::Region(finalImage->bounds()),
|
||||||
|
point));
|
||||||
|
tx.commit();
|
||||||
}
|
}
|
||||||
catch (const std::exception& ex) {
|
catch (const std::exception& ex) {
|
||||||
Console::showException(ex);
|
Console::showException(ex);
|
||||||
|
|
|
@ -10,12 +10,12 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "app/commands/command.h"
|
#include "app/commands/command.h"
|
||||||
|
#include "app/commands/new_params.h"
|
||||||
#include "app/context_access.h"
|
#include "app/context_access.h"
|
||||||
#include "app/file_selector.h"
|
#include "app/file_selector.h"
|
||||||
#include "app/i18n/strings.h"
|
#include "app/i18n/strings.h"
|
||||||
#include "app/util/msk_file.h"
|
#include "app/util/msk_file.h"
|
||||||
#include "base/fs.h"
|
#include "base/fs.h"
|
||||||
#include "new_params.h"
|
|
||||||
#include "ui/alert.h"
|
#include "ui/alert.h"
|
||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
#include "app/app.h"
|
#include "app/app.h"
|
||||||
#include "app/commands/commands.h"
|
#include "app/commands/commands.h"
|
||||||
|
#include "app/commands/new_params.h"
|
||||||
#include "app/commands/params.h"
|
#include "app/commands/params.h"
|
||||||
#include "app/context.h"
|
#include "app/context.h"
|
||||||
#include "app/doc.h"
|
#include "app/doc.h"
|
||||||
|
@ -19,7 +20,6 @@
|
||||||
#include "app/i18n/strings.h"
|
#include "app/i18n/strings.h"
|
||||||
#include "app/modules/palettes.h"
|
#include "app/modules/palettes.h"
|
||||||
#include "base/fs.h"
|
#include "base/fs.h"
|
||||||
#include "new_params.h"
|
|
||||||
#include "ui/alert.h"
|
#include "ui/alert.h"
|
||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
Loading…
Reference in New Issue