| 1 |
/* |
| 2 |
|
| 3 |
CsoundVirtualKeyboard.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 "CsoundVirtualKeyboard.h" |
| 27 |
|
| 28 |
static const int kTotalNumKeys = 25; |
| 29 |
|
| 30 |
@interface CsoundVirtualKeyboard() |
| 31 |
|
| 32 |
- (BOOL)isWhiteKey:(int)key; |
| 33 |
|
| 34 |
@end |
| 35 |
|
| 36 |
@implementation CsoundVirtualKeyboard |
| 37 |
|
| 38 |
- (id)initWithFrame:(CGRect)frame |
| 39 |
{ |
| 40 |
self = [super initWithFrame:frame]; |
| 41 |
if (self) { |
| 42 |
|
| 43 |
for (int i = 1; i < kTotalNumKeys; i++) { |
| 44 |
keyDown[i] = NO; |
| 45 |
} |
| 46 |
|
| 47 |
lastWidth = -1.0f; |
| 48 |
currentTouches = [[NSMutableSet alloc] init]; |
| 49 |
self.multipleTouchEnabled = YES; |
| 50 |
} |
| 51 |
return self; |
| 52 |
} |
| 53 |
|
| 54 |
- (id)initWithCoder:(NSCoder *)aDecoder |
| 55 |
{ |
| 56 |
self = [super initWithCoder:aDecoder]; |
| 57 |
if (self) { |
| 58 |
for (int i = 1; i < kTotalNumKeys; i++) { |
| 59 |
keyDown[i] = NO; |
| 60 |
} |
| 61 |
|
| 62 |
lastWidth = -1.0f; |
| 63 |
currentTouches = [[NSMutableSet alloc] init]; |
| 64 |
self.multipleTouchEnabled = YES; |
| 65 |
} |
| 66 |
return self; |
| 67 |
} |
| 68 |
|
| 69 |
- (BOOL)isWhiteKey:(int)key { |
| 70 |
switch (key % 12) { |
| 71 |
case 0: |
| 72 |
case 2: |
| 73 |
case 4: |
| 74 |
case 5: |
| 75 |
case 7: |
| 76 |
case 9: |
| 77 |
case 11: |
| 78 |
return YES; |
| 79 |
} |
| 80 |
|
| 81 |
return NO; |
| 82 |
} |
| 83 |
|
| 84 |
-(void)updateKeyRects { |
| 85 |
|
| 86 |
if (lastWidth == self.bounds.size.width) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
lastWidth = self.bounds.size.width; |
| 91 |
|
| 92 |
CGFloat whiteKeyHeight = self.bounds.size.height; |
| 93 |
CGFloat blackKeyHeight = whiteKeyHeight * .625; |
| 94 |
|
| 95 |
CGFloat whiteKeyWidth = self.bounds.size.width / 15.0f; |
| 96 |
CGFloat blackKeyWidth = whiteKeyWidth * .8333333f; |
| 97 |
|
| 98 |
CGFloat leftKeyBound = whiteKeyWidth - (blackKeyWidth / 2.0f); |
| 99 |
|
| 100 |
int lastWhiteKey = 0; |
| 101 |
|
| 102 |
keyRects[0] = CGRectMake(0, 0, whiteKeyWidth, whiteKeyHeight); |
| 103 |
|
| 104 |
for (int i = 1; i < kTotalNumKeys; i++) { |
| 105 |
|
| 106 |
if (![self isWhiteKey:i]) { |
| 107 |
keyRects[i] = CGRectMake((lastWhiteKey * whiteKeyWidth) + leftKeyBound, 0, blackKeyWidth, blackKeyHeight); |
| 108 |
} else { |
| 109 |
lastWhiteKey++; |
| 110 |
keyRects[i] = CGRectMake(lastWhiteKey * whiteKeyWidth, 0, whiteKeyWidth, whiteKeyHeight); |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
-(int)getKeyboardKey:(CGPoint)point { |
| 118 |
|
| 119 |
int keyNum = -1; |
| 120 |
|
| 121 |
for(int i = 0; i < kTotalNumKeys; i++) { |
| 122 |
if (CGRectContainsPoint(keyRects[i], point)) { |
| 123 |
keyNum = i; |
| 124 |
if (![self isWhiteKey:i]) { |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return keyNum; |
| 131 |
} |
| 132 |
|
| 133 |
- (void)updateKeyStates { |
| 134 |
|
| 135 |
[self updateKeyRects]; |
| 136 |
|
| 137 |
NSArray* touches = [currentTouches allObjects]; |
| 138 |
NSUInteger count = [touches count]; |
| 139 |
|
| 140 |
int currentKeyState[kTotalNumKeys]; |
| 141 |
|
| 142 |
for (int i = 0; i < kTotalNumKeys; i++) { |
| 143 |
currentKeyState[i] = NO; |
| 144 |
} |
| 145 |
|
| 146 |
for (NSUInteger i = 0; i < count; i++) { |
| 147 |
UITouch* touch = [touches objectAtIndex:i]; |
| 148 |
CGPoint point = [touch locationInView:self]; |
| 149 |
int index = [self getKeyboardKey:point]; |
| 150 |
|
| 151 |
if(index != -1) { |
| 152 |
currentKeyState[index] = YES; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
BOOL keysUpdated = NO; |
| 157 |
|
| 158 |
for (int i = 0; i < kTotalNumKeys; i++) { |
| 159 |
if (keyDown[i] != currentKeyState[i]) { |
| 160 |
keysUpdated = YES; |
| 161 |
BOOL keyDownState = currentKeyState[i]; |
| 162 |
|
| 163 |
keyDown[i] = keyDownState; |
| 164 |
|
| 165 |
if (_keyboardDelegate != nil) { |
| 166 |
if (keyDownState) { |
| 167 |
[_keyboardDelegate keyDown:self keyNum:i]; |
| 168 |
} else { |
| 169 |
[_keyboardDelegate keyUp:self keyNum:i]; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (keysUpdated) { |
| 176 |
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO]; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
#pragma mark Touch Handling Code |
| 181 |
|
| 182 |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
| 183 |
for (UITouch* touch in touches) { |
| 184 |
[currentTouches addObject:touch]; |
| 185 |
} |
| 186 |
[self updateKeyStates]; |
| 187 |
} |
| 188 |
|
| 189 |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
| 190 |
[self updateKeyStates]; |
| 191 |
} |
| 192 |
|
| 193 |
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
| 194 |
for (UITouch* touch in touches) { |
| 195 |
[currentTouches removeObject:touch]; |
| 196 |
} |
| 197 |
[self updateKeyStates]; |
| 198 |
} |
| 199 |
|
| 200 |
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
| 201 |
for (UITouch* touch in touches) { |
| 202 |
[currentTouches removeObject:touch]; |
| 203 |
} |
| 204 |
[self updateKeyStates]; |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
#pragma mark Drawing Code |
| 210 |
|
| 211 |
|
| 212 |
// Only override drawRect: if you perform custom drawing. |
| 213 |
// An empty implementation adversely affects performance during animation. |
| 214 |
- (void)drawRect:(CGRect)rect |
| 215 |
{ |
| 216 |
|
| 217 |
// Get the context |
| 218 |
CGContextRef context = UIGraphicsGetCurrentContext(); |
| 219 |
|
| 220 |
CGFloat whiteKeyHeight = rect.size.height; |
| 221 |
CGFloat blackKeyHeight = (int)(rect.size.height * .625); |
| 222 |
//CGFloat whiteKeyWidth = rect.size.width / 52.0f; |
| 223 |
CGFloat whiteKeyWidth = rect.size.width / 15.0f; |
| 224 |
CGFloat blackKeyWidth = whiteKeyWidth * .8333333; |
| 225 |
CGFloat blackKeyOffset = blackKeyWidth / 2; |
| 226 |
|
| 227 |
float runningX = 0; |
| 228 |
int yval = 0; |
| 229 |
|
| 230 |
|
| 231 |
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); |
| 232 |
CGContextFillRect(context, self.bounds); |
| 233 |
|
| 234 |
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); |
| 235 |
CGContextStrokeRect(context, self.bounds); |
| 236 |
|
| 237 |
int lineHeight = whiteKeyHeight - 1; |
| 238 |
|
| 239 |
// Draw White Keys |
| 240 |
for (int i = 0; i < kTotalNumKeys; i++) { |
| 241 |
if ([self isWhiteKey:i]) { |
| 242 |
int newX = (int) (runningX + 0.5); |
| 243 |
|
| 244 |
if (keyDown[i]) { |
| 245 |
int newW = (int) ((runningX + whiteKeyWidth + 0.5) - newX); |
| 246 |
|
| 247 |
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor); |
| 248 |
CGContextFillRect(context, CGRectMake(newX, yval, newW, whiteKeyHeight - 1)); |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
runningX += whiteKeyWidth; |
| 253 |
|
| 254 |
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); |
| 255 |
CGContextStrokeRect(context, CGRectMake(newX, yval, newX, lineHeight)); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
runningX = 0.0f; |
| 260 |
|
| 261 |
// Draw Black Keys |
| 262 |
for (int i = 0; i < kTotalNumKeys; i++) { |
| 263 |
if ([self isWhiteKey:i]) { |
| 264 |
runningX += whiteKeyWidth; |
| 265 |
} else { |
| 266 |
if (keyDown[i]) { |
| 267 |
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor); |
| 268 |
} else { |
| 269 |
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); |
| 270 |
} |
| 271 |
|
| 272 |
CGContextFillRect(context, CGRectMake((int)(runningX - blackKeyOffset), yval, (int)blackKeyWidth, blackKeyHeight)); |
| 273 |
|
| 274 |
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); |
| 275 |
CGContextStrokeRect(context, CGRectMake((int) (runningX - blackKeyOffset), yval, (int)blackKeyWidth, blackKeyHeight)); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
@end |