// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
using System;
using System.IO;
using System.Linq;
using FlaxEditor.Utilities;
using FlaxEngine;
namespace FlaxEditor.GUI.Timeline.Tracks
{
///
/// The timeline media that represents a post-process material media event.
///
///
public class PostProcessMaterialMedia : Media
{
private sealed class Proxy : ProxyBase
{
[EditorDisplay("General"), EditorOrder(10), Tooltip("The post process material to show.")]
public MaterialBase PostProcessMaterial
{
get => Track.Asset;
set => Track.Asset = value;
}
///
public Proxy(PostProcessMaterialTrack track, PostProcessMaterialMedia media)
: base(track, media)
{
}
}
///
/// Initializes a new instance of the class.
///
public PostProcessMaterialMedia()
{
CanSplit = true;
CanDelete = true;
}
///
public override void OnTimelineChanged(Track track)
{
base.OnTimelineChanged(track);
PropertiesEditObject = track != null ? new Proxy((PostProcessMaterialTrack)track, this) : null;
}
}
///
/// The timeline track that represents a post-process material playback.
///
///
public class PostProcessMaterialTrack : SingleMediaAssetTrack
{
///
/// Gets the archetype.
///
/// The archetype.
public static TrackArchetype GetArchetype()
{
return new TrackArchetype
{
TypeId = 2,
Name = "Post Process Material",
Create = options => new PostProcessMaterialTrack(ref options),
Load = LoadTrack,
Save = SaveTrack,
};
}
private static void LoadTrack(int version, Track track, BinaryReader stream)
{
var e = (PostProcessMaterialTrack)track;
Guid id = stream.ReadGuid();
e.Asset = FlaxEngine.Content.LoadAsync(id);
if (version <= 3)
{
// [Deprecated on 03.09.2021 expires on 03.09.2023]
var m = e.TrackMedia;
m.StartFrame = stream.ReadInt32();
m.DurationFrames = stream.ReadInt32();
}
else
{
var count = stream.ReadInt32();
while (e.Media.Count > count)
e.RemoveMedia(e.Media.Last());
while (e.Media.Count < count)
e.AddMedia(new PostProcessMaterialMedia());
for (int i = 0; i < count; i++)
{
var m = e.Media[i];
m.StartFrame = stream.ReadInt32();
m.DurationFrames = stream.ReadInt32();
}
}
}
private static void SaveTrack(Track track, BinaryWriter stream)
{
var e = (PostProcessMaterialTrack)track;
stream.WriteGuid(ref e.AssetID);
var count = e.Media.Count;
stream.Write(count);
for (int i = 0; i < count; i++)
{
var m = e.Media[i];
stream.Write(m.StartFrame);
stream.Write(m.DurationFrames);
}
}
///
/// Initializes a new instance of the class.
///
/// The options.
public PostProcessMaterialTrack(ref TrackCreateOptions options)
: base(ref options)
{
MinMediaCount = 1;
}
}
}