/*
iPhly: Radio Control on the iPhone

Copyright (C) 2010-2011 Ari Krupnik & Associates

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License V2
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#import "TiltAndSwipePadAir.h"

@implementation TiltAndSwipePadAir


- (id)initWithFrame:(CGRect)frame andProcessor:(ModelProcessor*) p {
    self = [super initWithFrame:frame andProcessor:p];
    if (self) {
        [self addSubview: attitudeIndicatorCard=[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_inside.png"]]];
        [self addSubview: glideSlope=[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_glideslope.png"]]];
        [self addSubview: localizer=[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_localizer.png"]]];
        [self addSubview: [[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Attitude_Indicator_frame.png"]] autorelease]];
        
        tiltXSink = [[processor channelByName: @"aileron"] retain];
        tiltYSink = [[processor channelByName: @"elevator"] retain];
        swipeXSink = [[processor channelByName: @"rudder"] retain];
        swipeYSink = [[processor channelByName: @"throttle"] retain];
        
        swipeYSpring = 0.0;
        [[processor channelByName: @"throttle"] setValue:-1.0];
        [self setNeedsDisplay];
        
        
        [self becomeFirstResponder];  // for shake motion
    }
    return self;
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}


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

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

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

- (TrimmerView*) trimmerForQuadrant:(enum quadrant) q {
    if (q==Q_LEFT) {
        return  nil;
    } else {
        return [super trimmerForQuadrant:q];
    }
}

- (void) dealloc {
    [tiltXSink release];
    [tiltYSink release];
    [swipeXSink release];
    [swipeYSink release];
    
	[attitudeIndicatorCard release];
	[glideSlope release];
	[localizer release];
	
	[super dealloc];
}

@end
