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

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

167 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
2017-09-11 03:21:22 +08:00
using System;
2020-03-31 15:42:35 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2017-09-11 03:21:22 +08:00
using osu.Framework.Extensions.Color4Extensions;
2017-05-09 19:55:20 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
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
{
2017-05-09 19:55:20 +08:00
/// <summary>
/// Represents length-wise portion of a hold note.
/// </summary>
public partial class DefaultBodyPiece : CompositeDrawable, IHoldNoteBody
{
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; } = null!;
private Container foregroundContainer = null!;
2018-04-13 17:19:50 +08:00
2020-03-31 15:42:35 +08:00
public DefaultBodyPiece()
{
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive;
2020-03-31 15:42:35 +08:00
}
[BackgroundDependencyLoader(true)]
private void load(DrawableHitObject? drawableObject)
2020-03-31 15:42:35 +08:00
{
InternalChildren = new[]
{
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 partial 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 = null!;
private BufferedContainer subtractionBuffer = null!;
private Container subtractionLayer = null!;
public ForegroundPiece()
{
RelativeSizeAxes = Axes.Both;
AddLayout(subtractionCache);
}
[BackgroundDependencyLoader]
private void load()
{
InternalChild = foregroundBuffer = new BufferedContainer(cachedFrameBuffer: true)
{
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive,
RelativeSizeAxes = Axes.Both,
2017-09-11 03:21:22 +08:00
Children = new Drawable[]
{
new Box { RelativeSizeAxes = Axes.Both },
subtractionBuffer = new BufferedContainer(cachedFrameBuffer: true)
2017-09-11 03:21:22 +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
2017-09-11 07:59:56 +08:00
Blending = new BlendingParameters { AlphaEquation = BlendingEquation.ReverseSubtract },
2017-09-11 03:21:22 +08:00
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()
2017-09-11 03:21:22 +08:00
{
base.Update();
if (!subtractionCache.IsValid)
2017-09-11 03:21:22 +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();
}
}
}
}
}