Debian Codegraph @

iOS/Csound iOS Obj-C Examples/Csound iOS Examples/ViewControllers/PitchShifter/ControlXYGrid.m

raw
1 /*
2
3 ControlXYGrid.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
27 #import "ControlXYGrid.h"
28
29 @interface ControlXYGrid ()
30 {
31 CGRect circleRect;
32
33 float xChannelValue, yChannelValue;
34 float *xChannelPtr, *yChannelPtr;
35
36 CGFloat borderWidth;
37 BOOL shouldTrack;
38 }
39 @end
40
41 @implementation ControlXYGrid
42
43 - (id)initWithFrame:(CGRect)frame
44 {
45 self = [super initWithFrame:frame];
46 if (self) {
47 [self setBackgroundColor:[UIColor clearColor]];
48 borderWidth = 10.0f;
49 circleRect = CGRectMake(borderWidth,
50 frame.size.height - 30.0f - borderWidth,
51 30.0f,
52 30.0f);
53 _xValue = 0.0f;
54 _yValue = 0.0f;
55 shouldTrack = NO;
56 }
57 return self;
58 }
59
60 - (id)initWithCoder:(NSCoder *)aDecoder
61 {
62 self = [super initWithCoder:aDecoder];
63 if (self) {
64 [self setBackgroundColor:[UIColor clearColor]];
65 borderWidth = 10.0f;
66 circleRect = CGRectMake(borderWidth,
67 self.frame.size.height - 30.0f - borderWidth,
68 30.0f,
69 30.0f);
70 _xValue = 0.0f;
71 _yValue = 0.0f;
72 }
73 return self;
74 }
75
76 - (void)setXValue:(Float32)xValue_
77 {
78 _xValue = xValue_;
79
80 // Limit it
81 CGFloat minX = borderWidth;
82 CGFloat maxX = self.frame.size.width - borderWidth - circleRect.size.width;
83
84 // Redraw
85 CGFloat xPosition = _xValue * (maxX - minX);
86 circleRect.origin.x = xPosition + borderWidth;
87
88 [self setNeedsDisplay];
89 [self sendActionsForControlEvents:UIControlEventValueChanged];
90 }
91
92 - (void)setYValue:(Float32)yValue_
93 {
94 _yValue = yValue_;
95
96 CGFloat minY = borderWidth;
97 CGFloat maxY = self.frame.size.height - borderWidth - circleRect.size.height;
98
99 CGFloat yPosition = _yValue * (maxY - minY);
100 yPosition = maxY - yPosition;
101 circleRect.origin.y = yPosition;
102
103 [self setNeedsDisplay];
104 [self sendActionsForControlEvents:UIControlEventValueChanged];
105 }
106
107 - (void)setCircleDiameter:(CGFloat)circleDiameter_
108 {
109 circleRect = CGRectMake(borderWidth,
110 self.frame.size.height - circleDiameter_ - borderWidth,
111 circleDiameter_,
112 circleDiameter_);
113 [self setNeedsDisplay];
114 }
115
116 #pragma mark - UIControl Overrides
117
118 - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
119 {
120 CGPoint location = [touch locationInView:self];
121 //if (CGRectContainsPoint(circleRect, location)) {
122
123 // Reposition the touch (origin is top left)
124 location.x -= circleRect.size.width/2.0f;
125 location.y -= circleRect.size.height/2.0f;
126
127 // Limit it
128 CGFloat minX = borderWidth;
129 CGFloat minY = borderWidth;
130 CGFloat maxX = self.frame.size.width - borderWidth - circleRect.size.width;
131 CGFloat maxY = self.frame.size.height - borderWidth - circleRect.size.height;
132 location.x = location.x < minX ? minX : location.x;
133 location.y = location.y < minY ? minY : location.y;
134 location.x = location.x > maxX ? maxX : location.x;
135 location.y = location.y > maxY ? maxY : location.y;
136
137 // Redraw
138 circleRect.origin.x = location.x;
139 circleRect.origin.y = location.y;
140
141 // Update values
142 _xValue = location.x / maxX;
143 _yValue = 1.0f - location.y / maxY;
144
145 shouldTrack = YES;
146 //}
147
148 [self setNeedsDisplay];
149 [self sendActionsForControlEvents:UIControlEventValueChanged];
150 return YES;
151 }
152
153 - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
154 {
155 CGPoint location = [touch locationInView:self];
156 if (shouldTrack) {
157
158 // Reposition the touch (origin is top left)
159 location.x -= circleRect.size.width/2.0f;
160 location.y -= circleRect.size.height/2.0f;
161
162 // Limit it
163 CGFloat minX = borderWidth;
164 CGFloat minY = borderWidth;
165 CGFloat maxX = self.frame.size.width - borderWidth - circleRect.size.width;
166 CGFloat maxY = self.frame.size.height - borderWidth - circleRect.size.height;
167 location.x = location.x < minX ? minX : location.x;
168 location.y = location.y < minY ? minY : location.y;
169 location.x = location.x > maxX ? maxX : location.x;
170 location.y = location.y > maxY ? maxY : location.y;
171
172 // Redraw
173 circleRect.origin.x = location.x;
174 circleRect.origin.y = location.y;
175
176 // Update values
177 _xValue = location.x / maxX;
178 _yValue = 1.0f - location.y / maxY;
179 }
180
181 [self setNeedsDisplay];
182 [self sendActionsForControlEvents:UIControlEventValueChanged];
183 return YES;
184 }
185
186 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
187 {
188 shouldTrack = NO;
189 }
190
191 - (void)drawRect:(CGRect)rect
192 {
193 UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10.0, 10.0)];
194 [clipPath addClip];
195
196 CGContextRef context = UIGraphicsGetCurrentContext();
197 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
198
199 // Draw border lines.
200 [[UIColor blackColor] set];
201 CGContextSetLineWidth(context, borderWidth * 2.0f);
202 CGContextSetLineJoin(context, kCGLineJoinRound);
203 CGContextSetLineWidth(context, borderWidth * 2.0f);
204
205 // Line 1
206 CGMutablePathRef borderPath = CGPathCreateMutable();
207 CGPathMoveToPoint(borderPath, NULL, 0.0f, 0.0f);
208 CGPathAddLineToPoint(borderPath, NULL, 0.0f, rect.size.height);
209 CGPathAddLineToPoint(borderPath, NULL, rect.size.width, rect.size.height);
210 CGPathAddLineToPoint(borderPath, NULL, rect.size.width, 0.0f);
211 CGPathAddLineToPoint(borderPath, NULL, 0.0f, 0.0f);
212 CGContextAddPath(context, borderPath);
213 CGPathRelease(borderPath);
214 CGContextDrawPath(context, kCGPathStroke);
215
216 CGContextAddEllipseInRect(context, circleRect);
217 CGContextFillEllipseInRect(context, circleRect);
218
219 CGColorSpaceRelease(colorSpace);
220 }
221
222 #pragma mark - Csound Binding
223
224 - (void)setup:(CsoundObj *)csoundObj
225 {
226 xChannelPtr = [csoundObj getInputChannelPtr:@"mix" channelType:CSOUND_CONTROL_CHANNEL];
227 yChannelPtr = [csoundObj getInputChannelPtr:@"pitch" channelType:CSOUND_CONTROL_CHANNEL];
228 xChannelValue = _xValue;
229 yChannelValue = _yValue;
230 [self addTarget:self
231 action:@selector(updateChannelValues:)
232 forControlEvents:UIControlEventValueChanged];
233 }
234
235 - (void)updateChannelValues:(id)sender
236 {
237 xChannelValue = ((ControlXYGrid *)sender).xValue;
238 yChannelValue = ((ControlXYGrid *)sender).yValue;
239 }
240
241 - (void)updateValuesToCsound
242 {
243 *xChannelPtr = xChannelValue;
244 *yChannelPtr = yChannelValue;
245
246 }
247
248 - (void)cleanup
249 {
250 [self removeTarget:self
251 action:@selector(updateChannelValues:)
252 forControlEvents:UIControlEventValueChanged];
253 }
254
255 @end