Skip to content

Commit

Permalink
Merge pull request #29 from hozuki/sd-edit-update
Browse files Browse the repository at this point in the history
SD edition update
  • Loading branch information
hozuki committed Mar 3, 2017
2 parents 49c7172 + c39e409 commit dc2fb54
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 107 deletions.
13 changes: 13 additions & 0 deletions Common/DereTore.Common/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ public static string BuildString<T>(this IEnumerable<T> enumerable, string separ
return stringBuilder.ToString();
}

public static bool CountMoreThan<T>(this IEnumerable<T> enumerable, int n) {
var counter = 0;
using (var iter = enumerable.GetEnumerator()) {
while (iter.MoveNext()) {
++counter;
if (counter > n) {
return true;
}
}
}
return false;
}

private static readonly string DefaultEnumerableStringSeparator = ", ";

}
Expand Down
46 changes: 39 additions & 7 deletions StarlightDirector/StarlightDirector.Entities/Note.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Windows;
using DereTore.Common;
using Newtonsoft.Json;
Expand Down Expand Up @@ -75,6 +76,7 @@ public Note PrevFlickOrSlideNote {
_prevFlickOrSlideNote = value;
UpdateFlickTypeStep2();
PrevFlickOrSlideNoteID = value?.ID ?? EntityID.Invalid;
DecideRenderingAsFlickOrSlide();
}
}

Expand All @@ -90,6 +92,7 @@ public Note NextFlickOrSlideNote {
_nextFlickOrSlideNote = value;
UpdateFlickTypeStep2();
NextFlickOrSlideNoteID = value?.ID ?? EntityID.Invalid;
DecideRenderingAsFlickOrSlide();
}
}

