Skip to content

Commit

Permalink
Allow filename sequence for description to output each slice separately
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Jul 25, 2024
1 parent 8ba555c commit d68bfd2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ The following table contains a list of all definitions, with the item each affec
| align-pivot | sprite | [key] | Aligns all sprites with identical _keys_, so their pivot points have identical offsets within the sprites' bounds. |
| tag | sprite | key, [value] | Adds a tag to a sprite (_value_ defaults to an empty string). See list of available [variables](#variables). |
| data | sprite | key, value | Adds a user defined data entry to a sprite. |
| **description** | - | filename | Adds an additional location where the output description should be written. |
| **description** | - | filename | Adds an additional location where the output description should be written. When the _filename_ is a sequence e.g. `"slice{0-}.plist"` then each _slice_ is output to a separate file. |
| template | description | filename | Sets the template which should be used for generating the output description. |
| set | - | key, value | Sets a variable value, which can be accessed in different places using `{{key}}`. See a list of existing [variables](#variables). |
| group | - | - | Can be used for opening a new scope, to limit for example the effect of a tag. |
Expand Down
88 changes: 77 additions & 11 deletions src/output_description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,51 @@ namespace {
}, variant);
return string;
}

void output_description(std::ostream& os,
const std::filesystem::path& template_filename,
const nlohmann::json& json) {
if (!template_filename.empty()) {
auto env = setup_inja_environment();
env.render_to(os, env.parse_template(
path_to_utf8(template_filename)), json);
}
else {
os << json.dump(1, '\t');
}
}

auto filter_by_slice(int slice_index,
const Slice& sole_slice,
const std::vector<Sprite>& sprites,
const std::vector<Texture>& textures) ->
std::tuple<std::vector<Sprite>, std::vector<Texture>> {

auto slice_sprites = std::vector<Sprite>();
std::copy_if(sprites.begin(), sprites.end(),
std::back_inserter(slice_sprites),
[&](const Sprite& sprite) {
return (sprite.slice_index == slice_index);
});

auto index = 0;
for (auto& sprite : slice_sprites) {
sprite.index = index++;
sprite.slice_index = 0;
}

auto slice_textures = std::vector<Texture>();
std::copy_if(textures.begin(), textures.end(),
std::back_inserter(slice_textures),
[&](const Texture& texture) {
return (texture.slice->index == slice_index);
});

for (auto& texture : slice_textures)
texture.slice = &sole_slice;

return { slice_sprites, slice_textures };
}
} // namespace

void evaluate_expressions(
Expand Down Expand Up @@ -371,25 +416,46 @@ void output_descriptions(
const std::vector<Texture>& textures,
const VariantMap& variables) {

const auto json = get_json_description(settings,
inputs, sprites, slices, textures, variables);
auto json = std::optional<nlohmann::json>();

for (const auto& description : descriptions) {
if (description.filename.empty())
continue;

auto ss = std::ostringstream();
auto& os = (description.filename.string() == "stdout" ? std::cout : ss);
if (!description.template_filename.empty()) {
auto env = setup_inja_environment(&json);
env.render_to(os, env.parse_template(
path_to_utf8(description.template_filename)), json);
const auto filenames = FilenameSequence(path_to_utf8(description.filename));
if (!filenames.is_sequence()) {
// output all slices in one output description
if (!json.has_value())
json = get_json_description(settings,
inputs, sprites, slices, textures, variables);

if (description.filename.string() != "stdout") {
auto ss = std::ostringstream();
output_description(ss, description.template_filename, *json);
update_textfile(description.filename, ss.str());
}
else {
output_description(std::cout, description.template_filename, *json);
}
}
else {
os << json.dump(1, '\t');
// output each slice in separate output description
for (const auto& slice : slices) {
auto sole_slice = slice;
sole_slice.index = 0;

const auto [slice_sprites, slice_textures] =
filter_by_slice(slice.index, sole_slice, sprites, textures);

const auto slice_json = get_json_description(settings,
inputs, slice_sprites, { sole_slice }, slice_textures, variables);

const auto filename = filenames.get_nth_filename(slice.index);
auto ss = std::ostringstream();
output_description(ss, description.template_filename, slice_json);
update_textfile(filename, ss.str());
}
}
if (description.filename.string() != "stdout")
update_textfile(description.filename, ss.str());
}
}

Expand Down

0 comments on commit d68bfd2

Please sign in to comment.