Skip to content

Instantly share code, notes, and snippets.

@AlexanderBollbach
Created August 24, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderBollbach/12b26e0f205d99525067 to your computer and use it in GitHub Desktop.
Save AlexanderBollbach/12b26e0f205d99525067 to your computer and use it in GitHub Desktop.
#import "OrbView.h"
@interface OrbView()
@property(nonatomic) double closestOrbDistance;
@property(nonatomic) NSString* closestOrbName;
@property(nonatomic,strong) CAShapeLayer* connectLayer;
@property(nonatomic) CGMutablePathRef connectPath;
@property(nonatomic,strong) OrbView* closestOrb;
@property(nonatomic) int tapTag;
@property(nonatomic) UIColor *color;
@property(nonatomic,strong) OrbManager* orbSingleton;
@property(nonatomic,strong) OrbMaster* orbMasterRef;
@end
typedef enum {
orbWhiteState,
orbOrangeState,
orbPurpleState,
} orbState;
@implementation OrbView {
orbState theState;
float centerOrbX;
float centerOrbY;
double aCounter;
}
-(instancetype)initWithFrame:(CGRect)frame ofSuperView:(UIView*)superView andName:(NSString*)name {
if (self = [super initWithFrame:frame]) {
// style
self.color = [UIColor whiteColor];
self.name = name;
self.backgroundColor = [UIColor clearColor];
// CONFIGURE ORB IN MANAGER
self.orbSingleton = [OrbManager orbManager];
[self.orbSingleton.managedOrbs addObject:self];
self.orbMasterRef = [OrbMaster orbMaster];
// AK Setup
self.instrumentAB = [AKInstrument instrument];
self.oscillatorAB = [AKOscillator oscillator];
self.ampAB = [self.instrumentAB createPropertyWithValue:1 minimum:0 maximum:0];
self.oscillatorAB.amplitude = self.ampAB;
self.FreqAB = [self.instrumentAB createPropertyWithValue:0 minimum:0 maximum:0];
self.oscillatorAB.frequency = self.FreqAB;
[self.instrumentAB setAudioOutput:self.oscillatorAB];
[AKOrchestra addInstrument:self.instrumentAB];
[self.ampAB setFloatValue:0.0];
[self.instrumentAB start];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self addGestureRecognizer:tap];
self.theSuperView = superView;
[superView addSubview:self];
// add connect layer
self.connectLayer = [[CAShapeLayer alloc] init];
[self.theSuperView.layer addSublayer:self.connectLayer];
self.closestOrbDistance = 1000;
self.tapTag = 0;
theState = orbWhiteState;
}
return self;
}
-(void)checkDistance:(NSMutableArray*)managedOrbs {
// self center
float selfX = [[self.layer presentationLayer]frame].origin.x + self.frame.size.width/2;
float selfY = [[self.layer presentationLayer]frame].origin.y + self.frame.size.height/2;
// center Orb
centerOrbX = self.orbMasterRef.center.x;
centerOrbY = self.orbMasterRef.center.y;
float dx = (centerOrbX - selfX);
float dy = (centerOrbY - selfY);
float dist = sqrt(dx*dx + dy*dy);
double theta = atan2((selfX - centerOrbX), (selfY - centerOrbY));
if ([self.name isEqualToString:@"orb1"]) {
NSLog(@"%f", theta);
}
if (dist < 500) {
if (theta > 3.0) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"C6"] floatValue]];
} else if (theta > 2.5 && theta < 3.0) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"B5"] floatValue]];
}else if (theta > 2.0 && theta < 2.5) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"A5"] floatValue]];
}else if (theta > 1.5 && theta < 2.0) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"G5"] floatValue]];
} else if (theta > 1 && theta < 1.5) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"F5"] floatValue]];
} else if (theta > 0.5 && theta < 1) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"E5"] floatValue]];
} else if (theta > 0 && theta < 0.5) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"D5"] floatValue]];
} else if (theta > -0.5 && theta < 0) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"C5"] floatValue]];
} else if (theta > -1.0 && theta < -0.5) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"B4"] floatValue]];
} else if (theta > -1.5 && theta < -1.0) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"A4"] floatValue]];
} else if (theta > -2.0 && theta < -1.5) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"G4"] floatValue]];
} else if (theta > -2.5 && theta < -2.0) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"F4"] floatValue]];
}else if (theta > -3.0 && theta < -2.5) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"E4"] floatValue]];
}else if (theta > -4.0 && theta < -3.0) {
[self.FreqAB setValue:[[self.orbSingleton.notesDict objectForKey:@"D4"] floatValue]];
}
if (dist > 450 && dist < 500) {
[self.ampAB setValue:0.001];
}else if (dist > 400 && dist < 450) {
[self.ampAB setValue:0.03];
}
else if (dist > 300 && dist < 400) {
[self.ampAB setValue:0.08];
} else if (dist > 200 && dist < 300) {
[self.ampAB setValue:0.1];
} else if (dist > 100 && dist < 200) {
[self.ampAB setValue:0.15];
} else if (dist > 0 && dist < 100) {
[self.ampAB setValue:0.2];
}
// draw Connect Layer
self.connectPath = CGPathCreateMutable();
CGPathMoveToPoint(self.connectPath, NULL, selfX, selfY);
CGPathAddLineToPoint(self.connectPath, NULL,centerOrbX,centerOrbY);
CGPathCloseSubpath(self.connectPath);
[self.connectLayer setPath:self.connectPath];
float normalizedDist = interpolate(0, 500, 0, 1, dist, 1);
float normalizedDist2 = interpolate(0, 500, 10, 1, dist, 5);
self.connectLayer.fillColor = [UIColor redColor].CGColor;
self.connectLayer.strokeColor = [UIColor colorWithRed:normalizedDist green:normalizedDist blue:1 alpha:normalizedDist2].CGColor;
self.connectLayer.lineWidth = normalizedDist2;
[self setNeedsDisplay];
} else {
self.connectLayer.fillColor = [UIColor clearColor].CGColor;
self.connectLayer.strokeColor = [UIColor clearColor].CGColor;
//kill sound
[self.ampAB setFloatValue:0.0];
[self setNeedsDisplay];
}
switch (theState) {
case orbWhiteState:{
break;
};
case orbOrangeState: {
break;
};
case orbPurpleState: {
float aFloat = 4;
if (aCounter == aFloat) {
aCounter = 0;
}
aCounter++;
if (aCounter > aFloat/2) {
self.center = CGPointMake(self.center.x + 20, self.center.y + 20 );
} else {
self.center = CGPointMake(self.center.x - 20, self.center.y - 20);
}
break;
};
default:
break;
}
}
-(void)setPath:(CGMutablePathRef)path withSpeed:(int)speed {
CAKeyframeAnimation* ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
ani.calculationMode = kCAAnimationCubicPaced;
ani.fillMode = kCAFillModeBackwards;
ani.repeatCount = INFINITY;
ani.duration = speed;
ani.path = path;
[self.layer addAnimation:ani forKey:@""];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 3);
CGContextSetFillColorWithColor(ctx, self.color.CGColor);
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(60, 60) ];
[path fill];
}
float interpolate(float originalMin, float originalMax,float newBegin,
float newEnd,float inputValue, float curve) {
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
float invFlag = 0;
float result = 0;
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ;
curve = pow(10, curve);
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin) {
NewRange = newEnd - newBegin;
}
else {
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange;
if (originalMin > originalMax ) {
result = 0;
return result;
}
if (invFlag == 0) {
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}else{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
result = rangedValue;
return result;
}
-(void)tap:(UITapGestureRecognizer*)gesture {
if (self.tapTag == 5) {
self.tapTag = 0;
}
switch (self.tapTag) {
case 0:
NSLog(@"taptag0");
self.color = [UIColor whiteColor];
[self setNeedsDisplay];
theState = orbWhiteState;
break;
case 1:
NSLog(@"taptag1");
self.color = [UIColor orangeColor];
[self setNeedsDisplay];
theState = orbOrangeState;
break;
case 2:
NSLog(@"taptag2");
self.color = [UIColor purpleColor];
[self setNeedsDisplay];
theState = orbPurpleState;
break;
default:
break;
}
self.tapTag++;
NSLog(@"%@", self.name);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment