Use IStateful<SelectionState> instead of ISelected

This commit is contained in:
Derrick Timmermans 2021-07-06 12:07:25 +02:00
parent 32ef2405c4
commit c5a0672277
No known key found for this signature in database
GPG Key ID: 8681B60806EF4A17
6 changed files with 56 additions and 43 deletions

View File

@ -1,6 +1,7 @@
// 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 osu.Framework;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
@ -11,7 +12,7 @@ namespace osu.Game.Graphics.Containers
/// A FillFlowContainer that provides functionality to cycle selection between children /// A FillFlowContainer that provides functionality to cycle selection between children
/// The selection wraps around when overflowing past the first or last child. /// The selection wraps around when overflowing past the first or last child.
/// </summary> /// </summary>
public class SelectionCycleFillFlowContainer<T> : FillFlowContainer<T> where T : Drawable, ISelectable public class SelectionCycleFillFlowContainer<T> : FillFlowContainer<T> where T : Drawable, IStateful<SelectionState>
{ {
private int? selectedIndex; private int? selectedIndex;
@ -22,13 +23,13 @@ namespace osu.Game.Graphics.Containers
// Deselect the previously-selected button // Deselect the previously-selected button
if (selectedIndex.HasValue) if (selectedIndex.HasValue)
this[selectedIndex.Value].Selected = false; this[selectedIndex.Value].State = SelectionState.NotSelected;
selectedIndex = value; selectedIndex = value;
// Select the newly-selected button // Select the newly-selected button
if (selectedIndex.HasValue) if (selectedIndex.HasValue)
this[selectedIndex.Value].Selected = true; this[selectedIndex.Value].State = SelectionState.Selected;
} }
public void SelectNext() public void SelectNext()

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 osu.Framework;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -19,7 +21,7 @@ using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
public class DialogButton : OsuClickableContainer, ISelectable public class DialogButton : OsuClickableContainer, IStateful<SelectionState>
{ {
private const float idle_width = 0.8f; private const float idle_width = 0.8f;
private const float hover_width = 0.9f; private const float hover_width = 0.9f;
@ -27,12 +29,21 @@ namespace osu.Game.Graphics.UserInterface
private const float hover_duration = 500; private const float hover_duration = 500;
private const float click_duration = 200; private const float click_duration = 200;
public readonly BindableBool SelectedBindable = new BindableBool(); public event Action<SelectionState> StateChanged;
public bool Selected private SelectionState state;
public SelectionState State
{ {
get => SelectedBindable.Value; get => state;
set => SelectedBindable.Value = value; set
{
if (state == value)
return;
state = value;
StateChanged?.Invoke(value);
}
} }
private readonly Container backgroundContainer; private readonly Container backgroundContainer;
@ -159,7 +170,7 @@ namespace osu.Game.Graphics.UserInterface
updateGlow(); updateGlow();
SelectedBindable.ValueChanged += selectionChanged; StateChanged += selectionChanged;
} }
private Color4 buttonColour; private Color4 buttonColour;
@ -227,7 +238,7 @@ namespace osu.Game.Graphics.UserInterface
.OnComplete(_ => .OnComplete(_ =>
{ {
clickAnimating = false; clickAnimating = false;
SelectedBindable.TriggerChange(); StateChanged?.Invoke(State);
}); });
return base.OnClick(e); return base.OnClick(e);
@ -241,7 +252,7 @@ namespace osu.Game.Graphics.UserInterface
protected override void OnMouseUp(MouseUpEvent e) protected override void OnMouseUp(MouseUpEvent e)
{ {
if (Selected) if (State == SelectionState.Selected)
colourContainer.ResizeWidthTo(hover_width, click_duration, Easing.In); colourContainer.ResizeWidthTo(hover_width, click_duration, Easing.In);
base.OnMouseUp(e); base.OnMouseUp(e);
} }
@ -249,7 +260,7 @@ namespace osu.Game.Graphics.UserInterface
protected override bool OnHover(HoverEvent e) protected override bool OnHover(HoverEvent e)
{ {
base.OnHover(e); base.OnHover(e);
Selected = true; State = SelectionState.Selected;
return true; return true;
} }
@ -257,15 +268,15 @@ namespace osu.Game.Graphics.UserInterface
protected override void OnHoverLost(HoverLostEvent e) protected override void OnHoverLost(HoverLostEvent e)
{ {
base.OnHoverLost(e); base.OnHoverLost(e);
Selected = false; State = SelectionState.NotSelected;
} }
private void selectionChanged(ValueChangedEvent<bool> args) private void selectionChanged(SelectionState newState)
{ {
if (clickAnimating) if (clickAnimating)
return; return;
if (args.NewValue) if (newState == SelectionState.Selected)
{ {
spriteText.TransformSpacingTo(hoverSpacing, hover_duration, Easing.OutElastic); spriteText.TransformSpacingTo(hoverSpacing, hover_duration, Easing.OutElastic);
colourContainer.ResizeWidthTo(hover_width, hover_duration, Easing.OutElastic); colourContainer.ResizeWidthTo(hover_width, hover_duration, Easing.OutElastic);

View File

@ -1,10 +0,0 @@
// 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.
namespace osu.Game.Graphics.UserInterface
{
public interface ISelectable
{
bool Selected { get; set; }
}
}

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Globalization; using System.Globalization;
using osu.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio; using osu.Framework.Audio;
using osu.Framework.Audio.Sample; using osu.Framework.Audio.Sample;
@ -26,7 +27,7 @@ using osuTK.Graphics;
namespace osu.Game.Overlays.Volume namespace osu.Game.Overlays.Volume
{ {
public class VolumeMeter : Container, IKeyBindingHandler<GlobalAction>, ISelectable public class VolumeMeter : Container, IKeyBindingHandler<GlobalAction>, IStateful<SelectionState>
{ {
private CircularProgress volumeCircle; private CircularProgress volumeCircle;
private CircularProgress volumeCircleGlow; private CircularProgress volumeCircleGlow;
@ -44,12 +45,21 @@ namespace osu.Game.Overlays.Volume
private Sample sample; private Sample sample;
private double sampleLastPlaybackTime; private double sampleLastPlaybackTime;
public BindableBool SelectedBindable = new BindableBool(); public event Action<SelectionState> StateChanged;
public bool Selected private SelectionState state;
public SelectionState State
{ {
get => SelectedBindable.Value; get => state;
set => SelectedBindable.Value = value; set
{
if (state == value)
return;
state = value;
StateChanged?.Invoke(value);
}
} }
public VolumeMeter(string name, float circleSize, Color4 meterColour) public VolumeMeter(string name, float circleSize, Color4 meterColour)
@ -220,7 +230,7 @@ namespace osu.Game.Overlays.Volume
bgProgress.Current.Value = 0.75f; bgProgress.Current.Value = 0.75f;
SelectedBindable.ValueChanged += selectionChanged; StateChanged += stateChanged;
} }
private int? displayVolumeInt; private int? displayVolumeInt;
@ -341,7 +351,7 @@ namespace osu.Game.Overlays.Volume
protected override bool OnHover(HoverEvent e) protected override bool OnHover(HoverEvent e)
{ {
Selected = true; State = SelectionState.Selected;
return false; return false;
} }
@ -349,9 +359,9 @@ namespace osu.Game.Overlays.Volume
{ {
} }
private void selectionChanged(ValueChangedEvent<bool> selected) private void stateChanged(SelectionState newState)
{ {
if (selected.NewValue) if (newState == SelectionState.Selected)
{ {
this.ScaleTo(1.04f, transition_length, Easing.OutExpo); this.ScaleTo(1.04f, transition_length, Easing.OutExpo);
focusGlowContainer.FadeIn(transition_length, Easing.OutExpo); focusGlowContainer.FadeIn(transition_length, Easing.OutExpo);
@ -371,12 +381,12 @@ namespace osu.Game.Overlays.Volume
switch (action) switch (action)
{ {
case GlobalAction.SelectPrevious: case GlobalAction.SelectPrevious:
Selected = true; State = SelectionState.Selected;
adjust(1, false); adjust(1, false);
return true; return true;
case GlobalAction.SelectNext: case GlobalAction.SelectNext:
Selected = true; State = SelectionState.Selected;
adjust(-1, false); adjust(-1, false);
return true; return true;
} }

View File

@ -13,6 +13,7 @@ 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;
@ -93,7 +94,7 @@ namespace osu.Game.Overlays
foreach (var volumeMeter in volumeMeters) foreach (var volumeMeter in volumeMeters)
{ {
volumeMeter.Bindable.ValueChanged += _ => Show(); volumeMeter.Bindable.ValueChanged += _ => Show();
volumeMeter.SelectedBindable.ValueChanged += selected => volumeMeterSelectionChanged(volumeMeter, selected.NewValue); volumeMeter.StateChanged += selected => volumeMeterSelectionChanged(volumeMeter, selected);
} }
muteButton.Current.ValueChanged += _ => Show(); muteButton.Current.ValueChanged += _ => Show();
@ -140,9 +141,9 @@ namespace osu.Game.Overlays
return false; return false;
} }
private void volumeMeterSelectionChanged(VolumeMeter meter, bool isSelected) private void volumeMeterSelectionChanged(VolumeMeter meter, SelectionState state)
{ {
if (!isSelected) if (state == SelectionState.NotSelected)
volumeMeters.Deselect(); volumeMeters.Deselect();
else else
volumeMeters.Select(meter); volumeMeters.Select(meter);

View File

@ -184,7 +184,7 @@ namespace osu.Game.Screens.Play
} }
}; };
button.SelectedBindable.ValueChanged += selected => buttonSelectionChanged(button, selected.NewValue); button.StateChanged += selected => buttonSelectionChanged(button, selected);
InternalButtons.Add(button); InternalButtons.Add(button);
} }
@ -217,9 +217,9 @@ namespace osu.Game.Screens.Play
{ {
} }
private void buttonSelectionChanged(DialogButton button, bool isSelected) private void buttonSelectionChanged(DialogButton button, SelectionState state)
{ {
if (!isSelected) if (state == SelectionState.NotSelected)
InternalButtons.Deselect(); InternalButtons.Deselect();
else else
InternalButtons.Select(button); InternalButtons.Select(button);
@ -263,7 +263,7 @@ namespace osu.Game.Screens.Play
protected override bool OnMouseMove(MouseMoveEvent e) protected override bool OnMouseMove(MouseMoveEvent e)
{ {
Selected = true; State = SelectionState.Selected;
return base.OnMouseMove(e); return base.OnMouseMove(e);
} }
} }