osu/osu.Game/Rulesets/Edit/PlacementBlueprint.cs

126 lines
4.6 KiB
C#
Raw Normal View History

// 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.
using System.Threading;
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Game.Beatmaps;
2019-04-25 08:36:17 +00:00
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Objects;
using osu.Game.Screens.Edit;
2018-11-06 09:28:22 +00:00
using osu.Game.Screens.Edit.Compose;
2018-11-20 07:51:59 +00:00
using osuTK;
namespace osu.Game.Rulesets.Edit
{
2018-10-31 03:01:10 +00:00
/// <summary>
2018-11-06 09:06:34 +00:00
/// A blueprint which governs the creation of a new <see cref="HitObject"/> to actualisation.
2018-10-31 03:01:10 +00:00
/// </summary>
public abstract class PlacementBlueprint : CompositeDrawable
{
2018-11-13 04:00:00 +00:00
/// <summary>
/// Whether the <see cref="HitObject"/> is currently mid-placement, but has not necessarily finished being placed.
2018-11-13 04:00:00 +00:00
/// </summary>
public bool PlacementActive { get; private set; }
2018-11-13 04:00:00 +00:00
/// <summary>
/// The <see cref="HitObject"/> that is being placed.
/// </summary>
public readonly HitObject HitObject;
[Resolved(canBeNull: true)]
protected EditorClock EditorClock { get; private set; }
private readonly IBindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
private Bindable<double> startTimeBindable;
[Resolved]
private IPlacementHandler placementHandler { get; set; }
2018-11-06 09:04:03 +00:00
protected PlacementBlueprint(HitObject hitObject)
{
HitObject = hitObject;
RelativeSizeAxes = Axes.Both;
2018-11-29 05:55:20 +00: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;
}
[BackgroundDependencyLoader]
private void load(IBindable<WorkingBeatmap> beatmap)
{
this.beatmap.BindTo(beatmap);
startTimeBindable = HitObject.StartTimeBindable.GetBoundCopy();
startTimeBindable.BindValueChanged(_ => ApplyDefaultsToHitObject(), true);
}
/// <summary>
/// Signals that the placement of <see cref="HitObject"/> has started.
/// </summary>
/// <param name="commitStart">Whether this call is committing a value for HitObject.StartTime and continuing with further adjustments.</param>
protected void BeginPlacement(bool commitStart = false)
{
placementHandler.BeginPlacement(HitObject);
PlacementActive |= commitStart;
}
/// <summary>
/// Signals that the placement of <see cref="HitObject"/> has finished.
/// This will destroy this <see cref="PlacementBlueprint"/>, and add the HitObject.StartTime to the <see cref="Beatmap"/>.
/// </summary>
/// <param name="commit">Whether the object should be committed.</param>
public void EndPlacement(bool commit)
{
if (!PlacementActive)
BeginPlacement();
placementHandler.EndPlacement(HitObject, commit);
PlacementActive = false;
}
/// <summary>
2020-11-30 09:35:30 +00:00
/// Updates the time and position of this <see cref="PlacementBlueprint"/> based on the provided snap information.
/// </summary>
2020-05-28 11:33:12 +00:00
/// <param name="result">The snap result information.</param>
public virtual void UpdateTimeAndPosition(SnapResult result)
{
if (!PlacementActive)
2020-05-28 11:33:12 +00:00
HitObject.StartTime = result.Time ?? EditorClock?.CurrentTime ?? Time.Current;
}
/// <summary>
/// Invokes <see cref="Objects.HitObject.ApplyDefaults(ControlPointInfo,BeatmapDifficulty, CancellationToken)"/>,
2019-04-25 08:36:17 +00:00
/// refreshing <see cref="Objects.HitObject.NestedHitObjects"/> and parameters for the <see cref="HitObject"/>.
/// </summary>
protected void ApplyDefaultsToHitObject() => HitObject.ApplyDefaults(beatmap.Value.Beatmap.ControlPointInfo, beatmap.Value.Beatmap.BeatmapInfo.BaseDifficulty);
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => Parent?.ReceivePositionalInputAt(screenSpacePos) ?? false;
protected override bool Handle(UIEvent e)
{
base.Handle(e);
switch (e)
{
case ScrollEvent _:
return false;
2019-04-01 03:44:46 +00:00
case DoubleClickEvent _:
return false;
case MouseButtonEvent _:
return true;
2019-04-01 03:44:46 +00:00
default:
return false;
}
}
}
}