// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Extensions.TypeExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Primitives; using osu.Framework.Logging; using osu.Framework.Threading; using osu.Game.Audio; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Scoring; using osu.Game.Skinning; using osu.Game.Configuration; using osu.Game.Rulesets.UI; using osuTK.Graphics; namespace osu.Game.Rulesets.Objects.Drawables { [Cached(typeof(DrawableHitObject))] public abstract class DrawableHitObject : SkinReloadableDrawable { /// /// Invoked after this 's applied has had its defaults applied. /// public event Action DefaultsApplied; /// /// Invoked after a has been applied to this . /// public event Action HitObjectApplied; /// /// The currently represented by this . /// public HitObject HitObject { get; private set; } /// /// The parenting , if any. /// [CanBeNull] protected internal DrawableHitObject ParentHitObject { get; internal set; } /// /// The colour used for various elements of this DrawableHitObject. /// public readonly Bindable AccentColour = new Bindable(Color4.Gray); protected PausableSkinnableSound Samples { get; private set; } public virtual IEnumerable GetSamples() => HitObject.Samples; private readonly Lazy> nestedHitObjects = new Lazy>(); public IReadOnlyList NestedHitObjects => nestedHitObjects.IsValueCreated ? nestedHitObjects.Value : (IReadOnlyList)Array.Empty(); /// /// Whether this object should handle any user input events. /// public bool HandleUserInput { get; set; } = true; public override bool PropagatePositionalInputSubTree => HandleUserInput; public override bool PropagateNonPositionalInputSubTree => HandleUserInput; /// /// Invoked by this or a nested after a has been applied. /// public event Action OnNewResult; /// /// Invoked by this or a nested prior to a being reverted. /// public event Action OnRevertResult; /// /// Invoked when a new nested hit object is created by . /// internal event Action OnNestedDrawableCreated; /// /// Whether a visual indicator should be displayed when a scoring result occurs. /// public virtual bool DisplayResult => true; /// /// Whether this and all of its nested s have been judged. /// public bool AllJudged => Judged && NestedHitObjects.All(h => h.AllJudged); /// /// Whether this has been hit. This occurs if is hit. /// Note: This does NOT include nested hitobjects. /// public bool IsHit => Result?.IsHit ?? false; /// /// Whether this has been judged. /// Note: This does NOT include nested hitobjects. /// public bool Judged => Result?.HasResult ?? true; /// /// The scoring result of this . /// public JudgementResult Result { get; private set; } /// /// The relative X position of this hit object for sample playback balance adjustment. /// /// /// This is a range of 0..1 (0 for far-left, 0.5 for centre, 1 for far-right). /// Dampening is post-applied to ensure the effect is not too intense. /// protected virtual float SamplePlaybackPosition => 0.5f; public readonly Bindable StartTimeBindable = new Bindable(); private readonly BindableList samplesBindable = new BindableList(); private readonly Bindable userPositionalHitSounds = new Bindable(); private readonly Bindable comboIndexBindable = new Bindable(); public override bool RemoveWhenNotAlive => false; public override bool RemoveCompletedTransforms => false; protected override bool RequiresChildrenUpdate => true; public override bool IsPresent => base.IsPresent || (State.Value == ArmedState.Idle && Clock?.CurrentTime >= LifetimeStart); private readonly Bindable state = new Bindable(); /// /// The state of this . /// /// /// For pooled hitobjects, is recommended to be used instead for better editor/rewinding support. /// public IBindable State => state; /// /// Whether is currently applied. /// private bool hasHitObjectApplied; /// /// The controlling the lifetime of the currently-attached . /// [CanBeNull] private HitObjectLifetimeEntry lifetimeEntry; [Resolved(CanBeNull = true)] private IPooledHitObjectProvider pooledObjectProvider { get; set; } /// /// Whether the initialization logic in has applied. /// internal bool IsInitialized; /// /// Creates a new . /// /// /// The to be initially applied to this . /// If null, a hitobject is expected to be later applied via (or automatically via pooling). /// protected DrawableHitObject([CanBeNull] HitObject initialHitObject = null) { HitObject = initialHitObject; } [BackgroundDependencyLoader] private void load(OsuConfigManager config) { config.BindWith(OsuSetting.PositionalHitSounds, userPositionalHitSounds); // Explicit non-virtual function call. base.AddInternal(Samples = new PausableSkinnableSound()); } protected override void LoadAsyncComplete() { base.LoadAsyncComplete(); if (HitObject != null) Apply(HitObject, lifetimeEntry); } protected override void LoadComplete() { base.LoadComplete(); comboIndexBindable.BindValueChanged(_ => UpdateComboColour(), true); updateState(ArmedState.Idle, true); } /// /// Applies a new to be represented by this . /// /// The to apply. /// The controlling the lifetime of . public void Apply([NotNull] HitObject hitObject, [CanBeNull] HitObjectLifetimeEntry lifetimeEntry) { free(); HitObject = hitObject ?? throw new InvalidOperationException($"Cannot apply a null {nameof(HitObject)}."); this.lifetimeEntry = lifetimeEntry; if (lifetimeEntry != null) { // Transfer lifetime from the entry. LifetimeStart = lifetimeEntry.LifetimeStart; LifetimeEnd = lifetimeEntry.LifetimeEnd; // Copy any existing result from the entry (required for rewind / judgement revert). Result = lifetimeEntry.Result; } else LifetimeStart = HitObject.StartTime - InitialLifetimeOffset; // Ensure this DHO has a result. Result ??= CreateResult(HitObject.CreateJudgement()) ?? throw new InvalidOperationException($"{GetType().ReadableName()} must provide a {nameof(JudgementResult)} through {nameof(CreateResult)}."); // Copy back the result to the entry for potential future retrieval. if (lifetimeEntry != null) lifetimeEntry.Result = Result; foreach (var h in HitObject.NestedHitObjects) { var pooledDrawableNested = pooledObjectProvider?.GetPooledDrawableRepresentation(h, this); var drawableNested = pooledDrawableNested ?? CreateNestedHitObject(h) ?? throw new InvalidOperationException($"{nameof(CreateNestedHitObject)} returned null for {h.GetType().ReadableName()}."); // Only invoke the event for non-pooled DHOs, otherwise the event will be fired by the playfield. if (pooledDrawableNested == null) OnNestedDrawableCreated?.Invoke(drawableNested); drawableNested.OnNewResult += onNewResult; drawableNested.OnRevertResult += onRevertResult; drawableNested.ApplyCustomUpdateState += onApplyCustomUpdateState; // This is only necessary for non-pooled DHOs. For pooled DHOs, this is handled inside GetPooledDrawableRepresentation(). // Must be done before the nested DHO is added to occur before the nested Apply()! drawableNested.ParentHitObject = this; nestedHitObjects.Value.Add(drawableNested); AddNestedHitObject(drawableNested); } StartTimeBindable.BindTo(HitObject.StartTimeBindable); StartTimeBindable.BindValueChanged(onStartTimeChanged); if (HitObject is IHasComboInformation combo) comboIndexBindable.BindTo(combo.ComboIndexBindable); samplesBindable.BindTo(HitObject.SamplesBindable); samplesBindable.BindCollectionChanged(onSamplesChanged, true); HitObject.DefaultsApplied += onDefaultsApplied; OnApply(); HitObjectApplied?.Invoke(this); // If not loaded, the state update happens in LoadComplete(). if (IsLoaded) { if (Result.IsHit) updateState(ArmedState.Hit, true); else if (Result.HasResult) updateState(ArmedState.Miss, true); else updateState(ArmedState.Idle, true); } hasHitObjectApplied = true; } /// /// Removes the currently applied /// private void free() { if (!hasHitObjectApplied) return; StartTimeBindable.UnbindFrom(HitObject.StartTimeBindable); if (HitObject is IHasComboInformation combo) comboIndexBindable.UnbindFrom(combo.ComboIndexBindable); samplesBindable.UnbindFrom(HitObject.SamplesBindable); // Changes in start time trigger state updates. When a new hitobject is applied, OnApply() automatically performs a state update anyway. StartTimeBindable.ValueChanged -= onStartTimeChanged; // When a new hitobject is applied, the samples will be cleared before re-populating. // In order to stop this needless update, the event is unbound and re-bound as late as possible in Apply(). samplesBindable.CollectionChanged -= onSamplesChanged; // Release the samples for other hitobjects to use. Samples.Samples = null; if (nestedHitObjects.IsValueCreated) { foreach (var obj in nestedHitObjects.Value) { obj.OnNewResult -= onNewResult; obj.OnRevertResult -= onRevertResult; obj.ApplyCustomUpdateState -= onApplyCustomUpdateState; } nestedHitObjects.Value.Clear(); ClearNestedHitObjects(); } HitObject.DefaultsApplied -= onDefaultsApplied; OnFree(); HitObject = null; ParentHitObject = null; Result = null; lifetimeEntry = null; clearExistingStateTransforms(); hasHitObjectApplied = false; } protected sealed override void FreeAfterUse() { base.FreeAfterUse(); // Freeing while not in a pool would cause the DHO to not be usable elsewhere in the hierarchy without being re-applied. if (!IsInPool) return; free(); } /// /// Invoked for this to take on any values from a newly-applied . /// protected virtual void OnApply() { } /// /// Invoked for this to revert any values previously taken on from the currently-applied . /// protected virtual void OnFree() { } /// /// Invoked by the base to populate samples, once on initial load and potentially again on any change to the samples collection. /// protected virtual void LoadSamples() { var samples = GetSamples().ToArray(); if (samples.Length <= 0) return; if (HitObject.SampleControlPoint == null) { throw new InvalidOperationException($"{nameof(HitObject)}s must always have an attached {nameof(HitObject.SampleControlPoint)}." + $" This is an indication that {nameof(HitObject.ApplyDefaults)} has not been invoked on {this}."); } Samples.Samples = samples.Select(s => HitObject.SampleControlPoint.ApplyTo(s)).Cast().ToArray(); } private void onSamplesChanged(object sender, NotifyCollectionChangedEventArgs e) => LoadSamples(); private void onStartTimeChanged(ValueChangedEvent startTime) => updateState(State.Value, true); private void onNewResult(DrawableHitObject drawableHitObject, JudgementResult result) => OnNewResult?.Invoke(drawableHitObject, result); private void onRevertResult(DrawableHitObject drawableHitObject, JudgementResult result) => OnRevertResult?.Invoke(drawableHitObject, result); private void onApplyCustomUpdateState(DrawableHitObject drawableHitObject, ArmedState state) => ApplyCustomUpdateState?.Invoke(drawableHitObject, state); private void onDefaultsApplied(HitObject hitObject) { Apply(hitObject, lifetimeEntry); DefaultsApplied?.Invoke(this); } /// /// Invoked by the base to add nested s to the hierarchy. /// /// The to be added. protected virtual void AddNestedHitObject(DrawableHitObject hitObject) { } /// /// Invoked by the base to remove all previously-added nested s. /// protected virtual void ClearNestedHitObjects() { } /// /// Creates the drawable representation for a nested . /// /// The . /// The drawable representation for . protected virtual DrawableHitObject CreateNestedHitObject(HitObject hitObject) => null; #region State / Transform Management /// /// Invoked by this or a nested to apply a custom state that can override the default implementation. /// public event Action ApplyCustomUpdateState; protected override void ClearInternal(bool disposeChildren = true) => throw new InvalidOperationException($"Should never clear a {nameof(DrawableHitObject)}"); private void updateState(ArmedState newState, bool force = false) { if (State.Value == newState && !force) return; LifetimeEnd = double.MaxValue; double transformTime = HitObject.StartTime - InitialLifetimeOffset; clearExistingStateTransforms(); using (BeginAbsoluteSequence(transformTime, true)) UpdateInitialTransforms(); using (BeginAbsoluteSequence(StateUpdateTime, true)) UpdateStartTimeStateTransforms(); #pragma warning disable 618 using (BeginAbsoluteSequence(StateUpdateTime + (Result?.TimeOffset ?? 0), true)) UpdateStateTransforms(newState); #pragma warning restore 618 using (BeginAbsoluteSequence(HitStateUpdateTime, true)) UpdateHitStateTransforms(newState); state.Value = newState; if (LifetimeEnd == double.MaxValue && (state.Value != ArmedState.Idle || HitObject.HitWindows == null)) LifetimeEnd = Math.Max(LatestTransformEndTime, HitStateUpdateTime + (Samples?.Length ?? 0)); // apply any custom state overrides ApplyCustomUpdateState?.Invoke(this, newState); if (!force && newState == ArmedState.Hit) PlaySamples(); } private void clearExistingStateTransforms() { base.ApplyTransformsAt(double.MinValue, true); // has to call this method directly (not ClearTransforms) to bypass the local ClearTransformsAfter override. base.ClearTransformsAfter(double.MinValue, true); } /// /// Apply (generally fade-in) transforms leading into the start time. /// The local drawable hierarchy is recursively delayed to for convenience. /// /// By default this will fade in the object from zero with no duration. /// /// /// This is called once before every . This is to ensure a good state in the case /// the was negative and potentially altered the pre-hit transforms. /// protected virtual void UpdateInitialTransforms() { this.FadeInFromZero(); } /// /// Apply transforms based on the current . Previous states are automatically cleared. /// In the case of a non-idle , and if was not set during this call, will be invoked. /// /// The new armed state. [Obsolete("Use UpdateStartTimeStateTransforms and UpdateHitStateTransforms instead")] // Can be removed 20210504 protected virtual void UpdateStateTransforms(ArmedState state) { } /// /// Apply passive transforms at the 's StartTime. /// This is called each time changes. /// Previous states are automatically cleared. /// protected virtual void UpdateStartTimeStateTransforms() { } /// /// Apply transforms based on the current . This call is offset by (HitObject.EndTime + Result.Offset), equivalent to when the user hit the object. /// If was not set during this call, will be invoked. /// Previous states are automatically cleared. /// /// The new armed state. protected virtual void UpdateHitStateTransforms(ArmedState state) { } public override void ClearTransformsAfter(double time, bool propagateChildren = false, string targetMember = null) { // Parent calls to this should be blocked for safety, as we are manually handling this in updateState. } public override void ApplyTransformsAt(double time, bool propagateChildren = false) { // Parent calls to this should be blocked for safety, as we are manually handling this in updateState. } #endregion protected sealed override void SkinChanged(ISkinSource skin, bool allowFallback) { base.SkinChanged(skin, allowFallback); UpdateComboColour(); ApplySkin(skin, allowFallback); if (IsLoaded) updateState(State.Value, true); } protected void UpdateComboColour() { if (!(HitObject is IHasComboInformation combo)) return; var comboColours = CurrentSkin.GetConfig>(GlobalSkinColours.ComboColours)?.Value ?? Array.Empty(); AccentColour.Value = combo.GetComboColour(comboColours); } /// /// Called to retrieve the combo colour. Automatically assigned to . /// Defaults to using to decide on a colour. /// /// /// This will only be called if the implements . /// /// A list of combo colours provided by the beatmap or skin. Can be null if not available. [Obsolete("Unused. Implement IHasComboInformation and IHasComboInformation.GetComboColour() on the HitObject model instead.")] // Can be removed 20210527 protected virtual Color4 GetComboColour(IReadOnlyList comboColours) { if (!(HitObject is IHasComboInformation combo)) throw new InvalidOperationException($"{nameof(HitObject)} must implement {nameof(IHasComboInformation)}"); return comboColours?.Count > 0 ? comboColours[combo.ComboIndex % comboColours.Count] : Color4.White; } /// /// Called when a change is made to the skin. /// /// The new skin. /// Whether fallback to default skin should be allowed if the custom skin is missing this resource. protected virtual void ApplySkin(ISkinSource skin, bool allowFallback) { } /// /// Calculate the position to be used for sample playback at a specified X position (0..1). /// /// The lookup X position. Generally should be . /// protected double CalculateSamplePlaybackBalance(double position) { const float balance_adjust_amount = 0.4f; return balance_adjust_amount * (userPositionalHitSounds.Value ? position - 0.5f : 0); } /// /// Plays all the hit sounds for this . /// This is invoked automatically when this is hit. /// public virtual void PlaySamples() { if (Samples != null) { Samples.Balance.Value = CalculateSamplePlaybackBalance(SamplePlaybackPosition); Samples.Play(); } } /// /// Stops playback of all relevant samples. Generally only looping samples should be stopped by this, and the rest let to play out. /// Automatically called when 's lifetime has been exceeded. /// public virtual void StopAllSamples() { if (Samples?.Looping == true) Samples.Stop(); } protected override void Update() { base.Update(); if (Result != null && Result.HasResult) { var endTime = HitObject.GetEndTime(); if (Result.TimeOffset + endTime > Time.Current) { OnRevertResult?.Invoke(this, Result); Result.TimeOffset = 0; Result.Type = HitResult.None; updateState(ArmedState.Idle); } } } public override bool UpdateSubTreeMasking(Drawable source, RectangleF maskingBounds) => false; protected override void UpdateAfterChildren() { base.UpdateAfterChildren(); UpdateResult(false); } /// /// Schedules an to this . /// /// /// Only provided temporarily until hitobject pooling is implemented. /// protected internal new ScheduledDelegate Schedule(Action action) => base.Schedule(action); public override double LifetimeStart { get => base.LifetimeStart; set => setLifetime(value, LifetimeEnd); } public override double LifetimeEnd { get => base.LifetimeEnd; set => setLifetime(LifetimeStart, value); } private void setLifetime(double lifetimeStart, double lifetimeEnd) { base.LifetimeStart = lifetimeStart; base.LifetimeEnd = lifetimeEnd; if (lifetimeEntry != null) { lifetimeEntry.LifetimeStart = lifetimeStart; lifetimeEntry.LifetimeEnd = lifetimeEnd; } } /// /// A safe offset prior to the start time of at which this may begin displaying contents. /// By default, s are assumed to display their contents within 10 seconds prior to the start time of . /// /// /// This is only used as an optimisation to delay the initial update of this and may be tuned more aggressively if required. /// It is indirectly used to decide the automatic transform offset provided to . /// A more accurate should be set for further optimisation (in , for example). /// /// Only has an effect if this is not being pooled. /// For pooled s, use instead. /// /// protected virtual double InitialLifetimeOffset => 10000; /// /// The time at which state transforms should be applied that line up to 's StartTime. /// This is used to offset calls to . /// public double StateUpdateTime => HitObject.StartTime; /// /// The time at which judgement dependent state transforms should be applied. This is equivalent of the (end) time of the object, in addition to any judgement offset. /// This is used to offset calls to . /// public double HitStateUpdateTime => Result?.TimeAbsolute ?? HitObject.GetEndTime(); /// /// Will be called at least once after this has become not alive. /// public virtual void OnKilled() { foreach (var nested in NestedHitObjects) nested.OnKilled(); // failsafe to ensure looping samples don't get stuck in a playing state. // this could occur in a non-frame-stable context where DrawableHitObjects get killed before a SkinnableSound has the chance to be stopped. StopAllSamples(); UpdateResult(false); } /// /// The maximum offset from the end time of at which this can be judged. /// The time offset of will be clamped to this value during . /// /// Defaults to the miss window of . /// /// /// /// This does not affect the time offset provided to invocations of . /// protected virtual double MaximumJudgementOffset => HitObject.HitWindows?.WindowFor(HitResult.Miss) ?? 0; /// /// Applies the of this , notifying responders such as /// the of the . /// /// The callback that applies changes to the . protected void ApplyResult(Action application) { if (Result.HasResult) throw new InvalidOperationException("Cannot apply result on a hitobject that already has a result."); application?.Invoke(Result); if (!Result.HasResult) throw new InvalidOperationException($"{GetType().ReadableName()} applied a {nameof(JudgementResult)} but did not update {nameof(JudgementResult.Type)}."); // Some (especially older) rulesets use scorable judgements instead of the newer ignorehit/ignoremiss judgements. // Can be removed 20210328 if (Result.Judgement.MaxResult == HitResult.IgnoreHit) { HitResult originalType = Result.Type; if (Result.Type == HitResult.Miss) Result.Type = HitResult.IgnoreMiss; else if (Result.Type >= HitResult.Meh && Result.Type <= HitResult.Perfect) Result.Type = HitResult.IgnoreHit; if (Result.Type != originalType) { Logger.Log($"{GetType().ReadableName()} applied an invalid hit result ({originalType}) when {nameof(HitResult.IgnoreMiss)} or {nameof(HitResult.IgnoreHit)} is expected.\n" + $"This has been automatically adjusted to {Result.Type}, and support will be removed from 2020-03-28 onwards.", level: LogLevel.Important); } } if (!Result.Type.IsValidHitResult(Result.Judgement.MinResult, Result.Judgement.MaxResult)) { throw new InvalidOperationException( $"{GetType().ReadableName()} applied an invalid hit result (was: {Result.Type}, expected: [{Result.Judgement.MinResult} ... {Result.Judgement.MaxResult}])."); } Result.TimeOffset = Math.Min(MaximumJudgementOffset, Time.Current - HitObject.GetEndTime()); if (Result.HasResult) updateState(Result.IsHit ? ArmedState.Hit : ArmedState.Miss); OnNewResult?.Invoke(this, Result); } /// /// Processes this , checking if a scoring result has occurred. /// /// Whether the user triggered this process. /// Whether a scoring result has occurred from this or any nested . protected bool UpdateResult(bool userTriggered) { // It's possible for input to get into a bad state when rewinding gameplay, so results should not be processed if (Time.Elapsed < 0) return false; if (Judged) return false; CheckForResult(userTriggered, Time.Current - HitObject.GetEndTime()); return Judged; } /// /// Checks if a scoring result has occurred for this . /// /// /// If a scoring result has occurred, this method must invoke to update the result and notify responders. /// /// Whether the user triggered this check. /// The offset from the end time of the at which this check occurred. /// A > 0 implies that this check occurred after the end time of the . protected virtual void CheckForResult(bool userTriggered, double timeOffset) { } /// /// Creates the that represents the scoring result for this . /// /// The that provides the scoring information. protected virtual JudgementResult CreateResult(Judgement judgement) => new JudgementResult(HitObject, judgement); protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); if (HitObject != null) HitObject.DefaultsApplied -= onDefaultsApplied; } } public abstract class DrawableHitObject : DrawableHitObject where TObject : HitObject { public new TObject HitObject => (TObject)base.HitObject; protected DrawableHitObject(TObject hitObject) : base(hitObject) { } } }