// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
namespace Microsoft.Xna.Framework.Content.Pipeline.Graphics
{
///
/// Provides methods and properties for maintaining a collection of named texture references.
///
/// In addition to texture references, opaque data values are stored in the OpaqueData property of the base class.
public class MaterialContent : ContentItem
{
readonly TextureReferenceDictionary _textures;
///
/// Gets the texture collection of the material.
///
/// Collection of textures used by the material.
public TextureReferenceDictionary Textures { get { return _textures; } }
///
/// Initializes a new instance of MaterialContent.
///
public MaterialContent()
{
_textures = new TextureReferenceDictionary();
}
///
/// Gets a reference type from the OpaqueDataDictionary collection.
///
/// Type of the related opaque data.
/// Key of the property being retrieved.
/// The related opaque data.
protected T GetReferenceTypeProperty(string key) where T : class
{
object value;
if (OpaqueData.TryGetValue(key, out value))
return (T)value;
return default(T);
}
///
/// Gets a value from the Textures collection.
///
/// Key of the texture being retrieved.
/// Reference to a texture from the collection.
protected ExternalReference GetTexture(string key)
{
ExternalReference texture;
_textures.TryGetValue(key, out texture);
return texture;
}
///
/// Gets a value type from the OpaqueDataDictionary collection.
///
/// Type of the value being retrieved.
/// Key of the value type being retrieved.
/// Index of the value type beng retrieved.
protected Nullable GetValueTypeProperty(string key) where T : struct
{
object value;
if (OpaqueData.TryGetValue(key, out value))
return (T)value;
return null;
}
///
/// Sets a value in the contained OpaqueDataDictionary object.
/// If null is passed, the value is removed.
///
/// Type of the element being set.
/// Name of the key being modified.
/// Value being set.
protected void SetProperty(string key, T value)
{
if (value != null)
OpaqueData[key] = value;
else
OpaqueData.Remove(key);
}
///
/// Sets a value in the contained TextureReferenceDictionary object.
/// If null is passed, the value is removed.
///
/// Name of the key being modified.
/// Value being set.
/// The key value differs depending on the type of attached dictionary.
/// If attached to a BasicMaterialContent dictionary (which becomes a BasicEffect object at run time), the value for the Texture key is used as the texture for the BasicEffect runtime object. Other keys are ignored.
/// If attached to a EffectMaterialContent dictionary, key names are the texture names used by the effect. These names are dependent upon the author of the effect object.
protected void SetTexture(string key, ExternalReference value)
{
if (value != null)
_textures[key] = value;
else
_textures.Remove(key);
}
///
/// Helper method to make a copy of a material.
///
/// A clone of the material.
public MaterialContent Clone()
{
// Construct it via reflection.
var clone = (MaterialContent)Activator.CreateInstance(GetType());
// Give it the same identity as the original material.
clone.Name = Name;
clone.Identity = Identity;
// Just copy the opaque data and textures which should
// result in the same properties being set if the material
// is implemented correctly.
foreach (var pair in Textures)
clone.Textures.Add(pair.Key, pair.Value);
foreach (var pair in OpaqueData)
clone.OpaqueData.Add(pair.Key, pair.Value);
return clone;
}
}
}