Skip to content

Commit

Permalink
Outputting sprite indices by input/source
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed May 7, 2023
1 parent b00e042 commit 3f5b8fd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ By default a [JSON](https://www.json.org) file containing all the information ab
{
"id": "sprite_0",
"index": 0,
"inputIndex": 0,
"inputSpriteIndex": 0,
"pivot": { "x": 8.0, "y": 8.0 },
"rect": { "x": 0, "y": 0, "w": 16, "h": 16 },
Expand All @@ -302,25 +303,26 @@ By default a [JSON](https://www.json.org) file containing all the information ab
],
"slices": [
{
"index": 0,
"inputFilename": "spright.conf",
"spriteIndices": [0]
}
],
"sources": [
{
"index": 0,
"filename": "source.png",
"path": "path",
"width": 256,
"height": 256,
"spriteIndices": [0]
}
],
"inputs": [
{
"filename": "source.png",
"sourceIndices": [0]
"sources": [
{
"index": 0,
"spriteIndices": [0]
}
]
}
],
"tags": {
Expand All @@ -330,7 +332,6 @@ By default a [JSON](https://www.json.org) file containing all the information ab
},
"textures": [
{
"index": 0,
"sliceIndex": 0,
"spriteIndices": [0],
"filename": "path/spright-0.png",
Expand Down
27 changes: 15 additions & 12 deletions src/InputParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ void InputParser::sprite_ends(State& state) {
};

if (state.skip_sprites > 0) {
++m_skips_in_current_source;
++m_skips_in_current_input;
advance();
return;
}

auto sprite = Sprite{ };
sprite.index = to_int(m_sprites.size());
sprite.input_sprite_index = m_sprites_in_current_source;
sprite.input_index = to_int(m_inputs.size());
sprite.input_sprite_index = m_sprites_in_current_input;
sprite.id = state.sprite_id;
sprite.sheet = get_sheet(state.sheet_id);
sprite.source = get_source(state);
Expand Down Expand Up @@ -194,7 +195,7 @@ void InputParser::sprite_ends(State& state) {
validate_sprite(sprite);
m_current_input_sources.push_back(sprite.source);
m_sprites.push_back(std::move(sprite));
++m_sprites_in_current_source;
++m_sprites_in_current_input;
}

void InputParser::skip_sprites(State& state) {
Expand All @@ -203,14 +204,14 @@ void InputParser::skip_sprites(State& state) {
}

bool InputParser::overlaps_sprite_rect(const Rect& rect) const {
return last_n_contain(m_sprites, m_sprites_in_current_source,
return last_n_contain(m_sprites, m_sprites_in_current_input,
[&](const Sprite& sprite) {
return overlapping(sprite.source_rect, rect);
});
}

void InputParser::deduce_sequence_sprites(State& state) {
auto skip_already_added = sprites_or_skips_in_current_sounce();
auto skip_already_added = sprites_or_skips_in_current_input();
for (auto i = 0; i < state.source_filenames.count(); ++i) {
if (skip_already_added) {
--skip_already_added;
Expand Down Expand Up @@ -284,7 +285,7 @@ void InputParser::deduce_grid_sprites(State& state) {
auto& x = m_current_grid_cell.x;
auto& y = m_current_grid_cell.y;

const auto is_update = (sprites_or_skips_in_current_sounce() != 0);
const auto is_update = (sprites_or_skips_in_current_input() != 0);
for (; y < cells_y; y += state.span.y) {
auto output_offset = (x != 0);
auto skipped = 0;
Expand Down Expand Up @@ -331,7 +332,7 @@ void InputParser::deduce_grid_sprites(State& state) {

void InputParser::deduce_atlas_sprites(State& state) {
const auto source = get_source(state);
const auto is_update = (sprites_or_skips_in_current_sounce() != 0);
const auto is_update = (sprites_or_skips_in_current_input() != 0);
for (const auto& rect : find_islands(*source,
state.atlas_merge_distance, state.trim_gray_levels)) {
if (is_update && overlaps_sprite_rect(rect))
Expand All @@ -352,7 +353,7 @@ void InputParser::deduce_atlas_sprites(State& state) {
}

void InputParser::deduce_single_sprite(State& state) {
if (sprites_or_skips_in_current_sounce())
if (sprites_or_skips_in_current_input())
return;

if (m_settings.mode == Mode::autocomplete)
Expand Down Expand Up @@ -442,7 +443,7 @@ void InputParser::input_ends(State& state) {
update_applied_definitions(Definition::input);
update_applied_definitions(Definition::sprite);

if (!sprites_or_skips_in_current_sounce() ||
if (!sprites_or_skips_in_current_input() ||
should_autocomplete(state.source_filenames.sequence_filename())) {
auto sprite_indent = state.indent + m_detected_indentation;
std::swap(state.indent, sprite_indent);
Expand All @@ -451,10 +452,12 @@ void InputParser::input_ends(State& state) {
}

m_inputs.push_back({
to_int(m_inputs.size()),
state.source_filenames.sequence_filename(),
make_unique_sort(std::move(m_current_input_sources))
});
m_sprites_in_current_source = { };
m_sprites_in_current_input = { };
m_skips_in_current_input = { };
m_current_sequence_index = { };
++m_inputs_in_current_glob;
}
Expand Down Expand Up @@ -633,8 +636,8 @@ void InputParser::parse(std::istream& input,
m_autocomplete_output << autocomplete_space.str();
}

int InputParser::sprites_or_skips_in_current_sounce() const {
return m_sprites_in_current_source + m_skips_in_current_source;
int InputParser::sprites_or_skips_in_current_input() const {
return m_sprites_in_current_input + m_skips_in_current_input;
}

void InputParser::update_applied_definitions(Definition definition) {
Expand Down
6 changes: 3 additions & 3 deletions src/InputParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class InputParser {
void update_not_applied_definitions(Definition definition, int line_number);
void check_not_applied_definitions();
void handle_exception(std::function<void()> function);
int sprites_or_skips_in_current_sounce() const;
int sprites_or_skips_in_current_input() const;

const Settings m_settings;
std::stringstream m_autocomplete_output;
Expand All @@ -67,8 +67,8 @@ class InputParser {
std::vector<Sprite> m_sprites;
VariantMap m_variables;
int m_inputs_in_current_glob{ };
int m_sprites_in_current_source{ };
int m_skips_in_current_source{ };
int m_sprites_in_current_input{ };
int m_skips_in_current_input{ };
Point m_current_grid_cell{ };
int m_current_sequence_index{ };
std::vector<ImagePtr> m_current_input_sources;
Expand Down
2 changes: 2 additions & 0 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct Extrude {
};

struct Input {
int index;
std::string source_filenames;
std::vector<ImagePtr> sources;
};
Expand Down Expand Up @@ -78,6 +79,7 @@ struct Sheet {

struct Sprite {
int index{ };
int input_index{ };
int input_sprite_index{ };
std::string id;
SheetPtr sheet;
Expand Down
26 changes: 14 additions & 12 deletions src/output_description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ namespace {

using TagKey = std::string;
using TagValue = std::string;
using InputIndex = int;
using SpriteIndex = int;
using SliceIndex = int;
using SourceIndex = int;
auto tags = std::map<TagKey, std::map<TagValue, std::vector<SpriteIndex>>>();
auto source_indices = std::map<ImagePtr, SourceIndex>();
auto slice_sprites = std::map<SliceIndex, std::vector<SpriteIndex>>();
auto source_sprites = std::map<SourceIndex, std::vector<SpriteIndex>>();
auto sprite_on_slice = std::map<SpriteIndex, SliceIndex>();
auto sprites_by_index = std::map<SpriteIndex, const Sprite*>();
auto input_source_sprites = std::map<std::pair<InputIndex, SourceIndex>, std::vector<SpriteIndex>>();
for (const auto& sprite : sprites)
sprites_by_index[sprite.index] = &sprite;
for (const auto& slice : slices)
Expand All @@ -79,15 +80,16 @@ namespace {
sprite->source, to_int(source_indices.size())).first->second;

json_sprite["id"] = sprite->id;
json_sprite["inputIndex"] = sprite->input_index;
json_sprite["inputSpriteIndex"] = sprite->input_sprite_index;
json_sprite["sourceIndex"] = source_index;
json_sprite["sourceRect"] = json_rect(sprite->source_rect);
json_sprite["tags"] = sprite->tags;
json_sprite["data"] = json_variant_map(sprite->data);
input_source_sprites[{ sprite->input_index, source_index }].push_back(sprite_index);

for (const auto& [key, value] : sprite->tags)
tags[key][value].push_back(sprite_index);
source_sprites[source_index].push_back(sprite_index);

// only available when packing was executed
if (const auto it = sprite_on_slice.find(sprite_index); it != sprite_on_slice.end()) {
Expand Down Expand Up @@ -116,8 +118,6 @@ namespace {
json_slices = nlohmann::json::array();
for (const auto& slice : slices) {
auto& json_slice = json_slices.emplace_back();
json_slice["index"] = slice.index;
json_slice["inputFilename"] = path_to_utf8(slice.sheet->input_file);
json_slice["spriteIndices"] = slice_sprites[slice.index];
}

Expand All @@ -128,26 +128,27 @@ namespace {
sources_by_index[index] = source;
for (const auto& [index, source] : sources_by_index) {
auto& json_source = json_sources.emplace_back();
json_source["index"] = index;
json_source["path"] = path_to_utf8(source->path());
json_source["filename"] = path_to_utf8(source->filename());
json_source["width"] = source->width();
json_source["height"] = source->height();
json_source["spriteIndices"] = source_sprites[index];
}

auto& json_inputs = json["inputs"];
json_inputs = nlohmann::json::array();
for (const auto& input : inputs) {
auto& json_input = json_inputs.emplace_back();
json_input["filename"] = input.source_filenames;
auto json_source_indices = nlohmann::json::array();
for (const auto& source : input.sources)
json_source_indices.push_back(source_indices[source]);
json_input["sourceIndices"] = std::move(json_source_indices);
auto json_sources = nlohmann::json::array();
for (const auto& source : input.sources) {
auto& json_source = json_sources.emplace_back();
const auto source_index = source_indices[source];
json_source["index"] = source_index;
json_source["spriteIndices"] = input_source_sprites[{ input.index, source_index }];
}
json_input["sources"] = std::move(json_sources);
}

auto texture_index = size_t{ };
auto& json_textures = json["textures"];
json_textures = nlohmann::json::array();
for (const auto& texture : textures) {
Expand All @@ -157,7 +158,6 @@ namespace {
auto& json_texture = json_textures.emplace_back();
const auto& slice = *texture.slice;
const auto& output = *texture.output;
json_texture["index"] = texture_index++;
json_texture["sliceIndex"] = texture.slice->index;
json_texture["spriteIndices"] = slice_sprites[slice.index];
json_texture["filename"] = texture.filename;
Expand Down Expand Up @@ -219,6 +219,8 @@ void evaluate_expressions(
replace_variables(expression, [&](std::string_view variable) {
if (variable == "index")
return std::to_string(sprite.index);
if (variable == "inputIndex")
return std::to_string(sprite.input_index);
if (variable == "inputSpriteIndex")
return std::to_string(sprite.input_sprite_index);
if (variable == "sheet.id")
Expand Down

0 comments on commit 3f5b8fd

Please sign in to comment.