Skip to content

Commit

Permalink
[Editor API] - Exposed texture overrides for animations to the conten…
Browse files Browse the repository at this point in the history
…t loader public interface.
  • Loading branch information
Tape-Worm committed May 11, 2022
1 parent c13a82b commit 292a15b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ GorgonTextureCache<GorgonTexture2D> TextureCache
/// Function to load an animation from the editor file system.
/// </summary>
/// <param name="path">The path to the animation content.</param>
/// <param name="textureOverrides">[Optional] The textures used to override the textures for a texture track in the animation.</param>
/// <returns>A new <see cref="IGorgonAnimation"/> containing the animation data from the file system.</returns>
/// <remarks>
/// <para>
Expand All @@ -185,6 +186,11 @@ GorgonTextureCache<GorgonTexture2D> TextureCache
/// Doing this will allow a user to create a custom image codec plug in and use that to read animation data.
/// </para>
/// <para>
/// When the <paramref name="textureOverrides"/> contains a list of textures, the loader will override any matching textures in any texture tracks within the animation. This allows user defined pre
/// loading of texture data for an animation. The textures in the <paramref name="textureOverrides"/> list will be matched by name to the key <see cref="GorgonKeyTexture2D.TextureName"/>. If the
/// texture is matched with one from the override list, then it will be used for the key. Otherwise, the codec will load the appropriate texture via other means.
/// </para>
/// <para>
/// <h2>Technical info</h2>
/// <para>
/// Plug ins must generate the following metadata for the files in the editor file system.
Expand All @@ -195,7 +201,7 @@ GorgonTextureCache<GorgonTexture2D> TextureCache
/// </para>
/// </para>
/// </remarks>
Task<IGorgonAnimation> LoadAnimationAsync(string path);
Task<IGorgonAnimation> LoadAnimationAsync(string path, IEnumerable<GorgonTexture2DView> textureOverrides = null);

/// <summary>
/// Function to load an image as a texture from the editor file system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public IReadOnlyDictionary<string, string> GetFileAttributes(string path)
/// Function to load an animation from the editor file system.
/// </summary>
/// <param name="path">The path to the animation content.</param>
/// <param name="textureOverrides">[Optional] The textures used to override the textures for a texture track in the animation.</param>
/// <returns>A new <see cref="IGorgonAnimation"/> containing the animation data from the file system.</returns>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="path"/> parameter is <b>null</b>.</exception>
/// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="path"/> parameter is empty.</exception>
Expand All @@ -321,6 +322,11 @@ public IReadOnlyDictionary<string, string> GetFileAttributes(string path)
/// Doing this will allow a user to create a custom image codec plug in and use that to read animation data.
/// </para>
/// <para>
/// When the <paramref name="textureOverrides"/> contains a list of textures, the loader will override any matching textures in any texture tracks within the animation. This allows user defined pre
/// loading of texture data for an animation. The textures in the <paramref name="textureOverrides"/> list will be matched by name to the key <see cref="GorgonKeyTexture2D.TextureName"/>. If the
/// texture is matched with one from the override list, then it will be used for the key. Otherwise, the codec will load the appropriate texture via other means.
/// </para>
/// <para>
/// <h2>Technical info</h2>
/// <para>
/// Plug ins must generate the following metadata for the files in the editor file system.
Expand All @@ -331,7 +337,7 @@ public IReadOnlyDictionary<string, string> GetFileAttributes(string path)
/// </para>
/// </para>
/// </remarks>
public async Task<IGorgonAnimation> LoadAnimationAsync(string path)
public async Task<IGorgonAnimation> LoadAnimationAsync(string path, IEnumerable<GorgonTexture2DView> textureOverrides = null)
{
if (path is null)
{
Expand Down Expand Up @@ -371,20 +377,36 @@ public async Task<IGorgonAnimation> LoadAnimationAsync(string path)

_graphics.Log.Print($"Loading animation '{path}'...", LoggingLevel.Verbose);

IEnumerable<GorgonTexture2DView> textures = null;

if ((metadata.DependsOn.TryGetValue(CommonEditorContentTypes.ImageType, out List<string> paths))
&& (paths is not null)
&& (paths.Count > 0))
{
IEnumerable<Task<GorgonTexture2D>> dependencyTasks = paths.Select(item => TextureCache.GetTextureAsync(item, ReadTextureAsync));
IEnumerable<Task<GorgonTexture2D>> dependencyTasks;

if ((textureOverrides is null) || (!textureOverrides.Any()))
{
dependencyTasks = paths.Select(item => TextureCache.GetTextureAsync(item, ReadTextureAsync));
}
else
{
dependencyTasks = paths.Except(textureOverrides.Select(item => item.Texture.Name))
.Select(item => TextureCache.GetTextureAsync(item, ReadTextureAsync));
}

await Task.WhenAll(dependencyTasks);

textures = dependencyTasks.Select(item => item.Result.GetShaderResourceView());
if (textureOverrides is null)
{
textureOverrides = dependencyTasks.Select(item => item.Result.GetShaderResourceView());
}
else
{
textureOverrides = textureOverrides.Concat(dependencyTasks.Select(item => item.Result.GetShaderResourceView()));
}
}

using Stream stream = animationFile.OpenStream();
return animationCodec.FromStream(stream, textureOverrides: textures);
return animationCodec.FromStream(stream, textureOverrides: textureOverrides);
}

/// <summary>
Expand Down

0 comments on commit 292a15b

Please sign in to comment.