Skip to content

Commit

Permalink
Merge pull request #21 from hozuki/sd-updates
Browse files Browse the repository at this point in the history
Starlight Director updates
  • Loading branch information
hozuki committed Dec 9, 2016
2 parents ffceb59 + fde6b9b commit dbb87c8
Show file tree
Hide file tree
Showing 21 changed files with 196 additions and 262 deletions.
97 changes: 37 additions & 60 deletions DereTore.Applications.StarlightDirector.Entities/Bar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,46 @@ public sealed class Bar {
public int GridPerSignature => Params?.UserDefinedGridPerSignature ?? Score.Project.Settings.GlobalGridPerSignature;

[JsonIgnore]
public int TotalGridCount => Signature*GridPerSignature;
public int TotalGridCount => Signature * GridPerSignature;

private double[] _timeAtGrid;

public double TimeAtSignature(int signature)
{
public double TimeAtSignature(int signature) {
if (signature < 0 || signature >= Signature)
throw new ArgumentException("signature out of range");

return TimeAtGrid(signature*GridPerSignature);
return TimeAtGrid(signature * GridPerSignature);
}

public double TimeAtGrid(int grid)
{
public double TimeAtGrid(int grid) {
if (grid < 0 || grid >= TotalGridCount)
throw new ArgumentException("grid out of range");

return _timeAtGrid[grid];
}

public void UpdateTimings()
{
public void UpdateTimings() {
UpdateStartTime();
UpdateStartBpm();
UpdateTimeLength();
}

private void UpdateStartTime()
{
private void UpdateStartTime() {
var bars = Score.Bars;

StartTime = Score.Project.Settings.StartTimeOffset;
for (int i = 0; i < Index; ++i)
{
StartTime += bars[i].TimeLength;
var startTime = Score.Project.Settings.StartTimeOffset;
var thisIndex = Index;
if (bars.Count > 0) {
for (var i = 0; i < thisIndex; ++i) {
startTime += bars[i].TimeLength;
}
}
StartTime = startTime;
}

private void UpdateStartBpm()
{
for (int i = Index - 1; i >= 0; --i)
{
private void UpdateStartBpm() {
for (int i = Index - 1; i >= 0; --i) {
var bar = Score.Bars[i];
if (bar.Notes.All(note => note.Type != NoteType.VariantBpm))
{
if (bar.Notes.All(note => note.Type != NoteType.VariantBpm)) {
continue;
}

Expand All @@ -82,59 +77,48 @@ private void UpdateStartBpm()
/// <summary>
/// Fill _timeAtGrid, index in range [from, to], assuming constant BPM (bpm) starting from referenceIdx
/// </summary>
private void SetTimeAtGrid(int from, int to, double bpm, int referenceIdx)
{
private void SetTimeAtGrid(int from, int to, double bpm, int referenceIdx) {
var secondsPerSignature = DirectorHelper.BpmToSeconds(bpm);
for (int i = from; i <= to; ++i)
{
for (int i = from; i <= to; ++i) {
var numGrids = i - referenceIdx;
_timeAtGrid[i] = _timeAtGrid[referenceIdx] + numGrids*secondsPerSignature/GridPerSignature;
_timeAtGrid[i] = _timeAtGrid[referenceIdx] + numGrids * secondsPerSignature / GridPerSignature;
}
}

private void UpdateTimeLength()
{
private void UpdateTimeLength() {
var length = 0.0;
Note lastBpmNote = null;
_timeAtGrid = new double[TotalGridCount];
_timeAtGrid[0] = StartTime;

// find all BpmNotes and compute the length between them
foreach (var note in Notes)
{
foreach (var note in Notes) {
if (note.Type != NoteType.VariantBpm)
continue;

if (lastBpmNote == null)
{
if (lastBpmNote == null) {
// length between start and first BpmNote
length += DirectorHelper.BpmToSeconds(StartBpm)*note.IndexInGrid/GridPerSignature;
length += DirectorHelper.BpmToSeconds(StartBpm) * note.IndexInGrid / GridPerSignature;
SetTimeAtGrid(1, note.IndexInGrid, StartBpm, 0);
}
else
{
} else {
// length between prev BpmNote and current
var deltaGridCount = note.IndexInGrid - lastBpmNote.IndexInGrid;
length += DirectorHelper.BpmToSeconds(lastBpmNote.ExtraParams.NewBpm)*deltaGridCount/GridPerSignature;
length += DirectorHelper.BpmToSeconds(lastBpmNote.ExtraParams.NewBpm) * deltaGridCount / GridPerSignature;
SetTimeAtGrid(lastBpmNote.IndexInGrid + 1, note.IndexInGrid, lastBpmNote.ExtraParams.NewBpm, lastBpmNote.IndexInGrid);
}

lastBpmNote = note;
}

// length from the last BpmNote to end
// if it's null, there is no BpmNote in the bar
if (lastBpmNote != null)
{
length += DirectorHelper.BpmToSeconds(lastBpmNote.ExtraParams.NewBpm)*(TotalGridCount - lastBpmNote.IndexInGrid)/GridPerSignature;
if (lastBpmNote != null) {
length += DirectorHelper.BpmToSeconds(lastBpmNote.ExtraParams.NewBpm) * (TotalGridCount - lastBpmNote.IndexInGrid) / GridPerSignature;
SetTimeAtGrid(lastBpmNote.IndexInGrid + 1, TotalGridCount - 1, lastBpmNote.ExtraParams.NewBpm, lastBpmNote.IndexInGrid);
}
else
{
} else {
length = DirectorHelper.BpmToSeconds(StartBpm) * Signature;
for (int i = 0; i < TotalGridCount; ++i)
{
_timeAtGrid[i] = StartTime + length*i/TotalGridCount;
for (int i = 0; i < TotalGridCount; ++i) {
_timeAtGrid[i] = StartTime + length * i / TotalGridCount;
}
}

Expand All @@ -144,19 +128,14 @@ private void UpdateTimeLength()
/// <summary>
/// UpdateTimings() of this Bar and all Bars in the Score after this one
/// </summary>
internal void UpdateTimingsChain()
{
internal void UpdateTimingsChain() {
var myIdx = Score.Bars.IndexOf(this);

if (myIdx >= 0)
{
for (int i = myIdx; i < Score.Bars.Count; ++i)
{
if (myIdx >= 0) {
for (int i = myIdx; i < Score.Bars.Count; ++i) {
Score.Bars[i].UpdateTimings();
}
}
else
{
} else {
UpdateTimings();
}
}
Expand All @@ -176,8 +155,7 @@ public bool RemoveNote(Note note) {
Notes.Remove(note);
Score.Notes.Remove(note);
Score.Project.ExistingIDs.Remove(note.ID);
if (note.Type == NoteType.VariantBpm)
{
if (note.Type == NoteType.VariantBpm) {
UpdateTimingsChain();
}
return true;
Expand Down Expand Up @@ -234,8 +212,7 @@ internal void SquashParams() {
}

// Note object will call this when IndexInGrid is changed
internal void SortNotes()
{
internal void SortNotes() {
Notes.Sort(Note.TimingThenPositionComparison);
Score.Notes.Sort(Note.TimingThenPositionComparison);
}
Expand Down
19 changes: 10 additions & 9 deletions DereTore.Applications.StarlightDirector.Entities/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ public sealed class Note : DependencyObject, IComparable<Note> {

// "PositionInGrid" was the first name of this property used in serialization.
[JsonProperty("positionInGrid")]
public int IndexInGrid
{
public int IndexInGrid {
get { return _indexInGrid; }
set
{
set {
_indexInGrid = value;
Bar?.SortNotes();
}
Expand Down Expand Up @@ -249,8 +247,7 @@ public NoteExtraParams ExtraParams {
public static readonly DependencyProperty ExtraParamsProperty = DependencyProperty.Register(nameof(ExtraParams), typeof(NoteExtraParams), typeof(Note),
new PropertyMetadata(null, OnExtraParamsChanged));

public static readonly Comparison<Note> TimingThenPositionComparison = (x, y) =>
{
public static readonly Comparison<Note> TimingThenPositionComparison = (x, y) => {
var r = TimingComparison(x, y);
return r == 0 ? TrackPositionComparison(x, y) : r;
};
Expand Down Expand Up @@ -318,7 +315,7 @@ public static void ConnectSync(Note first, Note second) {
* ... <==> second_prev
*/
if (first == second) {
throw new ArgumentException("A note should not be connected to itself", nameof(second));
throw new ArgumentException("A note should not be connected to itself", nameof(second));
} else if (first?.NextSyncTarget == second && second?.PrevSyncTarget == first) {
return;
}
Expand Down Expand Up @@ -473,6 +470,11 @@ internal void SetSpecialType(NoteType type) {
Type = type;
}

// Why is ugly functions like this even exist?
internal void SetIndexInGridWithoutSorting(int newIndex) {
_indexInGrid = newIndex;
}

private static void OnTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
var note = (Note)obj;
note.IsFlick = note.IsFlickInternal();
Expand All @@ -499,8 +501,7 @@ private bool IsFlickInternal() {
return Type == NoteType.TapOrFlick && (FlickType == NoteFlickType.FlickLeft || FlickType == NoteFlickType.FlickRight);
}

private void ExtraParams_ParamsChanged(object sender, EventArgs e)
{
private void ExtraParams_ParamsChanged(object sender, EventArgs e) {
// if we a BPM note is changed, inform the Bar to update its timings
Bar?.UpdateTimingsChain();
ExtraParamsChanged.Raise(sender, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -30,11 +31,15 @@ internal static ProjectVersion CheckProjectFileVersion(string fileName) {
}
}
}
} catch (Exception) {
} catch (Exception ex) {
Debug.Print(ex.Message);
Debug.Print(ex.StackTrace);
}
try {
string versionString = null;
using (var connection = new SQLiteConnection($"Data Source={fileName}")) {
var builder = new SQLiteConnectionStringBuilder();
builder.DataSource = fileName;
using (var connection = new SQLiteConnection(builder.ToString())) {
connection.Open();
var command = connection.CreateCommand();
command.CommandText = $"SELECT value FROM {Names.Table_Main} WHERE key = @key;";
Expand Down Expand Up @@ -73,12 +78,16 @@ internal static ProjectVersion CheckProjectFileVersion(string fileName) {
return ProjectVersion.Unknown;
}
} catch (Exception ex) {
Debug.Print(ex.Message);
Debug.Print(ex.StackTrace);
return ProjectVersion.Unknown;
}
}

private static Project LoadFromV01(string fileInput) {
Project project;
ScoreSettings settings = null;

using (var fileStream = File.Open(fileInput, FileMode.Open, FileAccess.Read)) {
using (var reader = new StreamReader(fileStream, Encoding.ASCII)) {
reader.ReadLine();
Expand All @@ -97,7 +106,6 @@ private static Project LoadFromV01(string fileInput) {

// Score settings
var rawScores = p.Property("scores").Values();
ScoreSettings settings = null;
foreach (var token in rawScores) {
var rawScore = (JObject)((JProperty)token).Value;
var settingsProp = rawScore.Property("settings");
Expand All @@ -106,12 +114,6 @@ private static Project LoadFromV01(string fileInput) {
break;
}
}
if (settings != null) {
project.Settings.GlobalBpm = settings.GlobalBpm;
project.Settings.GlobalGridPerSignature = settings.GlobalGridPerSignature;
project.Settings.GlobalSignature = settings.GlobalSignature;
project.Settings.StartTimeOffset = settings.StartTimeOffset;
}
}
}

Expand All @@ -122,6 +124,14 @@ private static Project LoadFromV01(string fileInput) {
score.Difficulty = difficulty;
}

// Call score.ResolveReferences() first.
if (settings != null) {
project.Settings.GlobalBpm = settings.GlobalBpm;
project.Settings.GlobalGridPerSignature = settings.GlobalGridPerSignature;
project.Settings.GlobalSignature = settings.GlobalSignature;
project.Settings.StartTimeOffset = settings.StartTimeOffset;
}

project.SaveFileName = fileInput;

GridLineFixup(project);
Expand All @@ -138,7 +148,9 @@ private static Project LoadFromV02(string fileName) {
IsChanged = false,
SaveFileName = fileName
};
using (var connection = new SQLiteConnection($"Data Source={fileName}")) {
var builder = new SQLiteConnectionStringBuilder();
builder.DataSource = fileName;
using (var connection = new SQLiteConnection(builder.ToString())) {
connection.Open();
SQLiteCommand getValues = null;

Expand Down
21 changes: 11 additions & 10 deletions DereTore.Applications.StarlightDirector.Exchange/ProjectIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static Project Load(string fileName) {
return Load(fileName, version);
}

internal static Project Load(string fileName, ProjectVersion versionOverride)
{
internal static Project Load(string fileName, ProjectVersion versionOverride) {
Project project = null;
switch (versionOverride) {
case ProjectVersion.Unknown:
Expand All @@ -48,11 +47,9 @@ internal static Project Load(string fileName, ProjectVersion versionOverride)
project = LoadCurrentVersion(fileName);

// Update bar timings, sort notes
foreach (var difficulty in Difficulties)
{
foreach (var difficulty in Difficulties) {
var score = project.GetScore(difficulty);
foreach (var bar in score.Bars)
{
foreach (var bar in score.Bars) {
bar.UpdateTimings();
bar.Notes.Sort(Note.TimingThenPositionComparison);
}
Expand All @@ -71,7 +68,9 @@ private static void Save(Project project, string fileName, bool createNewDatabas
} else {
File.Delete(fileName);
}
using (var connection = new SQLiteConnection($"Data Source={fileName}")) {
var builder = new SQLiteConnectionStringBuilder();
builder.DataSource = fileName;
using (var connection = new SQLiteConnection(builder.ToString())) {
connection.Open();
SQLiteCommand setValue = null, insertNote = null, insertNoteID = null, insertBarParams = null, insertSpecialNote = null;

Expand Down Expand Up @@ -156,7 +155,9 @@ private static Project LoadCurrentVersion(string fileName) {
IsChanged = false,
SaveFileName = fileName
};
using (var connection = new SQLiteConnection($"Data Source={fileName}")) {
var builder = new SQLiteConnectionStringBuilder();
builder.DataSource = fileName;
using (var connection = new SQLiteConnection(builder.ToString())) {
connection.Open();
SQLiteCommand getValues = null;

Expand Down Expand Up @@ -228,7 +229,7 @@ private static void GridLineFixup(Project project) {
}
var score = project.GetScore(difficulty);
foreach (var note in score.Notes) {
note.IndexInGrid *= k;
note.SetIndexInGridWithoutSorting(note.IndexInGrid * k);
}
}
} else if (oldGrids % newGrids == 0) {
Expand All @@ -244,7 +245,7 @@ private static void GridLineFixup(Project project) {
if (note.IndexInGrid % k != 0) {
incompatibleNotes.Add(note);
} else {
note.IndexInGrid /= k;
note.SetIndexInGridWithoutSorting(note.IndexInGrid / k);
}
}
}
Expand Down
Loading

0 comments on commit dbb87c8

Please sign in to comment.