Debian Codegraph @

iOS/Csound iOS Obj-C Examples/Csound iOS Examples/ViewControllers/MultiTouchXY/MultiTouchXYViewController.m

raw
1 /*
2
3 MultiTouchXYViewController.m:
4
5 Copyright (C) 2011 Steven Yi
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 "MultiTouchXYViewController.h"
27
28 @implementation MultiTouchXYViewController
29
30 -(void)viewDidLoad {
31 self.title = @"15. MultiTouch XY Pad";
32
33 for (int i = 0; i < 10; i++) {
34 touchIds[i] = 0;
35 touchX[i] = 0.0f;
36 touchY[i] = 0.0f;
37 touchArray[i] = nil;
38 }
39
40 touchesCount = 0;
41
42 [super viewDidLoad];
43
44
45 }
46
47 - (IBAction)showInfo:(UIButton *)sender {
48 UIViewController *infoVC = [[UIViewController alloc] init];
49 infoVC.modalPresentationStyle = UIModalPresentationPopover;
50
51 UIPopoverPresentationController *popover = infoVC.popoverPresentationController;
52 popover.sourceView = sender;
53 popover.sourceRect = sender.bounds;
54 [infoVC setPreferredContentSize:CGSizeMake(300, 120)];
55
56 UITextView *infoText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, infoVC.preferredContentSize.width, infoVC.preferredContentSize.height)];
57 infoText.editable = NO;
58 infoText.selectable = NO;
59 NSString *description = @"Multitouch XY Pad demonstrates a multitouch performance surface. Each touch is dynamically mapped to a unique instance of a Csound instrument.";
60 [infoText setAttributedText:[[NSAttributedString alloc] initWithString:description]];
61 infoText.font = [UIFont fontWithName:@"Menlo" size:16];
62 [infoVC.view addSubview:infoText];
63 popover.delegate = self;
64
65 [popover setPermittedArrowDirections:UIPopoverArrowDirectionUp];
66
67 [self presentViewController:infoVC animated:YES completion:nil];
68
69 }
70
71 -(void)viewDidAppear:(BOOL)animated {
72 [super viewDidAppear:animated];
73
74 NSString *tempFile = [[NSBundle mainBundle] pathForResource:@"multiTouchXY" ofType:@"csd"];
75 NSLog(@"FILE PATH: %@", tempFile);
76
77 [self.csound stop];
78
79 self.csound = [[CsoundObj alloc] init];
80
81 [self.csound addBinding:self];
82
83 [self.csound play:tempFile];
84 }
85
86
87 #pragma mark Csound Binding
88
89 -(void)setup:(CsoundObj *)csoundObj {
90
91 for (int i = 0; i < 10; i++) {
92 touchXPtr[i] = [csoundObj getInputChannelPtr:[NSString stringWithFormat:@"touch.%d.x", i, nil]
93 channelType:CSOUND_CONTROL_CHANNEL];
94 touchYPtr[i] = [csoundObj getInputChannelPtr:[NSString stringWithFormat:@"touch.%d.y", i, nil]
95 channelType:CSOUND_CONTROL_CHANNEL];
96 }
97 }
98
99 -(void)updateValuesToCsound {
100 for (int i = 0; i < 10; i++) {
101 *touchXPtr[i] = touchX[i];
102 *touchYPtr[i] = touchY[i];
103 }
104 }
105
106 -(void)updateValuesFromCsound {}
107
108 -(void)cleanup {
109 for (int i = 0; i < 10; i++) {
110 touchXPtr[i] = 0;
111 touchYPtr[i] = 0;
112 }
113 }
114
115 #pragma mark Touch Event Handling
116
117 - (int)getTouchIdAssignment {
118 for (int i = 0; i < 10; i++) {
119 if (touchIds[i] == 0) {
120 return i;
121 }
122 }
123 return -1;
124 }
125
126 - (int) getTouchId:(UITouch *)touch {
127 for (int i = 0; i < 10; i++) {
128 if (touchArray[i] == touch) {
129 return i;
130 }
131 }
132 return -1;
133 }
134
135 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
136 for (UITouch* touch in touches) {
137 int touchId = [self getTouchIdAssignment];
138 if (touchId != -1) {
139 touchArray[touchId] = touch;
140 touchIds[touchId] = 1;
141
142 CGPoint pt = [touch locationInView:self.view];
143 touchX[touchId] = pt.x / self.view.frame.size.width;
144 touchY[touchId] = 1 - (pt.y / self.view.frame.size.height); // flip y value so zero is on
145
146 *touchXPtr[touchId] = touchX[touchId];
147 *touchYPtr[touchId] = touchY[touchId];
148
149 [self.csound sendScore:[NSString stringWithFormat:@"i1.%d 0 -2 %d", touchId, touchId, nil]];
150 }
151 }
152 touchesCount += touches.allObjects.count;
153 touchesLabel.text = [NSString stringWithFormat:@"Touches: %lu", touchesCount];
154
155 if(touchesCount > [[event allTouches] count]) {
156 touchesCount = [[event allTouches] count];
157 }
158 }
159
160 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
161 for (UITouch* touch in touches) {
162 int touchId = [self getTouchId:touch];
163 if (touchId != -1) {
164 CGPoint pt = [touch locationInView:self.view];
165 touchX[touchId] = pt.x / self.view.frame.size.width;
166 touchY[touchId] = 1 - (pt.y / self.view.frame.size.height); // flip y value so zero is on bottom
167 }
168 }
169 }
170
171 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
172 for (UITouch* touch in touches) {
173 int touchId = [self getTouchId:touch];
174 if (touchId != -1) {
175 touchIds[touchId] = 0;
176 touchArray[touchId] = nil;
177 [self.csound sendScore:[NSString stringWithFormat:@"i-1.%d 0 0 %d", touchId, touchId]];
178
179 }
180 }
181 touchesCount -= touches.allObjects.count;
182 touchesLabel.text = [NSString stringWithFormat:@"Touches: %lu", touchesCount];
183 }
184
185 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
186 for (UITouch* touch in touches) {
187 int touchId = [self getTouchId:touch];
188 if (touchId != -1) {
189 touchIds[touchId] = 0;
190 touchArray[touchId] = nil;
191 [self.csound sendScore:[NSString stringWithFormat:@"i-1.%d 0 0", touchId, nil]];
192
193 }
194 }
195 }
196
197 @end