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.
544 lines
19 KiB
C++
544 lines
19 KiB
C++
#include "model/model.h"
|
|
|
|
#include <algorithm>
|
|
#include <unordered_set>
|
|
|
|
#include "image.h"
|
|
#include "path.h"
|
|
|
|
int Model::animations_count_get() const
|
|
{
|
|
return items_count(animations.entries);
|
|
}
|
|
|
|
const Animation* Model::animation_get(int index) const
|
|
{
|
|
auto slot = item_place_get(animations.entries, index).item;
|
|
return slot ? slot->get() : nullptr;
|
|
}
|
|
|
|
// Copy-on-write: an animation shared with undo history is cloned before it is changed.
|
|
Animation* Model::animation_edit(int index)
|
|
{
|
|
auto slot = item_place_get(animations.entries, index).item;
|
|
if (!slot || !*slot)
|
|
return nullptr;
|
|
if (slot->use_count() > 1)
|
|
*slot = std::make_shared<const Animation>(**slot);
|
|
return const_cast<Animation*>(slot->get());
|
|
}
|
|
|
|
const Animation* Model::animation_named_get(std::string_view name) const
|
|
{
|
|
for (auto [index, animation] : animations_get())
|
|
if (animation->name == name)
|
|
return animation;
|
|
return nullptr;
|
|
}
|
|
|
|
std::vector<std::pair<int, const Animation*>> Model::animations_get() const
|
|
{
|
|
std::vector<std::pair<int, const Animation*>> result;
|
|
for (int i = 0; i < animations_count_get(); ++i)
|
|
result.emplace_back(i, animation_get(i));
|
|
return result;
|
|
}
|
|
|
|
const AnimationGroup* Model::animation_group_get(int index) const
|
|
{
|
|
return item_place_get(animations.entries, index).group;
|
|
}
|
|
|
|
AnimationGroup* Model::animation_group_edit(int index)
|
|
{
|
|
return item_place_get(animations.entries, index).group;
|
|
}
|
|
|
|
int Model::animation_group_id_get(int index) const
|
|
{
|
|
auto group = animation_group_get(index);
|
|
return group ? group->id : -1;
|
|
}
|
|
|
|
template <class A> static auto animation_track_group_find(A& animation, int groupType, int groupId)
|
|
{
|
|
using Group = std::conditional_t<std::is_const_v<A>, const TrackGroup, TrackGroup>;
|
|
if (groupId < 0 || (groupType != LAYER && groupType != NULL_))
|
|
return (Group*)nullptr;
|
|
return group_find(animation_entries_get(animation, groupType), groupId);
|
|
}
|
|
|
|
TrackGroup* animation_track_group_get(Animation& animation, int groupType, int groupId)
|
|
{
|
|
return animation_track_group_find(animation, groupType, groupId);
|
|
}
|
|
|
|
const TrackGroup* animation_track_group_get(const Animation& animation, int groupType, int groupId)
|
|
{
|
|
return animation_track_group_find(animation, groupType, groupId);
|
|
}
|
|
|
|
// A track by item type and id. A group root when the item is ROOT with a group, and grouped tracks only match their
|
|
// own group when one is given.
|
|
template <class A> static auto animation_track_find(A& animation, int itemType, int id, int groupType, int groupId)
|
|
{
|
|
using T = std::conditional_t<std::is_const_v<A>, const Track, Track>;
|
|
T* result = nullptr;
|
|
auto isGrouped = groupType != NONE && groupId != -1;
|
|
if (itemType == ROOT)
|
|
{
|
|
if (!isGrouped)
|
|
return &animation.root;
|
|
auto group = animation_track_group_find(animation, groupType, groupId);
|
|
return group ? &group->root : result;
|
|
}
|
|
if (itemType == TRIGGER)
|
|
return &animation.triggers;
|
|
if (itemType != LAYER && itemType != NULL_)
|
|
return result;
|
|
|
|
items_each(animation_entries_get(animation, itemType), [&](auto& track, auto* group) {
|
|
if (result || track.id != id)
|
|
return;
|
|
if (isGrouped && (!group || group->id != groupId))
|
|
return;
|
|
result = &track;
|
|
});
|
|
return result;
|
|
}
|
|
|
|
const Track* animation_track_get(const Animation& animation, int itemType, int id, int groupType, int groupId)
|
|
{
|
|
return animation_track_find(animation, itemType, id, groupType, groupId);
|
|
}
|
|
|
|
Track* animation_track_get(Animation& animation, int itemType, int id, int groupType, int groupId)
|
|
{
|
|
return animation_track_find(animation, itemType, id, groupType, groupId);
|
|
}
|
|
|
|
const Track* Model::track_get(Reference reference) const
|
|
{
|
|
auto animation = animation_get(reference.animationIndex);
|
|
return animation ? animation_track_get(*animation, reference.itemType, reference.itemID, reference.groupType,
|
|
reference.groupId)
|
|
: nullptr;
|
|
}
|
|
|
|
Track* Model::track_edit(Reference reference)
|
|
{
|
|
if (!track_get(reference))
|
|
return nullptr;
|
|
auto animation = animation_edit(reference.animationIndex);
|
|
return animation_track_get(*animation, reference.itemType, reference.itemID, reference.groupType,
|
|
reference.groupId);
|
|
}
|
|
|
|
const TrackGroup* Model::track_group_get(int animationIndex, int groupType, int groupId) const
|
|
{
|
|
auto animation = animation_get(animationIndex);
|
|
return animation ? animation_track_group_get(*animation, groupType, groupId) : nullptr;
|
|
}
|
|
|
|
TrackGroup* Model::track_group_edit(int animationIndex, int groupType, int groupId)
|
|
{
|
|
if (!track_group_get(animationIndex, groupType, groupId))
|
|
return nullptr;
|
|
return animation_track_group_get(*animation_edit(animationIndex), groupType, groupId);
|
|
}
|
|
|
|
const Frame* Model::frame_get(Reference reference) const
|
|
{
|
|
auto track = track_get(reference);
|
|
if (!track || reference.frameIndex < 0 || reference.frameIndex >= (int)track->frames.size())
|
|
return nullptr;
|
|
return &track->frames[reference.frameIndex];
|
|
}
|
|
|
|
Frame* Model::frame_edit(Reference reference)
|
|
{
|
|
if (!frame_get(reference))
|
|
return nullptr;
|
|
return &track_edit(reference)->frames[reference.frameIndex];
|
|
}
|
|
|
|
const Spritesheet* Model::layer_spritesheet_get(int layerId) const
|
|
{
|
|
auto layer = item_get(content.layers, layerId);
|
|
return layer ? item_get(content.spritesheets, layer->spritesheetId) : nullptr;
|
|
}
|
|
|
|
// The frame with its region's crop/size/pivot applied, as it is drawn.
|
|
Frame Model::frame_effective(int layerId, const Frame& frame) const
|
|
{
|
|
auto resolved = frame;
|
|
auto spritesheet = frame.regionId == -1 ? nullptr : layer_spritesheet_get(layerId);
|
|
if (auto region = spritesheet ? item_get(spritesheet->regions, frame.regionId) : nullptr)
|
|
frame_region_rect_set(resolved, *region);
|
|
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)
|
|
{
|
|
std::set<int> unused;
|
|
for (auto& item : items)
|
|
if (!used.contains(item.id))
|
|
unused.insert(item.id);
|
|
return unused;
|
|
}
|
|
|
|
// Each reference to a content item of `type`, as the id it holds: a plain layer's spritesheet or a layer's library, a
|
|
// track's layer or null, a trigger's events or sounds, a frame's shader, or (from layers on spritesheet
|
|
// `spritesheetId`) a frame's region. With `only`, those in that animation (layers name spritesheets and libraries
|
|
// either way).
|
|
template <class Callback>
|
|
void references_each(const Model& model, ElementType type, int spritesheetId, const Animation* only,
|
|
Callback&& callback)
|
|
{
|
|
auto& content = model.content;
|
|
if (type == ElementType::SPRITESHEET || type == ElementType::LIBRARY)
|
|
{
|
|
// An animation layer's spritesheet isn't drawn.
|
|
for (auto& layer : content.layers)
|
|
if (type == ElementType::LIBRARY)
|
|
callback(layer.libraryId);
|
|
else if (!layer.is_animation())
|
|
callback(layer.spritesheetId);
|
|
return;
|
|
}
|
|
auto animation_references_each = [&](const Animation& animation) {
|
|
if (type == ElementType::LAYER_ELEMENT || type == ElementType::NULL_ELEMENT)
|
|
items_each(type == ElementType::LAYER_ELEMENT ? animation.layers : animation.nulls,
|
|
[&](const Track& track, auto*) { callback(track.id); });
|
|
if (type == ElementType::EVENT_ELEMENT || type == ElementType::SOUND_ELEMENT)
|
|
for (auto& trigger : animation.triggers.frames)
|
|
for (auto id : type == ElementType::EVENT_ELEMENT ? trigger.eventIds : trigger.soundIds)
|
|
callback(id);
|
|
if (type == ElementType::SHADER)
|
|
animation_tracks_each(animation, [&](const Track& track) {
|
|
for (auto& frame : track.frames)
|
|
callback(frame.shaderId);
|
|
});
|
|
if (type == ElementType::REGION)
|
|
items_each(animation.layers, [&](const Track& track, auto*) {
|
|
auto layer = item_get(content.layers, track.id);
|
|
if (!layer || layer->spritesheetId != spritesheetId)
|
|
return;
|
|
for (auto& frame : track.frames)
|
|
callback(frame.regionId);
|
|
});
|
|
};
|
|
if (only)
|
|
return animation_references_each(*only);
|
|
for (auto [index, animation] : model.animations_get())
|
|
animation_references_each(*animation);
|
|
}
|
|
|
|
std::set<int> Model::unused_get(ElementType type, const Animation* only) const
|
|
{
|
|
std::set<int> used;
|
|
references_each(*this, type, -1, only, [&](int id) { used.insert(id); });
|
|
switch (type)
|
|
{
|
|
case ElementType::SPRITESHEET:
|
|
return unused_ids_get(content.spritesheets, used);
|
|
case ElementType::SHADER:
|
|
return unused_ids_get(content.shaders, used);
|
|
case ElementType::LAYER_ELEMENT:
|
|
return unused_ids_get(content.layers, used);
|
|
case ElementType::NULL_ELEMENT:
|
|
return unused_ids_get(content.nulls, used);
|
|
case ElementType::EVENT_ELEMENT:
|
|
return unused_ids_get(content.events, used);
|
|
case ElementType::SOUND_ELEMENT:
|
|
return unused_ids_get(content.sounds, used);
|
|
case ElementType::LIBRARY:
|
|
return unused_ids_get(content.libraries, used);
|
|
default:
|
|
return {};
|
|
}
|
|
}
|
|
|
|
// How many references there are to the items `ids` (see references_each).
|
|
int Model::uses_count(ElementType type, const std::set<int>& ids, int spritesheetId, const Animation* only) const
|
|
{
|
|
int count = 0;
|
|
references_each(*this, type, spritesheetId, only, [&](int id) { count += ids.contains(id); });
|
|
return count;
|
|
}
|
|
|
|
Handle Model::handle_get(Reference reference, bool isGroup) const
|
|
{
|
|
Handle handle = {};
|
|
auto animation = animation_get(reference.animationIndex);
|
|
if (!animation)
|
|
return handle;
|
|
handle.animation = animation->uid;
|
|
|
|
if (isGroup)
|
|
{
|
|
if (auto group = animation_track_group_get(*animation, reference.itemType, reference.itemID))
|
|
handle.item = group->uid;
|
|
return handle;
|
|
}
|
|
|
|
auto track =
|
|
animation_track_get(*animation, reference.itemType, reference.itemID, reference.groupType, reference.groupId);
|
|
if (!track)
|
|
return handle;
|
|
handle.item = track->uid;
|
|
if (reference.frameIndex >= 0 && reference.frameIndex < (int)track->frames.size())
|
|
handle.frame = track->frames[reference.frameIndex].uid;
|
|
return handle;
|
|
}
|
|
|
|
std::uint64_t Model::uid_get(Reference reference, bool isGroup) const
|
|
{
|
|
auto handle = handle_get(reference, isGroup);
|
|
return handle.frame ? handle.frame : handle.item ? handle.item : handle.animation;
|
|
}
|
|
|
|
template <class Callback> static void animation_uids_each(Animation& animation, Callback&& callback)
|
|
{
|
|
callback(animation.uid);
|
|
for (auto* entries : {&animation.layers, &animation.nulls})
|
|
groups_each(*entries, [&](TrackGroup& group) { callback(group.uid); });
|
|
animation_tracks_each(animation, [&](Track& track) {
|
|
callback(track.uid);
|
|
for (auto& frame : track.frames)
|
|
callback(frame.uid);
|
|
});
|
|
}
|
|
|
|
// Gives any element that shares a uid with an earlier one a fresh uid (copies made without cloning).
|
|
void Model::uids_repair()
|
|
{
|
|
std::unordered_set<std::uint64_t> seen;
|
|
auto repair = [&](std::uint64_t& uid) {
|
|
if (!uid || !seen.insert(uid).second)
|
|
uid = uid_next();
|
|
};
|
|
|
|
for (int i = 0; i < animations_count_get(); ++i)
|
|
{
|
|
auto seenBefore = seen;
|
|
bool isDuplicate = false;
|
|
animation_uids_each(const_cast<Animation&>(*animation_get(i)),
|
|
[&](std::uint64_t& uid) { isDuplicate |= !uid || !seen.insert(uid).second; });
|
|
if (!isDuplicate)
|
|
continue;
|
|
seen = std::move(seenBefore);
|
|
animation_uids_each(*animation_edit(i), repair);
|
|
}
|
|
groups_each(animations.entries, [&](AnimationGroup& group) { repair(group.uid); });
|
|
for (auto& spritesheet : content.spritesheets)
|
|
{
|
|
repair(spritesheet.uid);
|
|
for (auto& region : spritesheet.regions)
|
|
repair(region.uid);
|
|
}
|
|
for (auto& shader : content.shaders)
|
|
repair(shader.uid);
|
|
for (auto& layer : content.layers)
|
|
repair(layer.uid);
|
|
for (auto& null : content.nulls)
|
|
repair(null.uid);
|
|
for (auto& event : content.events)
|
|
repair(event.uid);
|
|
for (auto& sound : content.sounds)
|
|
repair(sound.uid);
|
|
for (auto& library : content.libraries)
|
|
repair(library.uid);
|
|
}
|
|
|
|
// SVG spritesheets that layers show.
|
|
std::set<int> spritesheets_vector_used_get(const Model& model)
|
|
{
|
|
std::set<int> ids;
|
|
for (auto& layer : model.content.layers)
|
|
if (auto spritesheet = item_get(model.content.spritesheets, layer.spritesheetId);
|
|
spritesheet && !layer.is_animation() && is_path_extension(spritesheet->path, SVG_EXTENSION))
|
|
ids.insert(spritesheet->id);
|
|
return ids;
|
|
}
|
|
|
|
int track_frames_count_get(const Track& track)
|
|
{
|
|
return (int)track.frames.size();
|
|
}
|
|
|
|
int track_length_get(const Track& track)
|
|
{
|
|
int length = 0;
|
|
for (auto& frame : track.frames)
|
|
length = track.type == ItemType::TRIGGER ? std::max(length, frame.atFrame) : length + frame.duration;
|
|
return length;
|
|
}
|
|
|
|
// The frames playback stays within: [start, end), from the markers (or the whole animation).
|
|
IVec2 animation_play_range_get(const Animation& animation)
|
|
{
|
|
auto length = std::max(animation.frameNum, 1);
|
|
auto start = animation.startMarker != -1 ? std::clamp(animation.startMarker, 0, length - 1) : 0;
|
|
auto end = animation.endMarker != -1 ? std::clamp(animation.endMarker + 1, start + 1, length) : length;
|
|
return {start, end};
|
|
}
|
|
|
|
int animation_length_get(const Animation& animation)
|
|
{
|
|
int length = 0;
|
|
animation_tracks_each(animation, [&](const Track& track) { length = std::max(length, track_length_get(track)); });
|
|
return std::max(length, FRAME_DURATION_MIN);
|
|
}
|
|
|
|
Frame frame_clone(Frame frame)
|
|
{
|
|
frame.uid = uid_next();
|
|
return frame;
|
|
}
|
|
|
|
Track track_clone(Track track)
|
|
{
|
|
track.uid = uid_next();
|
|
for (auto& frame : track.frames)
|
|
frame.uid = uid_next();
|
|
return track;
|
|
}
|
|
|
|
Animation animation_clone(Animation animation)
|
|
{
|
|
animation_uids_each(animation, [](std::uint64_t& uid) { uid = uid_next(); });
|
|
return animation;
|
|
}
|
|
|
|
static void track_locations_add(std::unordered_map<std::uint64_t, UidIndex::Location>& locations, const Track& track,
|
|
Reference reference)
|
|
{
|
|
constexpr ElementType TRACK_TYPES[] = {ElementType::UNKNOWN, ElementType::ROOT_ANIMATION,
|
|
ElementType::LAYER_ANIMATION, ElementType::NULL_ANIMATION,
|
|
ElementType::TRIGGERS};
|
|
locations[track.uid] = {reference, TRACK_TYPES[(int)track.type]};
|
|
for (auto& frame : track.frames)
|
|
{
|
|
++reference.frameIndex;
|
|
locations[frame.uid] = {reference, track.type == ItemType::TRIGGER ? ElementType::TRIGGER : ElementType::FRAME};
|
|
}
|
|
}
|
|
|
|
UidIndex::UidIndex(const Model& model)
|
|
{
|
|
for (auto [index, animation] : model.animations_get())
|
|
{
|
|
locations[animation->uid] = {{index}, ElementType::ANIMATION};
|
|
track_locations_add(locations, animation->root, {index, ROOT});
|
|
track_locations_add(locations, animation->triggers, {index, TRIGGER});
|
|
for (auto type : {LAYER, NULL_})
|
|
entries_walk(
|
|
animation_entries_get(*animation, type),
|
|
[&](const Track& track, const TrackGroup* group) {
|
|
track_locations_add(locations, track,
|
|
{index, type, track.id, -1, group ? type : NONE, group ? group->id : -1});
|
|
},
|
|
[&](const TrackGroup& group) {
|
|
locations[group.uid] = {{index, type, group.id}, ElementType::GROUP};
|
|
track_locations_add(locations, group.root, {index, ROOT, -1, -1, type, group.id});
|
|
});
|
|
}
|
|
}
|
|
|
|
bool UidIndex::contains(std::uint64_t uid) const
|
|
{
|
|
return uid && locations.contains(uid);
|
|
}
|
|
|
|
std::optional<Reference> UidIndex::reference_get(std::uint64_t uid) const
|
|
{
|
|
auto it = locations.find(uid);
|
|
return it == locations.end() ? std::nullopt : std::optional<Reference>(it->second.reference);
|
|
}
|
|
|
|
ElementType UidIndex::type_get(std::uint64_t uid) const
|
|
{
|
|
auto it = locations.find(uid);
|
|
return it == locations.end() ? ElementType::UNKNOWN : it->second.type;
|
|
}
|
|
|
|
// Equality by value. Animations shared with another model compare by pointer first.
|
|
bool is_model_equal(const Model& left, const Model& right)
|
|
{
|
|
if (left.info != right.info || left.content != right.content || left.layout != right.layout ||
|
|
left.extras != right.extras || left.isValid != right.isValid ||
|
|
left.animations.defaultAnimation != right.animations.defaultAnimation ||
|
|
left.animations.extras != right.animations.extras ||
|
|
left.animations.entries.size() != right.animations.entries.size())
|
|
return false;
|
|
|
|
auto is_animation_equal = [](const std::shared_ptr<const Animation>& a, const std::shared_ptr<const Animation>& b) {
|
|
return a == b || (a && b && *a == *b);
|
|
};
|
|
for (std::size_t i = 0; i < left.animations.entries.size(); ++i)
|
|
{
|
|
auto& a = left.animations.entries[i];
|
|
auto& b = right.animations.entries[i];
|
|
if (a.index() != b.index())
|
|
return false;
|
|
if (auto animation = std::get_if<std::shared_ptr<const Animation>>(&a))
|
|
{
|
|
if (!is_animation_equal(*animation, std::get<std::shared_ptr<const Animation>>(b)))
|
|
return false;
|
|
continue;
|
|
}
|
|
auto& groupA = std::get<AnimationGroup>(a);
|
|
auto& groupB = std::get<AnimationGroup>(b);
|
|
if (groupA.uid != groupB.uid || groupA.id != groupB.id || groupA.name != groupB.name ||
|
|
groupA.isExpanded != groupB.isExpanded || groupA.isVisible != groupB.isVisible ||
|
|
groupA.extras != groupB.extras || groupA.items.size() != groupB.items.size())
|
|
return false;
|
|
for (std::size_t j = 0; j < groupA.items.size(); ++j)
|
|
if (!is_animation_equal(groupA.items[j], groupB.items[j]))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|