/// <inheritdoc/> public Boolean Equals(SpriteAnimationID other) { return (this.spriteAssetID == other.spriteAssetID && this.animationName == other.animationName && this.animationIndex == other.animationIndex); }
/// <summary> /// Converts the string representation of a sprite animation identifier to an instance of the SpriteAnimationID structure. /// </summary> /// <param name="manifests">The content manifest registry that contains the currently-loaded content manifests.</param> /// <param name="s">A string containing the sprite animation identifier to convert.</param> /// <param name="value">An instance of the SpriteAnimationID structure that is equivalent to the specified string.</param> /// <returns><see langword="true"/> if the string was successfully parsed; otherwise, <see langword="false"/>.</returns> public static Boolean TryParse(ContentManifestRegistry manifests, String s, out SpriteAnimationID value) { Contract.Require(manifests, nameof(manifests)); Contract.Require(s, nameof(s)); return(TryParseInternal(manifests, s, out value)); }
/// <summary> /// Loads the specified sprite animation. /// </summary> private static SpriteAnimation LoadInternal(this ContentManager contentManager, SpriteAnimationID id, ScreenDensityBucket density, Boolean cache) { var sprite = contentManager.Load <Sprite>(SpriteAnimationID.GetSpriteAssetIDRef(ref id), density, cache); var name = SpriteAnimationID.GetAnimationNameRef(ref id); if (!String.IsNullOrEmpty(name)) { var value = sprite[name]; if (value == null) { throw new ArgumentException(UltravioletStrings.InvalidSpriteAnimationReference.Format(id)); } return(value); } var index = SpriteAnimationID.GetAnimationIndexRef(ref id); if (index < 0 || index >= sprite.AnimationCount) { throw new ArgumentException(UltravioletStrings.InvalidSpriteAnimationReference.Format(id)); } return(sprite[index]); }
/// <summary> /// Loads the specified sprite animation. /// </summary> /// <remarks>Content managers maintain a cache of references to all loaded assets, so calling <see cref="ContentManager.Load"/> multiple /// times on a content manager with the same parameter will return the same object rather than reloading the source file.</remarks> /// <param name="contentManager">The <see cref="ContentManager"/> with which to load the animation's associated sprite asset.</param> /// <param name="id">The identifier that represents the sprite animation to load.</param> /// <param name="density">The screen density for which to load the sprite.</param> /// <param name="cache">A value indicating whether to add the sprite asset to the manager's cache.</param> /// <returns>The sprite animation that was loaded.</returns> public static SpriteAnimation Load(this ContentManager contentManager, SpriteAnimationID id, ScreenDensityBucket density, Boolean cache = true) { Contract.Require(contentManager, nameof(contentManager)); Contract.Ensure <ArgumentException>(id.IsValid, nameof(id)); Contract.EnsureNotDisposed(contentManager, contentManager.Disposed); return(LoadInternal(contentManager, id, density, cache)); }
/// <summary> /// Writes a sprite animation identifier to the stream. /// </summary> /// <param name="writer">The <see cref="BinaryWriter"/> with which to write the sprite animation identifier.</param> /// <param name="id">The <see cref="SpriteAnimationID"/> to write to the stream.</param> public static void Write(this BinaryWriter writer, SpriteAnimationID id) { writer.Write(id.IsValid); if (id.IsValid) { writer.Write(SpriteAnimationID.GetSpriteAssetIDRef(ref id)); writer.Write(SpriteAnimationID.GetAnimationNameRef(ref id)); writer.Write(SpriteAnimationID.GetAnimationIndexRef(ref id)); } }
/// <summary> /// Loads the specified sprite animation. /// </summary> /// <remarks>Content managers maintain a cache of references to all loaded assets, so calling <see cref="ContentManager.Load"/> multiple /// times on a content manager with the same parameter will return the same object rather than reloading the source file.</remarks> /// <param name="contentManager">The <see cref="ContentManager"/> with which to load the animation's associated sprite asset.</param> /// <param name="id">The identifier that represents the sprite animation to load.</param> /// <param name="cache">A value indicating whether to add the sprite asset to the manager's cache.</param> /// <returns>The sprite animation that was loaded.</returns> public static SpriteAnimation Load(this ContentManager contentManager, SpriteAnimationID id, Boolean cache = true) { Contract.Require(contentManager, nameof(contentManager)); Contract.Ensure <ArgumentException>(id.IsValid, nameof(id)); Contract.EnsureNotDisposed(contentManager, contentManager.Disposed); var primaryDisplay = contentManager.Ultraviolet.GetPlatform().Displays.PrimaryDisplay; var primaryDisplayDensity = primaryDisplay?.DensityBucket ?? ScreenDensityBucket.Desktop; return(LoadInternal(contentManager, id, primaryDisplayDensity, cache)); }
/// <summary> /// Converts the string representation of a <see cref="SpriteAnimationID"/> to an object instance. /// A return value indicates whether the conversion succeeded. /// </summary> /// <param name="s">The string to convert.</param> /// <param name="style">A set of <see cref="NumberStyles"/> values indicating which elements are present in <paramref name="s"/>.</param> /// <param name="provider">A format provider that provides culture-specific formatting information.</param> /// <param name="v">The converted value.</param> /// <returns><see langword="true"/> if the conversion succeeded; otherwise, <see langword="false"/>.</returns> public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out SpriteAnimationID v) { Contract.Require(s, nameof(s)); var uv = UltravioletContext.DemandCurrent(); return(TryParseInternal(uv.GetContent().Manifests, s, out v)); }
/// <summary> /// Converts the string representation of a <see cref="SpriteAnimationID"/> to an object instance. /// A return value indicates whether the conversion succeeded. /// </summary> /// <param name="s">The string to convert.</param> /// <param name="v">The converted value.</param> /// <returns><see langword="true"/> if the conversion succeeded; otherwise, <see langword="false"/>.</returns> public static Boolean TryParse(String s, out SpriteAnimationID v) { return(TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out v)); }
/// <summary> /// Converts the string representation of an asset identifier to an instance of the SpriteAnimationID structure. /// </summary> /// <param name="manifests">The content manifest registry that contains the currently-loaded content manifests.</param> /// <param name="s">A string containing the asset identifier to convert.</param> /// <param name="value">An instance of the SpriteAnimationID structure that is equivalent to the specified string.</param> /// <returns><see langword="true"/> if the string was successfully parsed; otherwise, <see langword="false"/>.</returns> private static Boolean TryParseInternal(ContentManifestRegistry manifests, String s, out SpriteAnimationID value) { value = default(SpriteAnimationID); var delimiterIndex = s.LastIndexOf(':'); if (delimiterIndex < 0) { return(false); } var assetID = AssetID.Invalid; if (!AssetID.TryParse(manifests, s.Substring(0, delimiterIndex), out assetID)) { return(false); } var animation = s.Substring(delimiterIndex + 1); if (String.IsNullOrEmpty(animation)) { return(false); } var animationIndex = 0; var animationIndexIsValid = Int32.TryParse(animation, out animationIndex); value = animationIndexIsValid ? new SpriteAnimationID(assetID, animationIndex) : new SpriteAnimationID(assetID, animation); return(true); }