A game framework written with osu! in mind.
at master 100 lines 3.4 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using osu.Framework.Allocation; 5using osu.Framework.Graphics.Sprites; 6using osu.Framework.Graphics.Textures; 7 8namespace osu.Framework.Graphics.Containers.Markdown 9{ 10 /// <summary> 11 /// Visualises an image. 12 /// </summary> 13 /// <code> 14 /// ![alt text](url) 15 /// </code> 16 public class MarkdownImage : CompositeDrawable 17 { 18 private readonly string url; 19 20 public MarkdownImage(string url) 21 { 22 this.url = url; 23 24 AutoSizeAxes = Axes.Both; 25 } 26 27 [BackgroundDependencyLoader] 28 private void load() 29 { 30 InternalChild = CreateContent(url); 31 } 32 33 /// <summary> 34 /// Creates the content of this <see cref="MarkdownImage"/>, including the <see cref="ImageContainer"/>. 35 /// </summary> 36 /// <param name="url">The image url.</param> 37 protected virtual Drawable CreateContent(string url) => new DelayedLoadWrapper(CreateImageContainer(url)); 38 39 /// <summary> 40 /// Creates an <see cref="ImageContainer"/> to display the image. 41 /// </summary> 42 /// <param name="url">The image URL.</param> 43 protected virtual ImageContainer CreateImageContainer(string url) 44 { 45 var converter = new ImageContainer(url); 46 converter.OnLoadComplete += d => d.FadeInFromZero(300, Easing.OutQuint); 47 return converter; 48 } 49 50 protected class ImageContainer : CompositeDrawable 51 { 52 private readonly string url; 53 private readonly Sprite image; 54 55 public ImageContainer(string url) 56 { 57 this.url = url; 58 59 AutoSizeAxes = Axes.Both; 60 61 InternalChild = image = CreateImageSprite(); 62 } 63 64 [BackgroundDependencyLoader] 65 private void load(TextureStore textures) 66 { 67 image.Texture = GetImageTexture(textures, url); 68 } 69 70 /// <summary> 71 /// Creates a <see cref="Sprite"/> to display the image. 72 /// </summary> 73 protected virtual Sprite CreateImageSprite() => new Sprite(); 74 75 /// <summary> 76 /// Retrieves a <see cref="Texture"/> for the image. 77 /// </summary> 78 /// <param name="textures">The texture store.</param> 79 /// <param name="url">The image URL.</param> 80 /// <returns>The image's <see cref="Texture"/>.</returns> 81 protected virtual Texture GetImageTexture(TextureStore textures, string url) 82 { 83 Texture texture = null; 84 if (!string.IsNullOrEmpty(url)) 85 texture = textures.Get(url); 86 87 // Use a default texture 88 texture ??= GetNotFoundTexture(textures); 89 return texture; 90 } 91 92 /// <summary> 93 /// Retrieves a default <see cref="Texture"/> to be displayed when the image can't be loaded. 94 /// </summary> 95 /// <param name="textures">The texture store.</param> 96 /// <returns>The <see cref="Texture"/>.</returns> 97 protected virtual Texture GetNotFoundTexture(TextureStore textures) => null; 98 } 99 } 100}