#pragma warning disable 672
using System;
using System.IO;
using TriLibCore.Interfaces;
using TriLibCore.Mappers;
namespace TriLibCore.Samples
{
///
/// A custom implementation that uses a user-supplied callback to retrieve texture data.
/// This mapper allows you to control how texture streams are opened, enabling scenarios such as loading from memory
/// or over the network.
///
public class SimpleTextureMapper : TextureMapper
{
///
/// The callback responsible for returning a to read texture data for a given .
///
private Func _streamReceivingCallback;
///
/// Configures the callback used for texture loading.
///
/// A required callback that returns a containing texture data.
/// Thrown if is null.
public void Setup(Func streamReceivingCallback)
{
if (streamReceivingCallback == null)
{
throw new Exception("Callback parameter is missing.");
}
_streamReceivingCallback = streamReceivingCallback;
}
///
/// Overrides the default texture mapping logic to use the user-supplied callback.
///
///
/// TriLib calls this method for each texture that needs to be loaded, allowing you
/// to retrieve texture data from a custom source.
///
/// The context containing information about the texture being loaded.
public override void Map(TextureLoadingContext textureLoadingContext)
{
// Use the provided callback to retrieve a Stream containing the texture data
var stream = _streamReceivingCallback(textureLoadingContext.Texture);
textureLoadingContext.Stream = stream;
}
}
}