// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. using System; using FlaxEditor.GUI.Timeline.Undo; using FlaxEditor.Scripting; using FlaxEngine; using FlaxEngine.GUI; namespace FlaxEditor.GUI.Timeline.Tracks { /// /// The timeline media that represents a media event with an asset reference. /// /// public class SingleMediaAssetMedia : Media { /// /// The asset id. /// public Guid Asset; } /// /// The base class for timeline tracks that use single media with an asset reference. /// /// The type of the asset. /// The type of the media event. /// public abstract class SingleMediaAssetTrack : SingleMediaTrack where TAsset : Asset where TMedia : SingleMediaAssetMedia, new() { /// /// The asset reference picker control. /// protected readonly AssetPicker _picker; /// /// Gets or sets the asset. /// public TAsset Asset { get => FlaxEngine.Content.LoadAsync(TrackMedia.Asset); set { TMedia media = TrackMedia; if (media.Asset == value?.ID) return; media.Asset = value?.ID ?? Guid.Empty; _picker.SelectedAsset = value; OnAssetChanged(); Timeline?.MarkAsEdited(); } } /// protected SingleMediaAssetTrack(ref TrackCreateOptions options) : base(ref options) { var width = 50.0f; var height = 36.0f; _picker = new AssetPicker(new ScriptType(typeof(TAsset)), Vector2.Zero) { AnchorPreset = AnchorPresets.MiddleRight, Offsets = new Margin(-width - 2 + _muteCheckbox.Offsets.Left, width, height * -0.5f, height), Parent = this, }; _picker.SelectedItemChanged += OnPickerSelectedItemChanged; Height = 4 + _picker.Height; } private void OnPickerSelectedItemChanged() { if (Asset == (TAsset)_picker.SelectedAsset) return; using (new TrackUndoBlock(this)) Asset = (TAsset)_picker.SelectedAsset; } /// /// Called when selected asset gets changed. /// protected virtual void OnAssetChanged() { } } }