Initial rewrite of timing changes to allow them to be more extensible.

This commit is contained in:
smoogipooo 2017-06-01 14:26:21 +09:00
parent e348f86ce1
commit f612914e87
8 changed files with 191 additions and 171 deletions

View File

@ -14,6 +14,7 @@
using osu.Framework.Configuration;
using OpenTK.Input;
using osu.Framework.Timing;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Desktop.VisualTests.Tests
{
@ -30,7 +31,7 @@ public override void Reset()
Action<int, SpecialColumnPosition> createPlayfield = (cols, pos) =>
{
Clear();
Add(new ManiaPlayfield(cols, new List<TimingChange>())
Add(new ManiaPlayfield(cols)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
@ -44,7 +45,7 @@ public override void Reset()
Clear();
ManiaPlayfield playField;
Add(playField = new ManiaPlayfield(cols, new List<TimingChange> { new TimingChange { BeatLength = 200 } })
Add(playField = new ManiaPlayfield(cols)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
@ -52,6 +53,8 @@ public override void Reset()
Scale = new Vector2(1, -1)
});
playField.Columns.ForEach(c => c.Add(new DrawableScrollingTimingChange(new TimingChange { BeatLength = 200 })));
for (int i = 0; i < cols; i++)
{
playField.Add(new DrawableNote(new Note
@ -69,7 +72,7 @@ public override void Reset()
var rateAdjustClock = new StopwatchClock(true) { Rate = 0.5 };
ManiaPlayfield playField;
Add(playField = new ManiaPlayfield(4, new List<TimingChange> { new TimingChange { BeatLength = 200 } })
Add(playField = new ManiaPlayfield(4)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
@ -77,6 +80,8 @@ public override void Reset()
Clock = new FramedClock(rateAdjustClock)
});
playField.Columns.ForEach(c => c.Add(new DrawableScrollingTimingChange(new TimingChange { BeatLength = 200 })));
for (int t = 1000; t <= 2000; t += 100)
{
playField.Add(new DrawableNote(new Note

View File

@ -1,156 +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 OpenTK;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Objects;
namespace osu.Game.Rulesets.Mania.Timing
{
/// <summary>
/// A container in which added drawables are put into a relative coordinate space spanned by a length of time.
/// <para>
/// This container contains <see cref="ControlPoint"/>s which scroll inside this container.
/// Drawables added to this container are moved inside the relevant <see cref="ControlPoint"/>,
/// and as such, will scroll along with the <see cref="ControlPoint"/>s.
/// </para>
/// </summary>
public class ControlPointContainer : Container<Drawable>
{
/// <summary>
/// The amount of time which this container spans.
/// </summary>
public double TimeSpan { get; set; }
private readonly List<DrawableControlPoint> drawableControlPoints;
public ControlPointContainer(IEnumerable<TimingChange> timingChanges)
{
drawableControlPoints = timingChanges.Select(t => new DrawableControlPoint(t)).ToList();
Children = drawableControlPoints;
}
/// <summary>
/// Adds a drawable to this container. Note that the drawable added must have its Y-position be
/// an absolute unit of time that is _not_ relative to <see cref="TimeSpan"/>.
/// </summary>
/// <param name="drawable">The drawable to add.</param>
public override void Add(Drawable drawable)
{
// Always add timing sections to ourselves
if (drawable is DrawableControlPoint)
{
base.Add(drawable);
return;
}
var controlPoint = drawableControlPoints.LastOrDefault(t => t.CanContain(drawable)) ?? drawableControlPoints.FirstOrDefault();
if (controlPoint == null)
throw new InvalidOperationException("Could not find suitable timing section to add object to.");
controlPoint.Add(drawable);
}
/// <summary>
/// A container that contains drawables within the time span of a timing section.
/// <para>
/// The content of this container will scroll relative to the current time.
/// </para>
/// </summary>
private class DrawableControlPoint : Container
{
private readonly TimingChange timingChange;
protected override Container<Drawable> Content => content;
private readonly Container content;
/// <summary>
/// Creates a drawable control point. The height of this container will be proportional
/// to the beat length of the control point it is initialized with such that, e.g. a beat length
/// of 500ms results in this container being twice as high as its parent, which further means that
/// the content container will scroll at twice the normal rate.
/// </summary>
/// <param name="timingChange">The control point to create the drawable control point for.</param>
public DrawableControlPoint(TimingChange timingChange)
{
this.timingChange = timingChange;
RelativeSizeAxes = Axes.Both;
AddInternal(content = new AutoTimeRelativeContainer
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both,
Y = (float)timingChange.Time
});
}
protected override void Update()
{
var parent = (ControlPointContainer)Parent;
// Adjust our height to account for the speed changes
Height = (float)(1000 / timingChange.BeatLength / timingChange.SpeedMultiplier);
RelativeCoordinateSpace = new Vector2(1, (float)parent.TimeSpan);
// Scroll the content
content.Y = (float)(timingChange.Time - Time.Current);
}
public override void Add(Drawable drawable)
{
// The previously relatively-positioned drawable will now become relative to content, but since the drawable has no knowledge of content,
// we need to offset it back by content's position position 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
drawable.Y -= (float)timingChange.Time;
base.Add(drawable);
}
/// <summary>
/// Whether this control point can contain a drawable. This control point can contain a drawable if the drawable is positioned "after" this control point.
/// </summary>
/// <param name="drawable">The drawable to check.</param>
public bool CanContain(Drawable drawable) => content.Y <= drawable.Y;
/// <summary>
/// A container which always keeps its height and relative coordinate space "auto-sized" to its children.
/// <para>
/// This is used in the case where children are relatively positioned/sized to time values (e.g. notes/bar lines) to keep
/// such children wrapped inside a container, otherwise they would disappear due to container flattening.
/// </para>
/// </summary>
private class AutoTimeRelativeContainer : Container
{
protected override IComparer<Drawable> DepthComparer => new HitObjectReverseStartTimeComparer();
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();
Height = height;
RelativeCoordinateSpace = new Vector2(1, height);
base.InvalidateFromChild(invalidation);
}
}
}
}
}

View File

@ -0,0 +1,112 @@
using System.Collections.Generic;
using System.Linq;
using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
namespace osu.Game.Rulesets.Mania.Timing
{
public abstract class DrawableTimingChange : Container<DrawableHitObject>
{
protected readonly TimingChange TimingChange;
protected override Container<DrawableHitObject> Content => content;
private readonly Container<DrawableHitObject> content;
public DrawableTimingChange(TimingChange timingChange)
{
TimingChange = timingChange;
RelativeSizeAxes = Axes.Both;
AddInternal(content = new RelativeCoordinateAutoSizingContainer
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both,
Y = (float)timingChange.Time
});
}
protected override void Update()
{
var parent = (TimingChangeContainer)Parent;
// Adjust our height to account for the speed changes
Height = (float)(1000 / TimingChange.BeatLength / TimingChange.SpeedMultiplier);
RelativeCoordinateSpace = new Vector2(1, (float)parent.TimeSpan);
}
public override void Add(DrawableHitObject drawable)
{
// The previously relatively-positioned drawable will now become relative to content, but since the drawable has no knowledge of content,
// we need to offset it back by content's position position 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
drawable.Y -= (float)TimingChange.Time;
base.Add(drawable);
}
/// <summary>
/// Whether this timing change can contain a drawable. This is true if the drawable occurs "after" after this timing change.
/// </summary>
public bool CanContain(DrawableHitObject hitObject) => TimingChange.Time <= hitObject.HitObject.StartTime;
private class RelativeCoordinateAutoSizingContainer : Container<DrawableHitObject>
{
protected override IComparer<Drawable> DepthComparer => new HitObjectReverseStartTimeComparer();
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();
Height = height;
RelativeCoordinateSpace = new Vector2(1, height);
base.InvalidateFromChild(invalidation);
}
}
}
public class DrawableScrollingTimingChange : DrawableTimingChange
{
public DrawableScrollingTimingChange(TimingChange timingChange)
: base(timingChange)
{
}
protected override void Update()
{
base.Update();
Content.Y = (float)(TimingChange.Time - Time.Current);
}
}
public class DrawableGravityTimingChange : DrawableTimingChange
{
public DrawableGravityTimingChange(TimingChange timingChange)
: base(timingChange)
{
}
protected override void Update()
{
base.Update();
// Todo: Gravity calculations here
}
}
}

View File

@ -0,0 +1,44 @@
// 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 OpenTK;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
namespace osu.Game.Rulesets.Mania.Timing
{
public class TimingChangeContainer : Container<DrawableTimingChange>
{
/// <summary>
/// The amount of time which this container spans.
/// </summary>
public double 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);
}
/// <summary>
/// Finds the most applicable timing change that can contain a hit object.
/// </summary>
/// <param name="hitObject">The hit object to contain.</param>
/// <returns>The last timing change which can contain <paramref name="hitObject"/>.</returns>
private DrawableTimingChange timingChangeFor(DrawableHitObject hitObject) => Children.LastOrDefault(c => c.CanContain(hitObject));
}
}

View File

@ -42,9 +42,9 @@ public class Column : Container, IHasAccentColour
private readonly Container hitTargetBar;
private readonly Container keyIcon;
public readonly ControlPointContainer ControlPointContainer;
private readonly TimingChangeContainer timingChanges;
public Column(IEnumerable<TimingChange> timingChanges)
public Column()
{
RelativeSizeAxes = Axes.Y;
Width = column_width;
@ -93,7 +93,7 @@ public Column(IEnumerable<TimingChange> timingChanges)
}
}
},
ControlPointContainer = new ControlPointContainer(timingChanges)
timingChanges = new TimingChangeContainer
{
Name = "Hit objects",
RelativeSizeAxes = Axes.Both,
@ -187,10 +187,17 @@ public Color4 AccentColour
}
}
public void Add(DrawableHitObject<ManiaHitObject, ManiaJudgement> hitObject)
public double TimeSpan
{
get { return timingChanges.TimeSpan; }
set { timingChanges.TimeSpan = value; }
}
public void Add(DrawableTimingChange timingChange) => timingChanges.Add(timingChange);
public void Add(DrawableHitObject hitObject)
{
hitObject.AccentColour = AccentColour;
ControlPointContainer.Add(hitObject);
timingChanges.Add(hitObject);
}
private bool onKeyDown(InputState state, KeyDownEventArgs args)

View File

@ -8,6 +8,7 @@
using OpenTK.Input;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Lists;
using osu.Framework.MathUtils;
@ -78,13 +79,17 @@ protected override Playfield<ManiaHitObject, ManiaJudgement> CreatePlayfield()
.GroupBy(s => s.BeatLength * s.SpeedMultiplier).Select(g => g.First())
.ToList();
return new ManiaPlayfield(Columns ?? (int)Math.Round(Beatmap.BeatmapInfo.Difficulty.CircleSize), timingChanges)
var playfield = new ManiaPlayfield(Columns ?? (int)Math.Round(Beatmap.BeatmapInfo.Difficulty.CircleSize))
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
// Invert by default for now (should be moved to config/skin later)
Scale = new Vector2(1, -1)
};
timingChanges.ForEach(t => playfield.Columns.ForEach(c => c.Add(new DrawableScrollingTimingChange(t))));
return playfield;
}
[BackgroundDependencyLoader]

