Debian Codegraph @

iOS/Csound iOS Swift Examples/Csound iOS SwiftExamples/ControlKnob.swift

raw
1 /*
2
3 ControlKnob.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 ControlKnob: UIControl {
30
31 // Use a computed property so we have a custom setter
32 fileprivate var _value: Float32 = 1.0
33 var value: Float32 {
34 set {
35 self._value = newValue
36 angle = CGFloat(_value/maximumValue) * 270.0
37 }
38 get { return self._value }
39 }
40
41 // Initial default, min, and max values
42 var defaultValue: Float32 = 1.0
43 var minimumValue: Float32 = 0.5
44 var maximumValue: Float32 = 2.0
45
46 fileprivate var channelValue: Float = 0
47 fileprivate var channelPtr: UnsafeMutablePointer<Float>?
48 private var angle: CGFloat = 0
49 private var lastTouchPoint: CGPoint = CGPoint(x: 0, y: 0)
50
51 override init(frame: CGRect) {
52 super.init(frame: frame)
53 backgroundColor = .clear
54 }
55
56 required init?(coder aDecoder: NSCoder) {
57 super.init(coder: aDecoder)
58 backgroundColor = .clear
59 }
60
61 // MARK: UIControl overrides
62 override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
63 return tracking(touch)
64 }
65
66 override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
67 return tracking(touch)
68 }
69
70 private func tracking(_ touch: UITouch) -> Bool {
71 let touchPoint = touch.location(in: superview)
72 if touchPoint.y < lastTouchPoint.y {
73 angle += angle < 270 ? 5 : 0
74 } else {
75 angle -= angle > 0 ? 5 : 0
76 }
77 _value = minimumValue + Float32(angle/270.0) * (maximumValue - minimumValue)
78
79 lastTouchPoint = touchPoint
80 setNeedsDisplay()
81 sendActions(for: .valueChanged)
82
83 return true
84 }
85
86 override func cancelTracking(with event: UIEvent?) { }
87
88 // MARK: Drawing
89 override func draw(_ rect: CGRect) {
90 if angle >= 360 {
91 angle -= 360
92 }
93
94 transform = CGAffineTransform(rotationAngle: (angle * .pi)/180.0) // Degrees to radians
95
96 let context = UIGraphicsGetCurrentContext()
97 let colorSpace = CGColorSpaceCreateDeviceRGB()
98
99 context?.translateBy(x: 0, y: rect.size.height)
100 context?.scaleBy(x: 1.0, y: -1.0) // Invert vertical (so 0 is at the bottom)
101
102 // Draw knob
103 let redComponents: [CGFloat] = [1, 0.1, 0, 1]
104 let redColor = CGColor(colorSpace: colorSpace, components: redComponents)
105 context?.setFillColor(redColor!)
106 context?.addEllipse(in: rect)
107 context?.fillEllipse(in: rect)
108
109 // Draw position indicator
110 context?.move(to: CGPoint(x: rect.size.width/4.0, y: rect.size.height/4.0))
111 context?.addLine(to: CGPoint(x: rect.size.width/2.0, y: rect.size.height/2.0))
112 let blackComponents: [CGFloat] = [0, 0, 0, 1]
113 let blackColor = CGColor(colorSpace: colorSpace, components: blackComponents)
114 context?.setStrokeColor(blackColor!)
115 context?.strokePath()
116 }
117 }
118
119 // MARK: Csound Binding
120 extension ControlKnob: CsoundBinding {
121 func setup(_ csoundObj: CsoundObj!) {
122 channelPtr = csoundObj.getInputChannelPtr("pitch", channelType: CSOUND_CONTROL_CHANNEL)
123 channelValue = _value
124 addTarget(self, action: #selector(updateChannelValue(_:)), for: .valueChanged)
125 }
126
127 @objc func updateChannelValue(_ sender: ControlKnob) {
128 channelValue = sender._value
129 }
130
131 func updateValuesToCsound() {
132 channelPtr?.pointee = channelValue
133 }
134 }