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

212 lines
6.7 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-11-20 07:51:59 +00:00
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
2019-04-02 05:51:28 +00:00
using osu.Framework.Graphics.Effects;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-04-13 09:19:50 +00:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays.Settings
{
public abstract class SettingsItem<T> : Container, IFilterable, ISettingsItem
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
public bool ShowsDefaultIndicator = true;
public virtual string LabelText
{
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
}
}
public virtual Bindable<T> Bindable
{
2019-12-06 08:10:06 +00:00
get => controlWithCurrent.Current;
set => controlWithCurrent.Current = value;
2018-04-13 09:19:50 +00:00
}
2019-11-20 16:42:57 +00:00
public virtual IEnumerable<string> FilterTerms => Keywords == null ? new[] { LabelText } : new List<string>(Keywords) { LabelText }.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 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(),
FlowContent = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = SettingsPanel.CONTENT_MARGINS },
2018-04-13 09:19:50 +00:00
Child = Control = CreateControl()
},
};
// 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)
restoreDefaultButton.Bindable = controlWithCurrent.Current;
}
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
private class RestoreDefaultValueButton : Container, IHasTooltip
{
private Bindable<T> bindable;
public Bindable<T> Bindable
{
get => bindable;
2018-04-13 09:19:50 +00:00
set
{
bindable = value;
bindable.ValueChanged += _ => UpdateState();
bindable.DisabledChanged += _ => UpdateState();
bindable.DefaultChanged += _ => UpdateState();
2019-12-06 09:03:50 +00:00
UpdateState();
2018-04-13 09:19:50 +00:00
}
}
private Color4 buttonColour;
private bool hovering;
public RestoreDefaultValueButton()
{
RelativeSizeAxes = Axes.Y;
Width = SettingsPanel.CONTENT_MARGINS;
2018-04-13 09:19:50 +00:00
Alpha = 0f;
}
[BackgroundDependencyLoader]
private void load(OsuColour colour)
{
buttonColour = colour.Yellow;
Child = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
CornerRadius = 3,
Masking = true,
Colour = buttonColour,
EdgeEffect = new EdgeEffectParameters
{
Colour = buttonColour.Opacity(0.1f),
Type = EdgeEffectType.Glow,
Radius = 2,
},
Size = new Vector2(0.33f, 0.8f),
Child = new Box { RelativeSizeAxes = Axes.Both },
};
}
protected override void LoadComplete()
{
base.LoadComplete();
UpdateState();
}
2020-01-14 19:26:54 +00:00
public string TooltipText => "revert to default";
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnClick(ClickEvent e)
2018-04-13 09:19:50 +00:00
{
if (bindable != null && !bindable.Disabled)
bindable.SetDefault();
return true;
}
2018-10-02 03:02:47 +00:00
protected override bool OnHover(HoverEvent e)
2018-04-13 09:19:50 +00:00
{
hovering = true;
UpdateState();
return false;
}
2018-10-02 03:02:47 +00:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 09:19:50 +00:00
{
hovering = false;
UpdateState();
}
public void SetButtonColour(Color4 buttonColour)
{
this.buttonColour = buttonColour;
UpdateState();
}
public void UpdateState()
{
if (bindable == null)
return;
this.FadeTo(bindable.IsDefault ? 0f :
hovering && !bindable.Disabled ? 1f : 0.65f, 200, Easing.OutQuint);
this.FadeColour(bindable.Disabled ? Color4.Gray : buttonColour, 200, Easing.OutQuint);
}
}
}
}