2019-02-20 07:53:57 +00: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.
2019-03-20 11:12:46 +00:00
using System ;
2019-02-20 07:53:57 +00:00
using osu.Framework.Allocation ;
2019-02-22 11:44:02 +00:00
using osu.Framework.Bindables ;
2019-02-20 07:53:57 +00:00
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using osu.Game.Configuration ;
2019-03-14 05:02:46 +00:00
using osu.Game.Graphics.Backgrounds ;
2019-03-27 10:29:27 +00:00
using osu.Game.Screens.Play ;
2019-03-13 07:47:03 +00:00
using osuTK ;
2019-02-20 07:53:57 +00:00
using osuTK.Graphics ;
namespace osu.Game.Graphics.Containers
{
2019-02-24 11:03:24 +00:00
/// <summary>
2019-03-13 07:47:03 +00:00
/// A container that applies user-configured visual settings to its contents.
2019-02-24 11:03:24 +00:00
/// This container specifies behavior that applies to both Storyboards and Backgrounds.
/// </summary>
2019-03-19 04:06:14 +00:00
public class UserDimContainer : Container
2019-02-20 07:53:57 +00:00
{
2019-02-28 11:01:15 +00:00
private const float background_fade_duration = 800 ;
2019-02-24 11:03:24 +00:00
/// <summary>
/// Whether or not user-configured dim levels should be applied to the container.
/// </summary>
2019-03-19 11:15:28 +00:00
public readonly Bindable < bool > EnableUserDim = new Bindable < bool > ( ) ;
2019-02-24 11:03:24 +00:00
/// <summary>
/// Whether or not the storyboard loaded should completely hide the background behind it.
/// </summary>
2019-02-28 09:25:58 +00:00
public readonly Bindable < bool > StoryboardReplacesBackground = new Bindable < bool > ( ) ;
2019-02-24 11:03:24 +00:00
2019-03-19 11:15:28 +00:00
/// <summary>
/// The amount of blur to be applied to the background in addition to user-specified blur.
/// </summary>
/// <remarks>
/// Used in contexts where there can potentially be both user and screen-specified blurring occuring at the same time, such as in <see cref="PlayerLoader"/>
/// </remarks>
2019-03-20 05:17:35 +00:00
public readonly Bindable < float > BlurAmount = new Bindable < float > ( ) ;
2019-03-19 11:15:28 +00:00
2019-03-20 10:38:53 +00:00
private Bindable < double > userDimLevel { get ; set ; }
2019-03-19 11:15:28 +00:00
2019-03-20 10:38:53 +00:00
private Bindable < double > userBlurLevel { get ; set ; }
2019-03-19 11:15:28 +00:00
private Bindable < bool > showStoryboard { get ; set ; }
2019-03-19 04:06:14 +00:00
protected Container DimContainer { get ; }
2019-02-24 11:03:24 +00:00
2019-03-19 04:06:14 +00:00
protected override Container < Drawable > Content = > DimContainer ;
2019-02-21 09:14:58 +00:00
private readonly bool isStoryboard ;
2019-03-20 11:12:46 +00:00
/// <summary>
/// As an optimisation, we add the two blur portions to be applied rather than actually applying two separate blurs.
/// </summary>
2019-03-19 11:15:28 +00:00
private Vector2 blurTarget = > EnableUserDim . Value
2019-03-20 10:38:53 +00:00
? new Vector2 ( BlurAmount . Value + ( float ) userBlurLevel . Value * 25 )
2019-03-20 05:17:35 +00:00
: new Vector2 ( BlurAmount . Value ) ;
2019-02-24 11:03:24 +00:00
/// <summary>
2019-03-19 04:06:14 +00:00
/// Creates a new <see cref="UserDimContainer"/>.
2019-02-24 11:03:24 +00:00
/// </summary>
2019-03-13 07:47:03 +00:00
/// <param name="isStoryboard"> Whether or not this instance contains a storyboard.
2019-02-28 11:30:23 +00:00
/// <remarks>
/// While both backgrounds and storyboards allow user dim levels to be applied, storyboards can be toggled via <see cref="showStoryboard"/>
2019-03-13 07:47:03 +00:00
/// and can cause backgrounds to become hidden via <see cref="StoryboardReplacesBackground"/>. Storyboards are also currently unable to be blurred.
2019-02-28 11:30:23 +00:00
/// </remarks>
2019-02-24 11:03:24 +00:00
/// </param>
2019-03-19 04:06:14 +00:00
public UserDimContainer ( bool isStoryboard = false )
2019-02-21 09:14:58 +00:00
{
this . isStoryboard = isStoryboard ;
2019-03-19 04:06:14 +00:00
AddInternal ( DimContainer = new Container { RelativeSizeAxes = Axes . Both } ) ;
2019-02-21 09:14:58 +00:00
}
2019-02-20 07:53:57 +00:00
2019-03-20 11:12:46 +00:00
private Background background ;
public Background Background
2019-03-20 05:17:35 +00:00
{
2019-03-20 11:12:46 +00:00
get = > background ;
set
2019-03-20 05:58:32 +00:00
{
2019-03-20 11:18:08 +00:00
base . Add ( background = value ) ;
2019-03-20 10:38:53 +00:00
background . BlurTo ( blurTarget , 0 , Easing . OutQuint ) ;
2019-03-20 05:58:32 +00:00
}
2019-03-20 11:12:46 +00:00
}
public override void Add ( Drawable drawable )
{
if ( drawable is Background )
throw new InvalidOperationException ( $"Use {nameof(Background)} to set a background." ) ;
2019-03-20 05:55:52 +00:00
base . Add ( drawable ) ;
2019-03-20 05:17:35 +00:00
}
2019-02-20 07:53:57 +00:00
[BackgroundDependencyLoader]
private void load ( OsuConfigManager config )
{
2019-03-20 10:38:53 +00:00
userDimLevel = config . GetBindable < double > ( OsuSetting . DimLevel ) ;
userBlurLevel = config . GetBindable < double > ( OsuSetting . BlurLevel ) ;
2019-02-28 11:30:23 +00:00
showStoryboard = config . GetBindable < bool > ( OsuSetting . ShowStoryboard ) ;
2019-03-20 10:38:53 +00:00
2019-03-20 05:17:35 +00:00
EnableUserDim . ValueChanged + = _ = > updateVisuals ( ) ;
2019-03-20 10:38:53 +00:00
userDimLevel . ValueChanged + = _ = > updateVisuals ( ) ;
userBlurLevel . ValueChanged + = _ = > updateVisuals ( ) ;
2019-03-20 05:17:35 +00:00
showStoryboard . ValueChanged + = _ = > updateVisuals ( ) ;
StoryboardReplacesBackground . ValueChanged + = _ = > updateVisuals ( ) ;
BlurAmount . ValueChanged + = _ = > updateVisuals ( ) ;
2019-02-20 07:53:57 +00:00
}
2019-02-24 11:03:24 +00:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2019-03-20 05:17:35 +00:00
updateVisuals ( ) ;
2019-02-24 11:03:24 +00:00
}
2019-03-20 05:17:35 +00:00
private void updateVisuals ( )
2019-02-20 07:53:57 +00:00
{
2019-02-21 09:14:58 +00:00
if ( isStoryboard )
{
2019-03-20 10:38:53 +00:00
DimContainer . FadeTo ( ! showStoryboard . Value | | userDimLevel . Value = = 1 ? 0 : 1 , background_fade_duration , Easing . OutQuint ) ;
2019-02-21 09:14:58 +00:00
}
else
{
2019-02-24 11:03:24 +00:00
// The background needs to be hidden in the case of it being replaced by the storyboard
2019-03-19 04:06:14 +00:00
DimContainer . FadeTo ( showStoryboard . Value & & StoryboardReplacesBackground . Value ? 0 : 1 , background_fade_duration , Easing . OutQuint ) ;
2019-03-13 07:47:03 +00:00
2019-03-20 11:12:46 +00:00
Background ? . BlurTo ( blurTarget , background_fade_duration , Easing . OutQuint ) ;
2019-02-21 09:14:58 +00:00
}
2019-03-20 10:38:53 +00:00
DimContainer . FadeColour ( EnableUserDim . Value ? OsuColour . Gray ( 1 - ( float ) userDimLevel . Value ) : Color4 . White , background_fade_duration , Easing . OutQuint ) ;
2019-02-20 07:53:57 +00:00
}
}
}