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:
shweetandClaude Opus 5.5 committed 2026-10-08 16:51:48 -04:00
1 parent 6edf5f0e5b
commit d1a6d89d14
44 files changed
+873 -81

No files matched your search

+12
View File
@@ -28,6 +28,8 @@
X(EVENT_ELEMENT, "Event") \
X(SOUNDS, "Sounds") \
X(SOUND_ELEMENT, "Sound") \
X(LIBRARIES, "Libraries") \
X(LIBRARY, "Library") \
X(ANIMATIONS, "Animations") \
X(ANIMATION, "Animation") \
X(ROOT_ANIMATION, "RootAnimation") \
@@ -70,6 +72,16 @@ namespace anm2ed
COUNT
};
// How an animation layer's child animation plays through a keyframe: looping, once (then holding its last frame),
// or held at the keyframe's start frame.
enum class ChildMode
{
LOOP,
ONCE,
HOLD,
COUNT
};
enum class ItemType
{
NONE,
+90 -4
View File
@@ -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);
}
}
+9
View File
@@ -1,5 +1,6 @@
#pragma once
#include <functional>
#include <vector>
#include "model.hpp"
@@ -22,16 +23,22 @@ namespace anm2ed::model
int indexOffset{};
};
// The document a library id of a document (the one drawn, or a library) points at; nullptr when not loaded.
using LibraryGet = std::function<const Model*(const Model&, int)>;
struct DrawOptions
{
float time{};
bool isRootTransform{};
bool isIndexSampled{};
std::vector<DrawSample> samples{};
LibraryGet library_get{};
};
// 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.
// A layer draw shows spritesheet `spritesheetId` of `source`; an animation layer's child animation draws as its
// layers' draws, with the animation layer's id and transform.
struct Draw
{
DrawType type{};
@@ -41,6 +48,8 @@ namespace anm2ed::model
float time{};
Frame frame{};
glm::mat4 parent{1.0f};
const Model* source{};
int spritesheetId{-1};
};
glm::mat4 draw_quad_model_get(const Draw&);
+4 -2
View File
@@ -209,7 +209,8 @@ namespace anm2ed::model
}
// Bounds of all visible layer frames over the animation's length, or -1 when nothing is drawn.
glm::vec4 animation_rect(const Model& model, const Animation& animation, bool isRootTransform)
glm::vec4 animation_rect(const Model& model, const Animation& animation, bool isRootTransform,
const LibraryGet& library_get)
{
constexpr glm::ivec2 CORNERS[4] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
glm::vec2 min{std::numeric_limits<float>::infinity()};
@@ -217,7 +218,8 @@ namespace anm2ed::model
bool isAny{};
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}))
for (const auto& draw : animation_draws_get(
model, animation, {.time = time, .isRootTransform = isRootTransform, .library_get = library_get}))
{
if (draw.type != DrawType::LAYER || draw.frame.size == glm::vec2()) continue;
auto transform = draw.parent * draw_quad_model_get(draw);
+2 -2
View File
@@ -3,7 +3,7 @@
#include <set>
#include <vector>
#include "model.hpp"
#include "draw.hpp"
namespace anm2ed::model
{
@@ -19,5 +19,5 @@ namespace anm2ed::model
void frames_generate_from_grid(Track&, glm::ivec2, glm::ivec2, glm::vec2, int, int, int);
void frames_sort_by_at_frame(Track&);
void frames_change(Track&, FrameChange, ItemType, ChangeType, const std::set<int>&);
glm::vec4 animation_rect(const Model&, const Animation&, bool);
glm::vec4 animation_rect(const Model&, const Animation&, bool, const LibraryGet& = {});
}
+13 -2
View File
@@ -47,6 +47,13 @@ namespace anm2ed::model
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{};
@@ -232,9 +239,9 @@ namespace anm2ed::model
});
};
if (type == ElementType::SPRITESHEET)
if (type == ElementType::SPRITESHEET || type == ElementType::LIBRARY)
for (const auto& layer : content.layers)
used.insert(layer.spritesheetId);
used.insert(type == ElementType::LIBRARY ? layer.libraryId : layer.spritesheetId);
else if (only)
used_insert(*only);
else
@@ -255,6 +262,8 @@ namespace anm2ed::model
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 {};
}
@@ -356,6 +365,8 @@ namespace anm2ed::model
repair(event.uid);
for (auto& sound : content.sounds)
repair(sound.uid);
for (auto& library : content.libraries)
repair(library.uid);
}
int track_frames_count_get(const Track& track) { return (int)track.frames.size(); }
+24
View File
@@ -55,6 +55,12 @@ namespace anm2ed::model
int regionId{-1};
int shaderId{-1};
int depth{}; // a layer frame draws this many layers above (or below) its place in the timeline; editor-only
// On an animation layer: the child animation (empty for the layer's own) and how it plays; editor-only.
std::string childAnimation{};
ChildMode childMode{ChildMode::LOOP};
int childStart{};
bool isChildStartRandom{};
bool isChildRestart{};
int atFrame{-1};
int eventId{-1};
std::vector<int> soundIds{};
@@ -193,11 +199,27 @@ namespace anm2ed::model
int id{-1};
std::string name{};
int spritesheetId{};
// An animation layer plays `animation` (from library `libraryId`, or this file at -1) instead of showing its
// spritesheet.
int libraryId{-1};
std::string animation{};
Extras extras{};
bool is_animation() const { return !animation.empty(); }
bool operator==(const Layer&) const = default;
};
// Another .anm2 whose animations this one's layers can play.
struct Library
{
std::uint64_t uid{util::uid_next()};
int id{-1};
std::filesystem::path path{};
Extras extras{};
bool operator==(const Library&) const = default;
};
struct Null
{
std::uint64_t uid{util::uid_next()};
@@ -248,6 +270,7 @@ namespace anm2ed::model
std::vector<Null> nulls{};
std::vector<Event> events{};
std::vector<Sound> sounds{};
std::vector<Library> libraries{};
std::vector<ElementType> layout{ElementType::SPRITESHEETS, ElementType::SHADERS, ElementType::LAYERS,
ElementType::NULLS, ElementType::EVENTS};
Extras extras{};
@@ -338,6 +361,7 @@ namespace anm2ed::model
int animations_count_get() const;
const Animation* animation_get(int) const;
const Animation* animation_named_get(std::string_view) const;
Animation* animation_edit(int);
int animation_group_id_get(int) const;
const AnimationGroup* animation_group_get(int) const;
+62 -11
View File
@@ -28,6 +28,8 @@ namespace anm2ed::model
};
constexpr std::string_view INTERPOLATION_VALUES[] = {"", "", "EaseIn", "EaseOut", "EaseInOut"};
constexpr std::string_view ORIGIN_VALUES[] = {"", "TopLeft", "Center"};
constexpr std::string_view CHILD_MODE_VALUES[] = {"Loop", "Once", "Hold"};
constexpr std::string_view CHILD_START_RANDOM = "Random";
constexpr const char* TINT_ATTRIBUTES[] = {"RedTint", "GreenTint", "BlueTint", "AlphaTint"};
constexpr const char* OFFSET_ATTRIBUTES[] = {"RedOffset", "GreenOffset", "BlueOffset"};
// Attributes the editor reads (or older builds wrote); any other attribute is kept verbatim as Extras.
@@ -38,7 +40,8 @@ namespace anm2ed::model
"ShaderId", "Depth", "SoundId", "GroupId", "Index", "Loop", "StartMarker", "EndMarker", "Visible", "ShowRect", "IsExpanded", "Enabled", "Rotation",
"XPivot", "YPivot", "XCrop", "YCrop", "XPosition", "YPosition", "Width", "Height", "XScale", "YScale", "SkewX",
"SkewY", "ShearX", "ShearY", "RedTint", "GreenTint", "BlueTint", "AlphaTint", "RedOffset", "GreenOffset", "BlueOffset",
"Interpolated", "Origin", "BakeInterpolation", "BakeDelay", "BakeCount", "OriginalDelay"};
"Interpolated", "Origin", "BakeInterpolation", "BakeDelay", "BakeCount", "OriginalDelay", "Library", "Animation",
"ChildAnimation", "ChildMode", "ChildStart", "Restart"};
// clang-format on
constexpr std::string_view SOURCE_DOCUMENT_TAG = "SourceDocument";
constexpr std::string_view FRAME_RESTORE_TAG = "FrameRestore";
@@ -220,6 +223,13 @@ namespace anm2ed::model
frame.regionId = int_get(element, "RegionId");
frame.shaderId = int_get(element, "ShaderId");
frame.depth = int_get(element, "Depth", 0);
frame.childAnimation = string_get(element, "ChildAnimation");
auto childMode = std::ranges::find(CHILD_MODE_VALUES, string_get(element, "ChildMode"));
if (childMode != std::end(CHILD_MODE_VALUES))
frame.childMode = (ChildMode)(childMode - std::begin(CHILD_MODE_VALUES));
frame.isChildStartRandom = string_get(element, "ChildStart") == CHILD_START_RANDOM;
frame.childStart = int_get(element, "ChildStart", 0);
frame.isChildRestart = bool_get(element, "Restart", false);
frame.atFrame = int_get(element, "AtFrame");
frame.eventId = int_get(element, "EventId");
frame.extras = extras_read(element);
@@ -588,6 +598,8 @@ namespace anm2ed::model
return {.id = int_get(element, "Id"),
.name = string_get(element, "Name"),
.spritesheetId = int_get(element, "SpritesheetId", 0),
.libraryId = int_get(element, "Library"),
.animation = string_get(element, "Animation"),
.extras = extras_read(element)};
}
template <> Null item_read(const XMLElement* element)
@@ -605,6 +617,10 @@ namespace anm2ed::model
{
return {.id = int_get(element, "Id"), .path = path_get(element, "Path"), .extras = extras_read(element)};
}
template <> Library item_read(const XMLElement* element)
{
return {.id = int_get(element, "Id"), .path = path_get(element, "Path"), .extras = extras_read(element)};
}
template <class Item> constexpr ElementType ITEM_TYPE{};
template <> constexpr ElementType ITEM_TYPE<Spritesheet>{ElementType::SPRITESHEET};
@@ -614,6 +630,7 @@ namespace anm2ed::model
template <> constexpr ElementType ITEM_TYPE<Null>{ElementType::NULL_ELEMENT};
template <> constexpr ElementType ITEM_TYPE<Event>{ElementType::EVENT_ELEMENT};
template <> constexpr ElementType ITEM_TYPE<Sound>{ElementType::SOUND_ELEMENT};
template <> constexpr ElementType ITEM_TYPE<Library>{ElementType::LIBRARY};
template <class Item> std::vector<Item> items_read(const XMLElement* container, Content& content, ElementType type)
{
@@ -640,7 +657,8 @@ namespace anm2ed::model
if (child->Name() == LEGACY_GROUPS_TAG) return;
auto isKnown = type == ElementType::SPRITESHEETS || type == ElementType::SHADERS ||
type == ElementType::LAYERS || type == ElementType::NULLS ||
type == ElementType::EVENTS || type == ElementType::SOUNDS;
type == ElementType::EVENTS || type == ElementType::SOUNDS ||
type == ElementType::LIBRARIES;
if (!isKnown || std::ranges::contains(content.layout, type))
return extras_child_add(content.extras, child, position);
content.layout.push_back(type);
@@ -651,6 +669,7 @@ namespace anm2ed::model
if (type == ElementType::NULLS) content.nulls = items_read<Null>(child, content, type);
if (type == ElementType::EVENTS) content.events = items_read<Event>(child, content, type);
if (type == ElementType::SOUNDS) content.sounds = items_read<Sound>(child, content, type);
if (type == ElementType::LIBRARIES) content.libraries = items_read<Library>(child, content, type);
});
return content;
}
@@ -691,6 +710,7 @@ namespace anm2ed::model
ids_repair(content.nulls);
ids_repair(content.events);
ids_repair(content.sounds);
ids_repair(content.libraries);
for (auto& spritesheet : content.spritesheets)
ids_repair(spritesheet.regions);
for (auto& shader : content.shaders)
@@ -1146,8 +1166,17 @@ namespace anm2ed::model
sink.attribute("Rotation", frame.rotation);
interpolation_add(sink, "Interpolated", frame.interpolation);
if (writer.is(SERIALIZE_EXTENSIONS) && frame.shaderId != -1) sink.attribute("ShaderId", frame.shaderId);
if (writer.is(SERIALIZE_EXTENSIONS) && type == ItemType::LAYER && frame.depth != 0)
sink.attribute("Depth", frame.depth);
if (writer.is(SERIALIZE_EXTENSIONS) && type == ItemType::LAYER)
{
if (frame.depth != 0) sink.attribute("Depth", frame.depth);
if (!frame.childAnimation.empty()) sink.attribute("ChildAnimation", frame.childAnimation);
if (frame.childMode != ChildMode::LOOP) sink.attribute("ChildMode", CHILD_MODE_VALUES[(int)frame.childMode]);
if (frame.isChildStartRandom)
sink.attribute("ChildStart", CHILD_START_RANDOM);
else if (frame.childStart != 0)
sink.attribute("ChildStart", frame.childStart);
if (frame.isChildRestart) sink.attribute("Restart", frame.isChildRestart);
}
extras_attributes_write(sink, frame.extras);
if (bake.delay > 0)
{
@@ -1325,6 +1354,7 @@ namespace anm2ed::model
IsaacIssues isaac_issues_get(const Model& model)
{
IsaacIssues issues{};
issues.animationLayers = (int)std::ranges::count_if(model.content.layers, &Layer::is_animation);
for (auto [index, animation] : model.animations_get())
{
for (const auto* entries : {&animation->layers, &animation->nulls})
@@ -1544,12 +1574,17 @@ namespace anm2ed::model
children.close();
}
void item_write(Sink& sink, const Writer&, const Layer& layer, int id)
void item_write(Sink& sink, const Writer& writer, const Layer& layer, int id)
{
sink.open(tag_get(ElementType::LAYER_ELEMENT));
sink.attribute("Id", id);
sink.attribute("Name", layer.name);
sink.attribute("SpritesheetId", layer.spritesheetId);
if (writer.is(SERIALIZE_EXTENSIONS) && layer.is_animation())
{
if (layer.libraryId != -1) sink.attribute("Library", layer.libraryId);
sink.attribute("Animation", layer.animation);
}
extras_write(sink, layer.extras);
element_close(sink, layer.extras);
}
@@ -1573,13 +1608,24 @@ namespace anm2ed::model
element_close(sink, event.extras);
}
// Sounds and libraries are a path.
template <class Item> void item_path_write(Sink& sink, const Item& item, ElementType type, int id)
{
sink.open(tag_get(type));
sink.attribute("Id", id);
path_add(sink, "Path", item.path);
extras_write(sink, item.extras);
element_close(sink, item.extras);
}
void item_write(Sink& sink, const Writer&, const Sound& sound, int id)
{
sink.open(tag_get(ElementType::SOUND_ELEMENT));
sink.attribute("Id", id);
path_add(sink, "Path", sound.path);
extras_write(sink, sound.extras);
element_close(sink, sound.extras);
item_path_write(sink, sound, ElementType::SOUND_ELEMENT, id);
}
void item_write(Sink& sink, const Writer&, const Library& library, int id)
{
item_path_write(sink, library, ElementType::LIBRARY, id);
}
// A content container's unknown XML (a file may repeat a container; its parts are merged in order).
@@ -1622,7 +1668,8 @@ namespace anm2ed::model
{ElementType::LAYERS, !content.layers.empty()},
{ElementType::NULLS, !content.nulls.empty()},
{ElementType::EVENTS, !content.events.empty()},
{ElementType::SOUNDS, !content.sounds.empty()}}))
{ElementType::SOUNDS, !content.sounds.empty()},
{ElementType::LIBRARIES, !content.libraries.empty()}}))
{
children.add();
auto extras = container_extras_get(content, type);
@@ -1634,6 +1681,9 @@ namespace anm2ed::model
if (type == ElementType::SOUNDS && writer.is(SERIALIZE_SOUNDS) &&
(!content.sounds.empty() || !extras.children.empty()))
items_write(sink, writer, content.sounds, type, extras);
if (type == ElementType::LIBRARIES && writer.is(SERIALIZE_EXTENSIONS) &&
(!content.libraries.empty() || !extras.children.empty()))
items_write(sink, writer, content.libraries, type, extras);
if (type == ElementType::SPRITESHEETS) items_write(sink, writer, content.spritesheets, type, extras);
if (type == ElementType::LAYERS) items_write(sink, writer, content.layers, type, extras);
if (type == ElementType::NULLS) items_write(sink, writer, content.nulls, type, extras);
@@ -1840,5 +1890,6 @@ namespace anm2ed::model
X(Null)
X(Event)
X(Sound)
X(Library)
#undef X
}
+1
View File
@@ -17,6 +17,7 @@ namespace anm2ed::model
int shaderFrames{};
int depthFrames{};
int triggerSounds{};
int animationLayers{};
bool operator==(const IsaacIssues&) const = default;
};