| 1 |
/* |
| 2 |
|
| 3 |
ControlXYGrid.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 ControlXYGrid: UIControl { |
| 30 |
|
| 31 |
private var borderWidth: CGFloat = 10 |
| 32 |
private var circleRect = CGRect() // Rect for position indicator circle |
| 33 |
private var shouldTrack = false |
| 34 |
fileprivate var xValue: Float = 0 |
| 35 |
fileprivate var yValue: Float = 0 |
| 36 |
fileprivate var xChannelValue: Float = 0 |
| 37 |
fileprivate var yChannelValue: Float = 0 |
| 38 |
fileprivate var xChannelPtr: UnsafeMutablePointer<Float>? |
| 39 |
fileprivate var yChannelPtr: UnsafeMutablePointer<Float>? |
| 40 |
|
| 41 |
override init(frame: CGRect) { |
| 42 |
super.init(frame: frame) |
| 43 |
backgroundColor = .clear |
| 44 |
circleRect = CGRect(x: borderWidth, y: frame.size.height - 30.0 - borderWidth, width: 30.0, height: 30.0) |
| 45 |
} |
| 46 |
|
| 47 |
required init?(coder aDecoder: NSCoder) { |
| 48 |
super.init(coder: aDecoder) |
| 49 |
backgroundColor = .clear |
| 50 |
circleRect = CGRect(x: borderWidth, y: frame.size.height - 30.0 - borderWidth, width: 30.0, height: 30.0) |
| 51 |
} |
| 52 |
|
| 53 |
func setXValue(_ value: CGFloat) { |
| 54 |
let maxX = frame.size.width - borderWidth - circleRect.size.width |
| 55 |
|
| 56 |
let xPosition: CGFloat = value * (maxX - borderWidth) |
| 57 |
circleRect.origin.x = xPosition * borderWidth |
| 58 |
|
| 59 |
setNeedsDisplay() |
| 60 |
sendActions(for: .valueChanged) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func setYValue(_ value: CGFloat) { |
| 65 |
xValue = Float(value) |
| 66 |
|
| 67 |
let maxY = frame.size.height - borderWidth - circleRect.size.height |
| 68 |
|
| 69 |
let yPosition = value * (maxY - borderWidth) |
| 70 |
circleRect.origin.y = yPosition |
| 71 |
|
| 72 |
setNeedsDisplay() |
| 73 |
sendActions(for: .valueChanged) |
| 74 |
} |
| 75 |
|
| 76 |
func setCircleDiameter(_ diameter: CGFloat) { |
| 77 |
circleRect = CGRect(x: borderWidth, y: frame.size.height - diameter - borderWidth, width: diameter, height: diameter) |
| 78 |
setNeedsDisplay() |
| 79 |
} |
| 80 |
|
| 81 |
// MARK: UIControl Overrides |
| 82 |
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { |
| 83 |
var location = touch.location(in: self) |
| 84 |
|
| 85 |
location.x -= circleRect.size.width/2.0 |
| 86 |
location.y -= circleRect.size.height/2.0 |
| 87 |
|
| 88 |
let maxX = frame.size.width - borderWidth - circleRect.size.width |
| 89 |
let maxY = frame.size.height - borderWidth - circleRect.size.height |
| 90 |
|
| 91 |
let minX = borderWidth, minY = borderWidth |
| 92 |
|
| 93 |
location.x = location.x < minX ? minX : location.x |
| 94 |
location.y = location.y < minY ? minY : location.y |
| 95 |
location.x = location.x > maxX ? maxX : location.x |
| 96 |
location.y = location.y > maxY ? maxY : location.y |
| 97 |
|
| 98 |
circleRect.origin.x = location.x |
| 99 |
circleRect.origin.y = location.y |
| 100 |
|
| 101 |
xValue = Float(location.x/maxX) |
| 102 |
yValue = Float(1 - location.y/maxY) |
| 103 |
|
| 104 |
shouldTrack = true |
| 105 |
|
| 106 |
setNeedsDisplay() |
| 107 |
sendActions(for: .valueChanged) |
| 108 |
|
| 109 |
return true |
| 110 |
} |
| 111 |
|
| 112 |
override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { |
| 113 |
var location = touch.location(in: self) |
| 114 |
|
| 115 |
if shouldTrack { |
| 116 |
location.x -= circleRect.size.width/2.0 |
| 117 |
location.y -= circleRect.size.height/2.0 |
| 118 |
|
| 119 |
let maxX = frame.size.width - borderWidth - circleRect.size.width |
| 120 |
let maxY = frame.size.height - borderWidth - circleRect.size.height |
| 121 |
|
| 122 |
let minX = borderWidth, minY = borderWidth |
| 123 |
|
| 124 |
location.x = location.x < minX ? minX : location.x |
| 125 |
location.y = location.y < minY ? minY : location.y |
| 126 |
location.x = location.x > maxX ? maxX : location.x |
| 127 |
location.y = location.y > maxY ? maxY : location.y |
| 128 |
|
| 129 |
circleRect.origin.x = location.x |
| 130 |
circleRect.origin.y = location.y |
| 131 |
|
| 132 |
xValue = Float(location.x/maxX) |
| 133 |
yValue = Float(1 - location.y/maxY) |
| 134 |
} |
| 135 |
|
| 136 |
setNeedsDisplay() |
| 137 |
sendActions(for: .valueChanged) |
| 138 |
|
| 139 |
return true |
| 140 |
} |
| 141 |
|
| 142 |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 143 |
shouldTrack = false |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
override func draw(_ rect: CGRect) { |
| 148 |
let clipPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 10, height: 10)) |
| 149 |
clipPath.addClip() |
| 150 |
|
| 151 |
let context = UIGraphicsGetCurrentContext() |
| 152 |
|
| 153 |
UIColor.black.set() |
| 154 |
context?.setLineWidth(borderWidth * 2.0) |
| 155 |
context?.setLineJoin(.round) |
| 156 |
|
| 157 |
let borderPath = CGMutablePath() |
| 158 |
borderPath.move(to: CGPoint(x: 0, y: 0)) |
| 159 |
borderPath.addLine(to: CGPoint(x: 0, y: rect.size.height)) |
| 160 |
borderPath.addLine(to: CGPoint(x: rect.size.width, y: rect.size.height)) |
| 161 |
borderPath.addLine(to: CGPoint(x: rect.size.width, y: 0)) |
| 162 |
borderPath.addLine(to: CGPoint(x: 0, y: 0)) |
| 163 |
context?.addPath(borderPath) |
| 164 |
context?.drawPath(using: .stroke) |
| 165 |
|
| 166 |
context?.addEllipse(in: circleRect) |
| 167 |
context?.fillEllipse(in: circleRect) |
| 168 |
} |
| 169 |
|
| 170 |
func cleanup() { |
| 171 |
removeTarget(self, action: #selector(updateChannelValues(_:)), for: .valueChanged) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
extension ControlXYGrid: CsoundBinding { |
| 176 |
func setup(_ csoundObj: CsoundObj!) { |
| 177 |
xChannelPtr = csoundObj.getInputChannelPtr("mix", channelType: CSOUND_CONTROL_CHANNEL) |
| 178 |
yChannelPtr = csoundObj.getInputChannelPtr("pitch", channelType: CSOUND_CONTROL_CHANNEL) |
| 179 |
|
| 180 |
xChannelValue = xValue |
| 181 |
yChannelValue = yValue |
| 182 |
|
| 183 |
addTarget(self, action: #selector(updateChannelValues(_:)), for: .valueChanged) |
| 184 |
} |
| 185 |
|
| 186 |
@objc func updateChannelValues(_ sender: AnyObject) { |
| 187 |
if sender is ControlXYGrid { |
| 188 |
let XYGrid = sender as! ControlXYGrid |
| 189 |
xChannelValue = XYGrid.xValue |
| 190 |
yChannelValue = XYGrid.yValue |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func updateValuesToCsound() { |
| 195 |
xChannelPtr?.pointee = xChannelValue |
| 196 |
yChannelPtr?.pointee = yChannelValue |
| 197 |
} |
| 198 |
} |