osu/osu.Game/Overlays/Settings/SettingsItem.cs

146 lines
4.9 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 09:19:50 +00:00
using System;
2018-04-13 09:19:50 +00:00
using System.Collections.Generic;
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00: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 09:19:50 +00:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2021-05-04 07:59:22 +00:00
using osu.Game.Graphics.Containers;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays.Settings
{
public abstract class SettingsItem<T> : Container, IFilterable, ISettingsItem, IHasCurrentValue<T>, IHasTooltip
2018-04-13 09:19:50 +00: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 labelText;
2018-04-13 09:19:50 +00:00
private OsuTextFlowContainer warningText;
2021-05-04 07:59:22 +00:00
2018-04-13 09:19:50 +00:00
public bool ShowsDefaultIndicator = true;
public LocalisableString TooltipText { get; set; }
[Resolved]
private OsuColour colours { get; set; }
public virtual LocalisableString LabelText
2018-04-13 09:19:50 +00:00
{
get => labelText?.Text ?? string.Empty;
2018-04-13 09:19:50 +00:00
set
{
if (labelText == null)
2018-04-13 09:19:50 +00:00
{
// construct lazily for cases where the label is not needed (may be provided by the Control).
FlowContent.Insert(-1, labelText = new OsuSpriteText());
updateDisabled();
2018-04-13 09:19:50 +00:00
}
labelText.Text = value;
2018-04-13 09:19:50 +00:00
}
}
2021-05-04 07:59:22 +00:00
/// <summary>
/// Text to be displayed at the bottom of this <see cref="SettingsItem{T}"/>.
2021-05-05 07:16:02 +00:00
/// Generally used to recommend the user change their setting as the current one is considered sub-optimal.
2021-05-04 07:59:22 +00:00
/// </summary>
2021-05-05 07:16:02 +00:00
public string WarningText
2021-05-04 07:59:22 +00:00
{
set
{
if (warningText == null)
{
// construct lazily for cases where the label is not needed (may be provided by the Control).
FlowContent.Add(warningText = new OsuTextFlowContainer
{
Colour = colours.Yellow,
Margin = new MarginPadding { Bottom = 5 },
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
});
}
2021-05-05 07:16:02 +00:00
warningText.Alpha = string.IsNullOrWhiteSpace(value) ? 0 : 1;
warningText.Text = value;
2021-05-04 07:59:22 +00:00
}
}
public virtual Bindable<T> Current
2018-04-13 09:19:50 +00:00
{
2019-12-06 08:10:06 +00:00
get => controlWithCurrent.Current;
set => controlWithCurrent.Current = value;
2018-04-13 09:19:50 +00:00
}
public virtual IEnumerable<string> FilterTerms => Keywords == null ? new[] { LabelText.ToString() } : new List<string>(Keywords) { LabelText.ToString() }.ToArray();
2019-11-20 16:27:34 +00:00
public IEnumerable<string> Keywords { get; set; }
2018-04-13 09:19:50 +00:00
public bool MatchingFilter
{
set => this.FadeTo(value ? 1 : 0);
2018-04-13 09:19:50 +00:00
}
2019-03-28 15:29:07 +00:00
public bool FilteringActive { get; set; }
public event Action SettingChanged;
2018-04-13 09:19:50 +00:00
protected SettingsItem()
{
RestoreDefaultValueButton<T> restoreDefaultButton;
2018-04-13 09:19:50 +00:00
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Right = SettingsPanel.CONTENT_MARGINS };
2018-04-13 09:19:50 +00:00
InternalChildren = new Drawable[]
{
restoreDefaultButton = new RestoreDefaultValueButton<T>(),
2018-04-13 09:19:50 +00:00
FlowContent = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = SettingsPanel.CONTENT_MARGINS },
2021-05-04 07:59:22 +00:00
Children = new[]
{
Control = CreateControl(),
},
2018-04-13 09:19:50 +00:00
},
};
// all bindable logic is in constructor intentionally to support "CreateSettingsControls" being used in a context it is
// never loaded, but requires bindable storage.
2018-04-13 09:19:50 +00:00
if (controlWithCurrent != null)
2019-12-06 08:10:06 +00:00
{
controlWithCurrent.Current.ValueChanged += _ => SettingChanged?.Invoke();
controlWithCurrent.Current.DisabledChanged += _ => updateDisabled();
2019-12-06 08:10:06 +00:00
if (ShowsDefaultIndicator)
2021-05-15 01:24:08 +00:00
restoreDefaultButton.Current = controlWithCurrent.Current;
2019-12-06 08:10:06 +00:00
}
2018-04-13 09:19:50 +00:00
}
private void updateDisabled()
{
if (labelText != null)
labelText.Alpha = controlWithCurrent.Current.Disabled ? 0.3f : 1;
}
2018-04-13 09:19:50 +00:00
}
}