1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 12:43:06 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Objects/Drawables/Pieces/BodyPiece.cs

161 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;
using osu.Framework.Caching;
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;
using osu.Game.Graphics;
namespace osu.Game.Rulesets.Mania.Objects.Drawables.Pieces
{
/// <summary>
/// Represents length-wise portion of a hold note.
/// </summary>
2018-11-19 17:59:05 +08:00
public class BodyPiece : Container, IHasAccentColour
2018-04-13 17:19:50 +08:00
{
private readonly Container subtractionLayer;
protected readonly Drawable Background;
protected readonly BufferedContainer Foreground;
2018-04-13 17:19:50 +08:00
private readonly BufferedContainer subtractionContainer;
public BodyPiece()
{
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive;
2018-04-13 17:19:50 +08:00
Children = new[]
{
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
}
}
}
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
updateAccentColour();
}
private Color4 accentColour;
2018-04-13 17:19:50 +08:00
public Color4 AccentColour
{
get => accentColour;
2018-04-13 17:19:50 +08:00
set
{
if (accentColour == value)
return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
accentColour = value;
updateAccentColour();
}
}
public bool Hitting
{
get => hitting;
set
{
hitting = value;
updateAccentColour();
}
}
private readonly Cached subtractionCache = new Cached();
2018-04-13 17:19:50 +08:00
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
{
if ((invalidation & Invalidation.DrawSize) > 0)
subtractionCache.Invalidate();
return base.Invalidate(invalidation, source, shallPropagate);
}
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();
}
}
private bool hitting;
2018-04-13 17:19:50 +08:00
private void updateAccentColour()
{
if (!IsLoaded)
return;
Foreground.Colour = AccentColour.Opacity(0.5f);
Background.Colour = AccentColour.Opacity(0.7f);
const float animation_length = 50;
Foreground.ClearTransforms(false, nameof(Foreground.Colour));
2019-04-01 11:16:05 +08:00
if (hitting)
{
// wait for the next sync point
double synchronisedOffset = animation_length * 2 - Time.Current % (animation_length * 2);
using (Foreground.BeginDelayedSequence(synchronisedOffset))
Foreground.FadeColour(AccentColour.Lighten(0.2f), animation_length).Then().FadeColour(Foreground.Colour, animation_length).Loop();
}
2018-04-13 17:19:50 +08:00
subtractionCache.Invalidate();
}
}
}