Share one set of group operations between tracks and animations
model/group.h walks, finds, takes, wraps, unwraps and expands lists of items and groups. Track and animation groups both keep their members in items, and the edits, bakes, merges and writers use it.
This commit is contained in:
1 parent
03bbb93ba6
commit
7e3c5cfeb5
19 files changed
+395
-460
No files matched your search
+53
-124
@@ -9,9 +9,7 @@ using AnimationPointer = std::shared_ptr<const Animation>;
|
||||
std::set<int> animation_group_ids_get(const Model& model)
|
||||
{
|
||||
std::set<int> ids;
|
||||
for (auto& entry : model.animations.entries)
|
||||
if (auto group = std::get_if<AnimationGroup>(&entry))
|
||||
ids.insert(group->id);
|
||||
groups_each(model.animations.entries, [&](const AnimationGroup& group) { ids.insert(group.id); });
|
||||
return ids;
|
||||
}
|
||||
|
||||
@@ -24,41 +22,16 @@ std::set<int> animation_group_indices_get(const Model& model, int groupId)
|
||||
return indices;
|
||||
}
|
||||
|
||||
// Animations and whole groups taken out of the list, remembering the order they had.
|
||||
struct AnimationsTaken
|
||||
// Takes out the animations at these flat indices (grouped ones too) and the groups in groupIds.
|
||||
static EntriesTaken<std::vector<AnimationEntry>> animations_take(Model& model, const std::set<int>& indices,
|
||||
const std::set<int>& groupIds)
|
||||
{
|
||||
std::vector<AnimationPointer> animations;
|
||||
std::vector<AnimationGroup> groups;
|
||||
};
|
||||
|
||||
static AnimationsTaken animations_take(Model& model, const std::set<int>& indices, const std::set<int>& groupIds)
|
||||
{
|
||||
AnimationsTaken taken = {};
|
||||
int index = 0;
|
||||
std::erase_if(model.animations.entries, [&](AnimationEntry& entry) {
|
||||
if (auto animation = std::get_if<AnimationPointer>(&entry))
|
||||
{
|
||||
if (!indices.contains(index++))
|
||||
return false;
|
||||
taken.animations.push_back(std::move(*animation));
|
||||
return true;
|
||||
}
|
||||
auto& group = std::get<AnimationGroup>(entry);
|
||||
if (groupIds.contains(group.id))
|
||||
{
|
||||
index += (int)group.animations.size();
|
||||
taken.groups.push_back(std::move(group));
|
||||
return true;
|
||||
}
|
||||
std::erase_if(group.animations, [&](AnimationPointer& animation) {
|
||||
if (!indices.contains(index++))
|
||||
return false;
|
||||
taken.animations.push_back(std::move(animation));
|
||||
return true;
|
||||
});
|
||||
return false;
|
||||
});
|
||||
return taken;
|
||||
std::set<std::uint64_t> uids;
|
||||
for (auto index : indices)
|
||||
if (auto animation = model.animation_get(index))
|
||||
uids.insert(animation->uid);
|
||||
return entries_take(
|
||||
model.animations.entries, [&](AnimationPointer& animation) { return uids.contains(animation->uid); }, groupIds);
|
||||
}
|
||||
|
||||
// Where an animation with this uid sits: its top-level entry, and its group (or nullptr) and position there.
|
||||
@@ -81,78 +54,47 @@ static AnimationPlace animation_place_get(Model& model, std::uint64_t uid)
|
||||
continue;
|
||||
}
|
||||
auto& group = std::get<AnimationGroup>(entries[i]);
|
||||
for (int j = 0; j < (int)group.animations.size(); ++j)
|
||||
if (group.animations[j]->uid == uid)
|
||||
for (int j = 0; j < (int)group.items.size(); ++j)
|
||||
if (group.items[j]->uid == uid)
|
||||
return {i, &group, j};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static int animation_group_entry_get(const Model& model, int groupId)
|
||||
{
|
||||
auto& entries = model.animations.entries;
|
||||
for (int i = 0; i < (int)entries.size(); ++i)
|
||||
if (auto group = std::get_if<AnimationGroup>(&entries[i]); group && group->id == groupId)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static AnimationGroup* animation_group_find(Model& model, int groupId)
|
||||
{
|
||||
auto entry = animation_group_entry_get(model, groupId);
|
||||
return entry == -1 ? nullptr : &std::get<AnimationGroup>(model.animations.entries[entry]);
|
||||
}
|
||||
|
||||
// Inserts animations before the animation at a flat index (or at the end), inside `groupId` when given.
|
||||
static void animations_insert(Model& model, int index, int groupId, std::vector<AnimationPointer> animations)
|
||||
{
|
||||
auto before = model.animation_get(index);
|
||||
auto place = before ? animation_place_get(model, before->uid) : AnimationPlace{};
|
||||
if (auto group = animation_group_find(model, groupId))
|
||||
auto& entries = model.animations.entries;
|
||||
if (auto group = group_find(entries, groupId))
|
||||
{
|
||||
auto position = place.group == group ? place.position : (int)group->animations.size();
|
||||
group->animations.insert(group->animations.begin() + position, animations.begin(), animations.end());
|
||||
auto position = place.group == group ? place.position : (int)group->items.size();
|
||||
group->items.insert(group->items.begin() + position, animations.begin(), animations.end());
|
||||
return;
|
||||
}
|
||||
auto& entries = model.animations.entries;
|
||||
auto position = place.entry == -1 ? (int)entries.size() : place.entry;
|
||||
entries.insert(entries.begin() + position, animations.begin(), animations.end());
|
||||
entries.insert(entries.begin() + (place.entry == -1 ? (int)entries.size() : place.entry), animations.begin(),
|
||||
animations.end());
|
||||
}
|
||||
|
||||
void animation_groups_remove(Model& model, const std::set<int>& groupIds)
|
||||
{
|
||||
auto& entries = model.animations.entries;
|
||||
for (int i = 0; i < (int)entries.size(); ++i)
|
||||
{
|
||||
auto group = std::get_if<AnimationGroup>(&entries[i]);
|
||||
if (!group || !groupIds.contains(group->id))
|
||||
continue;
|
||||
auto animations = std::move(group->animations);
|
||||
entries.erase(entries.begin() + i);
|
||||
entries.insert(entries.begin() + i, animations.begin(), animations.end());
|
||||
i += (int)animations.size() - 1;
|
||||
}
|
||||
groups_unwrap(model.animations.entries, groupIds);
|
||||
}
|
||||
|
||||
// An empty copy of a track list: same tracks and groups, no frames.
|
||||
// An empty copy of a track list: the same tracks and groups, without frames.
|
||||
static std::vector<TrackEntry> track_entries_shell_get(const std::vector<TrackEntry>& entries)
|
||||
{
|
||||
std::vector<TrackEntry> shell;
|
||||
auto track_shell_get = [](const Track& track) {
|
||||
return Track{.type = track.type, .id = track.id, .isVisible = track.isVisible};
|
||||
};
|
||||
for (auto& entry : entries)
|
||||
if (auto track = std::get_if<Track>(&entry))
|
||||
shell.emplace_back(track_shell_get(*track));
|
||||
else
|
||||
{
|
||||
auto& group = std::get<TrackGroup>(entry);
|
||||
TrackGroup copy{
|
||||
.id = group.id, .name = group.name, .isExpanded = group.isExpanded, .isVisible = group.isVisible};
|
||||
for (auto& groupTrack : group.tracks)
|
||||
copy.tracks.push_back(track_shell_get(groupTrack));
|
||||
shell.emplace_back(std::move(copy));
|
||||
}
|
||||
auto shell = entries_expand(entries, [](const Track& track, TrackGroup*) {
|
||||
return std::vector{Track{.type = track.type, .id = track.id, .isVisible = track.isVisible}};
|
||||
});
|
||||
groups_each(shell, [](TrackGroup& group) {
|
||||
group = TrackGroup{.id = group.id,
|
||||
.name = group.name,
|
||||
.isExpanded = group.isExpanded,
|
||||
.isVisible = group.isVisible,
|
||||
.items = std::move(group.items)};
|
||||
});
|
||||
return shell;
|
||||
}
|
||||
|
||||
@@ -193,11 +135,11 @@ Uids animations_move(Model& model, const std::set<int>& indices, const std::set<
|
||||
return {};
|
||||
|
||||
auto taken = animations_take(model, indices, groupIds);
|
||||
if (taken.animations.empty() && taken.groups.empty())
|
||||
if (taken.items.empty() && taken.groups.empty())
|
||||
return {};
|
||||
|
||||
Uids uids = {};
|
||||
for (auto& animation : taken.animations)
|
||||
for (auto& animation : taken.items)
|
||||
uids.push_back(animation->uid);
|
||||
|
||||
auto& entries = model.animations.entries;
|
||||
@@ -206,49 +148,43 @@ Uids animations_move(Model& model, const std::set<int>& indices, const std::set<
|
||||
{
|
||||
auto place = animation_place_get(model, targetUid);
|
||||
if (place.group)
|
||||
place.group->animations.insert(place.group->animations.begin() + place.position + target.isAfter,
|
||||
taken.animations.begin(), taken.animations.end());
|
||||
place.group->items.insert(place.group->items.begin() + place.position + target.isAfter, taken.items.begin(),
|
||||
taken.items.end());
|
||||
else if (place.entry != -1)
|
||||
entries.insert(entries.begin() + place.entry + target.isAfter, taken.animations.begin(),
|
||||
taken.animations.end());
|
||||
entries.insert(entries.begin() + place.entry + target.isAfter, taken.items.begin(), taken.items.end());
|
||||
place = animation_place_get(model, targetUid);
|
||||
if (place.entry != -1)
|
||||
topIndex = place.entry + (target.isAfter || place.group);
|
||||
}
|
||||
else if (auto entry = animation_group_entry_get(model, target.groupId); entry != -1)
|
||||
else if (auto entry = group_entry_get(entries, target.groupId); entry != -1)
|
||||
{
|
||||
auto& group = std::get<AnimationGroup>(entries[entry]);
|
||||
if (target.isInto)
|
||||
group.animations.insert(group.animations.end(), taken.animations.begin(), taken.animations.end());
|
||||
group.items.insert(group.items.end(), taken.items.begin(), taken.items.end());
|
||||
else
|
||||
entries.insert(entries.begin() + entry + target.isAfter, taken.animations.begin(), taken.animations.end());
|
||||
entries.insert(entries.begin() + entry + target.isAfter, taken.items.begin(), taken.items.end());
|
||||
if (!target.isInto && !target.isAfter)
|
||||
entry += (int)taken.animations.size();
|
||||
entry += (int)taken.items.size();
|
||||
topIndex = entry + (target.isAfter || target.isInto);
|
||||
}
|
||||
else
|
||||
entries.insert(entries.end(), taken.animations.begin(), taken.animations.end());
|
||||
entries.insert(entries.end(), taken.items.begin(), taken.items.end());
|
||||
|
||||
entries.insert(entries.begin() + std::clamp(topIndex, 0, (int)entries.size()),
|
||||
std::make_move_iterator(taken.groups.begin()), std::make_move_iterator(taken.groups.end()));
|
||||
return uids;
|
||||
}
|
||||
|
||||
// Wraps ungrouped animations in a new group placed where the first of them was.
|
||||
// Gathers animations into a new group placed where the first of them was.
|
||||
Uids animations_group(Model& model, const std::set<int>& indices, const std::string& name)
|
||||
{
|
||||
if (indices.empty())
|
||||
return {};
|
||||
auto first = model.animation_get(*indices.begin());
|
||||
auto first = indices.empty() ? nullptr : model.animation_get(*indices.begin());
|
||||
if (!first)
|
||||
return {};
|
||||
auto firstUid = first->uid;
|
||||
auto entry = animation_place_get(model, firstUid).entry;
|
||||
auto groupIds = animation_group_ids_get(model);
|
||||
auto taken = animations_take(model, indices, {});
|
||||
AnimationGroup group{
|
||||
.id = groupIds.empty() ? 0 : *groupIds.rbegin() + 1, .name = name, .animations = std::move(taken.animations)};
|
||||
auto& entries = model.animations.entries;
|
||||
auto entry = animation_place_get(model, first->uid).entry;
|
||||
AnimationGroup group = {
|
||||
.id = group_next_id(entries), .name = name, .items = animations_take(model, indices, {}).items};
|
||||
entries.insert(entries.begin() + std::clamp(entry, 0, (int)entries.size()), std::move(group));
|
||||
return {};
|
||||
}
|
||||
@@ -262,8 +198,7 @@ Uids animations_paste(Model& model, const std::string& text, int start, int targ
|
||||
if (entries.empty())
|
||||
return {};
|
||||
|
||||
auto existingGroupIds = animation_group_ids_get(model);
|
||||
auto nextGroupId = existingGroupIds.empty() ? 0 : *existingGroupIds.rbegin() + 1;
|
||||
auto nextGroupId = group_next_id(model.animations.entries);
|
||||
Uids uids = {};
|
||||
std::vector<AnimationPointer> loose;
|
||||
std::vector<AnimationEntry> placed;
|
||||
@@ -279,12 +214,12 @@ Uids animations_paste(Model& model, const std::string& text, int start, int targ
|
||||
auto& group = std::get<AnimationGroup>(entry);
|
||||
group.id = nextGroupId++;
|
||||
groupIds.insert(group.id);
|
||||
for (auto& animation : group.animations)
|
||||
for (auto& animation : group.items)
|
||||
uids.push_back(animation->uid);
|
||||
placed.push_back(std::move(entry));
|
||||
}
|
||||
|
||||
if (groupIds.empty() && targetGroupId != -1 && animation_group_find(model, targetGroupId))
|
||||
if (groupIds.empty() && group_find(model.animations.entries, targetGroupId))
|
||||
{
|
||||
animations_insert(model, start, targetGroupId, std::move(loose));
|
||||
return uids;
|
||||
@@ -326,8 +261,7 @@ static void track_entries_merge(std::vector<TrackEntry>& destination, const std:
|
||||
{
|
||||
std::map<int, int> groupRemap;
|
||||
std::set<int> matchedGroupIds;
|
||||
int nextGroupId = 0;
|
||||
groups_each(destination, [&](const TrackGroup& group) { nextGroupId = std::max(nextGroupId, group.id + 1); });
|
||||
auto nextGroupId = group_next_id(destination);
|
||||
|
||||
groups_each(source, [&](const TrackGroup& sourceGroup) {
|
||||
TrackGroup* matched = nullptr;
|
||||
@@ -352,17 +286,12 @@ static void track_entries_merge(std::vector<TrackEntry>& destination, const std:
|
||||
matchedGroupIds.insert(matched->id);
|
||||
});
|
||||
|
||||
tracks_each(source, [&](const Track& sourceTrack, const TrackGroup* sourceGroup) {
|
||||
TrackGroup* group = nullptr;
|
||||
if (sourceGroup)
|
||||
groups_each(destination, [&](TrackGroup& candidate) {
|
||||
if (candidate.id == groupRemap[sourceGroup->id])
|
||||
group = &candidate;
|
||||
});
|
||||
items_each(source, [&](const Track& sourceTrack, const TrackGroup* sourceGroup) {
|
||||
auto group = sourceGroup ? group_find(destination, groupRemap[sourceGroup->id]) : nullptr;
|
||||
Track* existing = nullptr;
|
||||
if (group)
|
||||
{
|
||||
for (auto& track : group->tracks)
|
||||
for (auto& track : group->items)
|
||||
if (track.id == sourceTrack.id)
|
||||
existing = &track;
|
||||
}
|
||||
@@ -374,7 +303,7 @@ static void track_entries_merge(std::vector<TrackEntry>& destination, const std:
|
||||
if (existing)
|
||||
track_merge(*existing, sourceTrack, type);
|
||||
else if (group)
|
||||
group->tracks.push_back(track_clone(sourceTrack));
|
||||
group->items.push_back(track_clone(sourceTrack));
|
||||
else
|
||||
destination.emplace_back(track_clone(sourceTrack));
|
||||
});
|
||||
|
||||
+11
-26
@@ -31,7 +31,7 @@ template <class Callback> static void layer_frames_each(Model& model, int animat
|
||||
if (!animation)
|
||||
return;
|
||||
std::vector<std::pair<int, int>> tracks;
|
||||
tracks_each(animation->layers, [&](const Track& track, const TrackGroup* group) {
|
||||
items_each(animation->layers, [&](const Track& track, const TrackGroup* group) {
|
||||
if (model.layer_spritesheet_get(track.id))
|
||||
tracks.emplace_back(track.id, group ? group->id : -1);
|
||||
});
|
||||
@@ -195,7 +195,7 @@ Uids content_remove(Model& model, ElementType type, const std::set<int>& ids, in
|
||||
break;
|
||||
case ElementType::REGION:
|
||||
animations_edit([&](Animation& animation, int) {
|
||||
tracks_each(animation.layers, [&](Track& track, auto*) {
|
||||
items_each(animation.layers, [&](Track& track, auto*) {
|
||||
auto layer = item_get(content.layers, track.id);
|
||||
if (!layer || layer->spritesheetId != spritesheetId)
|
||||
return;
|
||||
@@ -299,9 +299,9 @@ Uids frames_change_apply(Model& model, const ChangeTargets& targets, FrameChange
|
||||
if (targets.isRoot)
|
||||
whole_apply(animation->root);
|
||||
if (targets.isLayers)
|
||||
tracks_each(animation->layers, [&](Track& track, auto*) { whole_apply(track); });
|
||||
items_each(animation->layers, [&](Track& track, auto*) { whole_apply(track); });
|
||||
if (targets.isNulls)
|
||||
tracks_each(animation->nulls, [&](Track& track, auto*) { whole_apply(track); });
|
||||
items_each(animation->nulls, [&](Track& track, auto*) { whole_apply(track); });
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -421,28 +421,15 @@ bool file_merge(Model& model, const std::filesystem::path& path, const std::file
|
||||
}
|
||||
};
|
||||
|
||||
// Tracks follow their item's new id, and go with an item that didn't come along.
|
||||
auto entries_remap = [&](std::vector<TrackEntry>& entries, ElementType type) {
|
||||
std::vector<TrackEntry> result;
|
||||
auto track_keep = [&](Track& track) {
|
||||
entries_remove(entries, [&](Track& track) {
|
||||
track.id = id_remap(type, track.id);
|
||||
if (track.id < 0)
|
||||
return false;
|
||||
return true;
|
||||
track_remap(track);
|
||||
return true;
|
||||
};
|
||||
for (auto& entry : entries)
|
||||
if (auto track = std::get_if<Track>(&entry))
|
||||
{
|
||||
if (track_keep(*track))
|
||||
result.push_back(std::move(entry));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto& group = std::get<TrackGroup>(entry);
|
||||
std::erase_if(group.tracks, [&](Track& groupTrack) { return !track_keep(groupTrack); });
|
||||
result.push_back(std::move(entry));
|
||||
}
|
||||
entries = std::move(result);
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
auto animation_build = [&](const Animation& incoming) {
|
||||
@@ -500,16 +487,14 @@ bool file_merge(Model& model, const std::filesystem::path& path, const std::file
|
||||
for (auto [targetEntries, sourceEntries] :
|
||||
{std::pair{&target.layers, &processed.layers}, std::pair{&target.nulls, &processed.nulls}})
|
||||
{
|
||||
auto nextGroupId = 0;
|
||||
groups_each(*targetEntries,
|
||||
[&](const TrackGroup& group) { nextGroupId = std::max(nextGroupId, group.id + 1); });
|
||||
auto nextGroupId = group_next_id(*targetEntries);
|
||||
for (auto& entry : *sourceEntries)
|
||||
{
|
||||
if (auto group = std::get_if<TrackGroup>(&entry))
|
||||
{
|
||||
auto copy = *group;
|
||||
copy.id = nextGroupId++;
|
||||
std::erase_if(copy.tracks, [&](const Track& track) {
|
||||
std::erase_if(copy.items, [&](const Track& track) {
|
||||
auto existing = animation_track_get(target, (int)track.type, track.id);
|
||||
if (existing && !track.frames.empty())
|
||||
*existing = track;
|
||||
|
||||
+1
-14
@@ -114,20 +114,7 @@ Model animation_layers_flatten(Model model, const LibraryGet& library_get, const
|
||||
return tracks;
|
||||
};
|
||||
|
||||
std::vector<TrackEntry> entries;
|
||||
for (auto& entry : source.layers)
|
||||
if (auto track = std::get_if<Track>(&entry))
|
||||
for (auto& flat : tracks_flatten(*track))
|
||||
entries.emplace_back(std::move(flat));
|
||||
else
|
||||
{
|
||||
auto group = std::get<TrackGroup>(entry);
|
||||
group.tracks.clear();
|
||||
for (auto& groupTrack : std::get<TrackGroup>(entry).tracks)
|
||||
for (auto& flat : tracks_flatten(groupTrack))
|
||||
group.tracks.push_back(std::move(flat));
|
||||
entries.emplace_back(std::move(group));
|
||||
}
|
||||
auto entries = entries_expand(source.layers, [&](const Track& track, auto*) { return tracks_flatten(track); });
|
||||
// The flattened children's triggers join the animation's own, on the frames they fire (their events found by
|
||||
// name).
|
||||
auto triggers = source.triggers;
|
||||
|
||||
+9
-13
@@ -56,26 +56,22 @@ static void depths_bridge(Model& model)
|
||||
for (int i = 0; i < model.animations_count_get(); ++i)
|
||||
{
|
||||
auto isUsed = false;
|
||||
tracks_each(model.animation_get(i)->layers,
|
||||
[&](const Track& track, auto*) { isUsed |= std::ranges::any_of(track.frames, is_depth); });
|
||||
items_each(model.animation_get(i)->layers,
|
||||
[&](const Track& track, auto*) { isUsed |= std::ranges::any_of(track.frames, is_depth); });
|
||||
if (!isUsed)
|
||||
continue;
|
||||
auto& animation = *model.animation_edit(i);
|
||||
|
||||
// Groups dissolve, their roots and visibility baked into their tracks.
|
||||
std::vector<Track> tracks;
|
||||
for (auto& entry : animation.layers)
|
||||
if (auto track = std::get_if<Track>(&entry))
|
||||
tracks.push_back(std::move(*track));
|
||||
else
|
||||
items_each(animation.layers, [&](Track& track, TrackGroup* group) {
|
||||
if (group)
|
||||
{
|
||||
auto& group = std::get<TrackGroup>(entry);
|
||||
for (auto& groupTrack : group.tracks)
|
||||
{
|
||||
track_group_root_bake(groupTrack, group.root, animation.frameNum);
|
||||
groupTrack.isVisible = groupTrack.isVisible && group.isVisible;
|
||||
tracks.push_back(std::move(groupTrack));
|
||||
}
|
||||
track_group_root_bake(track, group->root, animation.frameNum);
|
||||
track.isVisible = track.isVisible && group->isVisible;
|
||||
}
|
||||
tracks.push_back(std::move(track));
|
||||
});
|
||||
|
||||
// Each track's place in drawing order: (its position + depth, depth), kept in position order when equal.
|
||||
std::vector<std::pair<std::pair<int, int>, Track>> placed;
|
||||
|
||||
@@ -176,7 +176,7 @@ std::optional<Image> spritesheets_merge(Model& model, const std::set<int>& ids,
|
||||
}
|
||||
|
||||
for (int i = 0; i < model.animations_count_get(); ++i)
|
||||
tracks_each(model.animation_edit(i)->layers, [&](Track& track, auto*) {
|
||||
items_each(model.animation_edit(i)->layers, [&](Track& track, auto*) {
|
||||
auto before = layerSpritesheetBefore.find(track.id);
|
||||
if (before == layerSpritesheetBefore.end() || before->second == baseId)
|
||||
return;
|
||||
|
||||
+22
-92
@@ -12,24 +12,12 @@ Uids items_remove(Model& model, int animationIndex, const std::map<int, std::set
|
||||
const std::map<int, std::set<int>>& groupIds)
|
||||
{
|
||||
for (auto type : {LAYER, NULL_})
|
||||
{
|
||||
auto typeIds = ids.contains(type) ? ids.at(type) : std::set<int>{};
|
||||
auto typeGroupIds = groupIds.contains(type) ? groupIds.at(type) : std::set<int>{};
|
||||
if (typeIds.empty() && typeGroupIds.empty())
|
||||
continue;
|
||||
auto entries = track_entries_edit(model, animationIndex, type);
|
||||
if (!entries)
|
||||
continue;
|
||||
|
||||
std::erase_if(*entries, [&](TrackEntry& entry) {
|
||||
if (auto group = std::get_if<TrackGroup>(&entry))
|
||||
{
|
||||
std::erase_if(group->tracks, [&](const Track& track) { return typeIds.contains(track.id); });
|
||||
return typeGroupIds.contains(group->id);
|
||||
}
|
||||
return typeIds.contains(std::get<Track>(entry).id);
|
||||
});
|
||||
}
|
||||
if (auto entries = track_entries_edit(model, animationIndex, type))
|
||||
{
|
||||
auto typeIds = ids.contains(type) ? ids.at(type) : std::set<int>{};
|
||||
auto typeGroupIds = groupIds.contains(type) ? groupIds.at(type) : std::set<int>{};
|
||||
entries_take(*entries, [&](Track& track) { return typeIds.contains(track.id); }, typeGroupIds);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -39,27 +27,10 @@ Uids items_group(Model& model, int animationIndex, int type, const std::set<int>
|
||||
auto entries = track_entries_edit(model, animationIndex, type);
|
||||
if (!entries)
|
||||
return {};
|
||||
|
||||
int nextId = 0;
|
||||
groups_each(*entries, [&](const TrackGroup& group) { nextId = std::max(nextId, group.id + 1); });
|
||||
TrackGroup group = {.id = nextId, .name = name};
|
||||
|
||||
auto insertIndex = -1;
|
||||
for (int i = 0; i < (int)entries->size(); ++i)
|
||||
{
|
||||
auto track = std::get_if<Track>(&(*entries)[i]);
|
||||
if (!track || !ids.contains(track->id))
|
||||
continue;
|
||||
if (insertIndex == -1)
|
||||
insertIndex = i;
|
||||
group.tracks.push_back(std::move(*track));
|
||||
entries->erase(entries->begin() + i--);
|
||||
}
|
||||
if (insertIndex == -1)
|
||||
return {};
|
||||
|
||||
TrackGroup group = {.id = group_next_id(*entries), .name = name};
|
||||
auto uid = group.uid;
|
||||
entries->insert(entries->begin() + insertIndex, std::move(group));
|
||||
if (!entries_wrap(*entries, [&](Track& track) { return ids.contains(track.id); }, std::move(group)))
|
||||
return {};
|
||||
return {uid};
|
||||
}
|
||||
|
||||
@@ -68,55 +39,22 @@ Uids items_group(Model& model, int animationIndex, int type, const std::set<int>
|
||||
Uids items_move(Model& model, int animationIndex, int type, const std::set<int>& ids, const std::set<int>& groupIds,
|
||||
RowTarget target, bool isDropAfter, bool isDropIntoGroup)
|
||||
{
|
||||
if (target.groupId != -1 && groupIds.contains(target.groupId))
|
||||
return {};
|
||||
if (target.isGroup && groupIds.contains(target.id))
|
||||
if ((target.groupId != -1 && groupIds.contains(target.groupId)) || (target.isGroup && groupIds.contains(target.id)))
|
||||
return {};
|
||||
auto entries = track_entries_edit(model, animationIndex, type);
|
||||
if (!entries)
|
||||
return {};
|
||||
|
||||
std::vector<Track> movedTracks;
|
||||
std::vector<TrackEntry> movedGroups;
|
||||
std::erase_if(*entries, [&](TrackEntry& entry) {
|
||||
if (auto group = std::get_if<TrackGroup>(&entry))
|
||||
{
|
||||
if (groupIds.contains(group->id))
|
||||
{
|
||||
movedGroups.push_back(std::move(entry));
|
||||
return true;
|
||||
}
|
||||
std::erase_if(group->tracks, [&](Track& track) {
|
||||
if (!ids.contains(track.id))
|
||||
return false;
|
||||
movedTracks.push_back(std::move(track));
|
||||
return true;
|
||||
});
|
||||
return false;
|
||||
}
|
||||
auto& track = std::get<Track>(entry);
|
||||
if (!ids.contains(track.id))
|
||||
return false;
|
||||
movedTracks.push_back(std::move(track));
|
||||
return true;
|
||||
});
|
||||
if (movedTracks.empty() && movedGroups.empty())
|
||||
auto taken = entries_take(*entries, [&](Track& track) { return ids.contains(track.id); }, groupIds);
|
||||
if (taken.items.empty() && taken.groups.empty())
|
||||
return {};
|
||||
|
||||
auto isAfter = type == LAYER ? !isDropAfter : isDropAfter;
|
||||
auto group_find = [&](int groupId) {
|
||||
return std::ranges::find_if(*entries, [&](const TrackEntry& entry) {
|
||||
auto group = std::get_if<TrackGroup>(&entry);
|
||||
return group && group->id == groupId;
|
||||
});
|
||||
};
|
||||
// Where top-level items go: beside the target row, or beside the group that holds it.
|
||||
auto isAfter = type == LAYER ? !isDropAfter : isDropAfter;
|
||||
auto topIndex = (int)entries->size();
|
||||
if (target.isGroup || target.groupId != -1)
|
||||
{
|
||||
auto group = group_find(target.isGroup ? target.id : target.groupId);
|
||||
if (group != entries->end())
|
||||
topIndex = (int)(group - entries->begin()) + isAfter;
|
||||
if (auto index = group_entry_get(*entries, target.isGroup ? target.id : target.groupId); index != -1)
|
||||
topIndex = index + isAfter;
|
||||
}
|
||||
else if (target.type == type)
|
||||
{
|
||||
@@ -128,28 +66,20 @@ Uids items_move(Model& model, int animationIndex, int type, const std::set<int>&
|
||||
topIndex = (int)(track - entries->begin()) + isAfter;
|
||||
}
|
||||
|
||||
// Into a group header lands just under it on screen. Beside a grouped track lands beside it.
|
||||
auto intoGroupId = target.isGroup && isDropIntoGroup ? target.id : !target.isGroup ? target.groupId : -1;
|
||||
auto intoGroup = intoGroupId == -1 ? entries->end() : group_find(intoGroupId);
|
||||
if (intoGroup != entries->end())
|
||||
if (auto group = group_find(*entries, intoGroupId))
|
||||
{
|
||||
// Into a group header lands just under it on screen. Beside a grouped track lands beside it.
|
||||
auto& tracks = std::get<TrackGroup>(*intoGroup).tracks;
|
||||
auto& tracks = group->items;
|
||||
auto beside = std::ranges::find_if(tracks, [&](const Track& track) { return track.id == target.id; });
|
||||
auto position = target.isGroup ? (type == LAYER ? tracks.end() : tracks.begin())
|
||||
: beside != tracks.end() ? beside + isAfter
|
||||
: tracks.end();
|
||||
tracks.insert(position, std::make_move_iterator(movedTracks.begin()),
|
||||
std::make_move_iterator(movedTracks.end()));
|
||||
movedTracks.clear();
|
||||
tracks.insert(position, std::make_move_iterator(taken.items.begin()),
|
||||
std::make_move_iterator(taken.items.end()));
|
||||
taken.items.clear();
|
||||
}
|
||||
|
||||
std::vector<TrackEntry> topMoved;
|
||||
for (auto& track : movedTracks)
|
||||
topMoved.emplace_back(std::move(track));
|
||||
topMoved.insert(topMoved.end(), std::make_move_iterator(movedGroups.begin()),
|
||||
std::make_move_iterator(movedGroups.end()));
|
||||
entries->insert(entries->begin() + std::clamp(topIndex, 0, (int)entries->size()),
|
||||
std::make_move_iterator(topMoved.begin()), std::make_move_iterator(topMoved.end()));
|
||||
entries_insert(*entries, topIndex, std::move(taken));
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user