//
//  FlyByTouchViewController.m
//  iPhly
//
//  Created by Ari Krupnik on 5/18/10.
//  Copyright 2010 lib.aero. All rights reserved.
//

#import "FlyByTouchViewController.h"


@implementation FlyByTouchViewController

- (void) setupOnLoad {	
	[swipePad addSubview: attitudeIndicatorCard=[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_inside.png"]]];
	[swipePad addSubview: glideSlope=[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_glideslope.png"]]];
	[swipePad addSubview: localizer=[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_localizer.png"]]];
	[swipePad addSubview: [[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_frame.png"]] autorelease]];
	
	tiltXSink = [processor channelByName: @"aileron"];
	tiltYSink = [processor channelByName: @"elevator"];
	swipeXSink = [processor channelByName: @"rudder"];
	swipeYSink = [processor channelByName: @"throttle"];

	[[self view] addSubview:[[[TrimmerView alloc] initWithSwipePad:swipePad andChannel: tiltXSink  inQuadrant:Q_TOP] autorelease]];
	[[self view] addSubview:[[[TrimmerView alloc] initWithSwipePad:swipePad andChannel: tiltYSink  inQuadrant:Q_RIGHT] autorelease]];
	[[self view] addSubview:[[[TrimmerView alloc] initWithSwipePad:swipePad andChannel: swipeXSink inQuadrant:Q_BOTTOM] autorelease]];
	//[[self view] addSubview:[[[TrimmerView alloc] initWithSwipePad:swipePad andChannel: swipeYSink inQuadrant:Q_LEFT] autorelease]];
	
	swipeYSpring = 0.0;
	[[processor channelByName: @"throttle"] setValue:-1.0];
	[self doDisplayThing];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
    [self becomeFirstResponder];
}


/* shaking kills throttle */
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
	[[processor channelByName: @"throttle"] setValue:-1.0];
}

- (void) doDisplayThing {
	CGAffineTransform rotation = CGAffineTransformMakeRotation(-M_PI_2*[tiltXSink value]);
	CGAffineTransform translation = CGAffineTransformMakeTranslation(0, -attitudeIndicatorCard.bounds.size.width/7*[tiltYSink value]);
	attitudeIndicatorCard.transform = CGAffineTransformConcat(translation, rotation);
	glideSlope.transform = CGAffineTransformMakeTranslation(0, -glideSlope.bounds.size.width/5*[swipeYSink value]);
	localizer.transform = CGAffineTransformMakeTranslation(localizer.bounds.size.height/3.5*[swipeXSink value], 0);
}

- (NSString *) gerund {
	return @"Flying";
}

- (void) dealloc {
	[attitudeIndicatorCard release];
	[glideSlope release];
	[localizer release];
	
	[super dealloc];
}

@end
