Export frame tags/layers to JSON data when --list-tags/layers is used

This commit is contained in:
David Capello 2015-11-04 19:34:23 -03:00
parent 4a67a96edd
commit d05dc56503
4 changed files with 72 additions and 9 deletions

View File

@ -448,10 +448,14 @@ void App::initialize(const AppOptions& options)
// --list-layers
else if (opt == &options.listLayers()) {
listLayers = true;
if (m_exporter)
m_exporter->setListLayers(true);
}
// --list-tags
else if (opt == &options.listTags()) {
listTags = true;
if (m_exporter)
m_exporter->setListFrameTags(true);
}
}
// File names aren't associated to any option

View File

@ -47,8 +47,8 @@ AppOptions::AppOptions(int argc, const char* argv[])
, m_crop(m_po.add("crop").requiresValue("x,y,width,height").description("Crop all the images to the given rectangle"))
, m_filenameFormat(m_po.add("filename-format").requiresValue("<fmt>").description("Special format to generate filenames"))
, m_script(m_po.add("script").requiresValue("<filename>").description("Execute a specific script"))
, m_listLayers(m_po.add("list-layers").description("List layers of the next given sprite"))
, m_listTags(m_po.add("list-tags").description("List tags of the next given sprite"))
, m_listLayers(m_po.add("list-layers").description("List layers of the next given sprite\nor include layers in JSON data"))
, m_listTags(m_po.add("list-tags").description("List tags of the next given sprite sprite\nor include frame tags in JSON data"))
, m_verbose(m_po.add("verbose").mnemonic('v').description("Explain what is being done"))
, m_help(m_po.add("help").mnemonic('?').description("Display this help and exits"))
, m_version(m_po.add("version").description("Output version information and exit"))

View File

@ -46,10 +46,11 @@ using namespace doc;
namespace {
std::string escape_path_for_json(const std::string& path)
std::string escape_for_json(const std::string& path)
{
std::string res = path;
base::replace_string(res, "\\", "\\\\");
base::replace_string(res, "\"", "\\\"");
return res;
}
@ -297,6 +298,8 @@ DocumentExporter::DocumentExporter()
, m_shapePadding(0)
, m_innerPadding(0)
, m_trimCels(false)
, m_listFrameTags(false)
, m_listLayers(false)
{
}
@ -593,10 +596,10 @@ void DocumentExporter::createDataFile(const Samples& samples, std::ostream& os,
gfx::Rect frameBounds = sample.inTextureBounds();
if (filename_as_key)
os << " \"" << escape_path_for_json(sample.filename()) << "\": {\n";
os << " \"" << escape_for_json(sample.filename()) << "\": {\n";
else if (filename_as_attr)
os << " {\n"
<< " \"filename\": \"" << escape_path_for_json(sample.filename()) << "\",\n";
<< " \"filename\": \"" << escape_for_json(sample.filename()) << "\",\n";
os << " \"frame\": { "
<< "\"x\": " << frameBounds.x << ", "
@ -621,19 +624,71 @@ void DocumentExporter::createDataFile(const Samples& samples, std::ostream& os,
else
os << "\n";
}
os << " " << frames_end;
os << " " << frames_end << ",\n"
// "meta" property
os << ",\n"
<< " \"meta\": {\n"
<< " \"app\": \"" << WEBSITE << "\",\n"
<< " \"version\": \"" << VERSION << "\",\n";
if (!m_textureFilename.empty())
os << " \"image\": \"" << escape_path_for_json(m_textureFilename).c_str() << "\",\n";
os << " \"image\": \"" << escape_for_json(m_textureFilename).c_str() << "\",\n";
os << " \"format\": \"" << (textureImage->pixelFormat() == IMAGE_RGB ? "RGBA8888": "I8") << "\",\n"
<< " \"size\": { "
<< "\"w\": " << textureImage->width() << ", "
<< "\"h\": " << textureImage->height() << " },\n"
<< " \"scale\": \"" << m_scale << "\"\n"
<< " }\n"
<< " \"scale\": \"" << m_scale << "\"";
// meta.frameTags
if (m_listFrameTags) {
os << ",\n"
<< " \"frameTags\": [";
bool firstTag = true;
for (auto& item : m_documents) {
Document* doc = item.doc;
Sprite* sprite = doc->sprite();
for (FrameTag* tag : sprite->frameTags()) {
if (firstTag)
firstTag = false;
else
os << ",";
os << "\n { \"name\": \"" << escape_for_json(tag->name()) << "\","
<< " \"from\": " << tag->fromFrame() << ","
<< " \"to\": " << tag->toFrame() << " }";
}
}
os << "\n ]";
}
// meta.layers
if (m_listLayers) {
os << ",\n"
<< " \"layers\": [";
bool firstLayer = true;
for (auto& item : m_documents) {
Document* doc = item.doc;
Sprite* sprite = doc->sprite();
std::vector<Layer*> layers;
sprite->getLayersList(layers);
for (Layer* layer : layers) {
if (firstLayer)
firstLayer = false;
else
os << ",";
os << "\n { \"name\": \"" << escape_for_json(layer->name()) << "\" }";
}
}
os << "\n ]";
}
os << "\n }\n"
<< "}\n";
}

View File

@ -60,6 +60,8 @@ namespace app {
void setInnerPadding(int padding) { m_innerPadding = padding; }
void setTrimCels(bool trim) { m_trimCels = trim; }
void setFilenameFormat(const std::string& format) { m_filenameFormat = format; }
void setListFrameTags(bool value) { m_listFrameTags = value; }
void setListLayers(bool value) { m_listLayers = value; }
void addDocument(Document* document,
doc::Layer* layer = nullptr,
@ -121,6 +123,8 @@ namespace app {
Items m_documents;
std::string m_filenameFormat;
doc::ImageBufferPtr m_sampleRenderBuf;
bool m_listFrameTags;
bool m_listLayers;
DISABLE_COPYING(DocumentExporter);
};