From 34c1af0a1b21ce870c9ff24a4823b2b36ce7f0e9 Mon Sep 17 00:00:00 2001 From: Gabriel Rauter Date: Wed, 23 Mar 2016 21:27:35 +0100 Subject: [PATCH] New cmdl parameters --range and --shrink-to --frame-range lets the user define a range from frame to frame instead of a frame tag. example --frame-range "0:1" exports 2 frame. frame 0 and frame 1. --shrink-to shrinks the sprite into the boundarys of width and height keeping the original aspect ratio. --- src/app/app.cpp | 36 ++++++++++++++++++++++++++++++++++-- src/app/app_options.cpp | 2 ++ src/app/app_options.h | 4 ++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index b91cf3712..869542e44 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -53,6 +53,7 @@ #include "app/ui_context.h" #include "app/util/clipboard.h" #include "app/webserver.h" +#include "base/convert_to.h" #include "base/exception.h" #include "base/fs.h" #include "base/path.h" @@ -253,6 +254,7 @@ void App::initialize(const AppOptions& options) std::string importLayerSaveAs; std::string filenameFormat; std::string frameTagName; + std::string frameRange; for (const auto& value : options.values()) { const AppOptions::Option* opt = value.option(); @@ -327,6 +329,10 @@ void App::initialize(const AppOptions& options) else if (opt == &options.frameTag()) { frameTagName = value.value(); } + // --frame-range + else if (opt == &options.frameRange()) { + frameRange = value.value(); + } // --ignore-empty else if (opt == &options.ignoreEmpty()) { ignoreEmpty = true; @@ -489,6 +495,27 @@ void App::initialize(const AppOptions& options) ctx->executeCommand(command); } } + // --shrink-to + else if (opt == &options.shrinkTo()) { + std::vector dimensions; + base::split_string(value.value(), dimensions, "x"); + double maxWidth = base::convert_to(dimensions.at(0)); + double maxHeight = base::convert_to(dimensions.at(1)); + double scaleWidth, scaleHeight, scale; + + // Shrink all sprites if needed + for (auto doc : ctx->documents()) { + ctx->setActiveDocument(static_cast(doc)); + scaleWidth = doc->width() > maxWidth ? maxWidth / doc->width() : 1.0; + scaleHeight = doc->height() > maxHeight ? maxHeight / doc->height() : 1.0; + if (scaleWidth < 1.0 || scaleHeight < 1.0) { + scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight; + Command* command = CommandsModule::instance()->getCommandByName(CommandId::SpriteSize); + static_cast(command)->setScale(scale, scale); + ctx->executeCommand(command); + } + } + } // --script else if (opt == &options.script()) { std::string script = value.value(); @@ -549,11 +576,16 @@ void App::initialize(const AppOptions& options) for (FrameTag* tag : doc->sprite()->frameTags()) std::cout << tag->name() << "\n"; } - if (m_exporter) { FrameTag* frameTag = nullptr; - if (!frameTagName.empty()) + if (!frameTagName.empty()) { frameTag = doc->sprite()->frameTags().getByName(frameTagName); + } else if (!frameRange.empty()) { + std::vector splitRange; + base::split_string(frameRange, splitRange, ":"); + frameTag = new FrameTag(base::convert_to(splitRange.at(0)), + base::convert_to(splitRange.at(1))); + } if (!importLayer.empty()) { Layer* foundLayer = NULL; diff --git a/src/app/app_options.cpp b/src/app/app_options.cpp index 72fd91937..cd5f1a0bf 100644 --- a/src/app/app_options.cpp +++ b/src/app/app_options.cpp @@ -30,6 +30,7 @@ AppOptions::AppOptions(int argc, const char* argv[]) , m_batch(m_po.add("batch").mnemonic('b').description("Do not start the UI")) , m_saveAs(m_po.add("save-as").requiresValue("").description("Save the last given document with other format")) , m_scale(m_po.add("scale").requiresValue("").description("Resize all previous opened documents")) + , m_shrinkTo(m_po.add("shrink-to").requiresValue("").description("Shrink to size if the Sprite is larger than width or height")) , m_data(m_po.add("data").requiresValue("").description("File to store the sprite sheet metadata")) , m_format(m_po.add("format").requiresValue("").description("Format to export the data file (json-hash, json-array)")) , m_sheet(m_po.add("sheet").requiresValue("").description("Image file to save the texture")) @@ -41,6 +42,7 @@ AppOptions::AppOptions(int argc, const char* argv[]) , m_layer(m_po.add("layer").alias("import-layer").requiresValue("").description("Include just the given layer in the sheet")) , m_allLayers(m_po.add("all-layers").description("Make all layers visible\nBy default hidden layers will be ignored")) , m_frameTag(m_po.add("frame-tag").requiresValue("").description("Include tagged frames in the sheet")) + , m_frameRange(m_po.add("frame-range").requiresValue("").description("Include frames from:to in the sheet")) , m_ignoreEmpty(m_po.add("ignore-empty").description("Do not export empty frames/cels")) , m_borderPadding(m_po.add("border-padding").requiresValue("").description("Add padding on the texture borders")) , m_shapePadding(m_po.add("shape-padding").requiresValue("").description("Add padding between frames")) diff --git a/src/app/app_options.h b/src/app/app_options.h index 921ccbfee..5c0ccc824 100644 --- a/src/app/app_options.h +++ b/src/app/app_options.h @@ -44,6 +44,7 @@ public: // Export options const Option& saveAs() const { return m_saveAs; } const Option& scale() const { return m_scale; } + const Option& shrinkTo() const { return m_shrinkTo; } const Option& data() const { return m_data; } const Option& format() const { return m_format; } const Option& sheet() const { return m_sheet; } @@ -55,6 +56,7 @@ public: const Option& layer() const { return m_layer; } const Option& allLayers() const { return m_allLayers; } const Option& frameTag() const { return m_frameTag; } + const Option& frameRange() const { return m_frameRange; } const Option& ignoreEmpty() const { return m_ignoreEmpty; } const Option& borderPadding() const { return m_borderPadding; } const Option& shapePadding() const { return m_shapePadding; } @@ -84,6 +86,7 @@ private: Option& m_batch; Option& m_saveAs; Option& m_scale; + Option& m_shrinkTo; Option& m_data; Option& m_format; Option& m_sheet; @@ -95,6 +98,7 @@ private: Option& m_layer; Option& m_allLayers; Option& m_frameTag; + Option& m_frameRange; Option& m_ignoreEmpty; Option& m_borderPadding; Option& m_shapePadding;