osu/osu.Game.Rulesets.Mania/Skinning/LegacyBodyPiece.cs

169 lines
5.8 KiB
C#
Raw Normal View History

2020-03-31 07:42:35 +00: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.
2020-08-26 11:21:56 +00:00
using System;
2020-08-27 14:16:54 +00:00
using JetBrains.Annotations;
2020-03-31 07:42:35 +00:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Animations;
using osu.Framework.Graphics.OpenGL.Textures;
2020-03-31 07:42:35 +00:00
using osu.Game.Rulesets.Mania.Objects.Drawables;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI.Scrolling;
using osu.Game.Skinning;
using osuTK;
namespace osu.Game.Rulesets.Mania.Skinning
{
public class LegacyBodyPiece : LegacyManiaColumnElement
{
private readonly IBindable<ScrollingDirection> direction = new Bindable<ScrollingDirection>();
private readonly IBindable<bool> isHitting = new Bindable<bool>();
2020-08-27 14:16:54 +00:00
[CanBeNull]
2020-08-26 11:21:56 +00:00
private Drawable bodySprite;
2020-08-27 14:16:54 +00:00
[CanBeNull]
2020-08-26 11:21:56 +00:00
private Drawable lightContainer;
2020-08-27 14:16:54 +00:00
[CanBeNull]
2020-08-26 11:21:56 +00:00
private Drawable light;
2020-03-31 07:42:35 +00:00
public LegacyBodyPiece()
{
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load(ISkinSource skin, IScrollingInfo scrollingInfo, DrawableHitObject drawableObject)
{
string imageName = GetColumnSkinConfig<string>(skin, LegacyManiaSkinConfigurationLookups.HoldNoteBodyImage)?.Value
2020-03-31 07:42:35 +00:00
?? $"mania-note{FallbackColumnIndex}L";
2020-08-26 11:21:56 +00:00
string lightImage = GetColumnSkinConfig<string>(skin, LegacyManiaSkinConfigurationLookups.HoldNoteLightImage)?.Value
?? "lightingL";
float lightScale = GetColumnSkinConfig<float>(skin, LegacyManiaSkinConfigurationLookups.HoldNoteLightScale)?.Value
?? 1;
// Create a temporary animation to retrieve the number of frames, in an effort to calculate the intended frame length.
// This animation is discarded and re-queried with the appropriate frame length afterwards.
var tmp = skin.GetAnimation(lightImage, true, false);
double frameLength = 0;
if (tmp is IFramedAnimation tmpAnimation && tmpAnimation.FrameCount > 0)
frameLength = Math.Max(1000 / 60.0, 170.0 / tmpAnimation.FrameCount);
light = skin.GetAnimation(lightImage, true, true, frameLength: frameLength).With(d =>
{
if (d == null)
return;
d.Origin = Anchor.Centre;
d.Blending = BlendingParameters.Additive;
d.Scale = new Vector2(lightScale);
});
if (light != null)
2020-08-26 11:46:12 +00:00
{
lightContainer = new HitTargetInsetContainer
{
Alpha = 0,
Child = light
};
}
2020-08-26 11:21:56 +00:00
bodySprite = skin.GetAnimation(imageName, WrapMode.ClampToEdge, WrapMode.ClampToEdge, true, true).With(d =>
2020-03-31 07:42:35 +00:00
{
if (d == null)
return;
if (d is TextureAnimation animation)
animation.IsPlaying = false;
d.Anchor = Anchor.TopCentre;
d.RelativeSizeAxes = Axes.Both;
d.Size = Vector2.One;
d.FillMode = FillMode.Stretch;
// Todo: Wrap
});
2020-08-26 11:21:56 +00:00
if (bodySprite != null)
InternalChild = bodySprite;
2020-03-31 07:42:35 +00:00
direction.BindTo(scrollingInfo.Direction);
direction.BindValueChanged(onDirectionChanged, true);
var holdNote = (DrawableHoldNote)drawableObject;
isHitting.BindTo(holdNote.IsHitting);
isHitting.BindValueChanged(onIsHittingChanged, true);
}
private void onIsHittingChanged(ValueChangedEvent<bool> isHitting)
{
2020-08-26 11:21:56 +00:00
if (bodySprite is TextureAnimation bodyAnimation)
{
bodyAnimation.GotoFrame(0);
bodyAnimation.IsPlaying = isHitting.NewValue;
}
2020-03-31 07:42:35 +00:00
2020-08-27 14:09:54 +00:00
if (lightContainer == null)
return;
if (isHitting.NewValue)
2020-08-26 11:21:56 +00:00
{
2020-08-27 14:09:54 +00:00
// Clear the fade out and, more importantly, the removal.
lightContainer.ClearTransforms();
2020-08-26 11:46:12 +00:00
2020-08-27 14:09:54 +00:00
// Only add the container if the removal has taken place.
if (lightContainer.Parent == null)
Column.TopLevelContainer.Add(lightContainer);
2020-08-26 11:21:56 +00:00
2020-08-27 14:09:54 +00:00
// The light must be seeked only after being loaded, otherwise a nullref occurs (https://github.com/ppy/osu-framework/issues/3847).
if (light is TextureAnimation lightAnimation)
lightAnimation.GotoFrame(0);
2020-08-26 11:46:12 +00:00
2020-08-27 14:09:54 +00:00
lightContainer.FadeIn(80);
}
else
{
lightContainer.FadeOut(120)
.OnComplete(d => Column.TopLevelContainer.Remove(d));
2020-08-26 11:21:56 +00:00
}
2020-03-31 07:42:35 +00:00
}
private void onDirectionChanged(ValueChangedEvent<ScrollingDirection> direction)
{
if (direction.NewValue == ScrollingDirection.Up)
{
2020-08-26 11:21:56 +00:00
if (bodySprite != null)
{
bodySprite.Origin = Anchor.BottomCentre;
bodySprite.Scale = new Vector2(1, -1);
}
if (light != null)
light.Anchor = Anchor.TopCentre;
2020-03-31 07:42:35 +00:00
}
else
{
2020-08-26 11:21:56 +00:00
if (bodySprite != null)
{
bodySprite.Origin = Anchor.TopCentre;
bodySprite.Scale = Vector2.One;
}
if (light != null)
light.Anchor = Anchor.BottomCentre;
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
lightContainer?.Expire();
}
2020-03-31 07:42:35 +00:00
}
}