-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVTabController.swift
407 lines (348 loc) · 15.3 KB
/
VTabController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// VTabController.swift
// tablution
//
// Created by Ian McCowan on 10/6/15.
//
//
import Foundation
let MAX_FRET = 22
@objc public class VTabController: NSViewController {
@IBOutlet weak var tabView: VTabView?
@IBOutlet weak var currentFretField: NSTextField?
@IBOutlet weak var chordModeField: NSTextField?
dynamic var baseFret: Int = 0
dynamic var soloMode: Bool = false
weak var tablature: VTablature?
var keyBindings: NSDictionary?
private var myContext = 0
// MARK: - Setup -
override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
let transformer: NSValueTransformer = VEditModeTransformer()
NSValueTransformer.setValueTransformer(transformer, forName: "editModeTransformer")
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupKeyBindings() {
if let plistPath: String = NSBundle.mainBundle().pathForResource("keyBindings", ofType: "plist") {
keyBindings = NSDictionary(contentsOfFile: plistPath)
if (keyBindings == nil) {
NSLog("Edit chars dictionary contains an error!")
}
}
else {
NSLog("Edit chars dictionary not found!")
}
}
public override func awakeFromNib() {
baseFret = 0
soloMode = false
self.setupKeyBindings()
}
public override func viewDidLoad() {
if let appDelegate: VTablutionDelegate = NSApplication.sharedApplication().delegate as? VTablutionDelegate {
appDelegate.viewController = self
}
}
public func setupTablature(tablature: VTablature) {
self.tablature = tablature
tabView!.tablature = tablature
tablature.addObserver(self, forKeyPath:"chords", options: [NSKeyValueObservingOptions.New, NSKeyValueObservingOptions.Old], context:&myContext)
tablature.addObserver(self, forKeyPath:"bars", options: NSKeyValueObservingOptions(rawValue: 0), context:&myContext)
}
// MARK: - Editing selectors -
// MARK: Chord-level changes
func insertBlankChord() {
let oneBlankChord: [VChord] = [VChord.chordWithIntArray([Int](count: tablature!.numStrings, repeatedValue: VNote.NO_FRET()))]
if tabView!.hasSelection() {
self.replaceSelectedChordsWithChords(oneBlankChord)
}
else {
let focusIndex: Int = tabView!.currFocusChordIndex + modeOffset()
let focusIndexSet: NSIndexSet = NSIndexSet(index: focusIndex)
self.insertChords(oneBlankChord, atIndexes: focusIndexSet, andSelectThem: false)
}
}
func insertChords(chordArray: [VChord], atIndexes indexes: NSIndexSet, andSelectThem doSelect: Bool) {
guard let tab = tablature, view = tabView else { return }
if let undoManager = self.undoManager as NSUndoManager? {
undoManager.registerUndoWithTarget(self, selector: Selector("deleteChordsAtIndexes:"), object: indexes)
undoManager.setActionName(NSLocalizedString("Insert Chords", comment: "insert chords undo"))
}
tab.insertChords(chordArray, atIndexes: indexes)
if doSelect {
tabView!.selectIndexes(indexes)
}
view.needsDisplay = true
}
func removeChordAtIndex(index: Int) {
tablature!.removeChordAtIndex(index)
}
func deleteChordsAtIndexes(indexes: NSIndexSet) {
guard let tab = tablature, view = tabView else { return }
let indexesAvailable = indexes.indexesInRange(NSRange(location: 0, length: tab.countOfChords()), options: NSEnumerationOptions(), passingTest: { (a, b) -> Bool in true })
if let undoManager = self.undoManager as NSUndoManager? {
undoManager.prepareWithInvocationTarget(self).insertChords(tab.chordsAtIndexes(indexesAvailable), atIndexes: indexesAvailable, andSelectThem: true)
undoManager.setActionName(NSLocalizedString("Delete Chords", comment: "delete chords undo"))
}
tab.removeChordsAtIndexes(indexesAvailable)
view.clearSelection()
}
func deleteSelectedChords() {
let selectedIndexes: NSIndexSet = tabView!.selectedIndexes()
deleteChordsAtIndexes(selectedIndexes)
}
func replaceChordsAtIndexes(indexes: NSIndexSet, withChords chords: [VChord]) {
tablature!.replaceChordsAtIndexes(indexes, withChords: chords)
}
func replaceSelectedChordsWithChords(chordArray: [VChord]) {
let insertionRange: NSRange = NSMakeRange(tabView!.selectedIndexes().firstIndex, chordArray.count)
let insertionIndexes: NSIndexSet = NSIndexSet(indexesInRange: insertionRange)
if let undoManager = self.undoManager as NSUndoManager? {
let undoTarget = undoManager.prepareWithInvocationTarget(self)
undoTarget.replaceChordsAtIndexes(insertionIndexes, withChords: tabView!.selectedChords() as! [VChord])
undoManager.setActionName(NSLocalizedString("Replace Selected Chords", comment: "replace selection undo"))
}
tablature!.removeChordsAtIndexes(tabView!.selectedIndexes())
tablature!.insertChords(chordArray, atIndexes: insertionIndexes)
tabView!.selectIndexes(insertionIndexes)
}
func toggleBarAtIndex(index: NSNumber) {
tablature!.toggleBarAtIndex(index.integerValue)
tabView!.needsDisplay = true
}
func toggleMeasureBar() {
let barIndex = Int(tabView!.currFocusChordIndex)
if let undoManager = self.undoManager as NSUndoManager? {
undoManager.registerUndoWithTarget(self, selector: Selector("toggleBarAtIndex:"), object: NSNumber(integer: barIndex))
undoManager.setActionName(NSLocalizedString("Undo Toggle Measure Bar", comment:"toggle bar undo"))
}
self.toggleBarAtIndex(barIndex)
}
// MARK: Note-level changes
func prepareUndoForChangeFromNote(atLocation loc: TabLocation) {
guard let undoManager = self.undoManager as NSUndoManager? else {
return
}
guard let note = tablature!.noteAtLocation(loc) else {
return
}
let undoTarget = undoManager.prepareWithInvocationTarget(self)
undoTarget.addNoteAtIndex(note, atIndex: loc.index, onString: loc.string)
undoManager.setActionName(NSLocalizedString("Change Note", comment: "change note undo"))
}
func addOpenString(whichString: NSNumber, reverseString doReverse: Bool) {
let stringAsInt = whichString.integerValue
self.addNoteAtFocus(onString: stringAsInt, onFret: 0, reverseString: doReverse)
}
// Ugh. This is just here because we can't use addNote from an undoManager
// since it involves a struct which is inaccessible from Obj-C.
func addNoteAtIndex(note: VNote, atIndex index: Int, onString whichString: Int) {
self.addNote(note, atLocation: TabLocation(index: index, string: whichString))
}
func addNote(note: VNote, atLocation loc: TabLocation) {
self.prepareUndoForChangeFromNote(atLocation: loc)
tablature!.insertNote(note, atLocation: loc)
}
func addNoteAtFocus(onString whichString: NSNumber, onFret whichFret: NSNumber, reverseString doReverse: Bool) {
// FIXME: I don't like this doReverse stuff; rather just set the right numbers in keyBindings.plist
let stringNum: Int = doReverse ? tablature!.numStrings - whichString.integerValue - 1 : whichString.integerValue
let fretNum: Int = whichFret.integerValue + baseFret
guard let focusIndex = tabView?.currFocusChordIndex else {
return
}
if whichString.integerValue < tablature!.numStrings {
if self.isInSoloMode() {
let newChord: VChord = VChord.chordWithOneFret(fretNum, onString: stringNum, numStrings: tablature!.numStrings)
self.insertChords([newChord], atIndexes: NSIndexSet(index: focusIndex), andSelectThem: false)
}
else {
self.addNote(VNote.noteAtFret(fretNum), atLocation: TabLocation(index: focusIndex, string: stringNum))
tabView!.focusNoteString = stringNum
}
}
}
func deleteFocusNote() {
let currentNote: VNote = tabView!.focusNote()
if currentNote.hasFret() {
self.prepareUndoForChangeFromNote(atLocation: TabLocation(index: tabView!.currFocusChordIndex, string: tabView!.focusNoteString))
tablature!.deleteNoteAtIndex(Int(tabView!.currFocusChordIndex), onString: Int(tabView!.focusNoteString))
}
}
func prepareUndoForNoteMarkChange(beforeNote: VNote, markType: MarkType) {
guard let undoManager = self.undoManager as NSUndoManager? else {
return
}
guard let undoTarget = undoManager.prepareWithInvocationTarget(self) as? VTabController else {
return
}
undoTarget.changeMarkForNote(beforeNote, toChar: markType == .Pre ? beforeNote.preMark.rawValue : beforeNote.postMark.rawValue)
undoManager.setActionName(NSLocalizedString("Change Note", comment: "change note undo"))
}
func changeMarkForNote(note: VNote, toChar markChar: Character) {
guard let markType = determineMarkType(markChar) as MarkType? else {
// FIXME: error?
return
}
if note.hasFret() {
self.prepareUndoForNoteMarkChange(note, markType: markType)
note.setMark(markChar)
}
}
func changeFocusNoteMark(markCharString: String) {
if markCharString.characters.count == 1 {
changeMarkForNote(tabView!.focusNote(), toChar: Character(markCharString))
}
}
// MARK: Mode changes
func incrementBaseFret() {
let currentFret: Int = baseFret
if currentFret < MAX_FRET {
baseFret = currentFret + 1
}
}
func decrementBaseFret() {
let currentFret: Int = baseFret
if currentFret > 0 {
baseFret = currentFret - 1
}
}
func toggleSoloMode() {
self.willChangeValueForKey("soloMode")
let currentMode: Bool = soloMode.boolValue
soloMode = !currentMode
self.didChangeValueForKey("soloMode")
tabView!.clearSelection()
if (tabView!.currFocusChordIndex + modeOffset() > tablature!.countOfChords()) {
focusPrevChord();
}
}
func modeOffset() -> Int {
return soloMode.boolValue ? 0 : 1
}
// MARK: Focus changes
func focusNextChord() -> Bool {
if Int(tabView!.currFocusChordIndex + modeOffset()) < tablature!.countOfChords() {
tabView!.focusNextChord()
return true
}
return false
}
func focusPrevChord() -> Bool {
if tabView!.currFocusChordIndex > 0 {
tabView!.focusPrevChord()
return true
}
return false
}
func focusUpString() -> Bool {
if tabView!.focusNoteString > 0 {
tabView!.focusUpString()
return true
}
return false
}
func focusDownString() -> Bool {
if Int(tabView!.focusNoteString) < tablature!.numStrings - 1 {
tabView!.focusDownString()
return true
}
return false
}
// MARK: Information
func isInSoloMode() -> Bool {
return soloMode.boolValue
}
// MARK: - AppKit overrides -
// MARK: inputManager
@IBAction override public func moveRight(sender: AnyObject?) {
self.focusNextChord()
}
@IBAction override public func moveLeft(sender: AnyObject?) {
self.focusPrevChord()
}
@IBAction override public func moveUp(sender: AnyObject?) {
if !isInSoloMode() {
self.focusUpString()
}
}
@IBAction override public func moveDown(sender: AnyObject?) {
if !isInSoloMode() {
self.focusDownString()
}
}
@IBAction override public func deleteForward(sender: AnyObject?) {
if isInSoloMode() {
guard let view = tabView else {
return
}
NSLog("%d", view.currFocusChordIndex)
let chordIndexToDelete: Int = Int(view.currFocusChordIndex)
deleteChordsAtIndexes(NSIndexSet(index: chordIndexToDelete))
}
else {
self.deleteFocusNote()
}
}
@IBAction override public func deleteBackward(sender: AnyObject?) {
if tabView!.hasSelection() {
self.deleteSelectedChords()
}
else {
guard let view = tabView else {
return
}
guard view.currFocusChordIndex > 0 else {
return
}
let chordIndexToDelete: Int = Int(view.currFocusChordIndex) - 1
deleteChordsAtIndexes(NSIndexSet(index: chordIndexToDelete))
}
}
// MARK: KVO
override public func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
NSLog("VTabController sees a change in %@!", keyPath!)
guard
let view = tabView,
let changeDict = change,
let changeKindNumber = changeDict["kind"] as? NSNumber,
let changeKind = NSKeyValueChange(rawValue: UInt(changeKindNumber.integerValue)) as NSKeyValueChange? else {
return
}
switch changeKind {
case .Replacement:
guard let oldChordArray = changeDict[NSKeyValueChangeOldKey],
let newChordArray = changeDict[NSKeyValueChangeNewKey] else {
return
}
if oldChordArray.count == 1 && newChordArray.count == 1 {
let oldChord: VChord = oldChordArray[0] as! VChord
let newChord: VChord = newChordArray[0] as! VChord
let changedNotesIndexes: NSIndexSet = newChord.indexesOfChangedNotesFrom(oldChord)!
if changedNotesIndexes.count == 1 {
view.focusNoteString = changedNotesIndexes.firstIndex
}
}
case .Insertion:
guard let indexes = changeDict["indexes"] as? NSIndexSet else {
return
}
let indexForFocusAdjustment: Int = Int(view.currFocusChordIndex) + (self.isInSoloMode() ? 3 : 2)
let rangeBeforeFocus: NSRange = NSMakeRange(0, indexForFocusAdjustment)
let indexesBeforeFocus: Int = indexes.countOfIndexesInRange(rangeBeforeFocus)
view.currFocusChordIndex = view.currFocusChordIndex + indexesBeforeFocus
case .Removal:
guard let indexes = changeDict["indexes"] as? NSIndexSet else {
return
}
let indexForFocusAdjustment: Int = Int(view.currFocusChordIndex)
let rangeBeforeFocus: NSRange = NSMakeRange(0, indexForFocusAdjustment)
let indexesBeforeFocus: Int = indexes.countOfIndexesInRange(rangeBeforeFocus)
view.currFocusChordIndex = view.currFocusChordIndex - indexesBeforeFocus
default: break
}
tabView!.needsDisplay = true
}
}