2018-01-05 19:21:19 +08:00
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-05-04 22:07:24 +08:00
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
2017-09-13 22:18:02 +08:00
|
|
|
using System.Collections.Generic;
|
2017-10-21 10:12:11 +08:00
|
|
|
using osu.Framework.Allocation;
|
2017-05-04 22:07:24 +08:00
|
|
|
using OpenTK.Graphics;
|
|
|
|
using osu.Framework.Configuration;
|
2017-10-21 10:12:11 +08:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2017-05-04 22:07:24 +08:00
|
|
|
using osu.Framework.Graphics;
|
2017-10-21 10:12:11 +08:00
|
|
|
using osu.Framework.Graphics.Colour;
|
2017-05-04 22:07:24 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-10-22 13:40:41 +08:00
|
|
|
using osu.Framework.Graphics.Cursor;
|
2017-10-21 10:12:11 +08:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
2017-05-04 22:07:24 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2017-10-21 10:12:11 +08:00
|
|
|
using osu.Framework.Input;
|
|
|
|
using osu.Game.Graphics;
|
2017-05-04 22:07:24 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
2017-05-15 09:55:29 +08:00
|
|
|
namespace osu.Game.Overlays.Settings
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
2017-10-21 10:12:11 +08:00
|
|
|
public abstract class SettingsItem<T> : Container, IFilterable
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
|
|
|
protected abstract Drawable CreateControl();
|
|
|
|
|
2017-05-04 22:32:27 +08:00
|
|
|
protected Drawable Control { get; }
|
2017-05-04 22:07:24 +08:00
|
|
|
|
2017-05-04 22:32:27 +08:00
|
|
|
private IHasCurrentValue<T> controlWithCurrent => Control as IHasCurrentValue<T>;
|
2017-05-04 22:07:24 +08:00
|
|
|
|
2017-10-21 10:12:11 +08:00
|
|
|
protected override Container<Drawable> Content => FlowContent;
|
|
|
|
|
|
|
|
protected readonly FillFlowContainer FlowContent;
|
|
|
|
|
2017-05-04 22:07:24 +08:00
|
|
|
private SpriteText text;
|
|
|
|
|
2017-10-30 21:24:11 +08:00
|
|
|
private readonly RestoreDefaultValueButton restoreDefaultValueButton = new RestoreDefaultValueButton();
|
2017-10-21 10:12:11 +08:00
|
|
|
|
|
|
|
public bool ShowsDefaultIndicator = true;
|
|
|
|
|
2017-10-22 13:40:41 +08:00
|
|
|
private Color4? restoreDefaultValueColour;
|
2017-10-21 13:35:37 +08:00
|
|
|
|
2017-10-22 13:40:41 +08:00
|
|
|
public Color4 RestoreDefaultValueColour
|
2017-10-21 13:35:37 +08:00
|
|
|
{
|
2017-10-22 13:40:41 +08:00
|
|
|
get { return restoreDefaultValueColour ?? Color4.White; }
|
2017-10-21 13:35:37 +08:00
|
|
|
set
|
|
|
|
{
|
2017-10-22 13:40:41 +08:00
|
|
|
restoreDefaultValueColour = value;
|
|
|
|
restoreDefaultValueButton?.SetButtonColour(RestoreDefaultValueColour);
|
2017-10-21 13:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 22:07:24 +08:00
|
|
|
public virtual string LabelText
|
|
|
|
{
|
|
|
|
get { return text?.Text ?? string.Empty; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (text == null)
|
|
|
|
{
|
|
|
|
// construct lazily for cases where the label is not needed (may be provided by the Control).
|
2017-07-13 14:02:45 +08:00
|
|
|
Add(text = new OsuSpriteText { Depth = 1 });
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
text.Text = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 09:55:29 +08:00
|
|
|
// hold a reference to the provided bindable so we don't have to in every settings section.
|
2017-05-04 22:07:24 +08:00
|
|
|
private Bindable<T> bindable;
|
|
|
|
|
2017-06-05 12:24:54 +08:00
|
|
|
public virtual Bindable<T> Bindable
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return bindable;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
bindable = value;
|
2017-05-08 09:11:45 +08:00
|
|
|
controlWithCurrent?.Current.BindTo(bindable);
|
2017-10-21 10:12:11 +08:00
|
|
|
if (ShowsDefaultIndicator)
|
|
|
|
{
|
2017-10-26 16:15:20 +08:00
|
|
|
restoreDefaultValueButton.Bindable = bindable.GetBoundCopy();
|
2017-10-22 13:40:41 +08:00
|
|
|
restoreDefaultValueButton.Bindable.TriggerChange();
|
2017-10-21 10:12:11 +08:00
|
|
|
}
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:18:02 +08:00
|
|
|
public IEnumerable<string> FilterTerms => new[] { LabelText };
|
2017-05-04 22:07:24 +08:00
|
|
|
|
2017-05-30 15:33:26 +08:00
|
|
|
public bool MatchingFilter
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
// probably needs a better transition.
|
2017-07-15 00:18:12 +08:00
|
|
|
this.FadeTo(value ? 1 : 0);
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 09:55:29 +08:00
|
|
|
protected SettingsItem()
|
2017-05-04 22:07:24 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
2017-10-21 10:12:11 +08:00
|
|
|
Padding = new MarginPadding { Right = SettingsOverlay.CONTENT_MARGINS };
|
|
|
|
|
|
|
|
FlowContent = new FillFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Padding = new MarginPadding { Left = SettingsOverlay.CONTENT_MARGINS, Right = 5 },
|
|
|
|
};
|
2017-05-04 22:07:24 +08:00
|
|
|
|
|
|
|
if ((Control = CreateControl()) != null)
|
|
|
|
{
|
2017-05-04 22:32:27 +08:00
|
|
|
if (controlWithCurrent != null)
|
|
|
|
controlWithCurrent.Current.DisabledChanged += disabled => { Colour = disabled ? Color4.Gray : Color4.White; };
|
2017-10-21 10:12:11 +08:00
|
|
|
FlowContent.Add(Control);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
AddInternal(FlowContent);
|
|
|
|
|
2017-10-22 13:40:41 +08:00
|
|
|
if (restoreDefaultValueButton != null)
|
2017-10-21 10:12:11 +08:00
|
|
|
{
|
2017-10-22 13:40:41 +08:00
|
|
|
if (!restoreDefaultValueColour.HasValue)
|
|
|
|
restoreDefaultValueColour = colours.Yellow;
|
|
|
|
restoreDefaultValueButton.SetButtonColour(RestoreDefaultValueColour);
|
|
|
|
AddInternal(restoreDefaultValueButton);
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-21 10:12:11 +08:00
|
|
|
|
2017-10-30 21:24:11 +08:00
|
|
|
private class RestoreDefaultValueButton : Box, IHasTooltip
|
2017-10-21 10:12:11 +08:00
|
|
|
{
|
2017-10-26 16:15:20 +08:00
|
|
|
private Bindable<T> bindable;
|
2017-11-21 10:49:42 +08:00
|
|
|
public Bindable<T> Bindable
|
2017-10-26 16:15:20 +08:00
|
|
|
{
|
|
|
|
get { return bindable; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
bindable = value;
|
|
|
|
bindable.ValueChanged += newValue => UpdateState();
|
|
|
|
bindable.DisabledChanged += disabled => UpdateState();
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 10:12:11 +08:00
|
|
|
|
2017-10-22 13:40:41 +08:00
|
|
|
private Color4 buttonColour;
|
2017-10-21 13:35:37 +08:00
|
|
|
|
2017-10-21 10:12:11 +08:00
|
|
|
private bool hovering;
|
|
|
|
|
2017-10-22 13:40:41 +08:00
|
|
|
public RestoreDefaultValueButton()
|
2017-10-21 10:12:11 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Y;
|
|
|
|
Width = SettingsOverlay.CONTENT_MARGINS;
|
|
|
|
Alpha = 0f;
|
|
|
|
}
|
|
|
|
|
2017-10-22 13:40:41 +08:00
|
|
|
public string TooltipText => "Revert to default";
|
|
|
|
|
2017-10-21 10:12:11 +08:00
|
|
|
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) => true;
|
|
|
|
|
|
|
|
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args) => true;
|
|
|
|
|
|
|
|
protected override bool OnClick(InputState state)
|
|
|
|
{
|
2017-10-26 16:15:20 +08:00
|
|
|
if (bindable != null && !bindable.Disabled)
|
|
|
|
bindable.SetDefault();
|
2017-10-21 10:12:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
{
|
|
|
|
hovering = true;
|
2017-10-21 13:35:37 +08:00
|
|
|
UpdateState();
|
2017-10-21 10:12:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
{
|
|
|
|
hovering = false;
|
2017-10-21 13:35:37 +08:00
|
|
|
UpdateState();
|
|
|
|
}
|
|
|
|
|
2017-11-21 10:49:42 +08:00
|
|
|
public void SetButtonColour(Color4 buttonColour)
|
2017-10-21 13:35:37 +08:00
|
|
|
{
|
2017-10-22 13:40:41 +08:00
|
|
|
this.buttonColour = buttonColour;
|
2017-10-21 13:35:37 +08:00
|
|
|
UpdateState();
|
2017-10-21 10:12:11 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 10:49:42 +08:00
|
|
|
public void UpdateState()
|
2017-10-21 13:35:37 +08:00
|
|
|
{
|
2017-10-26 16:15:20 +08:00
|
|
|
if (bindable == null)
|
|
|
|
return;
|
|
|
|
var colour = bindable.Disabled ? Color4.Gray : buttonColour;
|
|
|
|
this.FadeTo(bindable.IsDefault ? 0f : hovering && !bindable.Disabled ? 1f : 0.5f, 200, Easing.OutQuint);
|
2017-10-22 13:40:41 +08:00
|
|
|
this.FadeColour(ColourInfo.GradientHorizontal(colour.Opacity(0.8f), colour.Opacity(0)), 200, Easing.OutQuint);
|
2017-10-21 13:35:37 +08:00
|
|
|
}
|
2017-10-21 10:12:11 +08:00
|
|
|
}
|
2017-05-04 22:07:24 +08:00
|
|
|
}
|
|
|
|
}
|