View File

@ -58,14 +58,14 @@ public SpecialColumnPosition SpecialColumnPosition
private readonly FlowContainer<Column> columns;
public IEnumerable<Column> Columns => columns.Children;
private readonly ControlPointContainer barLineContainer;
private readonly TimingChangeContainer barLineContainer;
private List<Color4> normalColumnColours = new List<Color4>();
private Color4 specialColumnColour;
private readonly int columnCount;
public ManiaPlayfield(int columnCount, IEnumerable<TimingChange> timingChanges)
public ManiaPlayfield(int columnCount)
{
this.columnCount = columnCount;
@ -116,7 +116,7 @@ public ManiaPlayfield(int columnCount, IEnumerable<TimingChange> timingChanges)
Padding = new MarginPadding { Top = HIT_TARGET_POSITION },
Children = new[]
{
barLineContainer = new ControlPointContainer(timingChanges)
barLineContainer = new TimingChangeContainer
{
Name = "Bar lines",
Anchor = Anchor.TopCentre,
@ -131,7 +131,7 @@ public ManiaPlayfield(int columnCount, IEnumerable<TimingChange> timingChanges)
};
for (int i = 0; i < columnCount; i++)
columns.Add(new Column(timingChanges));
columns.Add(new Column());
TimeSpan = time_span_default;
}
@ -207,6 +207,8 @@ private bool isSpecialColumn(int column)
}
public override void Add(DrawableHitObject<ManiaHitObject, ManiaJudgement> h) => Columns.ElementAt(h.HitObject.Column).Add(h);
public void Add(DrawableTimingChange timingChange) => barLineContainer.Add(timingChange);
public void Add(DrawableBarLine barline) => barLineContainer.Add(barline);
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
@ -243,7 +245,7 @@ public double TimeSpan
timeSpan = MathHelper.Clamp(timeSpan, time_span_min, time_span_max);
barLineContainer.TimeSpan = value;
Columns.ForEach(c => c.ControlPointContainer.TimeSpan = value);
Columns.ForEach(c => c.TimeSpan = value);
}
}

View File

@ -83,8 +83,9 @@
<Compile Include="ManiaRuleset.cs" />
<Compile Include="Mods\ManiaMod.cs" />
<Compile Include="UI\SpecialColumnPosition.cs" />
<Compile Include="Timing\ControlPointContainer.cs" />
<Compile Include="Timing\DrawableTimingChange.cs" />
<Compile Include="Timing\TimingChange.cs" />
<Compile Include="Timing\TimingChangeContainer.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\osu-framework\osu.Framework\osu.Framework.csproj">