1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 07:23:22 +08:00
osu-lazer/osu.Game/Overlays/Settings/SettingsItem.cs

227 lines
7.6 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2021-05-04 15:59:22 +08:00
using osu.Game.Graphics.Containers;
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Settings
{
public abstract class SettingsItem<T> : Container, IFilterable, ISettingsItem, IHasCurrentValue<T>, IHasTooltip
2018-04-13 17:19:50 +08:00
{
protected abstract Drawable CreateControl();
protected Drawable Control { get; }
private IHasCurrentValue<T> controlWithCurrent => Control as IHasCurrentValue<T>;
protected override Container<Drawable> Content => FlowContent;
protected readonly FillFlowContainer FlowContent;
private SpriteText label;
2018-04-13 17:19:50 +08:00
private OsuTextFlowContainer warningText;
2021-05-04 15:59:22 +08:00
2018-04-13 17:19:50 +08:00
public bool ShowsDefaultIndicator = true;
2021-10-19 03:01:43 +08:00
private readonly Container defaultValueIndicatorContainer;
2018-04-13 17:19:50 +08:00
public LocalisableString TooltipText { get; set; }
[Resolved]
private OsuColour colours { get; set; }
private LocalisableString labelText;
public virtual LocalisableString LabelText
2018-04-13 17:19:50 +08:00
{
get => labelText;
2018-04-13 17:19:50 +08:00
set
{
ensureLabelCreated();
labelText = value;
updateLabelText();
}
}
private LocalisableString? contractedLabelText;
2018-04-13 17:19:50 +08:00
/// <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();
2018-04-13 17:19:50 +08:00
}
}
2021-05-04 15:59:22 +08:00
/// <summary>
/// Text to be displayed at the bottom of this <see cref="SettingsItem{T}"/>.
2021-05-05 15:16:02 +08:00
/// Generally used to recommend the user change their setting as the current one is considered sub-optimal.
2021-05-04 15:59:22 +08:00
/// </summary>
public LocalisableString? WarningText
2021-05-04 15:59:22 +08:00
{
set
{
bool hasValue = value != default;
if (warningText == null)
{
if (!hasValue)
return;
// construct lazily for cases where the label is not needed (may be provided by the Control).
FlowContent.Add(warningText = new SettingsNoticeText(colours) { Margin = new MarginPadding { Bottom = 5 } });
}
warningText.Alpha = hasValue ? 1 : 0;
warningText.Text = value ?? default;
2021-05-04 15:59:22 +08:00
}
}
public virtual Bindable<T> Current
2018-04-13 17:19:50 +08:00
{
2019-12-06 16:10:06 +08:00
get => controlWithCurrent.Current;
set => controlWithCurrent.Current = value;
2018-04-13 17:19:50 +08:00
}
public BindableBool Expanded { get; } = new BindableBool(true);
public bool IsControlDragged => Control.IsDragged;
public event Action SettingChanged;
public virtual IEnumerable<string> FilterTerms => Keywords == null ? new[] { LabelText.ToString() } : new List<string>(Keywords) { LabelText.ToString() }.ToArray();
2019-11-21 00:27:34 +08:00
public IEnumerable<string> Keywords { get; set; }
2018-04-13 17:19:50 +08:00
public bool MatchingFilter
{
set => Alpha = value ? 1 : 0;
2018-04-13 17:19:50 +08:00
}
2019-03-28 23:29:07 +08:00
public bool FilteringActive { get; set; }
2018-04-13 17:19:50 +08:00
protected SettingsItem()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Right = SettingsPanel.CONTENT_MARGINS };
2018-04-13 17:19:50 +08:00
2021-10-19 03:01:43 +08:00
InternalChildren = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2021-10-19 03:01:43 +08:00
defaultValueIndicatorContainer = new Container
2018-04-13 17:19:50 +08:00
{
2021-10-19 03:01:43 +08:00
Width = SettingsPanel.CONTENT_MARGINS,
2018-04-13 17:19:50 +08:00
},
2021-10-19 05:30:38 +08:00
new Container
{
2021-10-19 03:01:43 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = SettingsPanel.CONTENT_MARGINS },
2021-10-19 05:30:38 +08:00
Child = FlowContent = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0, 10),
Child = Control = CreateControl(),
}
}
2018-04-13 17:19:50 +08:00
};
// IMPORTANT: all bindable logic is in constructor intentionally to support "CreateSettingsControls" being used in a context it is
// never loaded, but requires bindable storage.
if (controlWithCurrent == null)
throw new ArgumentException(@$"Control created via {nameof(CreateControl)} must implement {nameof(IHasCurrentValue<T>)}");
2019-12-06 16:10:06 +08:00
controlWithCurrent.Current.ValueChanged += _ => SettingChanged?.Invoke();
controlWithCurrent.Current.DisabledChanged += _ => updateDisabled();
}
[BackgroundDependencyLoader]
private void load()
{
// intentionally done before LoadComplete to avoid overhead.
if (ShowsDefaultIndicator)
{
2021-10-19 03:01:43 +08:00
defaultValueIndicatorContainer.Add(new RestoreDefaultValueButton<T>
{
Current = controlWithCurrent.Current,
Anchor = Anchor.Centre,
Origin = Anchor.Centre
2021-10-19 03:01:43 +08:00
});
}
2018-04-13 17:19:50 +08:00
}
[Resolved(canBeNull: true)]
private IExpandingContainer expandingContainer { get; set; }
protected override void LoadComplete()
{
base.LoadComplete();
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.
2021-10-19 03:01:43 +08:00
// otherwise, it should be centred vertically to the left of the main control of the settings item.
defaultValueIndicatorContainer.Height = !string.IsNullOrEmpty(label?.Text.ToString()) ? label.DrawHeight : Control.DrawHeight;
}
private void updateDisabled()
{
if (label != null)
label.Alpha = controlWithCurrent.Current.Disabled ? 0.3f : 1;
}
2018-04-13 17:19:50 +08:00
}
}