1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 20:32:55 +08:00

Let selection container handle manual selection changes

This commit is contained in:
Derrick Timmermans 2021-07-06 13:24:18 +02:00
parent c5a0672277
commit 07d54d261a
No known key found for this signature in database
GPG Key ID: 8681B60806EF4A17
3 changed files with 31 additions and 24 deletions

View File

@ -1,6 +1,8 @@
// 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 osu.Framework; using osu.Framework;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -60,5 +62,34 @@ namespace osu.Game.Graphics.Containers
} }
public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null; public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null;
private readonly Dictionary<T, Action<SelectionState>> handlerMap = new Dictionary<T, Action<SelectionState>>();
public override void Add(T drawable)
{
// This event is used to update selection state when modified within the drawable itself.
// It is added to a dictionary so that we can unsubscribe if the drawable is removed from this container
drawable.StateChanged += handlerMap[drawable] = state => selectionChanged(drawable, state);
base.Add(drawable);
}
public override bool Remove(T drawable)
{
if (!base.Remove(drawable))
return false;
drawable.StateChanged -= handlerMap[drawable];
handlerMap.Remove(drawable);
return true;
}
private void selectionChanged(T drawable, SelectionState state)
{
if (state == SelectionState.NotSelected)
Deselect();
else
Select(drawable);
}
} }
} }

View File

@ -13,7 +13,6 @@ using osu.Framework.Input.Events;
using osu.Framework.Threading; using osu.Framework.Threading;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings; using osu.Game.Input.Bindings;
using osu.Game.Overlays.Volume; using osu.Game.Overlays.Volume;
using osuTK; using osuTK;
@ -92,10 +91,7 @@ namespace osu.Game.Overlays
base.LoadComplete(); base.LoadComplete();
foreach (var volumeMeter in volumeMeters) foreach (var volumeMeter in volumeMeters)
{
volumeMeter.Bindable.ValueChanged += _ => Show(); volumeMeter.Bindable.ValueChanged += _ => Show();
volumeMeter.StateChanged += selected => volumeMeterSelectionChanged(volumeMeter, selected);
}
muteButton.Current.ValueChanged += _ => Show(); muteButton.Current.ValueChanged += _ => Show();
} }
@ -141,14 +137,6 @@ namespace osu.Game.Overlays
return false; return false;
} }
private void volumeMeterSelectionChanged(VolumeMeter meter, SelectionState state)
{
if (state == SelectionState.NotSelected)
volumeMeters.Deselect();
else
volumeMeters.Select(meter);
}
private ScheduledDelegate popOutDelegate; private ScheduledDelegate popOutDelegate;
public override void Show() public override void Show()

View File

@ -3,7 +3,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Humanizer; using Humanizer;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
@ -184,8 +183,6 @@ namespace osu.Game.Screens.Play
} }
}; };
button.StateChanged += selected => buttonSelectionChanged(button, selected);
InternalButtons.Add(button); InternalButtons.Add(button);
} }
@ -216,15 +213,6 @@ namespace osu.Game.Screens.Play
public void OnReleased(GlobalAction action) public void OnReleased(GlobalAction action)
{ {
} }
private void buttonSelectionChanged(DialogButton button, SelectionState state)
{
if (state == SelectionState.NotSelected)
InternalButtons.Deselect();
else
InternalButtons.Select(button);
}
private void updateRetryCount() private void updateRetryCount()
{ {
// "You've retried 1,065 times in this session" // "You've retried 1,065 times in this session"