private Bounds CreateWholeNoteChordNotes(ABC.Chord chord, ABC.Clef clef, GameObject container, Vector3 offset, ref Bounds totalBounds) { var lastNotePos = Vector3.zero; var rootBounds = AddChordNoteHead(chord.notes[0].pitch, ABC.Length.Whole, clef, NoteDirection.Down, container, offset, ref lastNotePos); totalBounds.Encapsulate(rootBounds); bool right = false; for (int i = 1; i < chord.notes.Length; i++) { var noteOffset = offset; Bounds itemBounds; if (!right && chord.notes[i].pitch - chord.notes[i - 1].pitch == 1) { noteOffset.x += compressedChordWholeNoteOffset; right = true; } else { right = false; } itemBounds = AddChordNoteHead(chord.notes[i].pitch, ABC.Length.Whole, clef, NoteDirection.Down, container, noteOffset, ref lastNotePos); totalBounds.Encapsulate(itemBounds); if (!right) { rootBounds.Encapsulate(itemBounds); } } return(rootBounds); }
void AddChordDots(ABC.Chord chord, ABC.Clef clef, NoteDirection noteDirection, Bounds rootItem, GameObject container, ref Bounds totalBounds) { int oddStepOffset = noteDirection == NoteDirection.Up ? 1 : -1; var dotSet = new HashSet <int>(); foreach (var note in chord.notes) { int stepCount = note.pitch - clefZero[clef]; if (stepCount % 2 == 1) { stepCount += oddStepOffset; } dotSet.Add(stepCount); } foreach (var step in dotSet) { var positionX = rootItem.max.x; for (int i = 0; i < chord.dotCount; i++) { var dot = CreateNoteDot(step, container, positionX + dotAdvance); var dotBounds = dot.bounds; positionX += dotBounds.size.x; totalBounds.Encapsulate(dotBounds); } } }
private bool ChordIsCompressed(ABC.Chord chord, NoteDirection noteDirection) { if (noteDirection == NoteDirection.Down) { for (int i = chord.notes.Length - 2; i >= 0; i--) { if (chord.notes[i + 1].pitch - chord.notes[i].pitch == 1) { return(true); } } } else { for (int i = 1; i < chord.notes.Length; i++) { if (chord.notes[i].pitch - chord.notes[i - 1].pitch == 1) { return(true); } } } return(false); }
private NoteInfo CreateChord(ABC.Chord chord, ABC.Clef clef, NoteDirection noteDirection, Beam beam, IReadOnlyList <string> decorations, GameObject container) { var offset = Vector3.zero; var totalBounds = new Bounds(); totalBounds.SetMinMax(offset, offset); if (CreateChordAccidentals(chord.notes, clef, ref offset, container, ref totalBounds)) { offset.x = totalBounds.max.x + accidentalOffset; } var staffMarkers = CreateChordStaffMarkers(noteDirection, chord, clef, offset.x, compressedChordNoteOffset, ref totalBounds); if (staffMarkers != null) // this ensures that the note appears centered w.r.t the markers { offset += new Vector3(staffMarkerNoteOffset, 0.0f, 0.0f); } Bounds rootBounds = CreateChordNotes(noteDirection, chord, beam, clef, container, offset, ref totalBounds); AddDecorations(chord, decorations, rootBounds, container, ref totalBounds); if (chord.dotCount > 0) { AddChordDots(chord, clef, noteDirection, totalBounds, container, ref totalBounds); } if (staffMarkers != null) { staffMarkers.transform.parent = container.transform; } return(new NoteInfo(rootBounds, totalBounds)); }
private Bounds CreateChordNotes(NoteDirection noteDirection, ABC.Chord chord, Beam beam, ABC.Clef clef, GameObject container, Vector3 offset, ref Bounds totalBounds) { if (noteDirection == NoteDirection.Up) { return(CreateChordNotesUp(chord, beam, clef, container, offset, ref totalBounds)); } else { return(CreateChordNotesDown(chord, beam, clef, container, offset, ref totalBounds)); } }
public NoteInfo CreateChord(ABC.Chord chord, ABC.Clef clef, IReadOnlyList <string> decorations, GameObject container) { var noteDirection = DetermineChordNoteDirection(chord.notes, clef); if (chord.length == ABC.Length.Whole) { return(CreateWholeNoteChord(chord, clef, decorations, container)); } else { return(CreateChord(chord, clef, noteDirection, null, decorations, container)); } }
private GameObject CreateChordStaffMarkers(NoteDirection noteDirection, ABC.Chord chord, ABC.Clef clef, float position, float offsetSize, ref Bounds totalBounds) { if (noteDirection == NoteDirection.Up) { return(CreateChordStaffMarkersUp(chord, clef, position, offsetSize, ref totalBounds)); } else { if (ChordIsCompressed(chord, NoteDirection.Down)) { position += offsetSize; } return(CreateChordStaffMarkersDown(chord, clef, position, -offsetSize, ref totalBounds)); } }
private GameObject CreateChordStaffMarkersDown(ABC.Chord chord, ABC.Clef clef, float position, float offsetSize, ref Bounds totalBounds) { int stepCount = chord.notes[chord.notes.Length - 1].pitch - clefZero[clef]; if (stepCount < 9) { return(null); } if (stepCount % 2 == 0) { stepCount -= 1; } var staffMarkers = new GameObject("Staff Markers"); float staffMarkerSize = chord.length < ABC.Length.Whole ? 1.0f : wholeNoteStaffMarkerSize; for (int step = stepCount; step >= 9; step -= 2) { totalBounds.Encapsulate(CreateStaffMark(step, staffMarkers, position, staffMarkerSize)); } for (int i = chord.notes.Length - 2; i >= 0; i--) { if (stepCount < 9) { break; } if (chord.notes[i + 1].pitch - chord.notes[i].pitch == 1) { stepCount = chord.notes[i].pitch - clefZero[clef]; if (stepCount % 2 == 0) { stepCount -= 1; } for (int step = stepCount; step >= 9; step -= 2) { totalBounds.Encapsulate(CreateStaffMark(step, staffMarkers, position + offsetSize, staffMarkerSize)); } break; } } return(staffMarkers); }
private GameObject CreateChordStaffMarkersUp(ABC.Chord chord, ABC.Clef clef, float position, float offsetSize, ref Bounds totalBounds) { int stepCount = chord.notes[0].pitch - clefZero[clef]; if (stepCount > -3) { return(null); } if (stepCount % 2 == 0) { stepCount += 1; } var staffMarkers = new GameObject("Staff Markers"); float staffMarkerSize = chord.length < ABC.Length.Whole ? 1.0f : wholeNoteStaffMarkerSize; for (int step = stepCount; step <= -3; step += 2) { totalBounds.Encapsulate(CreateStaffMark(step, staffMarkers, position, staffMarkerSize)); } for (int i = 1; i < chord.notes.Length; i++) { if (stepCount > -3) { break; } if (chord.notes[i].pitch - chord.notes[i - 1].pitch == 1) { stepCount = chord.notes[i].pitch - clefZero[clef]; if (stepCount % 2 == 0) { stepCount += 1; } for (int step = stepCount; step <= -3; step += 2) { totalBounds.Encapsulate(CreateStaffMark(step, staffMarkers, position + offsetSize, staffMarkerSize)); } break; } } return(staffMarkers); }
public NoteInfo CreateChord(ABC.Chord chord, Beam beam, IReadOnlyList <string> decorations, GameObject container) { return(CreateChord(chord, beam.clef, beam.noteDirection, beam, decorations, container)); }
private Bounds CreateChordNotesDown(ABC.Chord chord, Beam beam, ABC.Clef clef, GameObject container, Vector3 offset, ref Bounds totalBounds) { var stem = spriteCache.GetSpriteObject("Note_Stem_Down"); stem.transform.parent = container.transform; var lastNotePos = Vector3.zero; if (ChordIsCompressed(chord, NoteDirection.Down)) { offset.x += compressedChordNoteOffset; } var dotValue = chord.length > ABC.Length.Quarter ? chord.length : ABC.Length.Quarter; var rootBounds = AddChordNoteHead(chord.notes[chord.notes.Length - 1].pitch, dotValue, clef, NoteDirection.Down, container, offset, ref lastNotePos); var chordBounds = rootBounds; var stemPos = container.transform.GetChild(container.transform.childCount - 1).localPosition + Beam.stemDownOffset; stem.transform.localPosition = stemPos; bool left = false; for (int i = chord.notes.Length - 2; i >= 0; i--) { var noteOffset = offset; if (!left && chord.notes[i + 1].pitch - chord.notes[i].pitch == 1) { noteOffset.x -= compressedChordNoteOffset; left = true; } else { left = false; } var noteBounds = AddChordNoteHead(chord.notes[i].pitch, dotValue, clef, NoteDirection.Down, container, noteOffset, ref lastNotePos); chordBounds.Encapsulate(noteBounds); if (!left) { rootBounds.Encapsulate(noteBounds); } } SpriteRenderer flag = null; float stemHeight = Mathf.Abs((lastNotePos.y - Beam.defaultStemHeight) - stemPos.y); if (beam == null) { // If this chord is not part of a beam it may need to have a flag attached. if (chord.length <= ABC.Length.Eighth) { flag = spriteCache.GetSpriteObject($"Note_Flag_{chord.length}_Down"); flag.transform.parent = container.transform; } } else if (beam.stemHeight != Beam.unspecifiedStemHeight) { // beam contains notes at different heights, used the calculated stem height stemHeight = Mathf.Abs(stemHeight - stemPos.y); } stem.transform.localScale = new Vector3(1.0f, stemHeight, 1.0f); var stemBounds = stem.bounds; if (flag != null) { flag.transform.localPosition = new Vector3(stemBounds.max.x, stemBounds.min.y, 0.0f); } rootBounds.Encapsulate(stemBounds); chordBounds.Encapsulate(stemBounds); totalBounds.Encapsulate(chordBounds); return(rootBounds); }