87 lines
4.0 KiB
C#
87 lines
4.0 KiB
C#
using GamePort.Assets;
|
|
using GamePort.Game;
|
|
|
|
namespace GamePort.Application;
|
|
|
|
/// <summary>Shared original clips for explicit inspection and simulation pose selection.</summary>
|
|
public sealed class PreviewAnimationLibrary
|
|
{
|
|
private readonly Dictionary<uint, (string Name, MorphAnimation Animation)> choices = [];
|
|
private readonly Dictionary<PlayerPose, MorphAnimation> movement = [];
|
|
private readonly Dictionary<string, MorphAnimation> actions = new(StringComparer.OrdinalIgnoreCase);
|
|
public MorphAnimation Current { get; private set; }
|
|
public string Name { get; private set; }
|
|
public string WindowTitle => $"Jimmy Neutron — scene preview | {Name}{(Current.HasVertexMotion ? "" : " (still pose)")} | 1 idle, 2 run, 3 jump, 4 scratch | C camera";
|
|
|
|
public PreviewAnimationLibrary(string manifest)
|
|
{
|
|
Current = new MorphAnimation(manifest);
|
|
Name = Path.GetFileName(Path.GetDirectoryName(Path.GetFullPath(manifest)))!;
|
|
var parent = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(manifest)!, ".."));
|
|
var loaded = new Dictionary<string, MorphAnimation>(StringComparer.Ordinal)
|
|
{ [Path.GetFullPath(manifest)] = Current };
|
|
MorphAnimation Load(string path)
|
|
{
|
|
path = Path.GetFullPath(path);
|
|
if (loaded.TryGetValue(path, out var cached)) return cached;
|
|
var clip = new MorphAnimation(path);
|
|
if (clip.Base.Vertices.Length != Current.Base.Vertices.Length
|
|
|| !clip.Base.Indices.SequenceEqual(Current.Base.Indices)
|
|
|| !clip.Base.Vertices.Select(v => v.Uv).SequenceEqual(Current.Base.Vertices.Select(v => v.Uv))
|
|
|| clip.Texture.Width != Current.Texture.Width || clip.Texture.Height != Current.Texture.Height
|
|
|| !clip.Texture.Rgba.SequenceEqual(Current.Texture.Rgba))
|
|
throw new InvalidDataException($"Animation family mismatch: {path}");
|
|
loaded[path] = clip;
|
|
return clip;
|
|
}
|
|
foreach (var (scancode, folder, label) in new[]
|
|
{
|
|
(30u, "jimstop", "Idle"), (31u, "jimrun", "Run cycle"),
|
|
(32u, "jimjump", "Jump cycle"), (33u, "jimscratch", "Scratch cycle")
|
|
})
|
|
{
|
|
var path = Path.Combine(parent, folder, "animation.json");
|
|
if (!File.Exists(path)) continue;
|
|
var clip = Load(path);
|
|
choices[scancode] = (label, clip);
|
|
if (ReferenceEquals(clip, Current)) Name = label;
|
|
}
|
|
foreach (var (pose, folder) in new[] { (PlayerPose.Idle, "jimstop"), (PlayerPose.Run, "jimrun"),
|
|
(PlayerPose.Backpedal, "jimbackpedal"), (PlayerPose.Jump, "jimjump"), (PlayerPose.Fall, "jimfall"), (PlayerPose.Fly, "jimfly"), (PlayerPose.Shoot, "jimshoot") })
|
|
{
|
|
var path = Path.Combine(parent, folder, "animation.json");
|
|
if (!File.Exists(path)) continue;
|
|
movement[pose] = Load(path);
|
|
}
|
|
foreach (var (action, folder) in new[] { ("STOP", "jimstop"), ("TALK", "jimtalk"), ("RUN", "jimrun"),
|
|
("JUMP", "jimjump"), ("FALL", "jimfall"), ("SCRATCH", "jimscratch"), ("FLY", "jimfly"), ("SHOOT", "jimshoot") })
|
|
{
|
|
var path = Path.Combine(parent, folder, "animation.json");
|
|
if (File.Exists(path)) actions[action] = Load(path);
|
|
}
|
|
}
|
|
|
|
public bool Select(uint scancode)
|
|
{
|
|
if (!choices.TryGetValue(scancode, out var clip)) return false;
|
|
Current = clip.Animation;
|
|
Name = clip.Name;
|
|
return true;
|
|
}
|
|
|
|
public bool SelectPose(PlayerPose pose)
|
|
{
|
|
if (!movement.TryGetValue(pose, out var clip)) throw new InvalidDataException($"Prepare original {pose} animation first");
|
|
if (ReferenceEquals(Current, clip)) return false;
|
|
Current = clip;
|
|
Name = pose.ToString();
|
|
return true;
|
|
}
|
|
|
|
public bool SelectAction(string action)
|
|
{
|
|
if (!actions.TryGetValue(action, out var clip) || ReferenceEquals(Current, clip)) return false;
|
|
Current = clip; Name = action; return true;
|
|
}
|
|
}
|