1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 06:43:22 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Edit/Blueprints/ManiaPlacementBlueprint.cs

119 lines
4.1 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 osu.Framework.Allocation;
using osu.Framework.Graphics;
2018-11-29 18:29:36 +08:00
using osu.Framework.Input;
2018-11-13 13:13:29 +08:00
using osu.Framework.Input.Events;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Mania.Objects;
2018-11-19 17:40:16 +08:00
using osu.Game.Rulesets.Mania.Objects.Drawables.Pieces;
using osu.Game.Rulesets.Mania.UI;
using osu.Game.Rulesets.UI.Scrolling;
2018-11-26 09:44:48 +08:00
using osuTK;
namespace osu.Game.Rulesets.Mania.Edit.Blueprints
{
2018-11-29 18:29:36 +08:00
public abstract class ManiaPlacementBlueprint<T> : PlacementBlueprint,
IRequireHighFrequencyMousePosition // the playfield could be moving behind us
where T : ManiaHitObject
{
protected new T HitObject => (T)base.HitObject;
2018-11-29 18:29:36 +08:00
protected Column Column;
2018-11-13 13:13:29 +08:00
/// <summary>
/// The current mouse position, snapped to the closest column.
/// </summary>
protected Vector2 SnappedMousePosition { get; private set; }
2018-11-19 17:40:16 +08:00
/// <summary>
/// The width of the closest column to the current mouse position.
/// </summary>
protected float SnappedWidth { get; private set; }
[Resolved]
2018-11-12 18:41:06 +08:00
private IManiaHitObjectComposer composer { get; set; }
[Resolved]
private IScrollingInfo scrollingInfo { get; set; }
2018-11-19 16:59:52 +08:00
protected ManiaPlacementBlueprint(T hitObject)
: base(hitObject)
{
RelativeSizeAxes = Axes.None;
}
2018-11-29 18:29:36 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
{
if (Column == null)
return base.OnMouseDown(e);
HitObject.StartTime = TimeAt(e.ScreenSpaceMousePosition);
HitObject.Column = Column.Index;
BeginPlacement();
return true;
}
protected override bool OnMouseUp(MouseUpEvent e)
{
EndPlacement();
return base.OnMouseUp(e);
}
2018-11-13 13:13:29 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
{
2018-11-29 18:29:36 +08:00
if (!PlacementBegun)
Column = ColumnAt(e.ScreenSpaceMousePosition);
2018-11-19 17:02:01 +08:00
2018-11-29 18:29:36 +08:00
if (Column == null) return false;
2018-11-19 17:05:21 +08:00
2018-11-29 18:29:36 +08:00
SnappedWidth = Column.DrawWidth;
2018-11-19 16:59:52 +08:00
2018-11-29 13:15:23 +08:00
// Snap to the column
2018-11-29 18:29:36 +08:00
var parentPos = Parent.ToLocalSpace(Column.ToScreenSpace(new Vector2(Column.DrawWidth / 2, 0)));
2018-11-29 13:15:23 +08:00
SnappedMousePosition = new Vector2(parentPos.X, e.MousePosition.Y);
2018-11-19 16:59:52 +08:00
return true;
2018-11-13 13:13:29 +08:00
}
protected double TimeAt(Vector2 screenSpacePosition)
{
2018-11-29 18:29:36 +08:00
if (Column == null)
return 0;
2018-11-29 18:29:36 +08:00
var hitObjectContainer = Column.HitObjectContainer;
// If we're scrolling downwards, a position of 0 is actually further away from the hit target
// so we need to flip the vertical coordinate in the hitobject container's space
2018-11-29 18:29:36 +08:00
var hitObjectPos = Column.HitObjectContainer.ToLocalSpace(applyPositionOffset(screenSpacePosition, false)).Y;
if (scrollingInfo.Direction.Value == ScrollingDirection.Down)
hitObjectPos = hitObjectContainer.DrawHeight - hitObjectPos;
return scrollingInfo.Algorithm.TimeAt(hitObjectPos,
EditorClock.CurrentTime,
scrollingInfo.TimeRange.Value,
hitObjectContainer.DrawHeight);
}
2018-11-29 18:29:36 +08:00
protected float PositionAt(double time)
{
var pos = scrollingInfo.Algorithm.PositionAt(time,
EditorClock.CurrentTime,
scrollingInfo.TimeRange.Value,
Column.HitObjectContainer.DrawHeight);
return applyPositionOffset(Column.HitObjectContainer.ToSpaceOfOtherDrawable(new Vector2(0, pos), Parent), true).Y;
}
protected Column ColumnAt(Vector2 screenSpacePosition)
2018-11-29 18:29:36 +08:00
=> composer.ColumnAt(applyPositionOffset(screenSpacePosition, false));
2018-11-29 18:29:36 +08:00
private Vector2 applyPositionOffset(Vector2 position, bool reverse)
{
2018-11-29 18:29:36 +08:00
position.Y += (scrollingInfo.Direction.Value == ScrollingDirection.Up && !reverse ? -1 : 1) * NotePiece.NOTE_HEIGHT / 2;
return position;
}
}
}