osu/osu.Game/Skinning/LegacyHealthDisplay.cs

267 lines
8.0 KiB
C#
Raw Normal View History

2020-10-16 05:39:45 +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-10-16 05:54:46 +00:00
using System;
2020-10-16 05:39:45 +00:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2020-10-16 06:10:39 +00:00
using osu.Framework.Graphics.Textures;
2020-10-16 05:54:46 +00:00
using osu.Framework.Utils;
2020-10-16 05:39:45 +00:00
using osu.Game.Rulesets.Judgements;
using osu.Game.Screens.Play.HUD;
using osuTK;
2020-10-16 07:45:28 +00:00
using osuTK.Graphics;
2020-10-16 05:39:45 +00:00
namespace osu.Game.Skinning
{
public class LegacyHealthDisplay : CompositeDrawable, IHealthDisplay
{
private const double epic_cutoff = 0.5;
2020-10-16 05:39:45 +00:00
private readonly Skin skin;
2020-10-16 07:45:28 +00:00
private LegacyHealthPiece fill;
private LegacyHealthPiece marker;
2020-10-16 05:54:46 +00:00
private float maxFillWidth;
2020-10-16 07:18:29 +00:00
private bool isNewStyle;
2020-10-16 06:10:39 +00:00
2020-10-16 07:18:29 +00:00
public Bindable<double> Current { get; } = new BindableDouble(1)
{
MinValue = 0,
MaxValue = 1
};
2020-10-16 05:39:45 +00:00
public LegacyHealthDisplay(Skin skin)
{
this.skin = skin;
}
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.Both;
2020-10-16 07:18:29 +00:00
isNewStyle = getTexture(skin, "marker") != null;
// background implementation is the same for both versions.
AddInternal(new Sprite { Texture = getTexture(skin, "bg") });
2020-10-16 06:10:39 +00:00
2020-10-16 07:18:29 +00:00
if (isNewStyle)
2020-10-16 05:39:45 +00:00
{
2020-10-16 07:18:29 +00:00
AddRangeInternal(new[]
2020-10-16 05:39:45 +00:00
{
2020-10-16 07:18:29 +00:00
fill = new LegacyNewStyleFill(skin),
marker = new LegacyNewStyleMarker(skin),
});
}
else
{
AddRangeInternal(new[]
2020-10-16 05:39:45 +00:00
{
2020-10-16 07:18:29 +00:00
fill = new LegacyOldStyleFill(skin),
marker = new LegacyOldStyleMarker(skin),
});
}
2020-10-16 05:39:45 +00:00
2020-10-16 07:45:28 +00:00
fill.Current.BindTo(Current);
2020-10-16 07:18:29 +00:00
marker.Current.BindTo(Current);
2020-10-16 07:45:28 +00:00
2020-10-16 05:54:46 +00:00
maxFillWidth = fill.Width;
2020-10-16 05:39:45 +00:00
}
protected override void Update()
{
base.Update();
2020-10-16 05:54:46 +00:00
fill.Width = Interpolation.ValueAt(
Math.Clamp(Clock.ElapsedFrameTime, 0, 200),
fill.Width, (float)Current.Value * maxFillWidth, 0, 200, Easing.OutQuint);
2020-10-16 05:39:45 +00:00
marker.Position = fill.Position + new Vector2(fill.DrawWidth, fill.DrawHeight / 2);
}
2020-10-16 07:18:29 +00:00
public void Flash(JudgementResult result) => marker.Flash(result);
private static Texture getTexture(Skin skin, string name) => skin.GetTexture($"scorebar-{name}");
2020-10-16 07:45:28 +00:00
private static Color4 getFillColour(double hp)
{
if (hp < 0.2)
return Interpolation.ValueAt(0.2 - hp, Color4.Black, Color4.Red, 0, 0.2);
if (hp < epic_cutoff)
2020-10-16 07:45:28 +00:00
return Interpolation.ValueAt(0.5 - hp, Color4.White, Color4.Black, 0, 0.5);
return Color4.White;
}
2020-10-16 08:09:00 +00:00
public class LegacyOldStyleMarker : LegacyMarker
2020-10-16 07:18:29 +00:00
{
private readonly Texture normalTexture;
private readonly Texture dangerTexture;
private readonly Texture superDangerTexture;
2020-10-16 07:18:29 +00:00
public LegacyOldStyleMarker(Skin skin)
{
normalTexture = getTexture(skin, "ki");
dangerTexture = getTexture(skin, "kidanger");
superDangerTexture = getTexture(skin, "kidanger2");
2020-10-16 07:18:29 +00:00
}
2020-10-16 08:09:00 +00:00
public override Sprite CreateSprite() => new Sprite
{
Texture = normalTexture,
Origin = Anchor.Centre,
};
protected override void LoadComplete()
{
base.LoadComplete();
Current.BindValueChanged(hp =>
{
if (hp.NewValue < 0.2f)
2020-10-16 08:09:00 +00:00
Main.Texture = superDangerTexture;
else if (hp.NewValue < epic_cutoff)
2020-10-16 08:09:00 +00:00
Main.Texture = dangerTexture;
else
2020-10-16 08:09:00 +00:00
Main.Texture = normalTexture;
});
}
2020-10-16 07:18:29 +00:00
}
2020-10-16 08:09:00 +00:00
public class LegacyNewStyleMarker : LegacyMarker
2020-10-16 05:39:45 +00:00
{
2020-10-16 08:09:00 +00:00
private readonly Skin skin;
2020-10-16 07:45:28 +00:00
2020-10-16 07:18:29 +00:00
public LegacyNewStyleMarker(Skin skin)
{
2020-10-16 08:09:00 +00:00
this.skin = skin;
2020-10-16 07:18:29 +00:00
}
2020-10-16 05:39:45 +00:00
2020-10-16 08:09:00 +00:00
public override Sprite CreateSprite() => new Sprite
{
Texture = getTexture(skin, "marker"),
Origin = Anchor.Centre,
};
2020-10-16 07:45:28 +00:00
protected override void Update()
2020-10-16 05:39:45 +00:00
{
2020-10-16 07:45:28 +00:00
base.Update();
2020-10-16 08:09:00 +00:00
Main.Colour = getFillColour(Current.Value);
Main.Blending = Current.Value < epic_cutoff ? BlendingParameters.Inherit : BlendingParameters.Additive;
2020-10-16 07:18:29 +00:00
}
}
2020-10-16 07:45:28 +00:00
internal class LegacyOldStyleFill : LegacyHealthPiece
2020-10-16 07:18:29 +00:00
{
public LegacyOldStyleFill(Skin skin)
{
// required for sizing correctly..
var firstFrame = getTexture(skin, "colour-0");
2020-10-16 05:39:45 +00:00
2020-10-16 07:18:29 +00:00
if (firstFrame == null)
2020-10-16 05:39:45 +00:00
{
2020-10-16 07:18:29 +00:00
InternalChild = new Sprite { Texture = getTexture(skin, "colour") };
Size = InternalChild.Size;
2020-10-16 05:39:45 +00:00
}
else
{
2020-10-16 07:18:29 +00:00
InternalChild = skin.GetAnimation("scorebar-colour", true, true, startAtCurrentTime: false, applyConfigFrameRate: true) ?? Drawable.Empty();
Size = new Vector2(firstFrame.DisplayWidth, firstFrame.DisplayHeight);
2020-10-16 05:39:45 +00:00
}
2020-10-16 07:18:29 +00:00
Position = new Vector2(3, 10) * 1.6f;
Masking = true;
2020-10-16 05:39:45 +00:00
}
}
2020-10-16 06:10:39 +00:00
2020-10-16 07:45:28 +00:00
internal class LegacyNewStyleFill : LegacyHealthPiece
2020-10-16 07:18:29 +00:00
{
public LegacyNewStyleFill(Skin skin)
{
2020-10-16 07:45:28 +00:00
InternalChild = new Sprite
{
Texture = getTexture(skin, "colour"),
};
Size = InternalChild.Size;
2020-10-16 07:18:29 +00:00
Position = new Vector2(7.5f, 7.8f) * 1.6f;
2020-10-16 07:45:28 +00:00
Masking = true;
}
protected override void Update()
{
base.Update();
2020-10-16 08:09:00 +00:00
Colour = getFillColour(Current.Value);
}
}
public abstract class LegacyMarker : LegacyHealthPiece
{
protected Sprite Main;
private Sprite explode;
protected LegacyMarker()
{
Origin = Anchor.Centre;
}
[BackgroundDependencyLoader]
private void load()
{
InternalChildren = new Drawable[]
{
Main = CreateSprite(),
explode = CreateSprite().With(s =>
{
s.Alpha = 0;
s.Blending = BlendingParameters.Additive;
}),
};
}
public abstract Sprite CreateSprite();
protected override void LoadComplete()
{
base.LoadComplete();
Current.BindValueChanged(val =>
{
if (val.NewValue > val.OldValue)
bulgeMain();
});
2020-10-16 07:45:28 +00:00
}
2020-10-16 08:09:00 +00:00
public override void Flash(JudgementResult result)
{
bulgeMain();
bool isEpic = Current.Value >= epic_cutoff;
explode.Blending = isEpic ? BlendingParameters.Additive : BlendingParameters.Inherit;
explode.ScaleTo(1).Then().ScaleTo(isEpic ? 2 : 1.6f, 120);
2020-10-16 08:09:00 +00:00
explode.FadeOutFromOne(120);
}
private void bulgeMain() =>
Main.ScaleTo(1.4f).Then().ScaleTo(1, 200, Easing.Out);
2020-10-16 07:45:28 +00:00
}
public class LegacyHealthPiece : CompositeDrawable, IHealthDisplay
{
public Bindable<double> Current { get; } = new Bindable<double>();
public virtual void Flash(JudgementResult result)
{
2020-10-16 07:18:29 +00:00
}
}
2020-10-16 05:39:45 +00:00
}
}