1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-21 14:23:06 +08:00

Add IExpandable support for SettingsItem<T>

This commit is contained in:
Salman Ahmed 2022-01-21 17:45:08 +03:00
parent 326f12f847
commit f4c7a332c3
2 changed files with 78 additions and 22 deletions

View File

@ -2,12 +2,17 @@
// 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;
using osu.Framework.Graphics;
namespace osu.Game.Overlays.Settings namespace osu.Game.Overlays.Settings
{ {
public interface ISettingsItem : IDrawable, IDisposable /// <summary>
/// A non-generic interface for <see cref="SettingsItem{T}"/>s.
/// </summary>
public interface ISettingsItem : IExpandable, IDisposable
{ {
/// <summary>
/// Invoked when the setting value has changed.
/// </summary>
event Action SettingChanged; event Action SettingChanged;
} }
} }

View File

@ -30,7 +30,7 @@ namespace osu.Game.Overlays.Settings
protected readonly FillFlowContainer FlowContent; protected readonly FillFlowContainer FlowContent;
private SpriteText labelText; private SpriteText label;
private OsuTextFlowContainer warningText; private OsuTextFlowContainer warningText;
@ -42,21 +42,34 @@ namespace osu.Game.Overlays.Settings
[Resolved] [Resolved]
private OsuColour colours { get; set; } private OsuColour colours { get; set; }
private LocalisableString labelText;
public virtual LocalisableString LabelText public virtual LocalisableString LabelText
{ {
get => labelText?.Text ?? string.Empty; get => labelText;
set set
{ {
if (labelText == null) ensureLabelCreated();
{
// construct lazily for cases where the label is not needed (may be provided by the Control).
FlowContent.Insert(-1, labelText = new OsuSpriteText());
updateDisabled(); labelText = value;
updateLabelText();
}
} }
labelText.Text = value; private LocalisableString? contractedLabelText;
updateLayout();
/// <summary>
/// Text to be displayed in place of <see cref="LabelText"/> when this <see cref="SettingsItem{T}"/> is in a contracted state.
/// </summary>
public LocalisableString? ContractedLabelText
{
get => contractedLabelText;
set
{
ensureLabelCreated();
contractedLabelText = value;
updateLabelText();
} }
} }
@ -90,6 +103,10 @@ namespace osu.Game.Overlays.Settings
set => controlWithCurrent.Current = value; set => controlWithCurrent.Current = value;
} }
public BindableBool Expanded { get; } = new BindableBool(true);
public event Action SettingChanged;
public virtual IEnumerable<string> FilterTerms => Keywords == null ? new[] { LabelText.ToString() } : new List<string>(Keywords) { LabelText.ToString() }.ToArray(); public virtual IEnumerable<string> FilterTerms => Keywords == null ? new[] { LabelText.ToString() } : new List<string>(Keywords) { LabelText.ToString() }.ToArray();
public IEnumerable<string> Keywords { get; set; } public IEnumerable<string> Keywords { get; set; }
@ -101,8 +118,6 @@ namespace osu.Game.Overlays.Settings
public bool FilteringActive { get; set; } public bool FilteringActive { get; set; }
public event Action SettingChanged;
protected SettingsItem() protected SettingsItem()
{ {
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
@ -151,23 +166,59 @@ namespace osu.Game.Overlays.Settings
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre Origin = Anchor.Centre
}); });
updateLayout();
} }
} }
private void updateLayout() [Resolved(canBeNull: true)]
private IExpandingContainer expandingContainer { get; set; }
protected override void LoadComplete()
{ {
bool hasLabel = labelText != null && !string.IsNullOrEmpty(labelText.Text.ToString()); base.LoadComplete();
// if the settings item is providing a label, the default value indicator should be centred vertically to the left of the label. expandingContainer?.Expanded.BindValueChanged(containerExpanded => Expanded.Value = containerExpanded.NewValue, true);
Expanded.BindValueChanged(v =>
{
updateLabelText();
Control.FadeTo(v.NewValue ? 1 : 0, 500, Easing.OutQuint);
Control.BypassAutoSizeAxes = v.NewValue ? Axes.None : Axes.Both;
}, true);
FinishTransforms(true);
}
private void ensureLabelCreated()
{
if (label != null)
return;
// construct lazily for cases where the label is not needed (may be provided by the Control).
FlowContent.Insert(-1, label = new OsuSpriteText());
updateDisabled();
}
private void updateLabelText()
{
if (label != null)
{
if (contractedLabelText is LocalisableString contractedText)
label.Text = Expanded.Value ? labelText : contractedText;
else
label.Text = labelText;
}
// if the settings item is providing a non-empty label, the default value indicator should be centred vertically to the left of the label.
// otherwise, it should be centred vertically to the left of the main control of the settings item. // otherwise, it should be centred vertically to the left of the main control of the settings item.
defaultValueIndicatorContainer.Height = hasLabel ? labelText.DrawHeight : Control.DrawHeight; defaultValueIndicatorContainer.Height = !string.IsNullOrEmpty(label?.Text.ToString()) ? label.DrawHeight : Control.DrawHeight;
} }
private void updateDisabled() private void updateDisabled()
{ {
if (labelText != null) if (label != null)
labelText.Alpha = controlWithCurrent.Current.Disabled ? 0.3f : 1; label.Alpha = controlWithCurrent.Current.Disabled ? 0.3f : 1;
} }
} }
} }