| 1 |
/* |
| 2 |
|
| 3 |
CsoundVirtualKeyboard.swift |
| 4 |
|
| 5 |
Nikhil Singh, Dr. Richard Boulanger |
| 6 |
Adapted from the Csound iOS Examples by Steven Yi and Victor Lazzarini |
| 7 |
|
| 8 |
This file is part of Csound iOS SwiftExamples. |
| 9 |
|
| 10 |
The Csound for iOS Library is free software; you can redistribute it |
| 11 |
and/or modify it under the terms of the GNU Lesser General Public |
| 12 |
License as published by the Free Software Foundation; either |
| 13 |
version 2.1 of the License, or (at your option) any later version. |
| 14 |
|
| 15 |
Csound is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU Lesser General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU Lesser General Public |
| 21 |
License along with Csound; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 23 |
02111-1307 USA |
| 24 |
|
| 25 |
*/ |
| 26 |
|
| 27 |
import UIKit |
| 28 |
|
| 29 |
class CsoundVirtualKeyboard: UIView { |
| 30 |
|
| 31 |
// Variable declarations |
| 32 |
private var keyDown = [Bool].init(repeating: false, count: 25) |
| 33 |
private var currentTouches = NSMutableSet() |
| 34 |
private var keyRects = [CGRect](repeatElement(CGRect(), count: 25)) |
| 35 |
private var lastWidth: CGFloat = 0.0 |
| 36 |
private let keysNum = 25 |
| 37 |
|
| 38 |
@IBOutlet var keyboardDelegate: AnyObject? // keyboardDelegate object will handle key presses and releases |
| 39 |
|
| 40 |
override init(frame: CGRect) { |
| 41 |
super.init(frame: frame) |
| 42 |
} |
| 43 |
|
| 44 |
required init?(coder aDecoder: NSCoder) { |
| 45 |
super.init(coder: aDecoder) |
| 46 |
isMultipleTouchEnabled = true // Enable multiple touch so we can press multiple keys |
| 47 |
} |
| 48 |
|
| 49 |
// Check if a key is a white key or a black key depending on its index |
| 50 |
func isWhiteKey(_ key: Int) -> Bool { |
| 51 |
switch (key % 12) { |
| 52 |
case 0, 2, 4, 5, 7, 9, 11: |
| 53 |
return true |
| 54 |
default: |
| 55 |
return false |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// MARK: Update key statuses |
| 60 |
private func updateKeyRects() { |
| 61 |
if lastWidth == bounds.size.width { return } |
| 62 |
|
| 63 |
lastWidth = bounds.size.width |
| 64 |
let whiteKeyHeight: CGFloat = bounds.size.height |
| 65 |
let blackKeyHeight: CGFloat = whiteKeyHeight * 0.625 |
| 66 |
|
| 67 |
let whiteKeyWidth: CGFloat = bounds.size.width/15.0 |
| 68 |
let blackKeyWidth: CGFloat = whiteKeyWidth * 0.833333 |
| 69 |
|
| 70 |
let leftKeyBound: CGFloat = whiteKeyWidth - (blackKeyWidth / 2.0) |
| 71 |
|
| 72 |
var lastWhiteKey: CGFloat = 0 |
| 73 |
|
| 74 |
keyRects[0] = CGRect(x: 0, y: 0, width: whiteKeyWidth, height: whiteKeyHeight) |
| 75 |
|
| 76 |
for i in 1 ..< keysNum { |
| 77 |
if !(isWhiteKey(i)) { |
| 78 |
keyRects[i] = CGRect(x: (lastWhiteKey * whiteKeyWidth) + leftKeyBound, y: 0, width: blackKeyWidth, height: blackKeyHeight) |
| 79 |
} else { |
| 80 |
lastWhiteKey += 1 |
| 81 |
keyRects[i] = CGRect(x: lastWhiteKey * whiteKeyWidth, y: 0, width: whiteKeyWidth, height: whiteKeyHeight) |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private func getKeyboardKey(_ point: CGPoint) -> Int { |
| 87 |
var keyNum = -1 |
| 88 |
|
| 89 |
for i in 0 ..< keysNum { |
| 90 |
if keyRects[i].contains(point) { |
| 91 |
keyNum = i |
| 92 |
if !(isWhiteKey(i)) { |
| 93 |
break |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
return keyNum |
| 98 |
} |
| 99 |
|
| 100 |
private func updateKeyStates() { |
| 101 |
updateKeyRects() |
| 102 |
|
| 103 |
var count = 0 |
| 104 |
|
| 105 |
let touches = currentTouches.allObjects |
| 106 |
count = touches.count |
| 107 |
|
| 108 |
var currentKeyState = [Bool](repeatElement(true, count: keysNum)) |
| 109 |
|
| 110 |
for i in 0 ..< keysNum { |
| 111 |
currentKeyState[i] = false |
| 112 |
} |
| 113 |
|
| 114 |
for i in 0 ..< count { |
| 115 |
let touch = touches[i] as! UITouch |
| 116 |
let point = touch.location(in: self) |
| 117 |
let index = getKeyboardKey(point) |
| 118 |
|
| 119 |
if index != -1 { |
| 120 |
currentKeyState[index] = true |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
var keysUpdated = false |
| 125 |
|
| 126 |
for i in 0 ..< keysNum { |
| 127 |
if keyDown[i] != currentKeyState[i] { |
| 128 |
keysUpdated = true |
| 129 |
|
| 130 |
let keyDownState = currentKeyState[i] |
| 131 |
keyDown[i] = keyDownState |
| 132 |
|
| 133 |
if keyboardDelegate != nil { |
| 134 |
if keyDownState { |
| 135 |
keyboardDelegate?.keyDown(self, keyNum: i) |
| 136 |
} else { |
| 137 |
keyboardDelegate?.keyUp(self, keyNum: i) |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if keysUpdated { |
| 144 |
DispatchQueue.main.async { [unowned self] in |
| 145 |
self.setNeedsDisplay() |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// MARK: Touch Handling Code |
| 151 |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 152 |
for touch in touches { |
| 153 |
currentTouches.add(touch) |
| 154 |
} |
| 155 |
updateKeyStates() |
| 156 |
} |
| 157 |
|
| 158 |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 159 |
updateKeyStates() |
| 160 |
} |
| 161 |
|
| 162 |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 163 |
for touch in touches { |
| 164 |
currentTouches.remove(touch) |
| 165 |
} |
| 166 |
updateKeyStates() |
| 167 |
} |
| 168 |
|
| 169 |
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 170 |
for touch in touches { |
| 171 |
currentTouches.remove(touch) |
| 172 |
} |
| 173 |
updateKeyStates() |
| 174 |
} |
| 175 |
|
| 176 |
// MARK: Drawing Code |
| 177 |
override func draw(_ rect: CGRect) { |
| 178 |
let context = UIGraphicsGetCurrentContext() |
| 179 |
|
| 180 |
let whiteKeyHeight = rect.size.height |
| 181 |
let blackKeyHeight = round(rect.size.height * 0.625) |
| 182 |
let whiteKeyWidth = rect.size.width/15.0 |
| 183 |
let blackKeyWidth = whiteKeyWidth * 0.8333333 |
| 184 |
let blackKeyOffset = blackKeyWidth/2.0 |
| 185 |
|
| 186 |
var runningX: CGFloat = 0 |
| 187 |
let yval = 0 |
| 188 |
|
| 189 |
context?.setFillColor(UIColor.white.cgColor) |
| 190 |
context?.fill(bounds) |
| 191 |
|
| 192 |
context?.setStrokeColor(UIColor.black.cgColor) |
| 193 |
context?.stroke(bounds) |
| 194 |
|
| 195 |
let lineHeight = whiteKeyHeight - 1 |
| 196 |
var newX = 0 |
| 197 |
|
| 198 |
for i in 0 ..< keysNum { |
| 199 |
if isWhiteKey(i) { |
| 200 |
newX = Int(round(runningX + 0.5)) |
| 201 |
|
| 202 |
if keyDown[i] { |
| 203 |
let newW = Int(round(runningX + whiteKeyWidth + 0.5) - CGFloat(newX)) |
| 204 |
context?.setFillColor(UIColor.blue.cgColor) |
| 205 |
context?.fill(CGRect(x: CGFloat(newX), y: CGFloat(yval), width: CGFloat(newW), height: whiteKeyHeight - 1)) |
| 206 |
} |
| 207 |
runningX += whiteKeyWidth |
| 208 |
|
| 209 |
context?.setStrokeColor(UIColor.black.cgColor) |
| 210 |
context?.stroke(CGRect(x: CGFloat(newX), y: CGFloat(yval), width: CGFloat(newX), height: lineHeight)) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
runningX = 0 |
| 215 |
|
| 216 |
for i in 0 ..< keysNum { |
| 217 |
if isWhiteKey(i) { |
| 218 |
runningX += whiteKeyWidth |
| 219 |
} else { |
| 220 |
if keyDown[i] { |
| 221 |
context?.setFillColor(UIColor.blue.cgColor) |
| 222 |
} else { |
| 223 |
context?.setFillColor(UIColor.black.cgColor) |
| 224 |
} |
| 225 |
|
| 226 |
context?.fill(CGRect(x: runningX - blackKeyOffset, y: CGFloat(yval), width: blackKeyWidth, height: blackKeyHeight)) |
| 227 |
context?.setStrokeColor(UIColor.black.cgColor) |
| 228 |
context?.stroke(CGRect(x: runningX - blackKeyOffset, y: CGFloat(yval), width: blackKeyWidth, height: blackKeyHeight)) |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// Protocol for delegate to conform to |
| 235 |
@objc protocol CsoundVirtualKeyboardDelegate { |
| 236 |
func keyUp(_ keybd:CsoundVirtualKeyboard, keyNum:Int) |
| 237 |
func keyDown(_ keybd:CsoundVirtualKeyboard, keyNum:Int) |
| 238 |
} |