Stage 9b: animations draw from a model draw list

- model::animation_draws_get returns what an animation draws at a time (root,
  group roots, layers, nulls; onion-skin samples first), with root and group
  root transforms as each item's parent and their tint/offset folded into
  layer frames.
- The preview, the About friends and animation_rect all use it; the
  preview's render lambda shrinks to drawing the list.
- Tests for draw order and root folding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 18:11:19 -04:00
1 parent 595658581f
commit 39ccfb723a
6 files changed
+306 -374

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
#include "draw.hpp"
#include "frames.hpp"
#include "math.hpp"
using namespace anm2ed::util;
namespace anm2ed::model
{
glm::mat4 draw_quad_model_get(const Draw& draw)
{
auto& frame = draw.frame;
return math::quad_model_get(frame.size, frame.position, frame.pivot, math::percent_to_unit(frame.scale),
frame.rotation, math::percent_to_unit(frame.shear));
}
// When an item is drawn: now, and for each sample its time if the track has a frame there.
std::vector<std::pair<int, float>> draw_times_get(const Animation& animation, const Track& track,
const DrawOptions& options)
{
std::vector<std::pair<int, float>> times{};
for (int i = 0; i < (int)options.samples.size(); ++i)
{
auto& sample = options.samples[i];
if (!options.isIndexSampled)
{
auto time = options.time + sample.timeOffset;
if (time >= 0.0f && time <= (float)animation.frameNum) times.emplace_back(i, time);
continue;
}
auto index = track.frames.empty() ? -1 : frame_index_from_time_get(track, options.time);
if (index < 0 || index + sample.indexOffset < 0 || index + sample.indexOffset >= (int)track.frames.size())
continue;
times.emplace_back(i, frame_time_from_index_get(track, index + sample.indexOffset));
}
times.emplace_back(-1, options.time);
return times;
}
std::vector<Draw> animation_draws_get(const Model& model, const Animation& animation, const DrawOptions& options)
{
std::vector<Draw> draws{};
auto parent_get = [&](float time, const TrackGroup* group)
{
glm::mat4 parent(1.0f);
if (!options.isRootTransform) return parent;
parent *= frame_parent_model_get(frame_generate(animation.root, time));
if (group) parent *= frame_parent_model_get(frame_generate(group->root, time));
return parent;
};
auto track_draws_add = [&](const Track& track, DrawType type, int id, int groupType, const TrackGroup* group)
{
for (auto [sample, time] : draw_times_get(animation, track, options))
{
auto frame = frame_generate(track, time);
if (!frame.isVisible) continue;
auto parent = parent_get(time, type == DrawType::ROOT ? nullptr : group);
if (type == DrawType::LAYER)
{
frame = model.frame_effective(id, frame);
if (options.isRootTransform)
for (auto* root : {&animation.root, group ? &group->root : nullptr})
if (root)
{
auto rootFrame = frame_generate(*root, time);
frame.tint *= rootFrame.tint;
frame.colorOffset += rootFrame.colorOffset;
}
}
draws.push_back({type, id, groupType, sample, time, frame, parent});
}
};
if (animation.root.isVisible) track_draws_add(animation.root, DrawType::ROOT, -1, NONE, nullptr);
for (auto [type, entries] : {std::pair{LAYER, &animation.layers}, std::pair{NULL_, &animation.nulls}})
{
auto drawType = type == LAYER ? DrawType::LAYER : DrawType::NULL_;
auto track_add = [&](const Track& track, const TrackGroup* group)
{
auto isFound = type == LAYER ? item_get(model.content.layers, track.id) != nullptr
: item_get(model.content.nulls, track.id) != nullptr;
if (track.isVisible && isFound) track_draws_add(track, drawType, track.id, NONE, group);
};
for (const auto& entry : *entries)
{
if (auto track = std::get_if<Track>(&entry))
{
track_add(*track, nullptr);
continue;
}
auto& group = std::get<TrackGroup>(entry);
if (!group.isVisible) continue;
if (group.root.isVisible) track_draws_add(group.root, DrawType::GROUP_ROOT, group.id, type, &group);
for (const auto& groupTrack : group.tracks)
track_add(groupTrack, &group);
}
}
return draws;
}
}
+48
View File
@@ -0,0 +1,48 @@
#pragma once
#include <vector>
#include "model.hpp"
// What an animation draws at a time, as plain data: every canvas (preview, render, about) draws from this list.
namespace anm2ed::model
{
enum class DrawType
{
ROOT,
GROUP_ROOT,
LAYER,
NULL_
};
// An onion-skin sample: a time offset, or (when sampling by index) an offset in each track's frames.
struct DrawSample
{
float timeOffset{};
int indexOffset{};
};
struct DrawOptions
{
float time{};
bool isRootTransform{};
bool isIndexSampled{};
std::vector<DrawSample> samples{};
};
// An item's frame at a time; `parent` is the transform of its root and group root when root transforms are shown
// (whose tint and color offset are then folded into a layer's frame). `sample` indexes the samples, -1 for now.
struct Draw
{
DrawType type{};
int id{-1};
int groupType{NONE};
int sample{-1};
float time{};
Frame frame{};
glm::mat4 parent{1.0f};
};
glm::mat4 draw_quad_model_get(const Draw&);
std::vector<Draw> animation_draws_get(const Model&, const Animation&, const DrawOptions&);
}
+14 -29
View File
@@ -3,6 +3,7 @@
#include <algorithm>
#include <limits>
#include "draw.hpp"
#include "math.hpp"
using namespace anm2ed::util;
@@ -228,35 +229,19 @@ namespace anm2ed::model
glm::vec2 max{-std::numeric_limits<float>::infinity()};
bool isAny{};
for (float t = 0.0f; t < (float)animation.frameNum; t += 1.0f)
{
glm::mat4 transform(1.0f);
if (isRootTransform) transform *= frame_parent_model_get(frame_generate(animation.root, t));
tracks_each(animation.layers,
[&](const Track& track, const TrackGroup* group)
{
if (!track.isVisible || (group && !group->isVisible)) return;
auto itemTransform = transform;
if (isRootTransform && group)
itemTransform *= frame_parent_model_get(frame_generate(group->root, t));
auto frame = model.frame_effective(track.id, frame_generate(track, t));
if (frame.size == glm::vec2() || !frame.isVisible) return;
auto layerTransform =
itemTransform * math::quad_model_get(frame.size, frame.position, frame.pivot,
math::percent_to_unit(frame.scale), frame.rotation,
math::percent_to_unit(frame.shear));
for (auto& corner : CORNERS)
{
auto world = glm::vec2(layerTransform * glm::vec4(corner, 0.0f, 1.0f));
min = glm::min(min, world);
max = glm::max(max, world);
isAny = true;
}
});
}
for (float time = 0.0f; time < (float)animation.frameNum; time += 1.0f)
for (const auto& draw : animation_draws_get(model, animation, {.time = time, .isRootTransform = isRootTransform}))
{
if (draw.type != DrawType::LAYER || draw.frame.size == glm::vec2()) continue;
auto transform = draw.parent * draw_quad_model_get(draw);
for (auto& corner : CORNERS)
{
auto world = glm::vec2(transform * glm::vec4(corner, 0.0f, 1.0f));
min = glm::min(min, world);
max = glm::max(max, world);
isAny = true;
}
}
if (!isAny) return glm::vec4(-1.0f);
return {min.x, min.y, max.x - min.x, max.y - min.y};