1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 10:57:30 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Skinning/Default/DefaultBodyPiece.cs

170 lines
6.4 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 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-04-13 17:19:50 +08:00
using System;
2020-03-31 15:42:35 +08:00
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
2020-02-24 19:52:15 +08:00
using osu.Framework.Layout;
2020-12-07 11:32:52 +08:00
using osu.Game.Rulesets.Mania.Objects.Drawables;
2020-03-31 15:42:35 +08:00
using osu.Game.Rulesets.Objects.Drawables;
2020-12-07 11:32:52 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2020-12-07 11:32:52 +08:00
namespace osu.Game.Rulesets.Mania.Skinning.Default
2018-04-13 17:19:50 +08:00
{
/// <summary>
/// Represents length-wise portion of a hold note.
/// </summary>
public class DefaultBodyPiece : CompositeDrawable, IHoldNoteBody
2018-04-13 17:19:50 +08:00
{
2020-03-31 15:42:35 +08:00
protected readonly Bindable<Color4> AccentColour = new Bindable<Color4>();
protected readonly IBindable<bool> IsHitting = new Bindable<bool>();
2020-03-31 15:42:35 +08:00
protected Drawable Background { get; private set; }
private Container foregroundContainer;
2018-04-13 17:19:50 +08:00
2020-03-31 15:42:35 +08:00
public DefaultBodyPiece()
2018-04-13 17:19:50 +08:00
{
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive;
2020-03-31 15:42:35 +08:00
}
[BackgroundDependencyLoader(true)]
private void load([CanBeNull] DrawableHitObject drawableObject)
{
InternalChildren = new[]
2018-04-13 17:19:50 +08:00
{
Background = new Box { RelativeSizeAxes = Axes.Both },
foregroundContainer = new Container { RelativeSizeAxes = Axes.Both }
};
if (drawableObject != null)
{
var holdNote = (DrawableHoldNote)drawableObject;
AccentColour.BindTo(drawableObject.AccentColour);
IsHitting.BindTo(holdNote.IsHitting);
}
AccentColour.BindValueChanged(onAccentChanged, true);
Recycle();
}
public void Recycle() => foregroundContainer.Child = CreateForeground();
protected virtual Drawable CreateForeground() => new ForegroundPiece
{
AccentColour = { BindTarget = AccentColour },
IsHitting = { BindTarget = IsHitting }
};
private void onAccentChanged(ValueChangedEvent<Color4> accent) => Background.Colour = accent.NewValue.Opacity(0.7f);
private class ForegroundPiece : CompositeDrawable
{
public readonly Bindable<Color4> AccentColour = new Bindable<Color4>();
public readonly IBindable<bool> IsHitting = new Bindable<bool>();
private readonly LayoutValue subtractionCache = new LayoutValue(Invalidation.DrawSize);
private BufferedContainer foregroundBuffer;
private BufferedContainer subtractionBuffer;
private Container subtractionLayer;
public ForegroundPiece()
{
RelativeSizeAxes = Axes.Both;
AddLayout(subtractionCache);
}
[BackgroundDependencyLoader]
private void load()
{
InternalChild = foregroundBuffer = new BufferedContainer(cachedFrameBuffer: true)
2018-04-13 17:19:50 +08:00
{
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive,
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box { RelativeSizeAxes = Axes.Both },
subtractionBuffer = new BufferedContainer(cachedFrameBuffer: true)
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
// This is needed because we're blending with another object
BackgroundColour = Color4.White.Opacity(0),
// The 'hole' is achieved by subtracting the result of this container with the parent
Blending = new BlendingParameters { AlphaEquation = BlendingEquation.ReverseSubtract },
Child = subtractionLayer = new CircularContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
// Height computed in Update
Width = 1,
Masking = true,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true
}
}
}
}
};
2020-03-31 15:57:58 +08:00
AccentColour.BindValueChanged(onAccentChanged, true);
IsHitting.BindValueChanged(_ => onAccentChanged(new ValueChangedEvent<Color4>(AccentColour.Value, AccentColour.Value)), true);
2020-03-31 15:42:35 +08:00
}
2018-04-13 17:19:50 +08:00
private void onAccentChanged(ValueChangedEvent<Color4> accent)
{
foregroundBuffer.Colour = accent.NewValue.Opacity(0.5f);
2018-04-13 17:19:50 +08:00
const float animation_length = 50;
2019-02-28 12:31:40 +08:00
foregroundBuffer.ClearTransforms(false, nameof(foregroundBuffer.Colour));
2018-04-13 17:19:50 +08:00
if (IsHitting.Value)
{
// wait for the next sync point
double synchronisedOffset = animation_length * 2 - Time.Current % (animation_length * 2);
using (foregroundBuffer.BeginDelayedSequence(synchronisedOffset))
foregroundBuffer.FadeColour(accent.NewValue.Lighten(0.2f), animation_length).Then().FadeColour(foregroundBuffer.Colour, animation_length).Loop();
}
2018-04-13 17:19:50 +08:00
subtractionCache.Invalidate();
}
protected override void Update()
2018-04-13 17:19:50 +08:00
{
base.Update();
if (!subtractionCache.IsValid)
2018-04-13 17:19:50 +08:00
{
subtractionLayer.Width = 5;
subtractionLayer.Height = Math.Max(0, DrawHeight - DrawWidth);
subtractionLayer.EdgeEffect = new EdgeEffectParameters
{
Colour = Color4.White,
Type = EdgeEffectType.Glow,
Radius = DrawWidth
};
2018-04-13 17:19:50 +08:00
foregroundBuffer.ForceRedraw();
subtractionBuffer.ForceRedraw();
2018-04-13 17:19:50 +08:00
subtractionCache.Validate();
}
2018-04-13 17:19:50 +08:00
}
}
}
}