2019-01-24 17:43:03 +09: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 18:19:50 +09:00
2022-06-17 16:37:17 +09:00
#nullable disable
2016-11-19 19:07:57 +09:00
using System ;
2023-09-29 18:23:22 +09:00
using System.Collections.Generic ;
2019-09-02 18:31:33 +09:00
using System.Diagnostics ;
2020-11-06 23:09:23 +09:00
using JetBrains.Annotations ;
2018-11-09 13:58:46 +09:00
using osu.Framework.Allocation ;
2016-10-13 10:10:15 +09:00
using osu.Framework.Graphics ;
2019-02-14 18:47:05 +09:00
using osu.Framework.Graphics.Containers ;
2020-06-15 22:44:55 +09:00
using osu.Framework.Input ;
2019-07-24 18:50:57 +09:00
using osu.Framework.Input.Bindings ;
2021-09-16 18:26:12 +09:00
using osu.Framework.Input.Events ;
2022-09-22 14:30:01 +09:00
using osu.Game.Graphics.Containers ;
2020-06-15 22:44:55 +09:00
using osu.Game.Rulesets.Judgements ;
2017-04-18 16:05:58 +09:00
using osu.Game.Rulesets.Objects.Drawables ;
2020-06-15 22:44:55 +09:00
using osu.Game.Rulesets.Osu.Judgements ;
2021-06-21 07:35:07 +03:00
using osu.Game.Rulesets.Osu.Skinning ;
2020-12-04 20:21:53 +09:00
using osu.Game.Rulesets.Osu.Skinning.Default ;
2023-07-17 23:31:21 -04:00
using osu.Game.Rulesets.Osu.UI ;
2017-12-30 21:23:18 +01:00
using osu.Game.Rulesets.Scoring ;
2019-07-24 18:50:57 +09:00
using osu.Game.Skinning ;
2019-10-01 08:15:48 +03:00
using osuTK ;
2018-04-13 18:19:50 +09:00
2017-04-18 16:05:58 +09:00
namespace osu.Game.Rulesets.Osu.Objects.Drawables
2016-10-13 10:10:15 +09:00
{
2022-11-09 13:18:24 +09:00
public partial class DrawableHitCircle : DrawableOsuHitObject , IHasApproachCircle
2016-10-19 16:13:53 +09:00
{
2023-12-15 16:13:32 +09:00
public OsuAction ? HitAction = > HitArea ? . HitAction ;
2020-03-28 13:39:08 +09:00
protected virtual OsuSkinComponents CirclePieceComponent = > OsuSkinComponents . HitCircle ;
2021-09-16 19:35:15 +09:00
public SkinnableDrawable ApproachCircle { get ; private set ; }
2020-11-05 13:51:46 +09:00
public HitReceptor HitArea { get ; private set ; }
public SkinnableDrawable CirclePiece { get ; private set ; }
2023-09-29 18:23:22 +09:00
protected override IEnumerable < Drawable > DimmablePieces = > new [ ]
{
CirclePiece ,
} ;
2021-06-21 07:35:07 +03:00
Drawable IHasApproachCircle . ApproachCircle = > ApproachCircle ;
2020-11-05 13:51:46 +09:00
private Container scaleContainer ;
2020-06-15 22:44:55 +09:00
private InputManager inputManager ;
2020-11-11 00:22:06 +09:00
public DrawableHitCircle ( )
: this ( null )
{
}
2020-11-06 23:09:23 +09:00
public DrawableHitCircle ( [ CanBeNull ] HitCircle h = null )
2018-03-15 15:58:04 +09:00
: base ( h )
2016-10-13 10:10:15 +09:00
{
2020-11-05 13:51:46 +09:00
}
2018-04-13 18:19:50 +09:00
2022-09-22 14:30:01 +09:00
private ShakeContainer shakeContainer ;
2020-11-05 13:51:46 +09:00
[BackgroundDependencyLoader]
private void load ( )
{
Origin = Anchor . Centre ;
2018-04-13 18:19:50 +09:00
2022-09-22 14:46:37 +09:00
AddRangeInternal ( new Drawable [ ]
2016-10-19 16:13:53 +09:00
{
2019-02-14 18:47:05 +09:00
scaleContainer = new Container
2016-10-19 16:13:53 +09:00
{
2019-02-14 18:47:05 +09:00
RelativeSizeAxes = Axes . Both ,
Origin = Anchor . Centre ,
Anchor = Anchor . Centre ,
2019-07-24 18:50:57 +09:00
Children = new Drawable [ ]
2016-11-26 16:51:51 +09:00
{
2019-10-21 17:14:08 +09:00
HitArea = new HitReceptor
2019-02-14 18:47:05 +09:00
{
2019-07-24 18:50:57 +09:00
Hit = ( ) = >
2019-02-14 18:47:05 +09:00
{
2019-07-24 18:50:57 +09:00
if ( AllJudged )
return false ;
UpdateResult ( true ) ;
return true ;
2019-02-14 18:47:05 +09:00
} ,
2019-07-24 18:50:57 +09:00
} ,
2022-09-22 14:30:01 +09:00
shakeContainer = new ShakeContainer
2019-07-24 18:50:57 +09:00
{
2022-09-22 14:30:01 +09:00
ShakeDuration = 30 ,
2021-09-16 19:35:15 +09:00
RelativeSizeAxes = Axes . Both ,
2022-09-22 14:30:01 +09:00
Children = new Drawable [ ]
{
2022-11-09 16:04:56 +09:00
CirclePiece = new SkinnableDrawable ( new OsuSkinComponentLookup ( CirclePieceComponent ) , _ = > new MainCirclePiece ( ) )
2022-09-22 14:30:01 +09:00
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
} ,
2022-11-09 16:04:56 +09:00
ApproachCircle = new ProxyableSkinnableDrawable ( new OsuSkinComponentLookup ( OsuSkinComponents . ApproachCircle ) , _ = > new DefaultApproachCircle ( ) )
2022-09-22 14:30:01 +09:00
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
RelativeSizeAxes = Axes . Both ,
Alpha = 0 ,
Scale = new Vector2 ( 4 ) ,
}
}
2019-02-14 18:47:05 +09:00
}
}
2017-02-15 23:23:55 +09:00
} ,
2022-09-22 14:46:37 +09:00
} ) ;
2018-04-13 18:19:50 +09:00
2019-10-21 17:14:08 +09:00
Size = HitArea . DrawSize ;
2018-04-13 18:19:50 +09:00
2022-10-26 15:26:20 +09:00
PositionBindable . BindValueChanged ( _ = > UpdatePosition ( ) ) ;
StackHeightBindable . BindValueChanged ( _ = > UpdatePosition ( ) ) ;
2020-11-06 23:35:47 +09:00
ScaleBindable . BindValueChanged ( scale = > scaleContainer . Scale = new Vector2 ( scale . NewValue ) ) ;
2018-03-15 15:58:04 +09:00
}
2018-04-13 18:19:50 +09:00
2020-06-15 22:44:55 +09:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
inputManager = GetContainingInputManager ( ) ;
}
2019-09-12 19:29:08 +09: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 ;
}
}
2022-10-26 15:26:20 +09:00
protected virtual void UpdatePosition ( )
{
Position = HitObject . StackedPosition ;
}
2022-09-22 14:30:01 +09:00
public override void Shake ( ) = > shakeContainer . Shake ( ) ;
2018-08-06 11:31:46 +09:00
protected override void CheckForResult ( bool userTriggered , double timeOffset )
2016-11-26 16:51:51 +09:00
{
2019-09-02 18:31:33 +09:00
Debug . Assert ( HitObject . HitWindows ! = null ) ;
2016-11-26 16:51:51 +09:00
if ( ! userTriggered )
{
2018-02-02 20:31:39 +09:00
if ( ! HitObject . HitWindows . CanBeHit ( timeOffset ) )
2024-02-05 13:21:01 +01:00
ApplyMinResult ( ) ;
2018-08-01 21:46:22 +09:00
2016-11-26 16:51:51 +09:00
return ;
}
2018-04-13 18:19:50 +09:00
2024-02-05 13:35:41 +01:00
var result = ResultFor ( timeOffset ) ;
var clickAction = CheckHittable ? . Invoke ( this , Time . Current , result ) ;
2019-04-01 12:16:05 +09:00
2023-08-23 13:12:18 +02:00
if ( clickAction = = ClickAction . Shake )
2022-09-22 14:30:01 +09:00
Shake ( ) ;
2018-04-13 18:19:50 +09:00
2024-02-05 13:35:41 +01:00
if ( result = = HitResult . None | | clickAction ! = ClickAction . Hit )
2018-02-02 20:31:39 +09:00
return ;
2018-04-13 18:19:50 +09:00
2024-02-05 13:21:01 +01:00
Vector2 ? hitPosition = null ;
// 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.
2024-02-05 13:35:41 +01:00
if ( result . IsHit ( ) )
2020-06-15 22:44:55 +09:00
{
2024-02-05 13:21:01 +01:00
var localMousePosition = ToLocalSpace ( inputManager . CurrentState . Mouse . Position ) ;
hitPosition = HitObject . StackedPosition + ( localMousePosition - DrawSize / 2 ) ;
}
2020-06-15 22:44:55 +09:00
2024-02-05 13:21:01 +01:00
ApplyResult < ( HitResult result , Vector2 ? position ) > ( ( r , state ) = >
{
var circleResult = ( OsuHitCircleJudgementResult ) r ;
2020-06-15 22:44:55 +09:00
2024-02-05 13:21:01 +01:00
circleResult . Type = state . result ;
circleResult . CursorPositionAtHit = state . position ;
2024-02-05 13:35:41 +01:00
} , ( result , hitPosition ) ) ;
2016-11-26 16:51:51 +09:00
}
2018-04-13 18:19:50 +09:00
2021-02-03 22:12:20 +09: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 15:33:12 +09:00
protected override void UpdateInitialTransforms ( )
2016-11-28 18:40:54 +09:00
{
2019-07-22 15:33:12 +09:00
base . UpdateInitialTransforms ( ) ;
2018-04-13 18:19:50 +09:00
2019-09-18 20:04:49 +09:00
CirclePiece . FadeInFromZero ( HitObject . TimeFadeIn ) ;
2019-08-28 18:10:58 +09:00
2023-09-29 12:44:26 +09:00
ApproachCircle . FadeTo ( 0.9f , Math . Min ( HitObject . TimeFadeIn * 2 , HitObject . TimePreempt ) ) ;
2019-07-24 19:32:24 +09:00
ApproachCircle . ScaleTo ( 1f , HitObject . TimePreempt ) ;
2018-12-13 14:55:28 +09:00
ApproachCircle . Expire ( true ) ;
2016-11-28 18:40:54 +09:00
}
2018-04-13 18:19:50 +09:00
2021-03-23 19:19:07 +09:00
protected override void UpdateStartTimeStateTransforms ( )
{
base . UpdateStartTimeStateTransforms ( ) ;
2021-06-17 15:08:22 +09:00
// always fade out at the circle's start time (to match user expectations).
2021-03-23 19:19:07 +09:00
ApproachCircle . FadeOut ( 50 ) ;
}
2020-11-04 16:19:07 +09:00
protected override void UpdateHitStateTransforms ( ArmedState state )
2016-11-28 18:40:54 +09:00
{
2019-09-02 18:31:33 +09:00
Debug . Assert ( HitObject . HitWindows ! = null ) ;
2021-03-23 19:19:07 +09:00
// todo: temporary / arbitrary, used for lifetime optimisation.
this . Delay ( 800 ) . FadeOut ( ) ;
2016-10-19 16:13:53 +09:00
switch ( state )
{
2022-10-05 13:42:04 +09:00
default :
ApproachCircle . FadeOut ( ) ;
break ;
2016-11-25 16:26:50 +09:00
case ArmedState . Idle :
2019-10-21 17:14:08 +09:00
HitArea . HitAction = null ;
2016-10-19 16:13:53 +09:00
break ;
2019-04-01 12:16:05 +09:00
2016-11-25 16:26:50 +09:00
case ArmedState . Miss :
2017-12-26 17:25:18 +01:00
this . FadeOut ( 100 ) ;
2016-11-25 16:26:50 +09:00
break ;
2019-07-24 18:50:57 +09:00
}
2020-11-17 23:19:59 +09:00
Expire ( ) ;
2019-07-24 18:50:57 +09:00
}
2018-04-13 18:19:50 +09:00
2019-07-24 18:50:57 +09:00
public Drawable ProxiedLayer = > ApproachCircle ;
2018-04-13 18:19:50 +09:00
2020-06-15 22:44:55 +09:00
protected override JudgementResult CreateResult ( Judgement judgement ) = > new OsuHitCircleJudgementResult ( HitObject , judgement ) ;
2020-03-06 21:21:20 +01:00
public partial class HitReceptor : CompositeDrawable , IKeyBindingHandler < OsuAction >
2019-07-24 18:50:57 +09:00
{
// IsHovered is used
public override bool HandlePositionalInput = > true ;
2018-04-13 18:19:50 +09:00
2019-07-24 18:50:57 +09:00
public Func < bool > Hit ;
2018-04-13 18:19:50 +09:00
2019-07-24 18:50:57 +09:00
public OsuAction ? HitAction ;
2019-10-21 17:14:08 +09:00
public HitReceptor ( )
2019-07-24 18:50:57 +09:00
{
2023-09-20 12:48:15 +09:00
Size = OsuHitObject . OBJECT_DIMENSIONS ;
2019-07-24 18:50:57 +09:00
Anchor = Anchor . Centre ;
Origin = Anchor . Centre ;
2020-03-06 21:21:20 +01:00
CornerRadius = OsuHitObject . OBJECT_RADIUS ;
CornerExponent = 2 ;
2016-10-19 16:13:53 +09:00
}
2018-04-13 18:19:50 +09:00
2021-09-16 18:26:12 +09:00
public bool OnPressed ( KeyBindingPressEvent < OsuAction > e )
2019-07-24 18:50:57 +09:00
{
2021-09-16 18:26:12 +09:00
switch ( e . Action )
2019-07-24 18:50:57 +09:00
{
case OsuAction . LeftButton :
case OsuAction . RightButton :
if ( IsHovered & & ( Hit ? . Invoke ( ) ? ? false ) )
{
2023-09-08 11:08:25 +02:00
HitAction ? ? = e . Action ;
2019-07-24 18:50:57 +09:00
return true ;
}
break ;
}
return false ;
}
2021-09-16 18:26:12 +09:00
public void OnReleased ( KeyBindingReleaseEvent < OsuAction > e )
2020-01-22 13:22:34 +09:00
{
}
2019-07-24 18:50:57 +09:00
}
2021-09-16 19:35:15 +09:00
private partial class ProxyableSkinnableDrawable : SkinnableDrawable
{
public override bool RemoveWhenNotAlive = > false ;
2022-11-09 16:04:56 +09:00
public ProxyableSkinnableDrawable ( ISkinComponentLookup lookup , Func < ISkinComponentLookup , Drawable > defaultImplementation = null , ConfineMode confineMode = ConfineMode . NoScaling )
2022-11-09 14:11:41 +09:00
: base ( lookup , defaultImplementation , confineMode )
2021-09-16 19:35:15 +09:00
{
}
}
2016-10-13 10:10:15 +09:00
}
}