2019-01-24 08:43:03 +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.
2018-04-13 09:19:50 +00:00
using System ;
2019-09-02 09:31:33 +00:00
using System.Diagnostics ;
2020-11-06 14:09:23 +00:00
using JetBrains.Annotations ;
2018-11-09 04:58:46 +00:00
using osu.Framework.Allocation ;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics ;
2019-02-14 09:47:05 +00:00
using osu.Framework.Graphics.Containers ;
2020-06-15 13:44:55 +00:00
using osu.Framework.Input ;
2019-07-24 09:50:57 +00:00
using osu.Framework.Input.Bindings ;
2020-06-15 13:44:55 +00:00
using osu.Game.Rulesets.Judgements ;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Objects.Drawables ;
2020-06-15 13:44:55 +00:00
using osu.Game.Rulesets.Osu.Judgements ;
2020-12-04 11:21:53 +00:00
using osu.Game.Rulesets.Osu.Skinning.Default ;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Scoring ;
2019-07-24 09:50:57 +00:00
using osu.Game.Skinning ;
2019-10-01 05:15:48 +00:00
using osuTK ;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Osu.Objects.Drawables
{
2020-11-22 09:36:10 +00:00
public class DrawableHitCircle : DrawableOsuHitObject
2018-04-13 09:19:50 +00:00
{
2019-10-21 08:14:08 +00:00
public OsuAction ? HitAction = > HitArea . HitAction ;
2020-03-28 04:39:08 +00:00
protected virtual OsuSkinComponents CirclePieceComponent = > OsuSkinComponents . HitCircle ;
2020-11-05 04:51:46 +00:00
public ApproachCircle ApproachCircle { get ; private set ; }
public HitReceptor HitArea { get ; private set ; }
public SkinnableDrawable CirclePiece { get ; private set ; }
private Container scaleContainer ;
2020-06-15 13:44:55 +00:00
private InputManager inputManager ;
2020-11-10 15:22:06 +00:00
public DrawableHitCircle ( )
: this ( null )
{
}
2020-11-06 14:09:23 +00:00
public DrawableHitCircle ( [ CanBeNull ] HitCircle h = null )
2018-04-13 09:19:50 +00:00
: base ( h )
{
2020-11-05 04:51:46 +00:00
}
2018-04-13 09:19:50 +00:00
2020-11-05 04:51:46 +00:00
[BackgroundDependencyLoader]
private void load ( )
{
Origin = Anchor . Centre ;
2018-04-13 09:19:50 +00:00
InternalChildren = new Drawable [ ]
{
2019-02-14 09:47:05 +00:00
scaleContainer = new Container
2018-04-13 09:19:50 +00:00
{
2019-02-14 09:47:05 +00:00
RelativeSizeAxes = Axes . Both ,
Origin = Anchor . Centre ,
Anchor = Anchor . Centre ,
2019-07-24 09:50:57 +00:00
Children = new Drawable [ ]
2018-04-13 09:19:50 +00:00
{
2019-10-21 08:14:08 +00:00
HitArea = new HitReceptor
2019-02-14 09:47:05 +00:00
{
2019-07-24 09:50:57 +00:00
Hit = ( ) = >
2019-02-14 09:47:05 +00:00
{
2019-07-24 09:50:57 +00:00
if ( AllJudged )
return false ;
UpdateResult ( true ) ;
return true ;
2019-02-14 09:47:05 +00:00
} ,
2019-07-24 09:50:57 +00:00
} ,
2021-04-20 09:36:11 +00:00
CirclePiece = new SkinnableDrawable ( new OsuSkinComponent ( CirclePieceComponent ) , _ = > new MainCirclePiece ( ) )
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
} ,
2019-07-24 09:50:57 +00:00
ApproachCircle = new ApproachCircle
{
Alpha = 0 ,
Scale = new Vector2 ( 4 ) ,
2019-02-14 09:47:05 +00:00
}
}
2018-04-13 09:19:50 +00:00
} ,
} ;
2019-10-21 08:14:08 +00:00
Size = HitArea . DrawSize ;
2018-04-13 09:19:50 +00:00
2020-11-06 14:35:47 +00:00
PositionBindable . BindValueChanged ( _ = > Position = HitObject . StackedPosition ) ;
StackHeightBindable . BindValueChanged ( _ = > Position = HitObject . StackedPosition ) ;
ScaleBindable . BindValueChanged ( scale = > scaleContainer . Scale = new Vector2 ( scale . NewValue ) ) ;
AccentColour . BindValueChanged ( accent = > ApproachCircle . Colour = accent . NewValue ) ;
2018-04-13 09:19:50 +00:00
}
2020-06-15 13:44:55 +00:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
inputManager = GetContainingInputManager ( ) ;
}
2019-09-12 10:29:08 +00:00
public override double LifetimeStart
{
get = > base . LifetimeStart ;
set
{
base . LifetimeStart = value ;
ApproachCircle . LifetimeStart = value ;
}
}
public override double LifetimeEnd
{
get = > base . LifetimeEnd ;
set
{
base . LifetimeEnd = value ;
ApproachCircle . LifetimeEnd = value ;
}
}
2018-08-06 02:31:46 +00:00
protected override void CheckForResult ( bool userTriggered , double timeOffset )
2018-04-13 09:19:50 +00:00
{
2019-09-02 09:31:33 +00:00
Debug . Assert ( HitObject . HitWindows ! = null ) ;
2018-04-13 09:19:50 +00:00
if ( ! userTriggered )
{
if ( ! HitObject . HitWindows . CanBeHit ( timeOffset ) )
2020-10-02 20:58:10 +00:00
ApplyResult ( r = > r . Type = r . Judgement . MinResult ) ;
2018-08-01 12:46:22 +00:00
2018-04-13 09:19:50 +00:00
return ;
}
2021-02-03 13:12:20 +00:00
var result = ResultFor ( timeOffset ) ;
2019-04-01 03:16:05 +00:00
2020-04-09 17:02:09 +00:00
if ( result = = HitResult . None | | CheckHittable ? . Invoke ( this , Time . Current ) = = false )
2018-06-28 11:41:23 +00:00
{
2019-09-06 06:24:00 +00:00
Shake ( Math . Abs ( timeOffset ) - HitObject . HitWindows . WindowFor ( HitResult . Miss ) ) ;
2018-04-13 09:19:50 +00:00
return ;
2018-06-28 11:41:23 +00:00
}
2018-04-13 09:19:50 +00:00
2020-06-15 13:44:55 +00:00
ApplyResult ( r = >
{
var circleResult = ( OsuHitCircleJudgementResult ) r ;
2020-06-19 10:58:35 +00:00
// Todo: This should also consider misses, but they're a little more interesting to handle, since we don't necessarily know the position at the time of a miss.
2020-10-02 20:57:49 +00:00
if ( result . IsHit ( ) )
2020-06-15 13:44:55 +00:00
{
var localMousePosition = ToLocalSpace ( inputManager . CurrentState . Mouse . Position ) ;
2020-06-19 10:58:35 +00:00
circleResult . CursorPositionAtHit = HitObject . StackedPosition + ( localMousePosition - DrawSize / 2 ) ;
2020-06-15 13:44:55 +00:00
}
circleResult . Type = result ;
} ) ;
2018-04-13 09:19:50 +00:00
}
2021-02-03 13:12:20 +00:00
/// <summary>
/// Retrieves the <see cref="HitResult"/> for a time offset.
/// </summary>
/// <param name="timeOffset">The time offset.</param>
/// <returns>The hit result, or <see cref="HitResult.None"/> if <paramref name="timeOffset"/> doesn't result in a judgement.</returns>
protected virtual HitResult ResultFor ( double timeOffset ) = > HitObject . HitWindows . ResultFor ( timeOffset ) ;
2019-07-22 06:33:12 +00:00
protected override void UpdateInitialTransforms ( )
2018-04-13 09:19:50 +00:00
{
2019-07-22 06:33:12 +00:00
base . UpdateInitialTransforms ( ) ;
2018-04-13 09:19:50 +00:00
2019-09-18 11:04:49 +00:00
CirclePiece . FadeInFromZero ( HitObject . TimeFadeIn ) ;
2019-08-28 09:10:58 +00:00
2018-07-05 02:32:09 +00:00
ApproachCircle . FadeIn ( Math . Min ( HitObject . TimeFadeIn * 2 , HitObject . TimePreempt ) ) ;
2019-07-24 10:32:24 +00:00
ApproachCircle . ScaleTo ( 1f , HitObject . TimePreempt ) ;
2018-12-13 05:55:28 +00:00
ApproachCircle . Expire ( true ) ;
2018-04-13 09:19:50 +00:00
}
2021-03-23 10:19:07 +00:00
protected override void UpdateStartTimeStateTransforms ( )
{
base . UpdateStartTimeStateTransforms ( ) ;
ApproachCircle . FadeOut ( 50 ) ;
}
2020-11-04 07:19:07 +00:00
protected override void UpdateHitStateTransforms ( ArmedState state )
2018-04-13 09:19:50 +00:00
{
2019-09-02 09:31:33 +00:00
Debug . Assert ( HitObject . HitWindows ! = null ) ;
2021-03-23 10:19:07 +00:00
// todo: temporary / arbitrary, used for lifetime optimisation.
this . Delay ( 800 ) . FadeOut ( ) ;
2021-04-21 10:15:10 +00:00
( CirclePiece . Drawable as IMainCirclePiece ) ? . Animate ( state ) ;
2018-04-13 09:19:50 +00:00
switch ( state )
{
case ArmedState . Idle :
2019-10-21 08:14:08 +00:00
HitArea . HitAction = null ;
2018-04-13 09:19:50 +00:00
break ;
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
case ArmedState . Miss :
this . FadeOut ( 100 ) ;
break ;
2019-07-24 09:50:57 +00:00
}
2020-11-17 14:19:59 +00:00
Expire ( ) ;
2019-07-24 09:50:57 +00:00
}
2018-04-13 09:19:50 +00:00
2019-07-24 09:50:57 +00:00
public Drawable ProxiedLayer = > ApproachCircle ;
2018-04-13 09:19:50 +00:00
2020-06-15 13:44:55 +00:00
protected override JudgementResult CreateResult ( Judgement judgement ) = > new OsuHitCircleJudgementResult ( HitObject , judgement ) ;
2020-03-06 20:21:20 +00:00
public class HitReceptor : CompositeDrawable , IKeyBindingHandler < OsuAction >
2019-07-24 09:50:57 +00:00
{
// IsHovered is used
public override bool HandlePositionalInput = > true ;
2018-04-13 09:19:50 +00:00
2019-07-24 09:50:57 +00:00
public Func < bool > Hit ;
2018-04-13 09:19:50 +00:00
2019-07-24 09:50:57 +00:00
public OsuAction ? HitAction ;
2019-10-21 08:14:08 +00:00
public HitReceptor ( )
2019-07-24 09:50:57 +00:00
{
Size = new Vector2 ( OsuHitObject . OBJECT_RADIUS * 2 ) ;
Anchor = Anchor . Centre ;
Origin = Anchor . Centre ;
2020-03-06 20:21:20 +00:00
CornerRadius = OsuHitObject . OBJECT_RADIUS ;
CornerExponent = 2 ;
2018-04-13 09:19:50 +00:00
}
2019-07-24 09:50:57 +00:00
public bool OnPressed ( OsuAction action )
{
switch ( action )
{
case OsuAction . LeftButton :
case OsuAction . RightButton :
if ( IsHovered & & ( Hit ? . Invoke ( ) ? ? false ) )
{
HitAction = action ;
return true ;
}
break ;
}
return false ;
}
2020-01-22 04:22:34 +00:00
public void OnReleased ( OsuAction action )
{
}
2019-07-24 09:50:57 +00:00
}
2018-04-13 09:19:50 +00:00
}
}