Replace copy-constructor/method with extension method

This commit is contained in:
smoogipoo
2019-02-20 19:32:30 +09:00
parent dbfa95b9e7
commit 4db5531e4b
40 changed files with 66 additions and 68 deletions

View File

@ -11,18 +11,10 @@ namespace osu.Game.Graphics
public static FontUsage Default => GetFont();
public static FontUsage GetFont(FontUsage usage, Typeface? typeface = null, float? size = null, FontWeight? weight = null, bool? italics = null, bool? fixedWidth = null)
{
string familyString = typeface != null ? getFamilyString(typeface.Value) : usage.Family;
string weightString = weight != null ? getWeightString(familyString, weight.Value) : usage.Weight;
return new FontUsage(usage, familyString, size, weightString, italics, fixedWidth);
}
public static FontUsage GetFont(Typeface typeface = Typeface.Exo, float size = DEFAULT_FONT_SIZE, FontWeight weight = FontWeight.Medium, bool italics = false, bool fixedWidth = false)
=> new FontUsage(getFamilyString(typeface), size, getWeightString(typeface, weight), italics, fixedWidth);
=> new FontUsage(GetFamilyString(typeface), size, GetWeightString(typeface, weight), italics, fixedWidth);
private static string getFamilyString(Typeface typeface)
public static string GetFamilyString(Typeface typeface)
{
switch (typeface)
{
@ -37,10 +29,10 @@ namespace osu.Game.Graphics
return null;
}
private static string getWeightString(Typeface typeface, FontWeight weight)
=> getWeightString(getFamilyString(typeface), weight);
public static string GetWeightString(Typeface typeface, FontWeight weight)
=> GetWeightString(GetFamilyString(typeface), weight);
private static string getWeightString(string family, FontWeight weight)
public static string GetWeightString(string family, FontWeight weight)
{
string weightString = weight.ToString();
@ -52,6 +44,17 @@ namespace osu.Game.Graphics
}
}
public static class OsuFontExtensions
{
public static FontUsage With(this FontUsage usage, Typeface? typeface = null, float? size = null, FontWeight? weight = null, bool? italics = null, bool? fixedWidth = null)
{
string familyString = typeface != null ? OsuFont.GetFamilyString(typeface.Value) : usage.Family;
string weightString = weight != null ? OsuFont.GetWeightString(familyString, weight.Value) : usage.Weight;
return usage.With(familyString, size, weightString, italics, fixedWidth);
}
}
public enum Typeface
{
Exo,

View File

@ -82,7 +82,7 @@ namespace osu.Game.Graphics.UserInterface
public BreadcrumbTabItem(T value) : base(value)
{
Text.Font = OsuFont.GetFont(Text.Font, size: 18);
Text.Font = Text.Font.With(size: 18);
Text.Margin = new MarginPadding { Vertical = 8 };
Padding = new MarginPadding { Right = padding + item_chevron_size };
Add(Chevron = new SpriteIcon

View File

@ -199,7 +199,7 @@ namespace osu.Game.Graphics.UserInterface
public float TextSize
{
get => spriteText.Font.Size;
set => spriteText.Font = OsuFont.GetFont(spriteText.Font, size: value);
set => spriteText.Font = spriteText.Font.With(size: value);
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => backgroundContainer.ReceivePositionalInputAt(screenSpacePos);

View File

@ -173,7 +173,7 @@ namespace osu.Game.Graphics.UserInterface
new HoverClickSounds()
};
Active.BindValueChanged(val => Text.Font = OsuFont.GetFont(Text.Font, weight: val ? FontWeight.Bold : FontWeight.Medium), true);
Active.BindValueChanged(val => Text.Font = Text.Font.With(weight: val ? FontWeight.Bold : FontWeight.Medium), true);
}
protected override void OnActivated() => fadeActive();

View File

@ -59,7 +59,7 @@ namespace osu.Game.Graphics.UserInterface
new HoverClickSounds()
};
Active.BindValueChanged(val => Text.Font = OsuFont.GetFont(Text.Font, weight: val ? FontWeight.Bold : FontWeight.Medium), true);
Active.BindValueChanged(val => Text.Font = Text.Font.With(weight: val ? FontWeight.Bold : FontWeight.Medium), true);
}
[BackgroundDependencyLoader]

View File

@ -22,7 +22,7 @@ namespace osu.Game.Graphics.UserInterface
public PercentageCounter()
{
DisplayedCountSpriteText.Font = OsuFont.GetFont(DisplayedCountSpriteText.Font, fixedWidth: true);
DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(fixedWidth: true);
Current.Value = DisplayedCount = 1.0f;
}

View File

@ -65,7 +65,7 @@ namespace osu.Game.Graphics.UserInterface
public float TextSize
{
get => DisplayedCountSpriteText.Font.Size;
set => DisplayedCountSpriteText.Font = OsuFont.GetFont(DisplayedCountSpriteText.Font, size: value);
set => DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(size: value);
}
public Color4 AccentColour

View File

@ -28,7 +28,7 @@ namespace osu.Game.Graphics.UserInterface
/// <param name="leading">How many leading zeroes the counter will have.</param>
public ScoreCounter(uint leading = 0)
{
DisplayedCountSpriteText.Font = OsuFont.GetFont(DisplayedCountSpriteText.Font, fixedWidth: true);
DisplayedCountSpriteText.Font = DisplayedCountSpriteText.Font.With(fixedWidth: true);
LeadingZeroes = leading;
}