1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 17:02:55 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Skinning/Default/SliderBall.cs

279 lines
11 KiB
C#
Raw Normal View History

2020-02-02 21:34:06 +08: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.
2018-04-13 17:19:50 +08:00
2019-01-21 09:57:14 +08:00
using System;
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2020-12-04 19:21:53 +08:00
using osu.Game.Graphics;
using osu.Game.Rulesets.Objects.Drawables;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Objects.Types;
2020-12-04 19:21:53 +08:00
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Skinning;
2018-11-20 15:51:59 +08:00
using osuTK;
2020-12-04 19:21:53 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2020-12-04 19:21:53 +08:00
namespace osu.Game.Rulesets.Osu.Skinning.Default
2018-04-13 17:19:50 +08:00
{
2020-03-14 18:45:55 +08:00
public class SliderBall : CircularContainer, ISliderProgress, IRequireHighFrequencyMousePosition, IHasAccentColour
2018-04-13 17:19:50 +08:00
{
2019-01-21 09:57:14 +08:00
public Func<OsuAction?> GetInitialHitAction;
2020-03-14 18:45:55 +08:00
public Color4 AccentColour
{
2020-03-14 19:07:52 +08:00
get => ball.Colour;
set => ball.Colour = value;
}
2021-02-05 15:59:13 +08:00
/// <summary>
/// Whether to track accurately to the visual size of this <see cref="SliderBall"/>.
/// If <c>false</c>, tracking will be performed at the final scale at all times.
/// </summary>
2021-02-10 17:53:23 +08:00
public bool InputTracksVisualSize = true;
2021-02-05 15:59:13 +08:00
2020-02-02 21:34:06 +08:00
private readonly Drawable followCircle;
private readonly DrawableSlider drawableSlider;
private readonly Drawable ball;
2018-04-13 17:19:50 +08:00
public SliderBall(DrawableSlider drawableSlider)
2018-04-13 17:19:50 +08:00
{
this.drawableSlider = drawableSlider;
2018-04-13 17:19:50 +08:00
Origin = Anchor.Centre;
Size = new Vector2(OsuHitObject.OBJECT_RADIUS * 2);
Children = new[]
2018-04-13 17:19:50 +08:00
{
2020-02-02 21:34:06 +08:00
followCircle = new FollowCircleContainer
2018-04-13 17:19:50 +08:00
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
2018-04-13 17:19:50 +08:00
Alpha = 0,
Child = new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.SliderFollowCircle), _ => new DefaultFollowCircle()),
2018-04-13 17:19:50 +08:00
},
ball = new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.SliderBall), _ => new DefaultSliderBall())
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
2018-04-13 17:19:50 +08:00
};
}
private Vector2? lastScreenSpaceMousePosition;
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 17:19:50 +08:00
{
lastScreenSpaceMousePosition = e.ScreenSpaceMousePosition;
2018-10-02 11:02:47 +08:00
return base.OnMouseMove(e);
2018-04-13 17:19:50 +08:00
}
public override void ClearTransformsAfter(double time, bool propagateChildren = false, string targetMember = null)
{
// Consider the case of rewinding - children's transforms are handled internally, so propagating down
// any further will cause weirdness with the Tracking bool below. Let's not propagate further at this point.
base.ClearTransformsAfter(time, false, targetMember);
}
public override void ApplyTransformsAt(double time, bool propagateChildren = false)
{
// For the same reasons as above w.r.t rewinding, we shouldn't propagate to children here either.
base.ApplyTransformsAt(time, false);
}
2018-04-13 17:19:50 +08:00
private bool tracking;
2018-07-31 15:13:52 +08:00
2018-04-13 17:19:50 +08:00
public bool Tracking
{
get => tracking;
2018-04-13 17:19:50 +08:00
private set
{
if (value == tracking)
return;
2018-04-13 17:19:50 +08:00
tracking = value;
2021-02-10 17:53:23 +08:00
if (InputTracksVisualSize)
2021-02-05 15:59:13 +08:00
followCircle.ScaleTo(tracking ? 2.4f : 1f, 300, Easing.OutQuint);
else
{
// We need to always be tracking the final size, at both endpoints. For now, this is achieved by removing the scale duration.
followCircle.ScaleTo(tracking ? 2.4f : 1f);
}
2020-02-02 21:34:06 +08:00
followCircle.FadeTo(tracking ? 1f : 0, 300, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
}
2019-01-21 09:57:14 +08:00
/// <summary>
2019-01-21 14:46:49 +08:00
/// If the cursor moves out of the ball's radius we still need to be able to receive positional updates to stop tracking.
2019-01-21 09:57:14 +08:00
/// </summary>
2019-01-21 14:46:49 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
/// <summary>
/// The point in time after which we can accept any key for tracking. Before this time, we may need to restrict tracking to the key used to hit the head circle.
///
/// This is a requirement to stop the case where a player holds down one key (from before the slider) and taps the second key while maintaining full scoring (tracking) of sliders.
/// Visually, this special case can be seen below (time increasing from left to right):
2019-01-21 14:46:49 +08:00
///
/// Z Z+X Z
/// o========o
///
/// Without this logic, tracking would continue through the entire slider even though no key hold action is directly attributing to it.
///
/// In all other cases, no special handling is required (either key being pressed is allowable as valid tracking).
2019-01-21 14:46:49 +08:00
///
/// The reason for storing this as a time value (rather than a bool) is to correctly handle rewind scenarios.
2019-01-21 14:46:49 +08:00
/// </summary>
private double? timeToAcceptAnyKeyAfter;
2018-04-13 17:19:50 +08:00
/// <summary>
/// The actions that were pressed in the previous frame.
/// </summary>
private readonly List<OsuAction> lastPressedActions = new List<OsuAction>();
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
2019-01-21 14:46:49 +08:00
// from the point at which the head circle is hit, this will be non-null.
// it may be null if the head circle was missed.
var headCircleHitAction = GetInitialHitAction();
2019-01-21 09:57:14 +08:00
2019-01-21 14:46:49 +08:00
if (headCircleHitAction == null)
timeToAcceptAnyKeyAfter = null;
2019-01-21 09:57:14 +08:00
var actions = drawableSlider.OsuActionInputManager?.PressedActions;
2019-01-21 14:46:49 +08:00
// if the head circle was hit with a specific key, tracking should only occur while that key is pressed.
if (headCircleHitAction != null && timeToAcceptAnyKeyAfter == null)
2019-01-21 09:57:14 +08:00
{
var otherKey = headCircleHitAction == OsuAction.RightButton ? OsuAction.LeftButton : OsuAction.RightButton;
// we can start accepting any key once all other keys have been released in the previous frame.
if (!lastPressedActions.Contains(otherKey))
timeToAcceptAnyKeyAfter = Time.Current;
2018-04-13 17:19:50 +08:00
}
Tracking =
// in valid time range
Time.Current >= drawableSlider.HitObject.StartTime && Time.Current < drawableSlider.HitObject.EndTime &&
// in valid position range
lastScreenSpaceMousePosition.HasValue && followCircle.ReceivePositionalInputAt(lastScreenSpaceMousePosition.Value) &&
// valid action
(actions?.Any(isValidTrackingAction) ?? false);
lastPressedActions.Clear();
2021-04-26 14:35:08 +08:00
if (actions != null)
lastPressedActions.AddRange(actions);
2018-04-13 17:19:50 +08:00
}
2019-01-21 14:46:49 +08:00
/// <summary>
/// Check whether a given user input is a valid tracking action.
/// </summary>
private bool isValidTrackingAction(OsuAction action)
{
bool headCircleHit = GetInitialHitAction().HasValue;
// if the head circle was hit, we may not yet be allowed to accept any key, so we must use the initial hit action.
if (headCircleHit && (!timeToAcceptAnyKeyAfter.HasValue || Time.Current <= timeToAcceptAnyKeyAfter.Value))
return action == GetInitialHitAction();
return action == OsuAction.LeftButton || action == OsuAction.RightButton;
}
2019-08-19 18:52:53 +08:00
private Vector2? lastPosition;
2018-04-13 17:19:50 +08:00
public void UpdateProgress(double completionProgress)
{
var newPos = drawableSlider.HitObject.CurvePositionAt(completionProgress);
2019-08-19 18:52:53 +08:00
var diff = lastPosition.HasValue ? lastPosition.Value - newPos : newPos - drawableSlider.HitObject.CurvePositionAt(completionProgress + 0.01f);
if (diff == Vector2.Zero)
return;
2019-08-19 18:52:53 +08:00
Position = newPos;
ball.Rotation = -90 + (float)(-Math.Atan2(diff.X, diff.Y) * 180 / Math.PI);
2019-08-19 18:52:53 +08:00
lastPosition = newPos;
2018-04-13 17:19:50 +08:00
}
2020-07-22 20:14:04 +08:00
private class FollowCircleContainer : CircularContainer
{
public override bool HandlePositionalInput => true;
}
public class DefaultFollowCircle : CompositeDrawable
{
public DefaultFollowCircle()
{
RelativeSizeAxes = Axes.Both;
InternalChild = new CircularContainer
{
RelativeSizeAxes = Axes.Both,
Masking = true,
BorderThickness = 5,
BorderColour = Color4.Orange,
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive,
Child = new Box
{
Colour = Color4.Orange,
RelativeSizeAxes = Axes.Both,
Alpha = 0.2f,
}
};
}
}
public class DefaultSliderBall : CompositeDrawable
{
private Box box;
[BackgroundDependencyLoader]
private void load(DrawableHitObject drawableObject, ISkinSource skin)
{
var slider = (DrawableSlider)drawableObject;
RelativeSizeAxes = Axes.Both;
float radius = skin.GetConfig<OsuSkinConfiguration, float>(OsuSkinConfiguration.SliderPathRadius)?.Value ?? OsuHitObject.OBJECT_RADIUS;
InternalChild = new CircularContainer
{
Masking = true,
RelativeSizeAxes = Axes.Both,
Scale = new Vector2(radius / OsuHitObject.OBJECT_RADIUS),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Blending = BlendingParameters.Additive,
BorderThickness = 10,
BorderColour = Color4.White,
Alpha = 1,
Child = box = new Box
{
Blending = BlendingParameters.Additive,
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
AlwaysPresent = true,
Alpha = 0
}
};
slider.Tracking.BindValueChanged(trackingChanged, true);
}
private void trackingChanged(ValueChangedEvent<bool> tracking) =>
box.FadeTo(tracking.NewValue ? 0.3f : 0.05f, 200, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
}
}