public static Drawable GetAnimation(this ISkin source, string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool animatable, bool looping, bool applyConfigFrameRate = false, string animationSeparator = "-", bool startAtCurrentTime = true, double?frameLength = null) { Texture texture; if (animatable) { var textures = getTextures().ToArray(); if (textures.Length > 0) { var animation = new SkinnableTextureAnimation(startAtCurrentTime) { DefaultFrameLength = frameLength ?? getFrameLength(source, applyConfigFrameRate, textures), Loop = looping, }; foreach (var t in textures) { animation.AddFrame(t); } return(animation); } } // if an animation was not allowed or not found, fall back to a sprite retrieval. if ((texture = source.GetTexture(componentName, wrapModeS, wrapModeT)) != null) { return new Sprite { Texture = texture } } ; return(null); IEnumerable <Texture> getTextures() { for (int i = 0; true; i++) { if ((texture = source.GetTexture($"{componentName}{animationSeparator}{i}", wrapModeS, wrapModeT)) == null) { break; } yield return(texture); } } }
public static Drawable?GetAnimation(this ISkin?source, string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool animatable, bool looping, bool applyConfigFrameRate = false, string animationSeparator = "-", bool startAtCurrentTime = true, double?frameLength = null) { if (source == null) { return(null); } var textures = GetTextures(source, componentName, wrapModeS, wrapModeT, animatable, animationSeparator, out var retrievalSource); switch (textures.Length) { case 0: return(null); case 1: return(new Sprite { Texture = textures[0] }); default: Debug.Assert(retrievalSource != null); var animation = new SkinnableTextureAnimation(startAtCurrentTime) { DefaultFrameLength = frameLength ?? getFrameLength(retrievalSource, applyConfigFrameRate, textures), Loop = looping, }; foreach (var t in textures) { animation.AddFrame(t); } return(animation); } }
public static Drawable GetAnimation(this ISkin source, string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool animatable, bool looping, bool applyConfigFrameRate = false, string animationSeparator = "-", bool startAtCurrentTime = true, double?frameLength = null) { Texture texture; // find the first source which provides either the animated or non-animated version. ISkin skin = (source as ISkinSource)?.FindProvider(s => { if (animatable && s.GetTexture(getFrameName(0)) != null) { return(true); } return(s.GetTexture(componentName, wrapModeS, wrapModeT) != null); }) ?? source; if (skin == null) { return(null); } if (animatable) { var textures = getTextures().ToArray(); if (textures.Length > 0) { var animation = new SkinnableTextureAnimation(startAtCurrentTime) { DefaultFrameLength = frameLength ?? getFrameLength(skin, applyConfigFrameRate, textures), Loop = looping, }; foreach (var t in textures) { animation.AddFrame(t); } return(animation); } } // if an animation was not allowed or not found, fall back to a sprite retrieval. if ((texture = skin.GetTexture(componentName, wrapModeS, wrapModeT)) != null) { return new Sprite { Texture = texture } } ; return(null); IEnumerable <Texture> getTextures() { for (int i = 0; true; i++) { if ((texture = skin.GetTexture(getFrameName(i), wrapModeS, wrapModeT)) == null) { break; } yield return(texture); } } string getFrameName(int frameIndex) => $"{componentName}{animationSeparator}{frameIndex}"; }