/*
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 "TrimmerView.h"
#import <AudioToolbox/AudioToolbox.h>
#import "ModelProcessor.h"

#define BUTTON_WIDTH 50
#define BUTTON_HEIGHT 30

#define TRIM_INCREMENT 0.01

@implementation TrimmerView

- (void) setNeedsDisplay {
    [label setText:[NSString stringWithFormat:@"%@ trim: %3.2f", [channel name], [channel trim]]];
    [super setNeedsDisplay];
}

- (void) incf {
    if ([[channel owner] trimsVibrate]) {
        AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    }
	[channel setTrim: [channel trim] + TRIM_INCREMENT];
    [self setNeedsDisplay];
}
- (void) decf {
    if ([[channel owner] trimsVibrate]) {
        AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    }
	[channel setTrim: [channel trim] - TRIM_INCREMENT];
    [self setNeedsDisplay];
}

- (TrimmerView *) initWithSwipePad: (UIView *) pad andChannel: (ModelProcessorChannel *) c inQuadrant: (enum quadrant) q {
	CGRect f = [pad frame];
	f.size.height = BUTTON_HEIGHT;
	CGAffineTransform t = CGAffineTransformIdentity;
	switch (q) {
		case Q_TOP:
			f.origin.y -= BUTTON_HEIGHT;
			break;
		case Q_RIGHT:
			f.origin.y += ([pad frame].size.height - f.size.height) / 2.0;
			f.origin.x += [pad frame].size.width / 2.0 + 4;  // magic 4
			t = CGAffineTransformMakeRotation(-M_PI_2);
			break;
		case Q_BOTTOM:
			f.origin.y += [pad frame].size.height;
			break;
		case Q_LEFT:
			f.origin.y += ([pad frame].size.height - f.size.height) / 2.0;
			f.origin.x -= [pad frame].size.width / 2.0 + 4;  // magic 4
			t = CGAffineTransformMakeRotation(-M_PI_2);
			break;
	}
	
	[super initWithFrame:f];
	
	[self setTransform: t];
	
	channel = c;
	[channel retain];
	
	label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, f.size.width, f.size.height)];
	[label setTextAlignment:UITextAlignmentCenter];
	[label setBaselineAdjustment: UIBaselineAdjustmentAlignCenters];
	[label setFont:[[label font] fontWithSize:16]];
	[label setBackgroundColor: [UIColor clearColor]];
	[self addSubview: label];
	
	UIButton *mb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[mb setFrame:CGRectMake(0,0, BUTTON_WIDTH, BUTTON_HEIGHT)];
	[mb setTitle:@"<" forState:UIControlStateNormal];
	[mb addTarget:self action:@selector(decf) forControlEvents:UIControlEventTouchDown];
	[self addSubview: mb];
	
	UIButton *pb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[pb setFrame:CGRectMake(f.size.width-BUTTON_WIDTH,0, BUTTON_WIDTH, BUTTON_HEIGHT)];
	[pb setTitle:@">" forState:UIControlStateNormal];
	[pb addTarget:self action:@selector(incf) forControlEvents:UIControlEventTouchDown];
	[self addSubview: pb];
	
	return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
	[channel release];
	[label release];
    [super dealloc];
}


@end
