1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 04:32:55 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs

248 lines
9.0 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;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Osu.Objects.Drawables.Pieces;
using System.Linq;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Containers;
2018-11-14 13:29:22 +08:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Osu.Skinning;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Scoring;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-12-08 05:24:24 +08:00
using osu.Game.Skinning;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Objects.Drawables
{
public class DrawableSlider : DrawableOsuHitObject, IDrawableHitObjectWithProxiedApproach
{
public DrawableSliderHead HeadCircle => headContainer.Child;
public DrawableSliderTail TailCircle => tailContainer.Child;
2018-04-13 17:19:50 +08:00
public readonly SliderBall Ball;
public readonly SkinnableDrawable Body;
2018-04-13 17:19:50 +08:00
private PlaySliderBody sliderBody => Body.Drawable as PlaySliderBody;
2019-12-17 17:16:25 +08:00
private readonly Container<DrawableSliderHead> headContainer;
private readonly Container<DrawableSliderTail> tailContainer;
private readonly Container<DrawableSliderTick> tickContainer;
private readonly Container<DrawableRepeatPoint> repeatContainer;
private readonly Slider slider;
2018-11-09 12:58:46 +08:00
private readonly IBindable<Vector2> positionBindable = new Bindable<Vector2>();
private readonly IBindable<int> stackHeightBindable = new Bindable<int>();
private readonly IBindable<float> scaleBindable = new BindableFloat();
2018-04-13 17:19:50 +08:00
public DrawableSlider(Slider s)
: base(s)
{
slider = s;
Position = s.StackedPosition;
InternalChildren = new Drawable[]
{
Body = new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.SliderBody), _ => new DefaultSliderBody(), confineMode: ConfineMode.NoScaling),
tickContainer = new Container<DrawableSliderTick> { RelativeSizeAxes = Axes.Both },
repeatContainer = new Container<DrawableRepeatPoint> { RelativeSizeAxes = Axes.Both },
Ball = new SliderBall(s, this)
2018-04-13 17:19:50 +08:00
{
2019-01-21 09:57:14 +08:00
GetInitialHitAction = () => HeadCircle.HitAction,
2018-04-13 17:19:50 +08:00
BypassAutoSizeAxes = Axes.Both,
Scale = new Vector2(s.Scale),
AlwaysPresent = true,
Alpha = 0
},
headContainer = new Container<DrawableSliderHead> { RelativeSizeAxes = Axes.Both },
tailContainer = new Container<DrawableSliderTail> { RelativeSizeAxes = Axes.Both },
2018-04-13 17:19:50 +08:00
};
2018-11-09 12:58:46 +08:00
}
2018-04-13 17:19:50 +08:00
2018-11-09 12:58:46 +08:00
[BackgroundDependencyLoader]
private void load()
2018-11-09 12:58:46 +08:00
{
positionBindable.BindValueChanged(_ => Position = HitObject.StackedPosition);
stackHeightBindable.BindValueChanged(_ => Position = HitObject.StackedPosition);
scaleBindable.BindValueChanged(scale => Ball.Scale = new Vector2(scale.NewValue));
2018-11-09 12:58:46 +08:00
positionBindable.BindTo(HitObject.PositionBindable);
stackHeightBindable.BindTo(HitObject.StackHeightBindable);
2018-11-09 12:58:46 +08:00
scaleBindable.BindTo(HitObject.ScaleBindable);
2018-04-13 17:19:50 +08:00
2019-07-22 13:45:25 +08:00
AccentColour.BindValueChanged(colour =>
2018-04-13 17:19:50 +08:00
{
2018-07-02 15:10:56 +08:00
foreach (var drawableHitObject in NestedHitObjects)
2019-07-22 13:45:25 +08:00
drawableHitObject.AccentColour.Value = colour.NewValue;
}, true);
2018-04-13 17:19:50 +08:00
}
protected override void AddNestedHitObject(DrawableHitObject hitObject)
{
base.AddNestedHitObject(hitObject);
2019-10-17 11:53:54 +08:00
switch (hitObject)
{
case DrawableSliderHead head:
headContainer.Child = head;
break;
case DrawableSliderTail tail:
tailContainer.Child = tail;
break;
case DrawableSliderTick tick:
tickContainer.Add(tick);
break;
case DrawableRepeatPoint repeat:
repeatContainer.Add(repeat);
break;
}
}
protected override void ClearNestedHitObjects()
{
base.ClearNestedHitObjects();
headContainer.Clear();
tailContainer.Clear();
repeatContainer.Clear();
tickContainer.Clear();
}
protected override DrawableHitObject CreateNestedHitObject(HitObject hitObject)
{
switch (hitObject)
{
case SliderTailCircle tail:
return new DrawableSliderTail(slider, tail);
case HitCircle head:
return new DrawableSliderHead(slider, head) { OnShake = Shake };
case SliderTick tick:
return new DrawableSliderTick(tick) { Position = tick.Position - slider.Position };
case RepeatPoint repeat:
return new DrawableRepeatPoint(repeat, this) { Position = repeat.Position - slider.Position };
}
return base.CreateNestedHitObject(hitObject);
}
2019-10-16 20:41:18 +08:00
protected override void UpdateInitialTransforms()
{
base.UpdateInitialTransforms();
Body.FadeInFromZero(HitObject.TimeFadeIn);
}
public readonly Bindable<bool> Tracking = new Bindable<bool>();
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
Tracking.Value = Ball.Tracking;
2018-04-13 17:19:50 +08:00
double completionProgress = Math.Clamp((Time.Current - slider.StartTime) / slider.Duration, 0, 1);
2018-04-13 17:19:50 +08:00
Ball.UpdateProgress(completionProgress);
sliderBody?.UpdateProgress(completionProgress);
foreach (DrawableHitObject hitObject in NestedHitObjects)
{
if (hitObject is ITrackSnaking s) s.UpdateSnakingPosition(slider.Path.PositionAt(sliderBody?.SnakedStart ?? 0), slider.Path.PositionAt(sliderBody?.SnakedEnd ?? 0));
if (hitObject is IRequireTracking t) t.Tracking = Ball.Tracking;
}
2018-04-13 17:19:50 +08:00
Size = sliderBody?.Size ?? Vector2.Zero;
OriginPosition = sliderBody?.PathOffset ?? Vector2.Zero;
2018-04-13 17:19:50 +08:00
if (DrawSize != Vector2.Zero)
{
var childAnchorPosition = Vector2.Divide(OriginPosition, DrawSize);
foreach (var obj in NestedHitObjects)
obj.RelativeAnchorPosition = childAnchorPosition;
Ball.RelativeAnchorPosition = childAnchorPosition;
}
}
public override void OnKilled()
{
base.OnKilled();
sliderBody?.RecyclePath();
}
protected override void ApplySkin(ISkinSource skin, bool allowFallback)
2018-12-08 05:24:24 +08:00
{
base.ApplySkin(skin, allowFallback);
2019-01-07 19:12:39 +08:00
bool allowBallTint = skin.GetConfig<OsuSkinConfiguration, bool>(OsuSkinConfiguration.AllowSliderBallTint)?.Value ?? false;
Ball.Colour = allowBallTint ? AccentColour.Value : Color4.White;
2018-12-08 05:24:24 +08:00
}
protected override void CheckForResult(bool userTriggered, double timeOffset)
2018-04-13 17:19:50 +08:00
{
if (userTriggered || Time.Current < slider.EndTime)
return;
ApplyResult(r =>
2018-04-13 17:19:50 +08:00
{
var judgementsCount = NestedHitObjects.Count;
2018-04-13 17:19:50 +08:00
var judgementsHit = NestedHitObjects.Count(h => h.IsHit);
var hitFraction = (double)judgementsHit / judgementsCount;
if (hitFraction == 1 && HeadCircle.Result.Type == HitResult.Great)
r.Type = HitResult.Great;
else if (hitFraction >= 0.5 && HeadCircle.Result.Type >= HitResult.Good)
r.Type = HitResult.Good;
2018-04-13 17:19:50 +08:00
else if (hitFraction > 0)
r.Type = HitResult.Meh;
2018-04-13 17:19:50 +08:00
else
r.Type = HitResult.Miss;
});
2018-04-13 17:19:50 +08:00
}
2019-07-22 14:33:12 +08:00
protected override void UpdateStateTransforms(ArmedState state)
2018-04-13 17:19:50 +08:00
{
2019-09-13 17:49:21 +08:00
base.UpdateStateTransforms(state);
2018-04-13 17:19:50 +08:00
Ball.FadeIn();
Ball.ScaleTo(HitObject.Scale);
using (BeginDelayedSequence(slider.Duration, true))
{
const float fade_out_time = 450;
// intentionally pile on an extra FadeOut to make it happen much faster.
Ball.FadeOut(fade_out_time / 4, Easing.Out);
switch (state)
{
case ArmedState.Hit:
Ball.ScaleTo(HitObject.Scale * 1.4f, fade_out_time, Easing.Out);
break;
}
2019-09-12 18:29:08 +08:00
this.FadeOut(fade_out_time, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
}
2019-10-17 11:50:22 +08:00
public Drawable ProxiedLayer => HeadCircle.ProxiedLayer;
2018-04-13 17:19:50 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => sliderBody?.ReceivePositionalInputAt(screenSpacePos) ?? base.ReceivePositionalInputAt(screenSpacePos);
private class DefaultSliderBody : PlaySliderBody
{
}
2018-04-13 17:19:50 +08:00
}
}