Skip to content

Commit

Permalink
Merge pull request #193 from WilliamQiufeng/timing-group-ssf2
Browse files Browse the repository at this point in the history
Add Scroll Speed Factor
  • Loading branch information
Swan authored Nov 5, 2024
2 parents 75f221f + efbab03 commit 745ff93
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Quaver.API/Maps/Qua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ public void Sort()
SortSoundEffects();
SortTimingPoints();
SortSliderVelocities();
SortScrollSpeedFactors();
}

/// <summary>
Expand Down Expand Up @@ -630,6 +631,14 @@ public TimingPointInfo GetTimingPointAt(double time)
public SliderVelocityInfo GetScrollVelocityAt(double time, string timingGroupId = DefaultScrollGroupId) =>
((ScrollGroup)TimingGroups[timingGroupId]).GetScrollVelocityAt(time);

/// <summary>
/// Gets a scroll velocity at a particular time in the map
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public ScrollSpeedFactorInfo GetScrollSpeedFactorAt(double time, string timingGroupId = DefaultScrollGroupId) =>
((ScrollGroup)TimingGroups[timingGroupId]).GetScrollSpeedFactorAt(time);

/// <summary>
/// Finds the length of a timing point.
/// </summary>
Expand Down Expand Up @@ -1019,6 +1028,18 @@ public void MirrorHitObjects()
/// </summary>
public void SortBookmarks() => Bookmarks.HybridSort();

/// <summary>
/// </summary>
public void SortScrollSpeedFactors()
{
foreach (var (_, timingGroup) in TimingGroups)
{
if (!(timingGroup is ScrollGroup scrollGroup))
continue;
scrollGroup.ScrollSpeedFactors.HybridSort();
}
}

/// <summary>
/// </summary>
public void SortHitObjects() => HitObjects.HybridSort();
Expand Down
14 changes: 13 additions & 1 deletion Quaver.API/Maps/Structures/ScrollGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,31 @@ public class ScrollGroup : TimingGroup
public List<SliderVelocityInfo> ScrollVelocities { get; [MoonSharpHidden] set; } =
new List<SliderVelocityInfo>();

/// <summary>
/// Scroll Speed Factor .qua data
/// </summary>
public List<ScrollSpeedFactorInfo> ScrollSpeedFactors { get; [MoonSharpHidden] set; } =
new List<ScrollSpeedFactorInfo>();

public SliderVelocityInfo GetScrollVelocityAt(double time)
{
return ScrollVelocities.AtTime((float)time);
}

public ScrollSpeedFactorInfo GetScrollSpeedFactorAt(double time)
{
return ScrollSpeedFactors.AtTime((float)time);
}

/// <summary>
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
protected bool Equals(ScrollGroup other)
{
return base.Equals(other) && InitialScrollVelocity.Equals(other.InitialScrollVelocity) &&
ScrollVelocities.SequenceEqual(other.ScrollVelocities, SliderVelocityInfo.ByValueComparer);
ScrollVelocities.SequenceEqual(other.ScrollVelocities, SliderVelocityInfo.ByValueComparer) &&
ScrollSpeedFactors.SequenceEqual(other.ScrollSpeedFactors, ScrollSpeedFactorInfo.ByValueComparer);
}

/// <summary>
Expand Down
107 changes: 107 additions & 0 deletions Quaver.API/Maps/Structures/ScrollSpeedFactorInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Text;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Interop;
using YamlDotNet.Serialization;

namespace Quaver.API.Maps.Structures
{
/// <summary>
/// ScrollSpeedFactors section of the .qua
/// </summary>
[Serializable]
[MoonSharpUserData]
public class ScrollSpeedFactorInfo : IStartTime
{
/// <summary>
/// The time in milliseconds at which the scroll speed factor will be exactly <see cref="Multiplier"/>
/// </summary>
public float StartTime { get; [MoonSharpHidden] set; }

/// <summary>
/// The multiplier given to the scroll speed.
/// It will be lerped to the next scroll speed factor like keyframes, unless this is the last factor change.
/// </summary>
public float Multiplier { get; [MoonSharpHidden] set; }

/// <summary>
/// Returns if the SSF is allowed to be edited in lua scripts
/// </summary>
[YamlIgnore]
public bool IsEditableInLuaScript
{
get;
[MoonSharpVisible(false)]
set;
}

/// <summary>
/// Sets the start time of the SSF.
/// FOR USE IN LUA SCRIPTS ONLY.
/// </summary>
/// <param name="time"></param>
/// <exception cref="InvalidOperationException"></exception>
public void SetStartTime(float time)
{
ThrowUneditableException();
StartTime = time;
}

/// <summary>
/// Sets the multiplier of the SSF.
/// FOR USE IN LUA SCRIPTS ONLY.
/// </summary>
/// <param name="multiplier"></param>
/// <exception cref="InvalidOperationException"></exception>
public void SetMultiplier(float multiplier)
{
ThrowUneditableException();
Multiplier = multiplier;
}

/// <summary>
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
private void ThrowUneditableException()
{
if (!IsEditableInLuaScript)
throw new InvalidOperationException("Value is not allowed to be edited in lua scripts.");
}

private sealed class StartTimeRelationalComparer : IComparer<ScrollSpeedFactorInfo>
{
public int Compare(ScrollSpeedFactorInfo x, ScrollSpeedFactorInfo y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
return x.StartTime.CompareTo(y.StartTime);
}
}

public static IComparer<ScrollSpeedFactorInfo> StartTimeComparer { get; } = new StartTimeRelationalComparer();

/// <summary>
/// By-value comparer, auto-generated by Rider.
/// </summary>
private sealed class ByValueEqualityComparer : IEqualityComparer<ScrollSpeedFactorInfo>
{
public bool Equals(ScrollSpeedFactorInfo x, ScrollSpeedFactorInfo y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return x.StartTime.Equals(y.StartTime) && x.Multiplier.Equals(y.Multiplier);
}

public int GetHashCode(ScrollSpeedFactorInfo obj)
{
return HashCode.Combine(obj.StartTime, obj.Multiplier);
}
}

public static IEqualityComparer<ScrollSpeedFactorInfo> ByValueComparer { get; } = new ByValueEqualityComparer();
}
}

0 comments on commit 745ff93

Please sign in to comment.