Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readOnly mode to PianoRoll #6

Merged
merged 5 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Rename readOnly to editable, reorder params by alphabetical order
  • Loading branch information
josephktcheung committed Oct 28, 2022
commit 88c28e10f014a3b4905921d35d3b152d7758e52f
20 changes: 10 additions & 10 deletions Sources/PianoRoll/PianoRoll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import SwiftUI
/// Note: Requires macOS 12 / iOS 15 due to SwiftUI bug (crashes in SwiftUI when deleting notes).
public struct PianoRoll: View {
@Binding var model: PianoRollModel
var editable: Bool
var gridColor: Color
var gridSize: CGSize
var noteColor: Color
var gridColor: Color
var readOnly: Bool
var noteLineOpacity: Double


/// Initialize PianoRoll with a binding to a model and a color
/// - Parameters:
/// - editable: Disable edition of any note in piano roll
/// - model: PianoRoll data
/// - noteColor: Color to use for the note indicator, defaults to system accent color
/// - noteLineOpacity: Opacity of the note view vertical black line
/// - gridSize: Size of a grid cell
/// - gridColor: Color of grid
/// - readOnly: Disable addition, modification and deletion of any note in piano roll
/// - gridSize: Size of a grid cell
public init(
editable: Bool = true,
model: Binding<PianoRollModel>,
noteColor: Color = .accentColor,
noteLineOpacity: Double = 1,
gridSize: CGSize = CGSize(width: 80, height: 40),
gridColor: Color = Color(red: 15.0 / 255.0, green: 17.0 / 255.0, blue: 16.0 / 255.0),
readOnly: Bool = false
gridSize: CGSize = CGSize(width: 80, height: 40)
) {
_model = model
self.noteColor = noteColor
self.noteLineOpacity = noteLineOpacity
self.gridSize = gridSize
self.gridColor = gridColor
self.readOnly = readOnly
self.editable = editable
}


Expand All @@ -52,7 +52,7 @@ public struct PianoRoll: View {
.stroke(lineWidth: 0.5)
.foregroundColor(gridColor)
.contentShape(Rectangle())
.gesture(readOnly ? nil : TapGesture().sequenced(before: dragGesture))
.gesture(editable ? TapGesture().sequenced(before: dragGesture) : nil)
ForEach(model.notes) { note in
PianoRollNoteView(
note: $model.notes[model.notes.firstIndex(of: note)!],
Expand All @@ -61,11 +61,11 @@ public struct PianoRoll: View {
sequenceLength: model.length,
sequenceHeight: model.height,
isContinuous: true,
readOnly: readOnly,
editable: editable,
lineOpacity: noteLineOpacity
)
.onTapGesture {
if readOnly { return }
guard editable else { return }
model.notes.removeAll(where: { $0 == note })
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/PianoRoll/PianoRollNoteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct PianoRollNoteView: View {
var sequenceLength: Int
var sequenceHeight: Int
var isContinuous = false
var readOnly: Bool = false
var editable: Bool = false
var lineOpacity: Double = 1

var noteColor: Color {
Expand Down Expand Up @@ -116,22 +116,22 @@ struct PianoRollNoteView: View {
.foregroundColor(.black)
.padding(4)
.frame(width: 10)
.opacity(lineOpacity)
.opacity(editable ? lineOpacity : 0)
}
.onHover { over in hovering = over }
.padding(1) // so we can see consecutive notes
.frame(width: max(gridSize.width, gridSize.width * CGFloat(note.length) + lengthOffset),
height: gridSize.height)
.offset( noteOffset(note: startNote ?? note, dragOffset: offset))
.gesture(readOnly ? nil : noteDragGesture)
.offset(noteOffset(note: startNote ?? note, dragOffset: offset))
.gesture(editable ? noteDragGesture : nil)

// Length tab at the end of the note.
HStack {
Spacer()
Rectangle()
.foregroundColor(.white.opacity(0.001))
.frame(width: gridSize.width * 0.5, height: gridSize.height)
.gesture(readOnly ? nil : lengthDragGesture)
.gesture(editable ? lengthDragGesture : nil)
}
.frame(width: gridSize.width * CGFloat(note.length),
height: gridSize.height)
Expand Down