Share the region and unique-name helpers
Matching a frame to a region, giving a frame its region's rectangle, a region's origin pivot, and naming something Name 2 each live in one place.
This commit is contained in:
1 parent
0f5a224e7a
commit
c44c7fe8df
6 files changed
+85
-96
No files matched your search
+12
-48
@@ -18,12 +18,6 @@ std::string region_name_get(const std::string& format, int number)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_region_matched(const Region& region, const Frame& frame)
|
|
||||||
{
|
|
||||||
return IVec2(region.crop) == IVec2(frame.crop) && IVec2(region.size) == IVec2(frame.size) &&
|
|
||||||
IVec2(region.pivot) == IVec2(frame.pivot);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Every frame of every layer track whose layer has a spritesheet, with that spritesheet's id.
|
// Every frame of every layer track whose layer has a spritesheet, with that spritesheet's id.
|
||||||
template <class Callback> static void layer_frames_each(Model& model, int animationIndex, Callback&& callback)
|
template <class Callback> static void layer_frames_each(Model& model, int animationIndex, Callback&& callback)
|
||||||
{
|
{
|
||||||
@@ -51,17 +45,13 @@ Uids regions_generate(Model& model, const std::set<int>& animationIndices, const
|
|||||||
std::unordered_map<int, int> regionNumbers;
|
std::unordered_map<int, int> regionNumbers;
|
||||||
auto frame_apply = [&](Frame& frame, int spritesheetId) {
|
auto frame_apply = [&](Frame& frame, int spritesheetId) {
|
||||||
auto& regions = item_get(model.content.spritesheets, spritesheetId)->regions;
|
auto& regions = item_get(model.content.spritesheets, spritesheetId)->regions;
|
||||||
auto region =
|
auto region = region_matched_get(regions, frame);
|
||||||
std::ranges::find_if(regions, [&](const Region& region) { return is_region_matched(region, frame); });
|
if (!region)
|
||||||
if (region == regions.end())
|
region = ®ions.emplace_back(Region{.id = item_next_id_get(regions),
|
||||||
{
|
.name = region_name_get(format, ++regionNumbers[spritesheetId]),
|
||||||
regions.push_back({.id = item_next_id_get(regions),
|
.crop = frame.crop,
|
||||||
.name = region_name_get(format, ++regionNumbers[spritesheetId]),
|
.size = frame.size,
|
||||||
.crop = frame.crop,
|
.pivot = frame.pivot});
|
||||||
.size = frame.size,
|
|
||||||
.pivot = frame.pivot});
|
|
||||||
region = regions.end() - 1;
|
|
||||||
}
|
|
||||||
if (mapping == RegionFrameMapping::SET)
|
if (mapping == RegionFrameMapping::SET)
|
||||||
frame.regionId = region->id;
|
frame.regionId = region->id;
|
||||||
};
|
};
|
||||||
@@ -86,12 +76,8 @@ Uids regions_scan(Model& model)
|
|||||||
layer_frames_each(model, i, [&](Frame& frame, int spritesheetId) {
|
layer_frames_each(model, i, [&](Frame& frame, int spritesheetId) {
|
||||||
if (frame.regionId != -1)
|
if (frame.regionId != -1)
|
||||||
return;
|
return;
|
||||||
for (auto& region : item_get(model.content.spritesheets, spritesheetId)->regions)
|
if (auto match = region_matched_get(item_get(model.content.spritesheets, spritesheetId)->regions, frame))
|
||||||
if (is_region_matched(region, frame))
|
frame.regionId = match->id;
|
||||||
{
|
|
||||||
frame.regionId = region.id;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -101,17 +87,7 @@ Uids region_frames_sync(Model& model)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < model.animations_count_get(); ++i)
|
for (int i = 0; i < model.animations_count_get(); ++i)
|
||||||
layer_frames_each(model, i, [&](Frame& frame, int spritesheetId) {
|
layer_frames_each(model, i, [&](Frame& frame, int spritesheetId) {
|
||||||
if (frame.regionId == -1)
|
frame_region_apply(frame, item_get(model.content.spritesheets, spritesheetId)->regions);
|
||||||
return;
|
|
||||||
auto region = item_get(item_get(model.content.spritesheets, spritesheetId)->regions, frame.regionId);
|
|
||||||
if (!region)
|
|
||||||
{
|
|
||||||
frame.regionId = -1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
frame.crop = region->crop;
|
|
||||||
frame.size = region->size;
|
|
||||||
frame.pivot = region->pivot;
|
|
||||||
});
|
});
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -341,13 +317,6 @@ bool file_merge(Model& model, const std::filesystem::path& path, const std::file
|
|||||||
return it == items.end() ? nullptr : &*it;
|
return it == items.end() ? nullptr : &*it;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto name_unique_get = [&](auto& items, const std::string& name) {
|
|
||||||
auto candidate = name;
|
|
||||||
for (int i = 2; name_find(items, candidate); ++i)
|
|
||||||
candidate = std::format("{} {}", name, i);
|
|
||||||
return candidate;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto named_merge = [&](auto& items, auto item) {
|
auto named_merge = [&](auto& items, auto item) {
|
||||||
auto existing = isAppendAsNew ? nullptr : name_find(items, item.name);
|
auto existing = isAppendAsNew ? nullptr : name_find(items, item.name);
|
||||||
if (existing)
|
if (existing)
|
||||||
@@ -359,7 +328,7 @@ bool file_merge(Model& model, const std::filesystem::path& path, const std::file
|
|||||||
}
|
}
|
||||||
item.id = item_next_id_get(items);
|
item.id = item_next_id_get(items);
|
||||||
if (isAppendAsNew)
|
if (isAppendAsNew)
|
||||||
item.name = name_unique_get(items, item.name);
|
item.name = name_unique_get(item.name, [&](auto& name) { return name_find(items, name) != nullptr; });
|
||||||
items.push_back(item);
|
items.push_back(item);
|
||||||
return item.id;
|
return item.id;
|
||||||
};
|
};
|
||||||
@@ -455,12 +424,7 @@ bool file_merge(Model& model, const std::filesystem::path& path, const std::file
|
|||||||
auto processed = animation_build(*incoming);
|
auto processed = animation_build(*incoming);
|
||||||
auto destination = animation_find(processed.name);
|
auto destination = animation_find(processed.name);
|
||||||
if (isAppendAsNew)
|
if (isAppendAsNew)
|
||||||
{
|
processed.name = name_unique_get(processed.name, [&](auto& name) { return animation_find(name) != -1; });
|
||||||
auto candidate = processed.name;
|
|
||||||
for (int i = 2; animation_find(candidate) != -1; ++i)
|
|
||||||
candidate = std::format("{} {}", processed.name, i);
|
|
||||||
processed.name = candidate;
|
|
||||||
}
|
|
||||||
if (incoming->name == source.animations.defaultAnimation)
|
if (incoming->name == source.animations.defaultAnimation)
|
||||||
defaultAnimationName = processed.name;
|
defaultAnimationName = processed.name;
|
||||||
|
|
||||||
|
|||||||
@@ -52,12 +52,8 @@ bool regions_trim(Model& model, int spritesheetId, const std::set<int>& ids, con
|
|||||||
auto previousCrop = region->crop;
|
auto previousCrop = region->crop;
|
||||||
region->crop = newCrop;
|
region->crop = newCrop;
|
||||||
region->size = newSize;
|
region->size = newSize;
|
||||||
if (region->origin == Origin::TOP_LEFT)
|
region->pivot = region->origin == Origin::CUSTOM ? region->pivot - (region->crop - previousCrop)
|
||||||
region->pivot = {};
|
: region_pivot_get(*region);
|
||||||
else if (region->origin == Origin::CENTER)
|
|
||||||
region->pivot = region->size * 0.5f;
|
|
||||||
else
|
|
||||||
region->pivot -= region->crop - previousCrop;
|
|
||||||
isChanged = true;
|
isChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-7
@@ -653,17 +653,17 @@ static void animations_write(JsonWriter& io, const Model& model)
|
|||||||
{
|
{
|
||||||
std::set<std::string> animationNames;
|
std::set<std::string> animationNames;
|
||||||
std::set<std::string> keys;
|
std::set<std::string> keys;
|
||||||
auto name_unique_get = [](const std::string& name, std::initializer_list<std::set<std::string>*> sets) {
|
auto name_claim = [](const std::string& name, std::initializer_list<std::set<std::string>*> sets) {
|
||||||
auto candidate = name;
|
auto candidate = name_unique_get(name, [&](auto& name) {
|
||||||
for (int i = 2; std::ranges::any_of(sets, [&](auto* names) { return names->contains(candidate); }); ++i)
|
return std::ranges::any_of(sets, [&](auto* names) { return names->contains(name); });
|
||||||
candidate = std::format("{} {}", name, i);
|
});
|
||||||
for (auto* names : sets)
|
for (auto* names : sets)
|
||||||
names->insert(candidate);
|
names->insert(candidate);
|
||||||
return candidate;
|
return candidate;
|
||||||
};
|
};
|
||||||
auto animation_write = [&](const Animation& animation, bool isTopLevel) {
|
auto animation_write = [&](const Animation& animation, bool isTopLevel) {
|
||||||
auto name = isTopLevel ? name_unique_get(animation.name, {&animationNames, &keys})
|
auto name = isTopLevel ? name_claim(animation.name, {&animationNames, &keys})
|
||||||
: name_unique_get(animation.name, {&animationNames});
|
: name_claim(animation.name, {&animationNames});
|
||||||
io.object(name, [&](JsonWriter&) { animation_io(io, animation); }, false);
|
io.object(name, [&](JsonWriter&) { animation_io(io, animation); }, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -679,7 +679,7 @@ static void animations_write(JsonWriter& io, const Model& model)
|
|||||||
}
|
}
|
||||||
auto& group = std::get<AnimationGroup>(entry);
|
auto& group = std::get<AnimationGroup>(entry);
|
||||||
io.object(
|
io.object(
|
||||||
name_unique_get(group.name, {&keys}),
|
name_claim(group.name, {&keys}),
|
||||||
[&](JsonWriter&) {
|
[&](JsonWriter&) {
|
||||||
io.field("expanded", group.isExpanded, true);
|
io.field("expanded", group.isExpanded, true);
|
||||||
io.field("visible", group.isVisible, true);
|
io.field("visible", group.isVisible, true);
|
||||||
|
|||||||
+43
-6
@@ -173,15 +173,52 @@ Frame Model::frame_effective(int layerId, const Frame& frame) const
|
|||||||
{
|
{
|
||||||
auto resolved = frame;
|
auto resolved = frame;
|
||||||
auto spritesheet = frame.regionId == -1 ? nullptr : layer_spritesheet_get(layerId);
|
auto spritesheet = frame.regionId == -1 ? nullptr : layer_spritesheet_get(layerId);
|
||||||
auto region = spritesheet ? item_get(spritesheet->regions, frame.regionId) : nullptr;
|
if (auto region = spritesheet ? item_get(spritesheet->regions, frame.regionId) : nullptr)
|
||||||
if (!region)
|
frame_region_rect_set(resolved, *region);
|
||||||
return resolved;
|
|
||||||
resolved.crop = region->crop;
|
|
||||||
resolved.size = region->size;
|
|
||||||
resolved.pivot = region->pivot;
|
|
||||||
return resolved;
|
return resolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Whether a frame shows exactly a region's rectangle, by whole pixels.
|
||||||
|
bool is_region_matched(const Region& region, const Frame& frame)
|
||||||
|
{
|
||||||
|
return IVec2(region.crop) == IVec2(frame.crop) && IVec2(region.size) == IVec2(frame.size) &&
|
||||||
|
IVec2(region.pivot) == IVec2(frame.pivot);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Region* region_matched_get(const std::vector<Region>& regions, const Frame& frame)
|
||||||
|
{
|
||||||
|
auto region = std::ranges::find_if(regions, [&](const Region& region) { return is_region_matched(region, frame); });
|
||||||
|
return region == regions.end() ? nullptr : &*region;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where a region's origin puts its pivot: its top left, its center, or wherever it was set.
|
||||||
|
Vec2 region_pivot_get(const Region& region)
|
||||||
|
{
|
||||||
|
if (region.origin == Origin::TOP_LEFT)
|
||||||
|
return {};
|
||||||
|
if (region.origin == Origin::CENTER)
|
||||||
|
return region.size * 0.5f;
|
||||||
|
return region.pivot;
|
||||||
|
}
|
||||||
|
|
||||||
|
void frame_region_rect_set(Frame& frame, const Region& region)
|
||||||
|
{
|
||||||
|
frame.crop = region.crop;
|
||||||
|
frame.size = region.size;
|
||||||
|
frame.pivot = region.pivot;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A layer frame on a region takes its rectangle. One on a region that isn't there loses it.
|
||||||
|
void frame_region_apply(Frame& frame, const std::vector<Region>& regions)
|
||||||
|
{
|
||||||
|
if (frame.regionId == -1)
|
||||||
|
return;
|
||||||
|
if (auto region = item_get(regions, frame.regionId))
|
||||||
|
frame_region_rect_set(frame, *region);
|
||||||
|
else
|
||||||
|
frame.regionId = -1;
|
||||||
|
}
|
||||||
|
|
||||||
template <class Items> static std::set<int> unused_ids_get(const Items& items, const std::set<int>& used)
|
template <class Items> static std::set<int> unused_ids_get(const Items& items, const std::set<int>& used)
|
||||||
{
|
{
|
||||||
std::set<int> unused;
|
std::set<int> unused;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <format>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -304,6 +305,15 @@ template <class Items> auto item_get(Items& items, int id) -> decltype(&items.fr
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `name`, or else the first of "name 2", "name 3" and so on that isTaken says is free.
|
||||||
|
template <class IsTaken> std::string name_unique_get(const std::string& name, IsTaken&& isTaken)
|
||||||
|
{
|
||||||
|
auto candidate = name;
|
||||||
|
for (int i = 2; isTaken(candidate); ++i)
|
||||||
|
candidate = std::format("{} {}", name, i);
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
template <class Items> int item_next_id_get(const Items& items)
|
template <class Items> int item_next_id_get(const Items& items)
|
||||||
{
|
{
|
||||||
int id = -1;
|
int id = -1;
|
||||||
@@ -392,6 +402,11 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool is_model_equal(const Model&, const Model&);
|
bool is_model_equal(const Model&, const Model&);
|
||||||
|
bool is_region_matched(const Region&, const Frame&);
|
||||||
|
const Region* region_matched_get(const std::vector<Region>&, const Frame&);
|
||||||
|
Vec2 region_pivot_get(const Region&);
|
||||||
|
void frame_region_rect_set(Frame&, const Region&);
|
||||||
|
void frame_region_apply(Frame&, const std::vector<Region>&);
|
||||||
std::set<int> spritesheets_vector_used_get(const Model&);
|
std::set<int> spritesheets_vector_used_get(const Model&);
|
||||||
int track_frames_count_get(const Track&);
|
int track_frames_count_get(const Track&);
|
||||||
int track_length_get(const Track&);
|
int track_length_get(const Track&);
|
||||||
|
|||||||
+6
-29
@@ -600,10 +600,7 @@ static Region region_read(pugi::xml_node element)
|
|||||||
for (std::size_t i = 0; i < std::size(ORIGIN_VALUES); ++i)
|
for (std::size_t i = 0; i < std::size(ORIGIN_VALUES); ++i)
|
||||||
if (!ORIGIN_VALUES[i].empty() && ORIGIN_VALUES[i] == origin)
|
if (!ORIGIN_VALUES[i].empty() && ORIGIN_VALUES[i] == origin)
|
||||||
region.origin = (Origin)i;
|
region.origin = (Origin)i;
|
||||||
if (region.origin == Origin::TOP_LEFT)
|
region.pivot = region_pivot_get(region);
|
||||||
region.pivot = {};
|
|
||||||
if (region.origin == Origin::CENTER)
|
|
||||||
region.pivot = region.size * 0.5f;
|
|
||||||
return region;
|
return region;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -772,12 +769,6 @@ template <class Items> static void ids_repair(Items& items)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_region_matched(const Region& region, const Frame& frame)
|
|
||||||
{
|
|
||||||
return IVec2(region.crop) == IVec2(frame.crop) && IVec2(region.size) == IVec2(frame.size) &&
|
|
||||||
IVec2(region.pivot) == IVec2(frame.pivot);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loaded documents get unique content ids, frames lose dangling shader ids, and layer frames point at the region
|
// Loaded documents get unique content ids, frames lose dangling shader ids, and layer frames point at the region
|
||||||
// matching their rectangle and take that region's rectangle.
|
// matching their rectangle and take that region's rectangle.
|
||||||
void model_repair(Model& model)
|
void model_repair(Model& model)
|
||||||
@@ -813,22 +804,11 @@ void model_repair(Model& model)
|
|||||||
return;
|
return;
|
||||||
for (auto& frame : track.frames)
|
for (auto& frame : track.frames)
|
||||||
{
|
{
|
||||||
auto region = frame.regionId == -1 ? nullptr : item_get(spritesheet->regions, frame.regionId);
|
auto region = item_get(spritesheet->regions, frame.regionId);
|
||||||
auto match = std::ranges::find_if(
|
if (auto match = region_matched_get(spritesheet->regions, frame);
|
||||||
spritesheet->regions, [&](const Region& candidate) { return is_region_matched(candidate, frame); });
|
match && (!region || !is_region_matched(*region, frame)))
|
||||||
if ((!region || !is_region_matched(*region, frame)) && match != spritesheet->regions.end())
|
|
||||||
frame.regionId = match->id;
|
frame.regionId = match->id;
|
||||||
if (frame.regionId == -1)
|
frame_region_apply(frame, spritesheet->regions);
|
||||||
continue;
|
|
||||||
region = item_get(spritesheet->regions, frame.regionId);
|
|
||||||
if (!region)
|
|
||||||
{
|
|
||||||
frame.regionId = -1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
frame.crop = region->crop;
|
|
||||||
frame.size = region->size;
|
|
||||||
frame.pivot = region->pivot;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1205,10 +1185,7 @@ static Frame frame_normalize(const Writer& writer, Frame frame, const Track* lay
|
|||||||
frame.regionId = regionIds == writer.regionIds.end() ? -1 : id_map_get(regionIds->second, frame.regionId);
|
frame.regionId = regionIds == writer.regionIds.end() ? -1 : id_map_get(regionIds->second, frame.regionId);
|
||||||
if (frame.regionId == -1)
|
if (frame.regionId == -1)
|
||||||
return frame;
|
return frame;
|
||||||
auto& region = spritesheet->regions[frame.regionId];
|
frame_region_rect_set(frame, spritesheet->regions[frame.regionId]);
|
||||||
frame.crop = region.crop;
|
|
||||||
frame.size = region.size;
|
|
||||||
frame.pivot = region.pivot;
|
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user