2019-01-24 16:43:03 +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-10-03 15:27:26 +08:00
2018-10-25 17:16:25 +08:00
using osu.Framework.Graphics ;
2018-10-03 15:27:26 +08:00
using osu.Framework.Graphics.Containers ;
2023-05-12 14:50:33 +08:00
using osu.Framework.Input.Bindings ;
2018-10-03 15:27:26 +08:00
using osu.Framework.Input.Events ;
2023-05-12 14:50:33 +08:00
using osu.Game.Input.Bindings ;
2018-10-03 15:27:26 +08:00
using osu.Game.Rulesets.Objects ;
2018-11-20 15:51:59 +08:00
using osuTK ;
2021-04-22 14:27:08 +08:00
using osuTK.Input ;
2018-10-03 15:27:26 +08:00
namespace osu.Game.Rulesets.Edit
{
2018-10-31 11:01:10 +08:00
/// <summary>
2024-09-23 22:33:36 +08:00
/// A blueprint which governs the placement of something.
2018-10-31 11:01:10 +08:00
/// </summary>
2023-05-12 14:50:33 +08:00
public abstract partial class PlacementBlueprint : CompositeDrawable , IKeyBindingHandler < GlobalAction >
2018-10-03 15:27:26 +08:00
{
2018-11-13 12:00:00 +08:00
/// <summary>
2020-02-13 08:03:48 +08:00
/// Whether the <see cref="HitObject"/> is currently mid-placement, but has not necessarily finished being placed.
2018-11-13 12:00:00 +08:00
/// </summary>
2021-04-16 13:10:21 +08:00
public PlacementState PlacementActive { get ; private set ; }
2018-11-13 12:00:00 +08:00
2023-05-12 15:00:40 +08: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 ;
2024-09-23 22:33:36 +08:00
protected PlacementBlueprint ( )
2018-10-03 15:27:26 +08:00
{
2018-10-25 17:16:25 +08:00
RelativeSizeAxes = Axes . Both ;
2018-11-13 11:52:04 +08:00
2018-11-29 13:55:20 +08: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 15:27:26 +08:00
}
/// <summary>
2024-09-23 22:33:36 +08:00
/// Signals that the placement has started.
2018-10-03 15:27:26 +08:00
/// </summary>
2024-09-23 22:33:36 +08:00
/// <param name="commitStart">Whether this call is committing a value and continuing with further adjustments.</param>
protected virtual void BeginPlacement ( bool commitStart = false )
2018-10-17 13:37:39 +08:00
{
2021-04-16 13:10:21 +08:00
if ( commitStart )
PlacementActive = PlacementState . Active ;
2018-10-17 13:37:39 +08:00
}
/// <summary>
/// Signals that the placement of <see cref="HitObject"/> has finished.
2024-09-23 22:33:36 +08:00
/// This will destroy this <see cref="PlacementBlueprint"/>, and commit the changes.
2018-10-17 13:37:39 +08:00
/// </summary>
2024-09-23 22:33:36 +08:00
/// <param name="commit">Whether the changes should be committed. Note that a commit may fail if <see cref="IsValidForPlacement"/> is <c>false</c>.</param>
public virtual void EndPlacement ( bool commit )
2018-10-17 13:37:39 +08:00
{
2021-04-16 13:10:21 +08:00
switch ( PlacementActive )
{
case PlacementState . Finished :
return ;
case PlacementState . Waiting :
// ensure placement was started before ending to make state handling simpler.
BeginPlacement ( ) ;
break ;
}
PlacementActive = PlacementState . Finished ;
2018-10-17 13:37:39 +08:00
}
2018-10-03 15:27:26 +08:00
2024-10-03 17:16:37 +08:00
/// <summary>
/// Determines which objects to snap to for the snap result in <see cref="UpdateTimeAndPosition"/>.
/// </summary>
2024-09-23 23:24:09 +08:00
public virtual SnapType SnapType = > SnapType . All ;
2024-09-23 22:33:36 +08:00
/// <summary>
/// Updates the time and position of this <see cref="PlacementBlueprint"/> based on the provided snap information.
/// </summary>
/// <param name="result">The snap result information.</param>
public virtual void UpdateTimeAndPosition ( SnapResult result )
{
}
2023-05-12 14:50:33 +08:00
public bool OnPressed ( KeyBindingPressEvent < GlobalAction > e )
{
if ( PlacementActive = = PlacementState . Waiting )
return false ;
switch ( e . Action )
{
case GlobalAction . Back :
EndPlacement ( false ) ;
return true ;
default :
return false ;
}
}
public void OnReleased ( KeyBindingReleaseEvent < GlobalAction > e )
{
}
2023-07-11 16:29:53 +08:00
public override bool ReceivePositionalInputAt ( Vector2 screenSpacePos ) = > true ;
2018-10-03 15:27:26 +08:00
protected override bool Handle ( UIEvent e )
{
base . Handle ( e ) ;
switch ( e )
{
2022-06-24 20:25:23 +08:00
case ScrollEvent :
2018-10-31 16:23:27 +08:00
return false ;
2019-04-01 11:44:46 +08:00
2022-06-24 20:25:23 +08:00
case DoubleClickEvent :
2020-04-13 12:57:15 +08:00
return false ;
2021-04-22 14:27:08 +08:00
case MouseButtonEvent mouse :
// placement blueprints should generally block mouse from reaching underlying components (ie. performing clicks on interface buttons).
2024-08-05 12:20:44 +08:00
return mouse . Button = = MouseButton . Left | | PlacementActive = = PlacementState . Active ;
2019-04-01 11:44:46 +08:00
2018-10-03 15:27:26 +08:00
default :
return false ;
}
}
2021-04-16 13:10:21 +08:00
public enum PlacementState
{
Waiting ,
Active ,
Finished
}
2018-10-03 15:27:26 +08:00
}
}