| 1 |
/* |
| 2 |
|
| 3 |
ControlKnob.m: |
| 4 |
|
| 5 |
Copyright (C) 2014 Thomas Hass, Aurelius Prochazka |
| 6 |
|
| 7 |
This file is part of Csound iOS Examples. |
| 8 |
|
| 9 |
The Csound for iOS Library is free software; you can redistribute it |
| 10 |
and/or modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 2.1 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
Csound is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with Csound; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 22 |
02111-1307 USA |
| 23 |
|
| 24 |
*/ |
| 25 |
|
| 26 |
#import "ControlKnob.h" |
| 27 |
#import "CsoundObj.h" |
| 28 |
|
| 29 |
@interface ControlKnob () |
| 30 |
{ |
| 31 |
float channelValue; |
| 32 |
float *channelPtr; |
| 33 |
|
| 34 |
CGFloat angle; |
| 35 |
CGPoint lastTouchPoint; |
| 36 |
} |
| 37 |
|
| 38 |
@end |
| 39 |
|
| 40 |
@implementation ControlKnob |
| 41 |
|
| 42 |
- (id)initWithFrame:(CGRect)frame |
| 43 |
{ |
| 44 |
self = [super initWithFrame:frame]; |
| 45 |
if (self) { |
| 46 |
[self setBackgroundColor:[UIColor clearColor]]; |
| 47 |
_defaultValue = _value = 0.0f; |
| 48 |
_minimumValue = 0.0f; |
| 49 |
_maximumValue = 1.0f; |
| 50 |
angle = 0; |
| 51 |
} |
| 52 |
return self; |
| 53 |
} |
| 54 |
|
| 55 |
- (id)initWithCoder:(NSCoder *)aDecoder |
| 56 |
{ |
| 57 |
self = [super initWithCoder:aDecoder]; |
| 58 |
if (self) { |
| 59 |
[self setBackgroundColor:[UIColor clearColor]]; |
| 60 |
_defaultValue = _value = 0.0f; |
| 61 |
_minimumValue = 0.0f; |
| 62 |
_maximumValue = 1.0f; |
| 63 |
angle = 0; |
| 64 |
} |
| 65 |
return self; |
| 66 |
} |
| 67 |
|
| 68 |
/* This method is inaccurate */ |
| 69 |
- (void)setValue:(Float32)value_ |
| 70 |
{ |
| 71 |
_value = value_; |
| 72 |
angle = (_value/_maximumValue) * 270.0f; |
| 73 |
[self setNeedsDisplay]; |
| 74 |
} |
| 75 |
|
| 76 |
#pragma mark - UIControl Overrides |
| 77 |
|
| 78 |
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event |
| 79 |
{ |
| 80 |
CGPoint touchPoint = [touch locationInView:[self superview]]; |
| 81 |
if (touchPoint.y < lastTouchPoint.y) { |
| 82 |
angle += angle < 270 ? 5 : 0; |
| 83 |
} else { |
| 84 |
angle -= angle > 0 ? 5 : 0; |
| 85 |
} |
| 86 |
_value = _minimumValue + angle/270.0f * (_maximumValue - _minimumValue); |
| 87 |
lastTouchPoint = touchPoint; |
| 88 |
[self setNeedsDisplay]; |
| 89 |
[self sendActionsForControlEvents:UIControlEventValueChanged]; |
| 90 |
return YES; |
| 91 |
} |
| 92 |
|
| 93 |
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event |
| 94 |
{ |
| 95 |
CGPoint touchPoint = [touch locationInView:[self superview]]; |
| 96 |
if (touchPoint.y < lastTouchPoint.y) { |
| 97 |
angle += angle < 270 ? 5 : 0; |
| 98 |
} else { |
| 99 |
angle -= angle > 0 ? 5 : 0; |
| 100 |
} |
| 101 |
_value = _minimumValue + angle/270.0f * (_maximumValue - _minimumValue); |
| 102 |
lastTouchPoint = touchPoint; |
| 103 |
[self setNeedsDisplay]; |
| 104 |
[self sendActionsForControlEvents:UIControlEventValueChanged]; |
| 105 |
return YES; |
| 106 |
} |
| 107 |
|
| 108 |
- (void)cancelTrackingWithEvent:(UIEvent *)event |
| 109 |
{ |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
#pragma mark - Drawing |
| 114 |
|
| 115 |
- (void)drawRect:(CGRect)rect |
| 116 |
{ |
| 117 |
self.transform = CGAffineTransformMakeRotation(angle*M_PI/180.0f); |
| 118 |
if (angle >= 360) { |
| 119 |
angle -= 360.0f; |
| 120 |
} |
| 121 |
|
| 122 |
// Get the context |
| 123 |
CGContextRef context = UIGraphicsGetCurrentContext(); |
| 124 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 125 |
|
| 126 |
// Flip coordinate system |
| 127 |
CGContextTranslateCTM(context, 0, rect.size.height); |
| 128 |
CGContextScaleCTM(context, 1.0, -1.0); |
| 129 |
|
| 130 |
// Draw circle |
| 131 |
CGFloat redComponents[] = {1.0f, 0.1f, 0.0f, 1.0f}; |
| 132 |
CGColorRef redColor = CGColorCreate(colorSpace, redComponents); |
| 133 |
CGContextSetFillColorWithColor(context, redColor); |
| 134 |
CGContextAddEllipseInRect(context, rect); |
| 135 |
CGContextFillEllipseInRect(context, rect); |
| 136 |
CGColorRelease(redColor); |
| 137 |
|
| 138 |
// Draw line |
| 139 |
CGContextMoveToPoint(context, rect.size.width/4.0f, rect.size.height/4.0f); |
| 140 |
CGContextAddLineToPoint(context, rect.size.width/2.0f, rect.size.height/2.0f); |
| 141 |
CGFloat blackComponents[] = {0.0f, 0.0f, 0.0f, 1.0f}; |
| 142 |
CGColorRef blackColor = CGColorCreate(colorSpace, blackComponents); |
| 143 |
CGContextSetStrokeColorWithColor(context, blackColor); |
| 144 |
CGContextStrokePath(context); |
| 145 |
CGColorRelease(blackColor); |
| 146 |
|
| 147 |
CGColorSpaceRelease(colorSpace); |
| 148 |
} |
| 149 |
|
| 150 |
#pragma mark - Csound Binding |
| 151 |
|
| 152 |
- (void)setup:(CsoundObj *)csoundObj |
| 153 |
{ |
| 154 |
channelPtr = [csoundObj getInputChannelPtr:@"pitch" channelType:CSOUND_CONTROL_CHANNEL]; |
| 155 |
channelValue = _value; |
| 156 |
[self addTarget:self |
| 157 |
action:@selector(updateChannelValue:) |
| 158 |
forControlEvents:UIControlEventValueChanged]; |
| 159 |
} |
| 160 |
|
| 161 |
- (void)updateChannelValue:(id)sender |
| 162 |
{ |
| 163 |
channelValue = ((ControlKnob *)sender).value; |
| 164 |
} |
| 165 |
|
| 166 |
- (void)updateValuesToCsound |
| 167 |
{ |
| 168 |
*channelPtr = channelValue; |
| 169 |
} |
| 170 |
|
| 171 |
- (void)cleanup |
| 172 |
{ |
| 173 |
[self removeTarget:self |
| 174 |
action:@selector(updateChannelValue:) |
| 175 |
forControlEvents:UIControlEventValueChanged]; |
| 176 |
} |
| 177 |
|
| 178 |
@end |