2019-01-24 16:43:03 +08:00
|
|
|
// 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
|
|
|
|
2019-12-20 17:00:04 +08:00
|
|
|
using System;
|
2017-09-13 22:18:02 +08:00
|
|
|
using System.Collections.Generic;
|
2017-10-21 10:12:11 +08:00
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2017-05-04 22:07:24 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-10-22 13:40:41 +08:00
|
|
|
using osu.Framework.Graphics.Cursor;
|
2017-05-04 22:07:24 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2021-02-22 16:14:00 +08:00
|
|
|
using osu.Framework.Localisation;
|
2022-03-24 18:32:34 +08:00
|
|
|
using osu.Game.Configuration;
|
2017-10-21 10:12:11 +08:00
|
|
|
using osu.Game.Graphics;
|
2017-05-04 22:07:24 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-05-04 15:59:22 +08:00
|
|
|
using osu.Game.Graphics.Containers;
|
2021-10-16 04:44:28 +08:00
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-05-15 09:55:29 +08:00
|
|
|
namespace osu.Game.Overlays.Settings
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
2021-02-11 15:38:17 +08:00
|
|
|
public abstract class SettingsItem<T> : Container, IFilterable, ISettingsItem, IHasCurrentValue<T>, IHasTooltip
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
|
|
|
protected abstract Drawable CreateControl();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-05-04 22:32:27 +08:00
|
|
|
protected Drawable Control { get; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-03-24 18:32:34 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The source component if this <see cref="SettingsItem{T}"/> was created via <see cref="SettingSourceAttribute"/>.
|
|
|
|
/// </summary>
|
2022-04-01 15:00:51 +08:00
|
|
|
public object SettingSourceObject { get; internal set; }
|
2022-03-24 18:32:34 +08:00
|
|
|
|
2022-04-22 17:04:43 +08:00
|
|
|
public const string CLASSIC_DEFAULT_SEARCH_TERM = @"has-classic-default";
|
|
|
|
|
2017-05-04 22:32:27 +08:00
|
|
|
private IHasCurrentValue<T> controlWithCurrent => Control as IHasCurrentValue<T>;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-10-21 10:12:11 +08:00
|
|
|
protected override Container<Drawable> Content => FlowContent;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-10-21 10:12:11 +08:00
|
|
|
protected readonly FillFlowContainer FlowContent;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2020-03-23 12:11:40 +08:00
|
|
|
private SpriteText labelText;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-05-05 15:19:06 +08:00
|
|
|
private OsuTextFlowContainer warningText;
|
2021-05-04 15:59:22 +08:00
|
|
|
|
2017-10-21 10:12:11 +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
|
|
|
|
2021-06-26 01:10:04 +08:00
|
|
|
public LocalisableString TooltipText { get; set; }
|
2021-02-11 15:41:21 +08:00
|
|
|
|
2021-05-05 15:19:06 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
2021-02-22 16:14:00 +08:00
|
|
|
public virtual LocalisableString LabelText
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
2020-03-23 12:11:40 +08:00
|
|
|
get => labelText?.Text ?? string.Empty;
|
2017-05-04 22:07:24 +08:00
|
|
|
set
|
|
|
|
{
|
2020-03-23 12:11:40 +08:00
|
|
|
if (labelText == null)
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
|
|
|
// construct lazily for cases where the label is not needed (may be provided by the Control).
|
2021-10-19 03:01:43 +08:00
|
|
|
FlowContent.Insert(-1, labelText = new OsuSpriteText());
|
2020-03-23 12:11:40 +08:00
|
|
|
|
|
|
|
updateDisabled();
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2020-03-23 12:11:40 +08:00
|
|
|
labelText.Text = value;
|
2021-10-16 04:44:28 +08:00
|
|
|
updateLayout();
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
|
|
|
}
|
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>
|
2021-07-26 15:59:27 +08:00
|
|
|
public LocalisableString? WarningText
|
2021-05-04 15:59:22 +08:00
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
2021-10-31 00:50:34 +08:00
|
|
|
bool hasValue = value != default;
|
2021-07-26 15:59:27 +08:00
|
|
|
|
2021-05-05 15:19:06 +08:00
|
|
|
if (warningText == null)
|
|
|
|
{
|
2021-07-26 15:59:27 +08:00
|
|
|
if (!hasValue)
|
|
|
|
return;
|
|
|
|
|
2021-05-05 15:19:06 +08:00
|
|
|
// construct lazily for cases where the label is not needed (may be provided by the Control).
|
2021-08-15 01:14:21 +08:00
|
|
|
FlowContent.Add(warningText = new SettingsNoticeText(colours) { Margin = new MarginPadding { Bottom = 5 } });
|
2021-05-05 15:19:06 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 04:33:05 +08:00
|
|
|
warningText.Alpha = hasValue ? 1 : 0;
|
2021-10-31 00:50:34 +08:00
|
|
|
warningText.Text = value ?? default;
|
2021-05-04 15:59:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 16:18:41 +08:00
|
|
|
public virtual Bindable<T> Current
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
2019-12-06 16:10:06 +08:00
|
|
|
get => controlWithCurrent.Current;
|
|
|
|
set => controlWithCurrent.Current = value;
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-04-22 17:04:43 +08:00
|
|
|
public virtual IEnumerable<string> FilterTerms
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var keywords = new List<string>(Keywords ?? Array.Empty<string>())
|
|
|
|
{
|
|
|
|
LabelText.ToString()
|
|
|
|
};
|
|
|
|
|
2022-04-27 14:59:39 +08:00
|
|
|
if (HasClassicDefault)
|
2022-04-22 17:04:43 +08:00
|
|
|
keywords.Add(CLASSIC_DEFAULT_SEARCH_TERM);
|
|
|
|
|
|
|
|
return keywords;
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 00:27:34 +08:00
|
|
|
|
|
|
|
public IEnumerable<string> Keywords { get; set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-04-23 04:50:58 +08:00
|
|
|
private bool matchingFilter = true;
|
2022-04-23 01:34:00 +08:00
|
|
|
|
|
|
|
public bool MatchingFilter
|
|
|
|
{
|
|
|
|
get => matchingFilter;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
bool wasPresent = IsPresent;
|
2022-04-14 19:06:15 +08:00
|
|
|
|
2022-04-23 01:34:00 +08:00
|
|
|
matchingFilter = value;
|
|
|
|
|
|
|
|
if (IsPresent != wasPresent)
|
|
|
|
Invalidate(Invalidation.Presence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool IsPresent => base.IsPresent && MatchingFilter;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-03-28 23:29:07 +08:00
|
|
|
public bool FilteringActive { get; set; }
|
|
|
|
|
2019-12-20 17:00:04 +08:00
|
|
|
public event Action SettingChanged;
|
|
|
|
|
2022-04-26 14:06:27 +08:00
|
|
|
private T classicDefault;
|
2022-04-27 14:59:39 +08:00
|
|
|
|
|
|
|
public bool HasClassicDefault { get; private set; }
|
2022-04-26 14:06:27 +08:00
|
|
|
|
2022-04-22 17:04:43 +08:00
|
|
|
/// <summary>
|
2022-04-26 14:06:27 +08:00
|
|
|
/// A "classic" default value for this setting.
|
2022-04-22 17:04:43 +08:00
|
|
|
/// </summary>
|
2022-04-26 14:06:27 +08:00
|
|
|
public T ClassicDefault
|
2022-04-22 17:04:43 +08:00
|
|
|
{
|
2022-04-26 14:06:27 +08:00
|
|
|
set
|
2022-04-22 17:04:43 +08:00
|
|
|
{
|
2022-04-26 14:06:27 +08:00
|
|
|
classicDefault = value;
|
2022-04-27 14:59:39 +08:00
|
|
|
HasClassicDefault = true;
|
2022-04-22 17:04:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 14:59:39 +08:00
|
|
|
public void ApplyClassicDefault()
|
2022-04-26 14:06:27 +08:00
|
|
|
{
|
2022-04-27 14:59:39 +08:00
|
|
|
if (!HasClassicDefault)
|
|
|
|
throw new InvalidOperationException($"Cannot apply a classic default to a setting which doesn't have one defined via {nameof(ClassicDefault)}.");
|
2022-04-26 14:33:39 +08:00
|
|
|
|
2022-04-27 14:59:39 +08:00
|
|
|
Current.Value = classicDefault;
|
2022-04-26 14:06:27 +08:00
|
|
|
}
|
|
|
|
|
2022-04-27 14:59:39 +08:00
|
|
|
public void ApplyDefault() => Current.SetDefault();
|
|
|
|
|
2017-05-15 09:55:29 +08:00
|
|
|
protected SettingsItem()
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
2019-05-14 09:45:05 +08:00
|
|
|
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[]
|
2017-10-21 10:12:11 +08:00
|
|
|
{
|
2021-10-19 03:01:43 +08:00
|
|
|
defaultValueIndicatorContainer = new Container
|
2018-02-02 14:18:39 +08:00
|
|
|
{
|
2021-10-19 03:01:43 +08:00
|
|
|
Width = SettingsPanel.CONTENT_MARGINS,
|
2018-02-02 14:18:39 +08:00
|
|
|
},
|
2021-10-19 05:30:38 +08:00
|
|
|
new Container
|
2021-10-16 04:44:28 +08:00
|
|
|
{
|
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(),
|
|
|
|
}
|
2021-10-16 04:44:28 +08:00
|
|
|
}
|
2017-10-21 10:12:11 +08:00
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-08-17 16:50:30 +08:00
|
|
|
// IMPORTANT: all bindable logic is in constructor intentionally to support "CreateSettingsControls" being used in a context it is
|
2019-12-20 17:00:04 +08:00
|
|
|
// never loaded, but requires bindable storage.
|
2021-07-08 14:52:49 +08:00
|
|
|
if (controlWithCurrent == null)
|
|
|
|
throw new ArgumentException(@$"Control created via {nameof(CreateControl)} must implement {nameof(IHasCurrentValue<T>)}");
|
2019-12-06 16:10:06 +08:00
|
|
|
|
2021-07-08 14:52:49 +08:00
|
|
|
controlWithCurrent.Current.ValueChanged += _ => SettingChanged?.Invoke();
|
|
|
|
controlWithCurrent.Current.DisabledChanged += _ => updateDisabled();
|
2021-08-17 16:50:30 +08:00
|
|
|
}
|
2021-07-08 14:52:49 +08:00
|
|
|
|
2021-08-17 16:50:30 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2021-08-16 18:16:48 +08:00
|
|
|
// intentionally done before LoadComplete to avoid overhead.
|
2021-07-08 14:52:49 +08:00
|
|
|
if (ShowsDefaultIndicator)
|
2021-08-16 18:16:48 +08:00
|
|
|
{
|
2021-10-19 03:01:43 +08:00
|
|
|
defaultValueIndicatorContainer.Add(new RestoreDefaultValueButton<T>
|
2021-08-16 18:16:48 +08:00
|
|
|
{
|
|
|
|
Current = controlWithCurrent.Current,
|
2021-10-16 04:44:28 +08:00
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre
|
2021-10-19 03:01:43 +08:00
|
|
|
});
|
2021-10-16 04:44:28 +08:00
|
|
|
updateLayout();
|
2021-08-16 18:16:48 +08:00
|
|
|
}
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-16 04:44:28 +08:00
|
|
|
private void updateLayout()
|
|
|
|
{
|
2021-10-19 03:01:43 +08:00
|
|
|
bool hasLabel = labelText != null && !string.IsNullOrEmpty(labelText.Text.ToString());
|
2021-10-16 04:44:28 +08:00
|
|
|
|
2021-10-19 03:01:43 +08:00
|
|
|
// if the settings item is providing a 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.
|
|
|
|
defaultValueIndicatorContainer.Height = hasLabel ? labelText.DrawHeight : Control.DrawHeight;
|
2021-10-16 04:44:28 +08:00
|
|
|
}
|
|
|
|
|
2020-03-23 12:11:40 +08:00
|
|
|
private void updateDisabled()
|
|
|
|
{
|
|
|
|
if (labelText != null)
|
|
|
|
labelText.Alpha = controlWithCurrent.Current.Disabled ? 0.3f : 1;
|
|
|
|
}
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
2021-06-26 01:10:04 +08:00
|
|
|
}
|