A game framework written with osu! in mind.
at master 79 lines 3.0 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 System; 5using JetBrains.Annotations; 6 7namespace osu.Framework.Graphics.Sprites 8{ 9 /// <summary> 10 /// Represents a specific usage of an icon. 11 /// </summary> 12 public readonly struct IconUsage : IEquatable<IconUsage> 13 { 14 /// <summary> 15 /// The font family name. 16 /// </summary> 17 [CanBeNull] 18 public string Family { get; } 19 20 /// <summary> 21 /// The font weight. 22 /// </summary> 23 [CanBeNull] 24 public string Weight { get; } 25 26 /// <summary> 27 /// The font's full name to be used for lookups. This is an aggregate of all other properties of <see cref="IconUsage"/>. 28 /// <remarks> 29 /// The format is of the form: <br /> 30 /// {Family} <br /> 31 /// {Family}-Italic <br /> 32 /// {Family}-{Weight}Italic 33 /// </remarks> 34 /// </summary> 35 [NotNull] 36 public string FontName { get; } 37 38 /// <summary> 39 /// The icon character. 40 /// </summary> 41 public char Icon { get; } 42 43 /// <summary> 44 /// Creates an instance of <see cref="IconUsage"/> using the specified font <paramref name="family"/>, font <paramref name="weight"/> and a value indicating whether the used font is italic or not. 45 /// </summary> 46 /// /// <param name="icon">The icon.</param> 47 /// <param name="family">The font family name.</param> 48 /// <param name="weight">The font weight.</param> 49 public IconUsage(char icon, [CanBeNull] string family = null, [CanBeNull] string weight = null) 50 { 51 Icon = icon; 52 Family = family; 53 Weight = weight; 54 55 FontName = Family + "-"; 56 if (!string.IsNullOrEmpty(weight)) 57 FontName += weight; 58 59 FontName = FontName.TrimEnd('-'); 60 } 61 62 /// <summary> 63 /// Creates a new <see cref="IconUsage"/> by applying adjustments to this <see cref="IconUsage"/>. 64 /// </summary> 65 /// <param name="family">The font family. If null, the value is copied from this <see cref="IconUsage"/>.</param> 66 /// <param name="weight">The font weight. If null, the value is copied from this <see cref="IconUsage"/>.</param> 67 /// <returns>The resulting <see cref="IconUsage"/>.</returns> 68 public IconUsage With([CanBeNull] string family = null, [CanBeNull] string weight = null) 69 => new IconUsage(Icon, family ?? Family, weight ?? Weight); 70 71 public override string ToString() => $"Icon={Icon} Font={FontName}"; 72 73 public bool Equals(IconUsage other) => Icon == other.Icon && Family == other.Family && Weight == other.Weight; 74 75 public override bool Equals(object obj) => obj is IconUsage other && Equals(other); 76 77 public override int GetHashCode() => HashCode.Combine(Family, Icon, Weight); 78 } 79}