reafactor: simplify type checking

This commit is contained in:
tsrk
2023-02-15 22:06:35 +00:00
parent b0a2e69f95
commit e9dcc257b4
2 changed files with 7 additions and 4 deletions

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -43,14 +42,15 @@ namespace osu.Game.Screens.Play
public override void Add(KeyCounter key) public override void Add(KeyCounter key)
{ {
base.Add(key); base.Add(key);
if (key is not DefaultKeyCounter defaultKey) DefaultKeyCounter defaultKey = (DefaultKeyCounter)key;
throw new ArgumentException($"{key.GetType()} is not a supported {nameof(KeyCounter)}.", nameof(key));
defaultKey.FadeTime = key_fade_time; defaultKey.FadeTime = key_fade_time;
defaultKey.KeyDownTextColor = KeyDownTextColor; defaultKey.KeyDownTextColor = KeyDownTextColor;
defaultKey.KeyUpTextColor = KeyUpTextColor; defaultKey.KeyUpTextColor = KeyUpTextColor;
} }
protected override bool CheckType(KeyCounter key) => key is DefaultKeyCounter;
protected override void UpdateVisibility() => protected override void UpdateVisibility() =>
// Isolate changing visibility of the key counters from fading this component. // Isolate changing visibility of the key counters from fading this component.
KeyFlow.FadeTo(AlwaysVisible.Value || ConfigVisibility.Value ? 1 : 0, duration); KeyFlow.FadeTo(AlwaysVisible.Value || ConfigVisibility.Value ? 1 : 0, duration);

View File

@ -29,12 +29,15 @@ namespace osu.Game.Screens.Play
public override void Add(KeyCounter key) public override void Add(KeyCounter key)
{ {
ArgumentNullException.ThrowIfNull(key); if (!CheckType(key))
throw new ArgumentException($"{key.GetType()} is not a supported {nameof(KeyCounter)}.", nameof(key));
base.Add(key); base.Add(key);
key.IsCounting = IsCounting; key.IsCounting = IsCounting;
} }
protected virtual bool CheckType(KeyCounter key) => true;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuConfigManager config) private void load(OsuConfigManager config)
{ {