1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 11:22:55 +08:00
osu-lazer/osu.Game/Graphics/Containers/UserDimContainer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
4.3 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.
using System;
using osu.Framework.Allocation;
2019-02-22 19:44:02 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Configuration;
using osu.Game.Screens.Play;
namespace osu.Game.Graphics.Containers
{
/// <summary>
/// A container that applies user-configured visual settings to its contents.
/// </summary>
2019-07-11 12:17:28 +08:00
public abstract partial class UserDimContainer : Container
{
/// <summary>
/// Amount of lightening to apply to current dim level during break times.
/// </summary>
2019-12-12 19:51:58 +08:00
public const float BREAK_LIGHTEN_AMOUNT = 0.3f;
public const double BACKGROUND_FADE_DURATION = 800;
/// <summary>
/// Whether or not user-configured settings relating to brightness of elements should be ignored.
/// </summary>
/// <remarks>
/// For best or worst, this also bypasses storyboard disable. Not sure this is correct but leaving it as to not break anything.
/// </remarks>
public readonly Bindable<bool> IgnoreUserSettings = new Bindable<bool>();
/// <summary>
/// Whether player is in break time.
/// Must be bound to <see cref="BreakTracker.IsBreakTime"/> to allow for dim adjustments in gameplay.
/// </summary>
public readonly IBindable<bool> IsBreakTime = new Bindable<bool>();
/// <summary>
/// Whether the content of this container is currently being displayed.
/// </summary>
public bool ContentDisplayed { get; private set; }
2022-11-02 14:09:40 +08:00
protected Bindable<double> UserDimLevel { get; private set; } = null!;
2022-11-02 14:11:49 +08:00
/// <summary>
2022-11-02 16:49:52 +08:00
/// The amount of dim to be used when <see cref="IgnoreUserSettings"/> is <c>true</c>.
2022-11-02 14:11:49 +08:00
/// </summary>
2023-10-06 15:51:24 +08:00
public Bindable<float> DimWhenUserSettingsIgnored { get; } = new Bindable<float>();
2022-10-25 12:00:53 +08:00
2022-11-02 14:09:40 +08:00
protected Bindable<bool> LightenDuringBreaks { get; private set; } = null!;
2022-11-02 14:09:40 +08:00
protected Bindable<bool> ShowStoryboard { get; private set; } = null!;
2019-12-12 19:51:58 +08:00
private float breakLightening => LightenDuringBreaks.Value && IsBreakTime.Value ? BREAK_LIGHTEN_AMOUNT : 0;
2019-12-17 11:52:55 +08:00
protected virtual float DimLevel => Math.Max(!IgnoreUserSettings.Value ? (float)UserDimLevel.Value - breakLightening : DimWhenUserSettingsIgnored.Value, 0);
2019-08-09 03:13:48 +08:00
2019-07-12 10:38:15 +08:00
protected override Container<Drawable> Content => dimContent;
2019-07-12 10:38:15 +08:00
private Container dimContent { get; }
/// <summary>
2019-03-19 12:06:14 +08:00
/// Creates a new <see cref="UserDimContainer"/>.
/// </summary>
2019-07-11 12:17:28 +08:00
protected UserDimContainer()
{
2019-07-12 10:38:15 +08:00
AddInternal(dimContent = new Container { RelativeSizeAxes = Axes.Both });
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
UserDimLevel = config.GetBindable<double>(OsuSetting.DimLevel);
LightenDuringBreaks = config.GetBindable<bool>(OsuSetting.LightenDuringBreaks);
ShowStoryboard = config.GetBindable<bool>(OsuSetting.ShowStoryboard);
2019-03-20 18:38:53 +08:00
2019-07-11 12:17:28 +08:00
UserDimLevel.ValueChanged += _ => UpdateVisuals();
2022-11-02 16:49:52 +08:00
DimWhenUserSettingsIgnored.ValueChanged += _ => UpdateVisuals();
LightenDuringBreaks.ValueChanged += _ => UpdateVisuals();
IsBreakTime.ValueChanged += _ => UpdateVisuals();
2019-07-11 12:17:28 +08:00
ShowStoryboard.ValueChanged += _ => UpdateVisuals();
IgnoreUserSettings.ValueChanged += _ => UpdateVisuals();
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-07-11 12:17:28 +08:00
UpdateVisuals();
}
/// <summary>
2019-07-12 10:38:15 +08:00
/// Whether the content of this container should currently be visible.
/// </summary>
2019-08-09 03:13:48 +08:00
protected virtual bool ShowDimContent => true;
2019-07-12 10:38:15 +08:00
/// <summary>
/// Should be invoked when any dependent dim level or user setting is changed and bring the visual state up-to-date.
/// </summary>
protected virtual void UpdateVisuals()
{
2019-08-08 22:29:52 +08:00
ContentDisplayed = ShowDimContent;
dimContent.FadeTo(ContentDisplayed ? 1 : 0, BACKGROUND_FADE_DURATION, Easing.OutQuint);
dimContent.FadeColour(OsuColour.Gray(1f - DimLevel), BACKGROUND_FADE_DURATION, Easing.OutQuint);
}
}
}