// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Textures; namespace osu.Framework.Graphics.Containers.Markdown { /// /// Visualises an image. /// /// /// ![alt text](url) /// public class MarkdownImage : CompositeDrawable { private readonly string url; public MarkdownImage(string url) { this.url = url; AutoSizeAxes = Axes.Both; } [BackgroundDependencyLoader] private void load() { InternalChild = CreateContent(url); } /// /// Creates the content of this , including the . /// /// The image url. protected virtual Drawable CreateContent(string url) => new DelayedLoadWrapper(CreateImageContainer(url)); /// /// Creates an to display the image. /// /// The image URL. protected virtual ImageContainer CreateImageContainer(string url) { var converter = new ImageContainer(url); converter.OnLoadComplete += d => d.FadeInFromZero(300, Easing.OutQuint); return converter; } protected class ImageContainer : CompositeDrawable { private readonly string url; private readonly Sprite image; public ImageContainer(string url) { this.url = url; AutoSizeAxes = Axes.Both; InternalChild = image = CreateImageSprite(); } [BackgroundDependencyLoader] private void load(TextureStore textures) { image.Texture = GetImageTexture(textures, url); } /// /// Creates a to display the image. /// protected virtual Sprite CreateImageSprite() => new Sprite(); /// /// Retrieves a for the image. /// /// The texture store. /// The image URL. /// The image's . protected virtual Texture GetImageTexture(TextureStore textures, string url) { Texture texture = null; if (!string.IsNullOrEmpty(url)) texture = textures.Get(url); // Use a default texture texture ??= GetNotFoundTexture(textures); return texture; } /// /// Retrieves a default to be displayed when the image can't be loaded. /// /// The texture store. /// The . protected virtual Texture GetNotFoundTexture(TextureStore textures) => null; } } }