From 3eb2e131e5b8b023db3ae3fd25d6ea1f389ce1e2 Mon Sep 17 00:00:00 2001 From: Maximilian Maksutovic Date: Thu, 17 Aug 2023 14:45:25 -0700 Subject: [PATCH 1/2] added drag gesture for keyboard --- Demo/KeyboardDemo.xcodeproj/project.pbxproj | 4 +- Demo/Shared/ContentView.swift | 111 ++++---------------- 2 files changed, 22 insertions(+), 93 deletions(-) diff --git a/Demo/KeyboardDemo.xcodeproj/project.pbxproj b/Demo/KeyboardDemo.xcodeproj/project.pbxproj index 46fe3a8..af42276 100644 --- a/Demo/KeyboardDemo.xcodeproj/project.pbxproj +++ b/Demo/KeyboardDemo.xcodeproj/project.pbxproj @@ -337,7 +337,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = W22K9JG3J6; + DEVELOPMENT_TEAM = UNPG3B9P9H; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; @@ -367,7 +367,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = W22K9JG3J6; + DEVELOPMENT_TEAM = UNPG3B9P9H; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; diff --git a/Demo/Shared/ContentView.swift b/Demo/Shared/ContentView.swift index 8be9388..c0c4343 100644 --- a/Demo/Shared/ContentView.swift +++ b/Demo/Shared/ContentView.swift @@ -66,109 +66,38 @@ struct ContentView: View { var body: some View { HStack { - Keyboard(layout: .verticalIsomorphic(pitchRange: Pitch(48) ... Pitch(77))).frame(width: 100) VStack { HStack { Stepper("Lowest Note: \(Pitch(intValue: lowNote).note(in: .C).description)", onIncrement: { - if lowNote < 126, highNote > lowNote + 12 { - lowNote += 1 - } - }, + if lowNote < 126, highNote > lowNote + 12 { + lowNote += 1 + } + }, onDecrement: { - if lowNote > 0 { - lowNote -= 1 - } - }) + if lowNote > 0 { + lowNote -= 1 + } + }) Stepper("Highest Note: \(Pitch(intValue: highNote).note(in: .C).description)", onIncrement: { - if highNote < 126 { - highNote += 1 - } - }, - onDecrement: { - if highNote > 1, highNote > lowNote + 12 { - highNote -= 1 - } - - }) - } - Keyboard(layout: .piano(pitchRange: Pitch(intValue: lowNote) ... Pitch(intValue: highNote)), - noteOn: noteOnWithVerticalVelocity(pitch:point:), noteOff: noteOff) - .frame(minWidth: 100, minHeight: 100) - - HStack { - Stepper("Root: \(root.description)", - onIncrement: { - let allSharpNotes = (0...11).map { Note(pitch: Pitch(intValue: $0)).noteClass } - var index = allSharpNotes.firstIndex(of: root.canonicalNote.noteClass) ?? 0 - index += 1 - if index > 11 { index = 0} - if index < 0 { index = 1} - rootIndex = index - root = allSharpNotes[index] + if highNote < 126 { + highNote += 1 + } }, onDecrement: { - let allSharpNotes = (0...11).map { Note(pitch: Pitch(intValue: $0)).noteClass } - var index = allSharpNotes.firstIndex(of: root.canonicalNote.noteClass) ?? 0 - index -= 1 - if index > 11 { index = 0} - if index < 0 { index = 1} - rootIndex = index - root = allSharpNotes[index] - }) - - Stepper("Scale: \(scale.description)", - onIncrement: { scaleIndex += 1 }, - onDecrement: { scaleIndex -= 1 }) - } - Keyboard(layout: .isomorphic(pitchRange: - Pitch(intValue: 12 + rootIndex) ... Pitch(intValue: 84 + rootIndex), - root: root, - scale: scale), - noteOn: noteOnWithReversedVerticalVelocity(pitch:point:), noteOff: noteOff) - .frame(minWidth: 100, minHeight: 100) - - Keyboard(layout: .guitar(), - noteOn: noteOn, noteOff: noteOff) { pitch, isActivated in - KeyboardKey(pitch: pitch, - isActivated: isActivated, - text: pitch.note(in: .F).description, - pressedColor: Color(PitchColor.newtonian[Int(pitch.pitchClass)]), - alignment: .center) - } - .frame(minWidth: 100, minHeight: 100) - - Keyboard(layout: .isomorphic(pitchRange: Pitch(48) ... Pitch(65))) { pitch, isActivated in - KeyboardKey(pitch: pitch, - isActivated: isActivated, - text: pitch.note(in: .F).description, - pressedColor: Color(PitchColor.newtonian[Int(pitch.pitchClass)])) - } - .frame(minWidth: 100, minHeight: 100) - - Keyboard(latching: true, noteOn: noteOn, noteOff: noteOff) { pitch, isActivated in - if isActivated { - ZStack { - Rectangle().foregroundColor(.black) - VStack { - Spacer() - Text(pitch.note(in: .C).description).font(.largeTitle) - }.padding() + if highNote > 1, highNote > lowNote + 12 { + highNote -= 1 } - - } else { - Rectangle().foregroundColor(randomColors[Int(pitch.intValue) % 12]) - } + + }) } - .frame(minWidth: 100, minHeight: 100) + Keyboard(layout: .piano(pitchRange: Pitch(intValue: lowNote) ... Pitch(intValue: highNote)), + noteOn: noteOnWithVerticalVelocity(pitch:point:), noteOff: noteOff) + .frame(minWidth: 100) + .frame(height: 100) } - Keyboard( - layout: .verticalPiano(pitchRange: Pitch(48) ... Pitch(77), - initialSpacerRatio: evenSpacingInitialSpacerRatio, - spacerRatio: evenSpacingSpacerRatio, - relativeBlackKeyWidth: evenSpacingRelativeBlackKeyWidth) - ).frame(width: 100) + } .background(colorScheme == .dark ? Color.clear : Color(red: 0.9, green: 0.9, blue: 0.9)) From 452470d4630929dbcdd7887e5c1978034d5db7ec Mon Sep 17 00:00:00 2001 From: Maximilian Maksutovic Date: Thu, 31 Aug 2023 16:19:40 -0700 Subject: [PATCH 2/2] Added @available tags for canvas and graphics context removed new preview macro for xcode 14 --- Sources/Keyboard/MIDIMonitorKeyboard.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Sources/Keyboard/MIDIMonitorKeyboard.swift b/Sources/Keyboard/MIDIMonitorKeyboard.swift index 7c1817f..6422aed 100644 --- a/Sources/Keyboard/MIDIMonitorKeyboard.swift +++ b/Sources/Keyboard/MIDIMonitorKeyboard.swift @@ -2,14 +2,15 @@ import SwiftUI import Tonic +@available(iOS 15, macOS 12.0, *) extension GraphicsContext { func fill(rect: CGRect, with color: Color) { fill(Path(roundedRect: rect, cornerRadius: 0), with: GraphicsContext.Shading.color(color)) } } +@available(iOS 15, macOS 12.0, *) struct MIDIMonitorKeyboard: View { - var layout: KeyboardLayout var activatedPitches: PitchSet var colorFunction: (Pitch)->Color @@ -87,9 +88,11 @@ struct MIDIMonitorKeyboard: View { } var p = PitchSet(pitches: [Pitch(65), Pitch(68), Pitch(71), Pitch(74)]) -#Preview { - MIDIMonitorKeyboard(layout: .piano(pitchRange: Pitch(61)...Pitch(88)), - activatedPitches: p, - colorFunction: { x in Color(cgColor: PitchColor.helmholtz[Int(x.pitchClass)])} - ).frame(width: 600, height: 100) -} + +//MARK: - we can have preview macro after Xcode 15 is released +//#Preview { +// MIDIMonitorKeyboard(layout: .piano(pitchRange: Pitch(61)...Pitch(88)), +// activatedPitches: p, +// colorFunction: { x in Color(cgColor: PitchColor.helmholtz[Int(x.pitchClass)])} +// ).frame(width: 600, height: 100) +//}