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-10-03 16:27:26 +09:00
2022-06-18 13:49:57 +09:00
using System.Linq ;
2020-05-15 18:07:41 +09:00
using System.Threading ;
2018-10-03 16:27:26 +09:00
using osu.Framework.Allocation ;
2019-02-21 19:04:31 +09:00
using osu.Framework.Bindables ;
2018-10-25 18:16:25 +09:00
using osu.Framework.Graphics ;
2018-10-03 16:27:26 +09:00
using osu.Framework.Graphics.Containers ;
2023-05-12 15:50:33 +09:00
using osu.Framework.Input.Bindings ;
2018-10-03 16:27:26 +09:00
using osu.Framework.Input.Events ;
2021-02-11 17:16:17 +09:00
using osu.Game.Audio ;
2018-10-03 16:27:26 +09:00
using osu.Game.Beatmaps ;
2019-04-25 17:36:17 +09:00
using osu.Game.Beatmaps.ControlPoints ;
2023-05-12 15:50:33 +09:00
using osu.Game.Input.Bindings ;
2018-10-03 16:27:26 +09:00
using osu.Game.Rulesets.Objects ;
2023-05-17 17:20:15 +09:00
using osu.Game.Rulesets.Objects.Types ;
2020-05-22 16:37:28 +09:00
using osu.Game.Screens.Edit ;
2018-11-06 18:28:22 +09:00
using osu.Game.Screens.Edit.Compose ;
2018-11-20 16:51:59 +09:00
using osuTK ;
2021-04-22 15:27:08 +09:00
using osuTK.Input ;
2018-10-03 16:27:26 +09:00
namespace osu.Game.Rulesets.Edit
{
2018-10-31 12:01:10 +09:00
/// <summary>
2018-11-06 18:06:34 +09:00
/// A blueprint which governs the creation of a new <see cref="HitObject"/> to actualisation.
2018-10-31 12:01:10 +09:00
/// </summary>
2023-05-12 15:50:33 +09:00
public abstract partial class PlacementBlueprint : CompositeDrawable , IKeyBindingHandler < GlobalAction >
2018-10-03 16:27:26 +09:00
{
2018-11-13 13:00:00 +09:00
/// <summary>
2020-02-13 09:03:48 +09:00
/// Whether the <see cref="HitObject"/> is currently mid-placement, but has not necessarily finished being placed.
2018-11-13 13:00:00 +09:00
/// </summary>
2021-04-16 14:10:21 +09:00
public PlacementState PlacementActive { get ; private set ; }
2018-11-13 13:00:00 +09:00
2023-05-24 17:11:12 +09:00
/// <summary>
/// Whether the sample bank should be taken from the previous hit object.
/// </summary>
2023-05-25 21:32:19 +02:00
public bool AutomaticBankAssignment { get ; set ; }
2023-05-24 17:11:12 +09:00
2018-10-03 16:27:26 +09:00
/// <summary>
/// The <see cref="HitObject"/> that is being placed.
/// </summary>
2020-09-25 14:10:30 +09:00
public readonly HitObject HitObject ;
2018-10-03 16:27:26 +09:00
2023-05-17 16:58:23 +09:00
[Resolved]
2023-05-17 20:54:17 +09:00
protected EditorClock EditorClock { get ; private set ; } = null ! ;
2018-10-03 16:44:37 +09:00
2021-05-19 18:34:02 +09:00
[Resolved]
2023-05-17 16:58:23 +09:00
private EditorBeatmap beatmap { get ; set ; } = null ! ;
2018-10-17 18:36:47 +09:00
2023-05-17 16:58:23 +09:00
private Bindable < double > startTimeBindable = null ! ;
2018-10-17 18:36:47 +09:00
2023-05-17 16:58:43 +09:00
private HitObject ? getPreviousHitObject ( ) = > beatmap . HitObjects . TakeWhile ( h = > h . StartTime < = startTimeBindable . Value ) . LastOrDefault ( ) ;
2020-05-22 22:41:06 +09:00
2018-10-17 15:46:30 +09:00
[Resolved]
2023-05-17 16:58:23 +09:00
private IPlacementHandler placementHandler { get ; set ; } = null ! ;
2018-10-17 15:46:30 +09:00
2023-05-12 16:00:40 +09:00
/// <summary>
/// Whether this blueprint is currently in a state that can be committed.
/// </summary>
/// <remarks>
/// Override this with any preconditions that should be double-checked on committing.
/// If <c>false</c> is returned and a commit is attempted, the blueprint will be destroyed instead.
/// </remarks>
protected virtual bool IsValidForPlacement = > true ;
2018-11-06 18:04:03 +09:00
protected PlacementBlueprint ( HitObject hitObject )
2018-10-03 16:27:26 +09:00
{
HitObject = hitObject ;
2018-10-25 18:16:25 +09:00
2021-02-11 17:16:17 +09:00
// adding the default hit sample should be the case regardless of the ruleset.
2023-05-18 14:12:57 +09:00
HitObject . Samples . Add ( new HitSampleInfo ( HitSampleInfo . HIT_NORMAL ) ) ;
2021-02-11 17:16:17 +09:00
2018-10-25 18:16:25 +09:00
RelativeSizeAxes = Axes . Both ;
2018-11-13 12:52:04 +09:00
2018-11-29 14:55:20 +09:00
// This is required to allow the blueprint's position to be updated via OnMouseMove/Handle
// on the same frame it is made visible via a PlacementState change.
AlwaysPresent = true ;
2018-10-03 16:27:26 +09:00
}
[BackgroundDependencyLoader]
2021-05-19 18:34:02 +09:00
private void load ( )
2018-10-03 16:27:26 +09:00
{
2020-05-22 22:41:06 +09:00
startTimeBindable = HitObject . StartTimeBindable . GetBoundCopy ( ) ;
startTimeBindable . BindValueChanged ( _ = > ApplyDefaultsToHitObject ( ) , true ) ;
2018-10-03 16:27:26 +09:00
}
/// <summary>
2018-10-17 14:37:39 +09:00
/// Signals that the placement of <see cref="HitObject"/> has started.
2018-10-03 16:27:26 +09:00
/// </summary>
2020-02-13 09:03:48 +09:00
/// <param name="commitStart">Whether this call is committing a value for HitObject.StartTime and continuing with further adjustments.</param>
2020-05-20 18:46:15 +09:00
protected void BeginPlacement ( bool commitStart = false )
2018-10-17 14:37:39 +09:00
{
2018-10-17 15:46:30 +09:00
placementHandler . BeginPlacement ( HitObject ) ;
2021-04-16 14:10:21 +09:00
if ( commitStart )
PlacementActive = PlacementState . Active ;
2018-10-17 14:37:39 +09:00
}
/// <summary>
/// Signals that the placement of <see cref="HitObject"/> has finished.
2020-02-13 09:03:48 +09:00
/// This will destroy this <see cref="PlacementBlueprint"/>, and add the HitObject.StartTime to the <see cref="Beatmap"/>.
2018-10-17 14:37:39 +09:00
/// </summary>
2023-05-12 16:00:40 +09:00
/// <param name="commit">Whether the object should be committed. Note that a commit may fail if <see cref="IsValidForPlacement"/> is <c>false</c>.</param>
2020-02-07 18:02:48 +09:00
public void EndPlacement ( bool commit )
2018-10-17 14:37:39 +09:00
{
2021-04-16 14:10:21 +09:00
switch ( PlacementActive )
{
case PlacementState . Finished :
return ;
case PlacementState . Waiting :
// ensure placement was started before ending to make state handling simpler.
BeginPlacement ( ) ;
break ;
}
2023-05-12 16:00:40 +09:00
placementHandler . EndPlacement ( HitObject , IsValidForPlacement & & commit ) ;
2021-04-16 14:10:21 +09:00
PlacementActive = PlacementState . Finished ;
2018-10-17 14:37:39 +09:00
}
2018-10-03 16:27:26 +09:00
2023-05-12 15:50:33 +09:00
public bool OnPressed ( KeyBindingPressEvent < GlobalAction > e )
{
if ( PlacementActive = = PlacementState . Waiting )
return false ;
switch ( e . Action )
{
case GlobalAction . Select :
EndPlacement ( true ) ;
return true ;
case GlobalAction . Back :
EndPlacement ( false ) ;
return true ;
default :
return false ;
}
}
public void OnReleased ( KeyBindingReleaseEvent < GlobalAction > e )
{
}
2019-10-03 16:14:42 +09:00
/// <summary>
2020-11-30 18:35:30 +09:00
/// Updates the time and position of this <see cref="PlacementBlueprint"/> based on the provided snap information.
2019-10-03 16:14:42 +09:00
/// </summary>
2020-05-28 20:33:12 +09:00
/// <param name="result">The snap result information.</param>
2020-11-26 19:16:18 +09:00
public virtual void UpdateTimeAndPosition ( SnapResult result )
2020-05-21 14:38:40 +09:00
{
2021-04-16 14:10:21 +09:00
if ( PlacementActive = = PlacementState . Waiting )
2023-05-17 17:20:15 +09:00
{
2023-05-17 20:54:17 +09:00
HitObject . StartTime = result . Time ? ? EditorClock . CurrentTime ;
2023-05-17 17:20:15 +09:00
if ( HitObject is IHasComboInformation comboInformation )
comboInformation . UpdateComboInformation ( getPreviousHitObject ( ) as IHasComboInformation ) ;
}
2023-05-24 17:11:12 +09:00
2024-07-04 14:52:04 +02:00
var lastHitObject = getPreviousHitObject ( ) ;
2024-07-04 14:40:40 +02:00
2023-05-24 17:11:12 +09:00
if ( AutomaticBankAssignment )
{
2024-07-04 00:56:53 +02:00
// Create samples based on the sample settings of the previous hit object
if ( lastHitObject ! = null )
{
for ( int i = 0 ; i < HitObject . Samples . Count ; i + + )
HitObject . Samples [ i ] = lastHitObject . CreateHitSampleInfo ( HitObject . Samples [ i ] . Name ) ;
2024-07-04 14:40:40 +02:00
}
2024-07-04 14:52:04 +02:00
}
else
{
var lastHitNormal = lastHitObject ? . Samples ? . FirstOrDefault ( o = > o . Name = = HitSampleInfo . HIT_NORMAL ) ;
2023-05-24 17:11:12 +09:00
if ( lastHitNormal ! = null )
2024-07-04 01:09:06 +02:00
{
2024-07-04 14:40:40 +02:00
// Only inherit the volume from the previous hit object
2024-07-04 01:09:06 +02:00
for ( int i = 0 ; i < HitObject . Samples . Count ; i + + )
HitObject . Samples [ i ] = HitObject . Samples [ i ] . With ( newVolume : lastHitNormal . Volume ) ;
2024-07-04 00:56:53 +02:00
}
2023-05-24 17:11:12 +09:00
}
2024-07-04 00:39:12 +02:00
if ( HitObject is IHasRepeats hasRepeats )
{
// Make sure all the node samples are identical to the hit object's samples
for ( int i = 0 ; i < hasRepeats . NodeSamples . Count ; i + + )
hasRepeats . NodeSamples [ i ] = HitObject . Samples . Select ( o = > o . With ( ) ) . ToList ( ) ;
}
2020-05-21 14:38:40 +09:00
}
2019-10-03 16:14:42 +09:00
2018-10-17 18:36:47 +09:00
/// <summary>
2021-10-01 14:56:42 +09:00
/// Invokes <see cref="Objects.HitObject.ApplyDefaults(ControlPointInfo,IBeatmapDifficultyInfo,CancellationToken)"/>,
2019-04-25 17:36:17 +09:00
/// refreshing <see cref="Objects.HitObject.NestedHitObjects"/> and parameters for the <see cref="HitObject"/>.
2018-10-17 18:36:47 +09:00
/// </summary>
2021-10-02 12:34:29 +09:00
protected void ApplyDefaultsToHitObject ( ) = > HitObject . ApplyDefaults ( beatmap . ControlPointInfo , beatmap . Difficulty ) ;
2018-10-17 18:36:47 +09:00
2023-07-11 17:29:53 +09:00
public override bool ReceivePositionalInputAt ( Vector2 screenSpacePos ) = > true ;
2018-10-03 16:27:26 +09:00
protected override bool Handle ( UIEvent e )
{
base . Handle ( e ) ;
switch ( e )
{
2022-06-24 21:25:23 +09:00
case ScrollEvent :
2018-10-31 17:23:27 +09:00
return false ;
2019-04-01 12:44:46 +09:00
2022-06-24 21:25:23 +09:00
case DoubleClickEvent :
2020-04-13 13:57:15 +09:00
return false ;
2021-04-22 15:27:08 +09:00
case MouseButtonEvent mouse :
// placement blueprints should generally block mouse from reaching underlying components (ie. performing clicks on interface buttons).
// for now, the one exception we want to allow is when using a non-main mouse button when shift is pressed, which is used to trigger object deletion
// while in placement mode.
return mouse . Button = = MouseButton . Left | | ! mouse . ShiftPressed ;
2019-04-01 12:44:46 +09:00
2018-10-03 16:27:26 +09:00
default :
return false ;
}
}
2021-04-16 14:10:21 +09:00
public enum PlacementState
{
Waiting ,
Active ,
Finished
}
2018-10-03 16:27:26 +09:00
}
}