Source

CS193P Assignment 1 / Calculator / GraphingViewController.m

Full commit
//
//  GraphingViewController.m
//  Calculator
//
//  Created by Jay Yu on 21/07/12.
//  Copyright (c) 2012 JayYu.com. All rights reserved.
//

#import "GraphingViewController.h"
#import "GraphingView.h"
#import "CalculatorBrain.h"

@interface GraphingViewController ()
@property (nonatomic, strong) id program;
@property (nonatomic, strong) NSDictionary *variables;
@property (readonly) NSString *title;
@end

@implementation GraphingViewController
@synthesize graphingView = _graphingView;
@synthesize program = _program;
@synthesize variables = _variables;

- (NSString *)title
{
    return [CalculatorBrain descriptionOfProgram:self.program];
}

- (void)setGraphingView:(GraphingView *)graphingView
{
    _graphingView = graphingView;
    self.graphingView.dataSource = self;

    // enable pinch gestures in the FaceView using its pinch: handler
    [self.graphingView addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self.graphingView action:@selector(pinch:)]];

    [self.graphingView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self.graphingView action:@selector(pan:)]];

    UITapGestureRecognizer *tripleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.graphingView action:@selector(tripleTap:)];
    tripleTapGestureRecognizer.numberOfTapsRequired = 3;

    [self.graphingView addGestureRecognizer:tripleTapGestureRecognizer];
}

- (void)setup
{

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)initProgram:(id)program
{
    self.program = program;
}

- (double)yAxisValue:(GraphingView *)sender atXAxis:(double)xAxis
{
    NSDictionary *vars = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithDouble:xAxis], @"x", nil];

    double yAxis = [CalculatorBrain runProgram:self.program usingVariableValues:vars];
//    NSLog(@"yAxis: x axis: %f, y axis: %f", xAxis, yAxis);

    return yAxis;

}

#define SCALE_KEY @"GraphingViewController.Scale"
#define ORIGIN_X_KEY @"GraphingViewController.OriginX"
#define ORIGIN_Y_KEY @"GraphingViewController.OriginY"

- (void)setDefault:(GraphingView *)sender origin:(CGPoint)origin scale:(CGFloat)scale
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setFloat:origin.x forKey:ORIGIN_X_KEY];
    [defaults setFloat:origin.y forKey:ORIGIN_Y_KEY];
    [defaults setFloat:scale forKey:SCALE_KEY];
    [defaults synchronize];

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    CGPoint origin = [defaults objectForKey:ORIGIN_KEY];
//    CGFloat scale = [defaults objectForKey:SCALE_KEY];

}

- (void)viewDidUnload
{
    [self setGraphingView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

@end