Libraries window, animation layers and child playback
A Libraries window (docked with Spritesheets and Sounds) links other .anm2 files: cards list each one's animations, open it as a tab, and reload when it changes on disk. A layer's Source is a spritesheet or an animation, from this file or a library; the preview, renders and CLI draw the child in the layer's frame. Animation-layer keyframes set the child's animation, mode (loop, once, hold), start frame (or random) and restart; Child is also a copyable frame property. All of it is editor-only and counted by the Isaac warning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6edf5f0e5b
commit
d1a6d89d14
44 files changed
+873
-81
No files matched your search
+90
-4
@@ -1,6 +1,7 @@
|
||||
#include "draw.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "frames.hpp"
|
||||
#include "math.hpp"
|
||||
@@ -9,6 +10,9 @@ using namespace anm2ed::util;
|
||||
|
||||
namespace anm2ed::model
|
||||
{
|
||||
constexpr unsigned RANDOM_MULTIPLIER_A = 73856093u;
|
||||
constexpr unsigned RANDOM_MULTIPLIER_B = 19349663u;
|
||||
|
||||
glm::mat4 draw_quad_model_get(const Draw& draw)
|
||||
{
|
||||
auto& frame = draw.frame;
|
||||
@@ -39,7 +43,43 @@ namespace anm2ed::model
|
||||
return times;
|
||||
}
|
||||
|
||||
std::vector<Draw> animation_draws_get(const Model& model, const Animation& animation, const DrawOptions& options)
|
||||
// A keyframe's start frame in its child animation; a random start is fixed per layer and keyframe.
|
||||
float child_start_get(const Frame& frame, int length, int seed, int index)
|
||||
{
|
||||
if (!frame.isChildStartRandom) return (float)frame.childStart;
|
||||
return (float)((((unsigned)seed * RANDOM_MULTIPLIER_A) ^ ((unsigned)index * RANDOM_MULTIPLIER_B)) %
|
||||
(unsigned)length);
|
||||
}
|
||||
|
||||
// Where an animation layer's child animation is at `time`. Its clock runs from the last keyframe that restarted it
|
||||
// (the first, one set to restart, or one that changes the animation), from that keyframe's start frame; the keyframe
|
||||
// playing then loops the child, stops it at its end, or holds its own start frame.
|
||||
float child_time_get(const Track& track, float time, int length, int seed)
|
||||
{
|
||||
float start{};
|
||||
float clockStart{};
|
||||
float clockOffset{};
|
||||
for (int i = 0; i < (int)track.frames.size(); ++i)
|
||||
{
|
||||
auto& frame = track.frames[i];
|
||||
if (i == 0 || frame.isChildRestart || frame.childAnimation != track.frames[i - 1].childAnimation)
|
||||
{
|
||||
clockStart = start;
|
||||
clockOffset = child_start_get(frame, length, seed, i);
|
||||
}
|
||||
start += frame.duration;
|
||||
if (time >= start && i + 1 < (int)track.frames.size()) continue;
|
||||
if (frame.childMode == ChildMode::HOLD) return child_start_get(frame, length, seed, i);
|
||||
auto childTime = clockOffset + std::max(time, 0.0f) - clockStart;
|
||||
if (frame.childMode == ChildMode::ONCE) return std::min(childTime, (float)(length - 1));
|
||||
return std::fmod(childTime, (float)length);
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
// `playing` holds the animations being drawn into one another, so an animation never draws itself.
|
||||
std::vector<Draw> animation_draws_get(const Model& model, const Animation& animation, const DrawOptions& options,
|
||||
std::vector<const Animation*>& playing)
|
||||
{
|
||||
std::vector<Draw> draws{};
|
||||
// Each layer draw's place in the drawing order: its track's position among the layers plus its frame's depth; a
|
||||
@@ -54,14 +94,50 @@ namespace anm2ed::model
|
||||
if (group) parent *= frame_parent_model_get(frame_generate(group->root, time));
|
||||
return parent;
|
||||
};
|
||||
// An animation layer's child animation, placed by the layer's frame (its pivot is the child's origin).
|
||||
auto child_draws_add =
|
||||
[&](const Track& track, const Layer& layer, int sample, float time, const Frame& frame, const glm::mat4& parent)
|
||||
{
|
||||
auto childModel = layer.libraryId == -1 ? &model
|
||||
: options.library_get ? options.library_get(model, layer.libraryId)
|
||||
: nullptr;
|
||||
auto childAnimation =
|
||||
childModel
|
||||
? childModel->animation_named_get(frame.childAnimation.empty() ? layer.animation : frame.childAnimation)
|
||||
: nullptr;
|
||||
if (!childAnimation || childAnimation->frameNum < FRAME_NUM_MIN || std::ranges::contains(playing, childAnimation))
|
||||
return;
|
||||
playing.push_back(childAnimation);
|
||||
auto transform = parent * math::quad_model_get(glm::vec2(1.0f), frame.position, frame.pivot,
|
||||
math::percent_to_unit(frame.scale), frame.rotation,
|
||||
math::percent_to_unit(frame.skew));
|
||||
DrawOptions childOptions{.time = child_time_get(track, time, childAnimation->frameNum, layer.id),
|
||||
.isRootTransform = true,
|
||||
.library_get = options.library_get};
|
||||
for (auto child : animation_draws_get(*childModel, *childAnimation, childOptions, playing))
|
||||
{
|
||||
if (child.type != DrawType::LAYER) continue;
|
||||
child.id = layer.id;
|
||||
child.sample = sample;
|
||||
child.time = time;
|
||||
child.parent = transform * child.parent;
|
||||
child.frame.tint *= frame.tint;
|
||||
child.frame.colorOffset += frame.colorOffset;
|
||||
if (child.source != &model) child.frame.shaderId = -1;
|
||||
layerOrders.emplace_back(layerOrder + frame.depth, frame.depth);
|
||||
draws.push_back(std::move(child));
|
||||
}
|
||||
playing.pop_back();
|
||||
};
|
||||
auto track_draws_add = [&](const Track& track, DrawType type, int id, int groupType, const TrackGroup* group)
|
||||
{
|
||||
auto layer = type == DrawType::LAYER ? item_get(model.content.layers, id) : nullptr;
|
||||
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)
|
||||
if (layer)
|
||||
{
|
||||
frame = model.frame_effective(id, frame);
|
||||
if (options.isRootTransform)
|
||||
@@ -72,12 +148,16 @@ namespace anm2ed::model
|
||||
frame.tint *= rootFrame.tint;
|
||||
frame.colorOffset += rootFrame.colorOffset;
|
||||
}
|
||||
if (layer->is_animation())
|
||||
{
|
||||
child_draws_add(track, *layer, sample, time, frame, parent);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
layerOrders.emplace_back(layerOrder + frame.depth, frame.depth);
|
||||
draws.push_back({type, id, groupType, sample, time, frame, parent});
|
||||
draws.push_back({type, id, groupType, sample, time, frame, parent, &model, layer ? layer->spritesheetId : -1});
|
||||
}
|
||||
};
|
||||
|
||||
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}})
|
||||
{
|
||||
@@ -117,4 +197,10 @@ namespace anm2ed::model
|
||||
draws[layerIndices[i]] = std::move(layerDraws[i]);
|
||||
return draws;
|
||||
}
|
||||
|
||||
std::vector<Draw> animation_draws_get(const Model& model, const Animation& animation, const DrawOptions& options)
|
||||
{
|
||||
std::vector<const Animation*> playing{&animation};
|
||||
return animation_draws_get(model, animation, options, playing);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user