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

#import "TrimmerView.h"

#define BUTTON_WIDTH 50
#define BUTTON_HEIGHT 30

#define TRIM_INCREMENT 0.01

@implementation TrimmerView


- (void) updateLabel {
	[self setNeedsDisplay];
	[label setText:[NSString stringWithFormat:@"%@ trim: %3.2f", [channel name], [channel trim]]];
	[self setNeedsDisplay];  // do I really need two of these?
}

- (void) incf {
	[channel setTrim: [channel trim] + TRIM_INCREMENT];
	[self updateLabel];
}
- (void) decf {
	[channel setTrim: [channel trim] - TRIM_INCREMENT];
	[self updateLabel];
}

- (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 updateLabel];
	[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
