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

152 lines
4.9 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.
2018-11-13 04:00:00 +00:00
using System;
using osu.Framework;
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.Framework.Timing;
using osu.Game.Beatmaps;
2019-04-25 08:36:17 +00:00
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Objects;
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, IStateful<PlacementState>
{
2018-11-13 04:00:00 +00:00
/// <summary>
/// Invoked when <see cref="State"/> has changed.
/// </summary>
public event Action<PlacementState> StateChanged;
/// <summary>
/// Whether the <see cref="HitObject"/> is currently being placed, but has not necessarily finished being placed.
/// </summary>
public bool PlacementBegun { get; private set; }
/// <summary>
/// The <see cref="HitObject"/> that is being placed.
/// </summary>
protected readonly HitObject HitObject;
2018-10-10 06:32:55 +00:00
protected IClock EditorClock { get; private set; }
private readonly IBindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
[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;
Alpha = 0;
}
[BackgroundDependencyLoader]
2019-02-01 06:42:15 +00:00
private void load(IBindable<WorkingBeatmap> beatmap, IAdjustableClock clock)
{
this.beatmap.BindTo(beatmap);
2018-10-10 06:32:55 +00:00
EditorClock = clock;
ApplyDefaultsToHitObject();
}
2018-11-13 04:00:00 +00:00
private PlacementState state;
public PlacementState State
{
get => state;
set
{
if (state == value)
return;
2019-02-28 04:31:40 +00:00
2018-11-13 04:00:00 +00:00
state = value;
if (state == PlacementState.Shown)
Show();
else
Hide();
StateChanged?.Invoke(value);
}
}
/// <summary>
/// Signals that the placement of <see cref="HitObject"/> has started.
/// </summary>
/// <param name="startTime">The start time of <see cref="HitObject"/> at the placement point. If null, the current clock time is used.</param>
protected void BeginPlacement(double? startTime = null)
{
HitObject.StartTime = startTime ?? EditorClock.CurrentTime;
placementHandler.BeginPlacement(HitObject);
PlacementBegun = true;
}
/// <summary>
/// Signals that the placement of <see cref="HitObject"/> has finished.
2018-11-06 09:04:03 +00:00
/// This will destroy this <see cref="PlacementBlueprint"/>, and add the <see cref="HitObject"/> to the <see cref="Beatmap"/>.
/// </summary>
/// <param name="commit">Whether the object should be committed.</param>
public void EndPlacement(bool commit)
{
if (!PlacementBegun)
BeginPlacement();
placementHandler.EndPlacement(HitObject, commit);
}
/// <summary>
/// Updates the position of this <see cref="PlacementBlueprint"/> to a new screen-space position.
/// </summary>
/// <param name="screenSpacePosition">The screen-space position.</param>
public abstract void UpdatePosition(Vector2 screenSpacePosition);
/// <summary>
2019-04-25 08:36:17 +00:00
/// Invokes <see cref="Objects.HitObject.ApplyDefaults(ControlPointInfo,BeatmapDifficulty)"/>,
/// 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 MouseButtonEvent _:
return true;
2019-04-01 03:44:46 +00:00
default:
return false;
}
}
}
2018-11-13 04:00:00 +00:00
public enum PlacementState
{
Hidden,
Shown,
}
}