1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 20:22:56 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Objects/Drawables/Pieces/DefaultBodyPiece.cs

139 lines
5.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.
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-11-20 15:51:59 +08:00
using osuTK.Graphics;
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-03-31 15:42:35 +08:00
using osu.Game.Rulesets.Objects.Drawables;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Mania.Objects.Drawables.Pieces
{
/// <summary>
/// Represents length-wise portion of a hold note.
/// </summary>
2020-03-31 15:42:35 +08:00
public class DefaultBodyPiece : CompositeDrawable
2018-04-13 17:19:50 +08:00
{
2020-03-31 15:42:35 +08:00
protected readonly Bindable<Color4> AccentColour = new Bindable<Color4>();
2018-04-13 17:19:50 +08:00
2020-03-31 15:42:35 +08:00
private readonly LayoutValue subtractionCache = new LayoutValue(Invalidation.DrawSize);
private readonly IBindable<bool> isHitting = new Bindable<bool>();
protected Drawable Background { get; private set; }
protected BufferedContainer Foreground { get; private set; }
private BufferedContainer subtractionContainer;
private Container subtractionLayer;
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
{
2020-03-31 15:42:35 +08:00
RelativeSizeAxes = Axes.Both;
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive;
2018-04-13 17:19:50 +08:00
2020-03-31 15:42:35 +08:00
AddLayout(subtractionCache);
}
[BackgroundDependencyLoader(true)]
private void load([CanBeNull] DrawableHitObject drawableObject)
{
InternalChildren = new[]
2018-04-13 17:19:50 +08:00
{
Background = new Box { RelativeSizeAxes = Axes.Both },
Foreground = new BufferedContainer
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,
CacheDrawnFrameBuffer = true,
Children = new Drawable[]
{
new Box { RelativeSizeAxes = Axes.Both },
subtractionContainer = new BufferedContainer
{
RelativeSizeAxes = Axes.Both,
// This is needed because we're blending with another object
BackgroundColour = Color4.White.Opacity(0),
CacheDrawnFrameBuffer = true,
// 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-02-24 19:52:15 +08:00
2020-03-31 15:42:35 +08:00
if (drawableObject != null)
{
2020-03-31 15:57:58 +08:00
var holdNote = (DrawableHoldNote)drawableObject;
2020-03-31 15:42:35 +08:00
AccentColour.BindTo(drawableObject.AccentColour);
2020-03-31 15:57:58 +08:00
isHitting.BindTo(holdNote.IsHitting);
2020-03-31 15:42:35 +08:00
}
2018-04-13 17:19:50 +08:00
2020-03-31 15:57:58 +08:00
AccentColour.BindValueChanged(onAccentChanged, true);
2020-03-31 15:42:35 +08:00
isHitting.BindValueChanged(_ => onAccentChanged(new ValueChangedEvent<Color4>(AccentColour.Value, AccentColour.Value)), true);
2018-04-13 17:19:50 +08:00
}
2020-03-31 15:42:35 +08:00
private void onAccentChanged(ValueChangedEvent<Color4> accent)
2018-04-13 17:19:50 +08:00
{
2020-03-31 15:42:35 +08:00
Foreground.Colour = accent.NewValue.Opacity(0.5f);
Background.Colour = accent.NewValue.Opacity(0.7f);
2019-02-28 12:31:40 +08:00
2020-03-31 15:42:35 +08:00
const float animation_length = 50;
2018-04-13 17:19:50 +08:00
2020-03-31 15:42:35 +08:00
Foreground.ClearTransforms(false, nameof(Foreground.Colour));
2018-04-13 17:19:50 +08:00
2020-03-31 15:42:35 +08:00
if (isHitting.Value)
{
2020-03-31 15:42:35 +08:00
// wait for the next sync point
double synchronisedOffset = animation_length * 2 - Time.Current % (animation_length * 2);
using (Foreground.BeginDelayedSequence(synchronisedOffset))
Foreground.FadeColour(accent.NewValue.Lighten(0.2f), animation_length).Then().FadeColour(Foreground.Colour, animation_length).Loop();
}
2020-03-31 15:42:35 +08:00
subtractionCache.Invalidate();
}
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
if (!subtractionCache.IsValid)
{
subtractionLayer.Width = 5;
subtractionLayer.Height = Math.Max(0, DrawHeight - DrawWidth);
subtractionLayer.EdgeEffect = new EdgeEffectParameters
{
Colour = Color4.White,
Type = EdgeEffectType.Glow,
Radius = DrawWidth
};
Foreground.ForceRedraw();
2018-04-13 17:19:50 +08:00
subtractionContainer.ForceRedraw();
subtractionCache.Validate();
}
}
}
}