Merge branch 'cli-options-for-thumbnails'

This commit is contained in:
David Capello 2016-04-04 13:12:30 -03:00
commit 5847624d0e
3 changed files with 62 additions and 10 deletions

View File

@ -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 from,to
else if (opt == &options.frameRange()) {
frameRange = value.value();
}
// --ignore-empty
else if (opt == &options.ignoreEmpty()) {
ignoreEmpty = true;
@ -350,16 +356,19 @@ void App::initialize(const AppOptions& options)
else if (opt == &options.trim()) {
trim = true;
}
// --crop
// --crop x,y,width,height
else if (opt == &options.crop()) {
std::vector<std::string> parts;
base::split_string(value.value(), parts, ",");
if (parts.size() == 4) {
cropParams.set("x", parts[0].c_str());
cropParams.set("y", parts[1].c_str());
cropParams.set("width", parts[2].c_str());
cropParams.set("height", parts[3].c_str());
}
if (parts.size() < 4)
throw std::runtime_error("--crop needs four parameters separated by comma (,)\n"
"Usage: --crop x,y,width,height\n"
"E.g. --crop 0,0,32,32");
cropParams.set("x", parts[0].c_str());
cropParams.set("y", parts[1].c_str());
cropParams.set("width", parts[2].c_str());
cropParams.set("height", parts[3].c_str());
}
// --filename-format
else if (opt == &options.filenameFormat()) {
@ -489,6 +498,32 @@ void App::initialize(const AppOptions& options)
ctx->executeCommand(command);
}
}
// --shrink-to <width,height>
else if (opt == &options.shrinkTo()) {
std::vector<std::string> dimensions;
base::split_string(value.value(), dimensions, ",");
if (dimensions.size() < 2)
throw std::runtime_error("--shrink-to needs two parameters separated by comma (,)\n"
"Usage: --shrink-to width,height\n"
"E.g. --shrink-to 128,64");
double maxWidth = base::convert_to<double>(dimensions[0]);
double maxHeight = base::convert_to<double>(dimensions[1]);
double scaleWidth, scaleHeight, scale;
// Shrink all sprites if needed
for (auto doc : ctx->documents()) {
ctx->setActiveDocument(static_cast<app::Document*>(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 = MIN(scaleWidth, scaleHeight);
Command* command = CommandsModule::instance()->getCommandByName(CommandId::SpriteSize);
static_cast<SpriteSizeCommand*>(command)->setScale(scale, scale);
ctx->executeCommand(command);
}
}
}
// --script <filename>
else if (opt == &options.script()) {
std::string script = value.value();
@ -549,11 +584,22 @@ 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<std::string> splitRange;
base::split_string(frameRange, splitRange, ",");
if (splitRange.size() < 2)
throw std::runtime_error("--frame-range needs two parameters separated by comma (,)\n"
"Usage: --frame-range from,to\n"
"E.g. --frame-range 0,99");
frameTag = new FrameTag(base::convert_to<frame_t>(splitRange[0]),
base::convert_to<frame_t>(splitRange[1]));
}
if (!importLayer.empty()) {
Layer* foundLayer = NULL;

View File

@ -30,8 +30,9 @@ 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("<filename>").description("Save the last given document with other format"))
, m_scale(m_po.add("scale").requiresValue("<factor>").description("Resize all previous opened documents"))
, m_shrinkTo(m_po.add("shrink-to").requiresValue("width,height").description("Shrink each sprite if it is\nlarger than width or height"))
, m_data(m_po.add("data").requiresValue("<filename.json>").description("File to store the sprite sheet metadata"))
, m_format(m_po.add("format").requiresValue("<format>").description("Format to export the data file (json-hash, json-array)"))
, m_format(m_po.add("format").requiresValue("<format>").description("Format to export the data file\n(json-hash, json-array)"))
, m_sheet(m_po.add("sheet").requiresValue("<filename.png>").description("Image file to save the texture"))
, m_sheetWidth(m_po.add("sheet-width").requiresValue("<pixels>").description("Sprite sheet width"))
, m_sheetHeight(m_po.add("sheet-height").requiresValue("<pixels>").description("Sprite sheet height"))
@ -41,6 +42,7 @@ AppOptions::AppOptions(int argc, const char* argv[])
, m_layer(m_po.add("layer").alias("import-layer").requiresValue("<name>").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("<name>").description("Include tagged frames in the sheet"))
, m_frameRange(m_po.add("frame-range").requiresValue("from,to").description("Only export frames in the [from,to] range"))
, m_ignoreEmpty(m_po.add("ignore-empty").description("Do not export empty frames/cels"))
, m_borderPadding(m_po.add("border-padding").requiresValue("<value>").description("Add padding on the texture borders"))
, m_shapePadding(m_po.add("shape-padding").requiresValue("<value>").description("Add padding between frames"))

View File

@ -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;