Move timing change stuff to osu.Game project.

This commit is contained in:
smoogipooo
2017-06-02 20:17:44 +09:00
parent e5720b9477
commit 7022c8d496
15 changed files with 43 additions and 28 deletions

View File

@ -1,6 +1,8 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Game.Rulesets.Timing;
namespace osu.Game.Rulesets.Mania.Timing.Drawables
{
public class DrawableGravityTimingChange : DrawableManiaTimingChange

View File

@ -2,6 +2,8 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics;
using osu.Game.Rulesets.Timing;
using osu.Game.Rulesets.Timing.Drawables;
namespace osu.Game.Rulesets.Mania.Timing.Drawables
{

View File

@ -1,6 +1,8 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Game.Rulesets.Timing;
namespace osu.Game.Rulesets.Mania.Timing.Drawables
{
public class DrawableScrollingTimingChange : DrawableManiaTimingChange

View File

@ -1,133 +0,0 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using OpenTK;
namespace osu.Game.Rulesets.Mania.Timing.Drawables
{
/// <summary>
/// Represents a container in which contains hit objects and moves relative to the current time.
/// </summary>
public abstract class DrawableTimingChange : Container<DrawableHitObject>
{
public readonly TimingChange TimingChange;
protected override Container<DrawableHitObject> Content => content;
private readonly Container<DrawableHitObject> content;
private readonly Axes scrollingAxes;
/// <summary>
/// Creates a new drawable timing change which contains hit objects and scrolls relative to the current time.
/// </summary>
/// <param name="timingChange">The encapsulated timing change that provides the speed changes.</param>
/// <param name="scrollingAxes">The axes through which this timing change scrolls.</param>
protected DrawableTimingChange(TimingChange timingChange, Axes scrollingAxes)
{
this.scrollingAxes = scrollingAxes;
TimingChange = timingChange;
// We have to proxy the hit objects to an internal container since we're
// going to be modifying our height to apply speed changes
AddInternal(content = new RelativeCoordinateAutoSizingContainer(scrollingAxes)
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both
});
}
public override Axes RelativeSizeAxes
{
get { return Axes.Both; }
set { throw new InvalidOperationException($"{nameof(DrawableTimingChange)} must always be relatively-sized."); }
}
protected override void Update()
{
var parent = Parent as IHasTimeSpan;
if (parent == null)
return;
// Adjust our size to account for the speed changes
float speedAdjustedSize = (float)(1000 / TimingChange.BeatLength / TimingChange.SpeedMultiplier);
Size = new Vector2((scrollingAxes & Axes.X) > 0 ? speedAdjustedSize : 1,
(scrollingAxes & Axes.Y) > 0 ? speedAdjustedSize : 1);
RelativeCoordinateSpace = new Vector2((scrollingAxes & Axes.X) > 0 ? parent.TimeSpan.X : 1,
(scrollingAxes & Axes.Y) > 0 ? parent.TimeSpan.Y : 1);
}
public override void Add(DrawableHitObject hitObject)
{
// The previously relatively-positioned hit object will now become relative to content, but since the hit object has no knowledge of content,
// we need to offset it back by content's position (timing change time) so that it becomes correctly relatively-positioned to content
// This can be removed if hit objects were stored such that either their StartTime or their "beat offset" was relative to the timing change
// they belonged to, but this requires a radical change to the beatmap format which we're not ready to do just yet
hitObject.Position = new Vector2((scrollingAxes & Axes.X) > 0 ? hitObject.X - (float)TimingChange.Time : hitObject.X,
(scrollingAxes & Axes.Y) > 0 ? hitObject.Y - (float)TimingChange.Time : hitObject.Y);
base.Add(hitObject);
}
/// <summary>
/// Whether this timing change can contain a hit object. This is true if the hit object occurs "after" after this timing change.
/// </summary>
public bool CanContain(DrawableHitObject hitObject) => TimingChange.Time <= hitObject.HitObject.StartTime;
/// <summary>
/// A container which cann be relatively-sized while auto-sizing to its children on desired axes. The relative coordinate space of
/// this container follows its auto-sized height.
/// </summary>
private class RelativeCoordinateAutoSizingContainer : Container<DrawableHitObject>
{
protected override IComparer<Drawable> DepthComparer => new HitObjectReverseStartTimeComparer();
private readonly Axes autoSizingAxes;
/// <summary>
/// The axes which this container should calculate its size from its children on.
/// Note that this is not the same as <see cref="Container{T}.AutoSizeAxes"/>, because that would not allow this container
/// to be relatively sized - desired in the case where the playfield re-defines <see cref="Container{T}.RelativeCoordinateSpace"/>.
/// </summary>
/// <param name="autoSizingAxes"></param>
public RelativeCoordinateAutoSizingContainer(Axes autoSizingAxes)
{
this.autoSizingAxes = autoSizingAxes;
}
public override void InvalidateFromChild(Invalidation invalidation)
{
// We only want to re-compute our size when a child's size or position has changed
if ((invalidation & Invalidation.Geometry) == 0)
{
base.InvalidateFromChild(invalidation);
return;
}
if (!Children.Any())
return;
float height = Children.Select(child => child.Y + child.Height).Max();
float width = Children.Select(child => child.X + child.Width).Max();
Size = new Vector2((autoSizingAxes & Axes.X) > 0 ? width : Size.X,
(autoSizingAxes & Axes.Y) > 0 ? height : Size.Y);
RelativeCoordinateSpace = new Vector2((autoSizingAxes & Axes.X) > 0 ? width : 1,
(autoSizingAxes & Axes.Y) > 0 ? height : 1);
base.InvalidateFromChild(invalidation);
}
}
}
}

View File

@ -1,13 +0,0 @@
using OpenTK;
using osu.Framework.Graphics.Containers;
namespace osu.Game.Rulesets.Mania.Timing
{
public interface IHasTimeSpan : IContainer
{
/// <summary>
/// The amount of time which this container spans. Drawables can be relatively positioned to this value.
/// </summary>
Vector2 TimeSpan { get; }
}
}

View File

@ -1,23 +0,0 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Rulesets.Mania.Timing
{
public class TimingChange
{
/// <summary>
/// The time at which this timing change happened.
/// </summary>
public double Time;
/// <summary>
/// The beat length.
/// </summary>
public double BeatLength = 500;
/// <summary>
/// The speed multiplier.
/// </summary>
public double SpeedMultiplier = 1;
}
}

View File

@ -1,67 +0,0 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Mania.Timing.Drawables;
using OpenTK;
namespace osu.Game.Rulesets.Mania.Timing
{
public class TimingChangeContainer : Container<DrawableTimingChange>, IHasTimeSpan
{
public Vector2 TimeSpan { get; set; }
/// <summary>
/// Adds a hit object to the most applicable timing change in this container.
/// </summary>
/// <param name="hitObject">The hit object to add.</param>
public void Add(DrawableHitObject hitObject)
{
var target = timingChangeFor(hitObject);
if (target == null)
throw new ArgumentException("No timing change could be found that can contain the hit object.", nameof(hitObject));
target.Add(hitObject);
}
protected override IComparer<Drawable> DepthComparer => new TimingChangeReverseStartTimeComparer();
/// <summary>
/// Finds the most applicable timing change that can contain a hit object. If the hit object occurs before the first (time-wise)
/// timing change, then the timing change returned is the first (time-wise) timing change.
/// </summary>
/// <param name="hitObject">The hit object to contain.</param>
/// <returns>The last (time-wise) timing change which can contain <paramref name="hitObject"/>. Null if no timing change exists.</returns>
private DrawableTimingChange timingChangeFor(DrawableHitObject hitObject) => Children.FirstOrDefault(c => c.CanContain(hitObject)) ?? Children.LastOrDefault();
}
/// <summary>
/// Compares two timing changes by their start time, falling back to creation order if their start time is equal.
/// This will compare the two timing changes in reverse order.
/// </summary>
public class TimingChangeReverseStartTimeComparer : Drawable.ReverseCreationOrderDepthComparer
{
public override int Compare(Drawable x, Drawable y)
{
var timingChangeX = x as DrawableTimingChange;
var timingChangeY = y as DrawableTimingChange;
// If either of the two drawables are not hit objects, fall back to the base comparer
if (timingChangeX?.TimingChange == null || timingChangeY?.TimingChange == null)
return base.Compare(x, y);
// Compare by start time
int i = timingChangeY.TimingChange.Time.CompareTo(timingChangeX.TimingChange.Time);
if (i != 0)
return i;
return base.Compare(x, y);
}
}
}