1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 18:23:04 +08:00

Refactor visibility states to read better

This commit is contained in:
Dean Herbert 2022-10-13 11:22:40 +09:00
parent 09cc89cfa0
commit e240f659c2
2 changed files with 22 additions and 25 deletions

View File

@ -35,6 +35,8 @@ namespace osu.Game.Graphics.Cursor
private Bindable<bool> cursorRotate = null!; private Bindable<bool> cursorRotate = null!;
private Sample tapSample = null!; private Sample tapSample = null!;
private bool visible;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuConfigManager config, ScreenshotManager? screenshotManager, AudioManager audio) private void load(OsuConfigManager config, ScreenshotManager? screenshotManager, AudioManager audio)
{ {
@ -52,46 +54,41 @@ namespace osu.Game.Graphics.Cursor
[Resolved] [Resolved]
private OsuGame? game { get; set; } private OsuGame? game { get; set; }
private readonly IBindable<bool> mouseInputSource = new BindableBool(); private readonly IBindable<bool> lastInputWasMouse = new BindableBool();
private readonly IBindable<bool> isIdle = new BindableBool(); private readonly IBindable<bool> isIdle = new BindableBool();
private readonly Bindable<Visibility> internalState = new Bindable<Visibility>();
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
internalState.ValueChanged += onInternalStateChanged;
if (inputManager != null) if (inputManager != null)
{ {
mouseInputSource.BindTo(inputManager.IsMouseInputSource); lastInputWasMouse.BindTo(inputManager.LastInputWasMouseSource);
mouseInputSource.BindValueChanged(_ => updateInternalVisibility(), true); lastInputWasMouse.BindValueChanged(_ => updateState(), true);
} }
if (game != null) if (game != null)
{ {
isIdle.BindTo(game.IsIdle); isIdle.BindTo(game.IsIdle);
isIdle.BindValueChanged(_ => updateInternalVisibility()); isIdle.BindValueChanged(_ => updateState());
} }
} }
private void updateInternalVisibility() protected override void UpdateState(ValueChangedEvent<Visibility> state) => updateState();
{
bool visible = mouseInputSource.Value;
internalState.Value = visible ? Visibility.Visible : Visibility.Hidden;
}
private void onInternalStateChanged(ValueChangedEvent<Visibility> internalState) private void updateState()
{ {
if (State.Value == Visibility.Visible) bool combinedVisibility = State.Value == Visibility.Visible && lastInputWasMouse.Value && !isIdle.Value;
base.UpdateState(internalState);
}
protected override void UpdateState(ValueChangedEvent<Visibility> state) if (visible == combinedVisibility)
{ return;
if (internalState.Value == Visibility.Visible)
base.UpdateState(state); visible = combinedVisibility;
if (visible)
PopIn();
else
PopOut();
} }
protected override void Update() protected override void Update()

View File

@ -15,9 +15,9 @@ namespace osu.Game.Input
/// <summary> /// <summary>
/// Whether the last input applied to the game is sourced from mouse. /// Whether the last input applied to the game is sourced from mouse.
/// </summary> /// </summary>
public IBindable<bool> IsMouseInputSource => isMouseInputSource; public IBindable<bool> LastInputWasMouseSource => lastInputWasMouseSource;
private readonly Bindable<bool> isMouseInputSource = new Bindable<bool>(); private readonly Bindable<bool> lastInputWasMouseSource = new Bindable<bool>();
internal OsuUserInputManager() internal OsuUserInputManager()
{ {
@ -29,11 +29,11 @@ namespace osu.Game.Input
{ {
case ButtonStateChangeEvent<MouseButton>: case ButtonStateChangeEvent<MouseButton>:
case MousePositionChangeEvent: case MousePositionChangeEvent:
isMouseInputSource.Value = true; lastInputWasMouseSource.Value = true;
break; break;
default: default:
isMouseInputSource.Value = false; lastInputWasMouseSource.Value = false;
break; break;
} }