Expand Down Expand Up @@ -387,18 +390,27 @@ internal Note(int id, Bar bar) {

internal void Reset() {
if (NextFlickOrSlideNote != null) {
NextFlickOrSlideNote.PrevFlickOrSlideNote = null;
var next = NextFlickOrSlideNote;
next.PrevFlickOrSlideNote = null;
if (next.IsSlide && !next.IsSlideStart) {
next.Type = NoteType.TapOrFlick;
}
}
NextFlickOrSlideNote = null;
if (PrevFlickOrSlideNote != null) {
PrevFlickOrSlideNote.NextFlickOrSlideNote = null;
var prev = PrevFlickOrSlideNote;
prev.NextFlickOrSlideNote = null;
if (prev.IsSlide && !prev.IsSlideEnd) {
prev.Type = NoteType.TapOrFlick;
}
}
PrevFlickOrSlideNote = null;
if (HoldTarget != null) {
HoldTarget.HoldTarget = null;
if (HoldTarget != null) {
if (!HoldTarget.HasNextFlickOrSlide && !HoldTarget.HasPrevFlickOrSlide && HoldTarget.FlickType != NoteFlickType.Tap) {
HoldTarget.FlickType = NoteFlickType.Tap;
var hold = HoldTarget;
hold.HoldTarget = null;
if (hold != null) {
if (!hold.HasNextFlickOrSlide && !hold.HasPrevFlickOrSlide && hold.FlickType != NoteFlickType.Tap) {
hold.FlickType = NoteFlickType.Tap;
}
}
}
Expand Down Expand Up @@ -428,6 +440,12 @@ private static void OnTypeChanged(DependencyObject obj, DependencyPropertyChange
note.IsSlide = note.IsSlideInternal();
note.UpdateFlickTypeStep2();
note.DecideRenderingAsFlickOrSlide();

if (note.IsFlick) {
note.UpdateAsFlickNote();
} else if (note.IsSlide) {
note.UpdateAsSlideNote();
}
}

private static void OnFlickTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
Expand Down Expand Up @@ -492,7 +510,7 @@ private void UpdateFlickTypeStep2() {
private void DecideRenderingAsFlickOrSlide() {
if (IsFlick || IsSlide) {
if (IsSlide) {
ShouldBeRenderedAsSlide = !HasNextFlickOrSlide || !NextFlickOrSlideNote.IsFlick;
ShouldBeRenderedAsSlide = FlickType == NoteFlickType.Tap && (!HasNextFlickOrSlide || !NextFlickOrSlideNote.IsFlick);
} else {
ShouldBeRenderedAsSlide = false;
}
Expand Down Expand Up @@ -524,6 +542,20 @@ private void SetNextSyncTargetInternal(Note next) {
IsSync = _prevSyncTarget != null || _nextSyncTarget != null;
}

private void UpdateAsSlideNote() {
if (HasPrevFlickOrSlide && PrevFlickOrSlideNote.IsSlide) {
PrevFlickOrSlideNote.FlickType = NoteFlickType.Tap;
}
}

private void UpdateAsFlickNote() {
if (HasPrevFlickOrSlide && PrevFlickOrSlideNote.IsSlide) {
var pos1 = (int)PrevFlickOrSlideNote.FinishPosition;
var pos2 = (int)FinishPosition;
PrevFlickOrSlideNote.FlickType = pos2 >= pos1 ? NoteFlickType.FlickRight : NoteFlickType.FlickLeft;
}
}

private Note _prevFlickOrSlideNote;
private Note _nextFlickOrSlideNote;
private Note _prevSyncTarget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ScoreBarHitTestInfo HitTest(Point pointRelativeToScoreBar) {
row = (int)Math.Round((double)row / zoomMod) * zoomMod;
var gridCrossingPosition = new Point(column * unitWidth, row * unitHeight);
var distance = Point.Subtract(gridCrossingPosition, destPoint);
if (distance.Length > 2 * NoteRadius) {
if (distance.Length > HitTestRadiusScale * NoteRadius) {
return new ScoreBarHitTestInfo(this, Bar, new Point(), column, row, false, false);
}
if (column < 0 || column > columnCount - 1) {
Expand Down Expand Up @@ -133,5 +133,7 @@ private int GetBestFitZoomMod() {
public static readonly int SignatureBase = ScoreSettings.DefaultGlobalGridPerSignature * ScoreSettings.DefaultGlobalSignature;
public static readonly double ZoomFactor = 1.2;

private static readonly double HitTestRadiusScale = 1;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ public TextBlock NoteInfoBlock {
set { SetValue(NoteInfoBlockProperty, value); }
}

public ContextMenu ExtraNotesContextMenu {
get { return (ContextMenu)GetValue(ExtraNotesContextMenuProperty); }
set { SetValue(ExtraNotesContextMenuProperty, value); }
}

public static readonly DependencyProperty EditModeProperty = DependencyProperty.Register(nameof(EditMode), typeof(EditMode), typeof(ScoreEditor),
new PropertyMetadata(EditMode.Select));
new PropertyMetadata(EditMode.CreateRelations));

public static readonly DependencyProperty ProjectProperty = DependencyProperty.Register(nameof(Project), typeof(Project), typeof(ScoreEditor),
new PropertyMetadata(null, OnProjectChanged));

public static readonly DependencyProperty NoteInfoBlockProperty = DependencyProperty.Register(nameof(NoteInfoBlock), typeof(TextBlock), typeof(ScoreEditor),
new PropertyMetadata(null));

public static readonly DependencyProperty ExtraNotesContextMenuProperty = DependencyProperty.Register(nameof(ExtraNotesContextMenu), typeof(ContextMenu), typeof(ScoreEditor),
new PropertyMetadata(null, OnExtraNotesContextMenuChanged));

private static void OnProjectChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
var editor = (ScoreEditor)obj;
var oldproject = (Project)e.OldValue;
Expand All @@ -43,5 +51,10 @@ private static void OnProjectChanged(DependencyObject obj, DependencyPropertyCha
CommandManager.InvalidateRequerySuggested();
}

private static void OnExtraNotesContextMenuChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
var editor = (ScoreEditor)obj;
editor.SpecialNoteLayer.ContextMenu = (ContextMenu)e.NewValue;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using DereTore.Common;
using StarlightDirector.Entities;
using StarlightDirector.Entities.Extensions;
using StarlightDirector.Extensions;
Expand Down Expand Up @@ -75,14 +76,21 @@ private void ScoreNote_MouseDown(object sender, MouseButtonEventArgs e) {
return;
}
var scoreNote = (ScoreNote)sender;
if (scoreNote.IsSelected) {
scoreNote.IsSelected = EditMode != EditMode.Select;
var isControlPressed = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
if (isControlPressed) {
scoreNote.IsSelected = !scoreNote.IsSelected;
} else {
scoreNote.IsSelected = true;
var originalSelected = scoreNote.IsSelected;
var originalMoreThanOneSelectedNotes = GetSelectedScoreNotes().CountMoreThan(1);
UnselectAllScoreNotes();
// !originalSelected || (originalSelected && originalAnySelectedNotes)
if (!originalSelected || originalMoreThanOneSelectedNotes) {
scoreNote.IsSelected = true;
}
}
if (scoreNote.IsSelected && EditMode != EditMode.Select && EditMode != EditMode.Clear) {
if (scoreNote.IsSelected && EditMode != EditMode.ResetNote) {
switch (EditMode) {
case EditMode.Relations:
case EditMode.CreateRelations:
EditingLine.Stroke = LineLayer.RelationBrush;
break;
default:
Expand Down Expand Up @@ -112,14 +120,11 @@ private void ScoreNote_MouseUp(object sender, MouseButtonEventArgs e) {
Debug.Assert(DraggingEndNote != null, "DraggingEndNote != null");
if (DraggingStartNote != null && DraggingEndNote != null) {
var mode = EditMode;
if (mode == EditMode.Select) {
return;
}
var start = DraggingStartNote;
var end = DraggingEndNote;
var ns = start.Note;
var ne = end.Note;
if (mode == EditMode.Clear) {
if (mode == EditMode.ResetNote) {
ns.Reset();
LineLayer.NoteRelations.RemoveAll(start, NoteRelation.Hold);
LineLayer.NoteRelations.RemoveAll(start, NoteRelation.FlickOrSlide);
Expand All @@ -135,7 +140,7 @@ private void ScoreNote_MouseUp(object sender, MouseButtonEventArgs e) {
MessageBox.Show(Application.Current.FindResource<string>(App.ResourceKeys.NoteRelationAlreadyExistsPrompt), App.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
return;
}
if (mode != EditMode.Relations) {
if (mode != EditMode.CreateRelations) {
throw new ArgumentOutOfRangeException(nameof(mode));
}
var first = ns < ne ? ns : ne;
Expand All @@ -145,7 +150,7 @@ private void ScoreNote_MouseUp(object sender, MouseButtonEventArgs e) {
Note.ConnectSync(ns, ne);
LineLayer.NoteRelations.Add(start, end, NoteRelation.Sync);
LineLayer.InvalidateVisual();
} else if (ns.FinishPosition != ne.FinishPosition && (ns.Bar != ne.Bar || ns.IndexInGrid != ne.IndexInGrid) && (!ns.IsHoldStart && !ne.IsHoldStart) && !second.IsFlick) {
} else if (ns.FinishPosition != ne.FinishPosition && (ns.Bar != ne.Bar || ns.IndexInGrid != ne.IndexInGrid) && (!ns.IsHoldStart && !ne.IsHoldStart) && (first.IsSlide == second.IsSlide)) {
// flick
if (first.HasNextFlickOrSlide || second.HasPrevFlickOrSlide) {
MessageBox.Show(Application.Current.FindResource<string>(App.ResourceKeys.FlickRelationIsFullPrompt), App.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
Expand Down Expand Up @@ -185,24 +190,22 @@ private void ScoreNote_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
}
var scoreNote = (ScoreNote)sender;
var note = scoreNote.Note;
if (note.IsHoldEnd) {
if (note > note.HoldTarget) {
switch (note.FlickType) {
case NoteFlickType.Tap:
note.FlickType = NoteFlickType.FlickLeft;
Project.IsChanged = true;
break;
case NoteFlickType.FlickLeft:
note.FlickType = NoteFlickType.FlickRight;
Project.IsChanged = true;
break;
case NoteFlickType.FlickRight:
note.FlickType = NoteFlickType.Tap;
Project.IsChanged = true;
break;
default:
throw new ArgumentOutOfRangeException(nameof(note.FlickType));
}
if (note.IsHoldEnd || note.IsSlideEnd) {
switch (note.FlickType) {
case NoteFlickType.Tap:
note.FlickType = NoteFlickType.FlickLeft;
Project.IsChanged = true;
break;
case NoteFlickType.FlickLeft:
note.FlickType = NoteFlickType.FlickRight;
Project.IsChanged = true;
break;
case NoteFlickType.FlickRight:
note.FlickType = NoteFlickType.Tap;
Project.IsChanged = true;
break;
default:
throw new ArgumentOutOfRangeException(nameof(note.FlickType));
}
}
e.Handled = true;
Expand Down Expand Up @@ -292,15 +295,12 @@ private void ScoreEditor_OnMouseUp(object sender, MouseButtonEventArgs e) {
var hitTestInfo = ((ScoreBar)element).HitTest(e.GetPosition(element));
LastHitTestInfo = hitTestInfo;
} else {
ScoreBar s = null;
foreach (var scoreBar in ScoreBars) {
var top = Canvas.GetTop(scoreBar);
var bottom = top + scoreBar.ActualHeight;
if (top <= myPosition.Y && myPosition.Y < bottom) {
s = scoreBar;
break;
}
}
var s = (from scoreBar in ScoreBars
let top = Canvas.GetTop(scoreBar)
let bottom = top + scoreBar.ActualHeight
where top <= myPosition.Y && myPosition.Y < bottom
select scoreBar)
.FirstOrDefault();
if (s != null) {
var hitTestInfo = s.HitTest(e.GetPosition(s));
LastHitTestInfo = hitTestInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Grid.LayoutTransform>
<ScaleTransform ScaleY="-1" ScaleX="1" />
</Grid.LayoutTransform>
<Canvas Grid.Column="2" Name="SpecialNoteLayer" Background="Transparent"/>
<Grid Name="ContentsGrid" Grid.Column="1">
<Canvas Name="BarLayer" SizeChanged="BarLayer_OnSizeChanged"/>
<Canvas Name="EditingLineLayer">
Expand All @@ -29,6 +30,5 @@
<primitives:LineLayer x:Name="LineLayer"/>
<Canvas Name="NoteLayer" SizeChanged="NoteLayer_OnSizeChanged"/>
</Grid>
<Canvas Grid.Column="2" Name="SpecialNoteLayer"/>
</Grid>
</controls:ScoreViewerBase>
11 changes: 7 additions & 4 deletions StarlightDirector/StarlightDirector/UI/EditMode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace StarlightDirector.UI {
using System.ComponentModel;

namespace StarlightDirector.UI {
public enum EditMode {

Select,
Relations,
Clear
[Description("Mode: Create relations")]
CreateRelations,
[Description("Mode: Reset note")]
ResetNote

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,23 @@
namespace StarlightDirector.UI.Windows {
partial class MainWindow {

public static readonly ICommand CmdEditModeSelect = CommandHelper.RegisterCommand("Alt+1", "Alt+NumPad1");
public static readonly ICommand CmdEditModeEditRelations = CommandHelper.RegisterCommand("Alt+2", "Alt+NumPad2");
public static readonly ICommand CmdEditModeClear = CommandHelper.RegisterCommand("Alt+0", "Alt+NumPad0");
public static readonly ICommand CmdEditModeCreateRelations = CommandHelper.RegisterCommand("Alt+1", "Alt+NumPad1");
public static readonly ICommand CmdEditModeResetNote = CommandHelper.RegisterCommand("Alt+2", "Alt+NumPad2");

private void CmdEditModeSelect_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = Editor.EditMode != EditMode.Select;
private void CmdEditModeCreateRelations_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = Editor.EditMode != EditMode.CreateRelations;
}

private void CmdEditModeSelect_Executed(object sender, ExecutedRoutedEventArgs e) {
Debug.Print("Edit mode: select");
Editor.EditMode = EditMode.Select;
private void CmdEditModeCreateRelations_Executed(object sender, ExecutedRoutedEventArgs e) {
Editor.EditMode = EditMode.CreateRelations;
}

private void CmdEditModeEditRelations_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = Editor.EditMode != EditMode.Relations;
private void CmdEditModeResetNote_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = Editor.EditMode != EditMode.ResetNote;
}

private void CmdEditModeEditRelations_Executed(object sender, ExecutedRoutedEventArgs e) {
if (Editor.EditMode == EditMode.Relations) {
Editor.EditMode = EditMode.Select;
return;
}
Editor.EditMode = EditMode.Relations;
Debug.Print("Edit mode: relations");
}

private void CmdEditModeClear_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = Editor.EditMode != EditMode.Clear;
}

private void CmdEditModeClear_Executed(object sender, ExecutedRoutedEventArgs e) {
if (Editor.EditMode == EditMode.Clear) {
Editor.EditMode = EditMode.Select;
return;
}
Editor.EditMode = EditMode.Clear;
Debug.Print("Edit mode: clear");
private void CmdEditModeResetNote_Executed(object sender, ExecutedRoutedEventArgs e) {
Editor.EditMode = EditMode.ResetNote;
}

}
Expand Down
Loading

0 comments on commit dc2fb54

Please sign in to comment.