Add --format option to specify JSON array format (fix #641)

This commit is contained in:
David Capello 2015-04-30 12:29:31 -03:00
parent 90dd876654
commit c81aa88fc7
5 changed files with 49 additions and 6 deletions

View File

@ -252,6 +252,19 @@ void App::initialize(const AppOptions& options)
if (m_exporter)
m_exporter->setDataFilename(value.value());
}
// --format <format>
else if (opt == &options.format()) {
if (m_exporter) {
DocumentExporter::DataFormat format = DocumentExporter::DefaultDataFormat;
if (value.value() == "json-hash")
format = DocumentExporter::JsonHashDataFormat;
else if (value.value() == "json-array")
format = DocumentExporter::JsonArrayDataFormat;
m_exporter->setDataFormat(format);
}
}
// --sheet <file.png>
else if (opt == &options.sheet()) {
if (m_exporter)

View File

@ -31,6 +31,7 @@ AppOptions::AppOptions(int argc, const char* argv[])
, 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_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_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"))

View File

@ -39,6 +39,7 @@ public:
const Option& saveAs() const { return m_saveAs; }
const Option& scale() const { return m_scale; }
const Option& data() const { return m_data; }
const Option& format() const { return m_format; }
const Option& sheet() const { return m_sheet; }
const Option& sheetWidth() const { return m_sheetWidth; }
const Option& sheetHeight() const { return m_sheetHeight; }
@ -72,6 +73,7 @@ private:
Option& m_saveAs;
Option& m_scale;
Option& m_data;
Option& m_format;
Option& m_sheet;
Option& m_sheetWidth;
Option& m_sheetHeight;

View File

@ -490,7 +490,28 @@ void DocumentExporter::renderTexture(const Samples& samples, Image* textureImage
void DocumentExporter::createDataFile(const Samples& samples, std::ostream& os, Image* textureImage)
{
os << "{ \"frames\": {\n";
std::string frames_begin;
std::string frames_end;
bool filename_as_key = false;
bool filename_as_attr = false;
// TODO we should use some string templates system here
switch (m_dataFormat) {
case JsonHashDataFormat:
frames_begin = "{";
frames_end = "}";
filename_as_key = true;
filename_as_attr = false;
break;
case JsonArrayDataFormat:
frames_begin = "[";
frames_end = "]";
filename_as_key = false;
filename_as_attr = true;
break;
}
os << "{ \"frames\": " << frames_begin << "\n";
for (Samples::const_iterator
it = samples.begin(),
end = samples.end(); it != end; ) {
@ -499,8 +520,13 @@ void DocumentExporter::createDataFile(const Samples& samples, std::ostream& os,
gfx::Rect spriteSourceBounds = sample.trimmedBounds();
gfx::Rect frameBounds = sample.inTextureBounds();
os << " \"" << sample.filename() << "\": {\n"
<< " \"frame\": { "
if (filename_as_key)
os << " \"" << sample.filename() << "\": {\n";
else if (filename_as_attr)
os << " {\n"
<< " \"filename\": \"" << sample.filename() << "\",\n";
os << " \"frame\": { "
<< "\"x\": " << frameBounds.x << ", "
<< "\"y\": " << frameBounds.y << ", "
<< "\"w\": " << frameBounds.w << ", "
@ -524,7 +550,7 @@ void DocumentExporter::createDataFile(const Samples& samples, std::ostream& os,
os << "\n";
}
os << " },\n"
os << " " << frames_end << ",\n"
<< " \"meta\": {\n"
<< " \"app\": \"" << WEBSITE << "\",\n"
<< " \"version\": \"" << VERSION << "\",\n";

View File

@ -28,8 +28,9 @@ namespace app {
class DocumentExporter {
public:
enum DataFormat {
JsonDataFormat,
DefaultDataFormat = JsonDataFormat
JsonHashDataFormat,
JsonArrayDataFormat,
DefaultDataFormat = JsonHashDataFormat
};
enum TextureFormat {