1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 11:42:54 +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 Sample tapSample = null!;
private bool visible;
[BackgroundDependencyLoader]
private void load(OsuConfigManager config, ScreenshotManager? screenshotManager, AudioManager audio)
{
@ -52,46 +54,41 @@ namespace osu.Game.Graphics.Cursor
[Resolved]
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 Bindable<Visibility> internalState = new Bindable<Visibility>();
protected override void LoadComplete()
{
base.LoadComplete();
internalState.ValueChanged += onInternalStateChanged;
if (inputManager != null)
{
mouseInputSource.BindTo(inputManager.IsMouseInputSource);
mouseInputSource.BindValueChanged(_ => updateInternalVisibility(), true);
lastInputWasMouse.BindTo(inputManager.LastInputWasMouseSource);
lastInputWasMouse.BindValueChanged(_ => updateState(), true);
}
if (game != null)
{
isIdle.BindTo(game.IsIdle);
isIdle.BindValueChanged(_ => updateInternalVisibility());
isIdle.BindValueChanged(_ => updateState());
}
}
private void updateInternalVisibility()
{
bool visible = mouseInputSource.Value;
internalState.Value = visible ? Visibility.Visible : Visibility.Hidden;
}
protected override void UpdateState(ValueChangedEvent<Visibility> state) => updateState();
private void onInternalStateChanged(ValueChangedEvent<Visibility> internalState)
private void updateState()
{
if (State.Value == Visibility.Visible)
base.UpdateState(internalState);
}
bool combinedVisibility = State.Value == Visibility.Visible && lastInputWasMouse.Value && !isIdle.Value;
protected override void UpdateState(ValueChangedEvent<Visibility> state)
{
if (internalState.Value == Visibility.Visible)
base.UpdateState(state);
if (visible == combinedVisibility)
return;
visible = combinedVisibility;
if (visible)
PopIn();
else
PopOut();
}
protected override void Update()

View File

@ -15,9 +15,9 @@ namespace osu.Game.Input
/// <summary>
/// Whether the last input applied to the game is sourced from mouse.
/// </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()
{
@ -29,11 +29,11 @@ namespace osu.Game.Input
{
case ButtonStateChangeEvent<MouseButton>:
case MousePositionChangeEvent:
isMouseInputSource.Value = true;
lastInputWasMouseSource.Value = true;
break;
default:
isMouseInputSource.Value = false;
lastInputWasMouseSource.Value = false;
break;
}