2020-07-16 16:26:18 +08: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.
2024-02-06 22:58:27 +08:00
using System ;
2024-02-15 23:40:58 +08:00
using System.ComponentModel ;
2020-07-16 16:26:18 +08:00
using osu.Framework.Allocation ;
using osu.Framework.Bindables ;
using osu.Framework.Extensions.Color4Extensions ;
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Colour ;
using osu.Framework.Graphics.Containers ;
using osu.Framework.Graphics.Shapes ;
2024-02-06 22:58:27 +08:00
using osu.Framework.Utils ;
2020-07-16 16:26:18 +08:00
using osu.Game.Rulesets.UI.Scrolling ;
2020-07-16 20:29:39 +08:00
using osuTK ;
2020-07-16 16:26:18 +08:00
using osuTK.Graphics ;
2024-02-15 23:40:58 +08:00
using Container = osu . Framework . Graphics . Containers . Container ;
2020-07-16 16:26:18 +08:00
namespace osu.Game.Rulesets.Mania.UI
{
/// <summary>
2024-02-15 23:40:58 +08:00
/// A <see cref="Framework.Graphics.Containers.Container"/> that has its contents partially hidden by an adjustable "cover". This is intended to be used in a playfield.
2020-07-16 16:26:18 +08:00
/// </summary>
2020-07-16 20:18:24 +08:00
public partial class PlayfieldCoveringWrapper : CompositeDrawable
2020-07-16 16:26:18 +08:00
{
2024-02-06 22:32:54 +08:00
/// <summary>
/// The relative area that should be completely covered. This does not include the fade.
/// </summary>
public readonly BindableFloat Coverage = new BindableFloat ( ) ;
2020-07-16 16:26:18 +08:00
/// <summary>
/// The complete cover, including gradient and fill.
/// </summary>
2020-07-16 20:29:39 +08:00
private readonly Drawable cover ;
2020-07-16 16:26:18 +08:00
/// <summary>
/// The gradient portion of the cover.
/// </summary>
private readonly Box gradient ;
/// <summary>
/// The fully-opaque portion of the cover.
/// </summary>
private readonly Box filled ;
private readonly IBindable < ScrollingDirection > scrollDirection = new Bindable < ScrollingDirection > ( ) ;
2024-02-15 21:05:25 +08:00
private float currentCoverageHeight ;
2024-02-07 23:20:32 +08:00
2020-07-16 20:18:24 +08:00
public PlayfieldCoveringWrapper ( Drawable content )
2020-07-16 16:26:18 +08:00
{
InternalChild = new BufferedContainer
{
RelativeSizeAxes = Axes . Both ,
Children = new [ ]
{
2020-07-16 20:17:51 +08:00
content ,
2020-07-16 20:29:39 +08:00
cover = new Container
2020-07-16 16:26:18 +08:00
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
RelativeSizeAxes = Axes . Both ,
Blending = new BlendingParameters
{
// Don't change the destination colour.
RGBEquation = BlendingEquation . Add ,
Source = BlendingType . Zero ,
Destination = BlendingType . One ,
// Subtract the cover's alpha from the destination (points with alpha 1 should make the destination completely transparent).
AlphaEquation = BlendingEquation . Add ,
SourceAlpha = BlendingType . Zero ,
DestinationAlpha = BlendingType . OneMinusSrcAlpha
} ,
Children = new Drawable [ ]
{
gradient = new Box
{
Anchor = Anchor . BottomLeft ,
Origin = Anchor . BottomLeft ,
RelativeSizeAxes = Axes . Both ,
RelativePositionAxes = Axes . Both ,
Height = 0.25f ,
Colour = ColourInfo . GradientVertical (
Color4 . White . Opacity ( 0f ) ,
Color4 . White . Opacity ( 1f )
)
} ,
filled = new Box
{
Anchor = Anchor . BottomLeft ,
Origin = Anchor . BottomLeft ,
2020-07-16 16:35:00 +08:00
RelativeSizeAxes = Axes . Both ,
Height = 0
2020-07-16 16:26:18 +08:00
}
}
}
}
} ;
}
[BackgroundDependencyLoader]
private void load ( IScrollingInfo scrollingInfo )
{
scrollDirection . BindTo ( scrollingInfo . Direction ) ;
scrollDirection . BindValueChanged ( onScrollDirectionChanged , true ) ;
}
2024-02-06 22:32:54 +08:00
protected override void LoadComplete ( )
2020-07-16 16:26:18 +08:00
{
2024-02-06 22:32:54 +08:00
base . LoadComplete ( ) ;
2024-02-15 21:05:25 +08:00
updateCoverSize ( true ) ;
2024-02-06 22:58:27 +08:00
}
protected override void Update ( )
{
base . Update ( ) ;
2024-02-15 21:05:25 +08:00
updateCoverSize ( false ) ;
2024-02-06 22:58:27 +08:00
}
2024-02-15 21:05:25 +08:00
private void updateCoverSize ( bool instant )
2024-02-06 22:58:27 +08:00
{
2024-02-15 21:05:25 +08:00
float targetCoverage ;
float targetAlpha ;
if ( instant )
{
targetCoverage = Coverage . Value ;
targetAlpha = Coverage . Value > 0 ? 1 : 0 ;
}
else
{
targetCoverage = ( float ) Interpolation . DampContinuously ( currentCoverageHeight , Coverage . Value , 25 , Math . Abs ( Time . Elapsed ) ) ;
targetAlpha = ( float ) Interpolation . DampContinuously ( gradient . Alpha , Coverage . Value > 0 ? 1 : 0 , 25 , Math . Abs ( Time . Elapsed ) ) ;
}
filled . Height = GetHeight ( targetCoverage ) ;
gradient . Y = - GetHeight ( targetCoverage ) ;
gradient . Alpha = targetAlpha ;
2024-02-07 23:20:32 +08:00
2024-02-15 21:05:25 +08:00
currentCoverageHeight = targetCoverage ;
2020-07-16 16:26:18 +08:00
}
2020-07-16 20:29:39 +08:00
2024-02-07 23:20:32 +08:00
protected virtual float GetHeight ( float coverage ) = > coverage ;
2024-02-06 22:32:54 +08:00
private void onScrollDirectionChanged ( ValueChangedEvent < ScrollingDirection > direction )
= > cover . Rotation = direction . NewValue = = ScrollingDirection . Up ? 0 : 180f ;
2020-07-16 20:29:39 +08:00
/// <summary>
/// The direction in which the cover expands.
/// </summary>
public CoverExpandDirection Direction
{
set = > cover . Scale = value = = CoverExpandDirection . AlongScroll ? Vector2 . One : new Vector2 ( 1 , - 1 ) ;
}
}
public enum CoverExpandDirection
{
/// <summary>
/// The cover expands along the scrolling direction.
/// </summary>
2024-02-15 23:40:58 +08:00
[Description("Along scroll")]
2020-07-16 20:29:39 +08:00
AlongScroll ,
/// <summary>
/// The cover expands against the scrolling direction.
/// </summary>
2024-02-15 23:40:58 +08:00
[Description("Against scroll")]
2020-07-16 20:29:39 +08:00
AgainstScroll
2020-07-16 16:26:18 +08:00
}
}