Skip to content

Commit

Permalink
View controller wires up the User Interface and shader model
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Ratcliffe committed Mar 4, 2010
1 parent 0a99240 commit 07dfbe4
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 25 deletions.
87 changes: 87 additions & 0 deletions SampleAppiPhone/Classes/GlossCausticShaderViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,96 @@

#import <UIKit/UIKit.h>

@class GlossCausticShaderView;

@interface GlossCausticShaderViewController : UIViewController
{
GlossCausticShaderView *shaderView;
UIView *controlsView;
UIScrollView *scrollView;
UIButton *colorButton;
UILabel *settingsTab;

// non-caustic base colour
UISlider *redSlider;
UISlider *greenSlider;
UISlider *blueSlider;
UILabel *redLabel;
UILabel *greenLabel;
UILabel *blueLabel;

// gloss
UISlider *exponentialCoefficientSlider;
UISlider *glossReflectionPowerSlider;
UISlider *glossStartingWhiteSlider;
UISlider *glossEndingWhiteSlider;
UILabel *exponentialCoefficientLabel;
UILabel *glossReflectionPowerLabel;
UILabel *glossStartingWhiteLabel;
UILabel *glossEndingWhiteLabel;

// caustic
UISlider *causticHueSlider;
UISlider *graySaturationThresholdSlider;
UISlider *causticSaturationForGraysSlider;
UISlider *redHueThresholdSlider;
UISlider *blueHueThresholdSlider;
UISlider *blueCausticHueSlider;
UISlider *causticFractionDomainFactorSlider;
UISlider *causticFractionRangeFactorSlider;
UILabel *causticHueLabel;
UILabel *graySaturationThresholdLabel;
UILabel *causticSaturationForGraysLabel;
UILabel *redHueThresholdLabel;
UILabel *blueHueThresholdLabel;
UILabel *blueCausticHueLabel;
UILabel *causticFractionDomainFactorLabel;
UILabel *causticFractionRangeFactorLabel;
}

@property(nonatomic, retain) IBOutlet GlossCausticShaderView *shaderView;
@property(nonatomic, retain) IBOutlet UIView *controlsView;
@property(nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property(nonatomic, retain) IBOutlet UIButton *colorButton;
@property(nonatomic, retain) IBOutlet UILabel *settingsTab;

@property(nonatomic, retain) IBOutlet UISlider *redSlider;
@property(nonatomic, retain) IBOutlet UISlider *greenSlider;
@property(nonatomic, retain) IBOutlet UISlider *blueSlider;
@property(nonatomic, retain) IBOutlet UILabel *redLabel;
@property(nonatomic, retain) IBOutlet UILabel *greenLabel;
@property(nonatomic, retain) IBOutlet UILabel *blueLabel;

@property(nonatomic, retain) IBOutlet UISlider *exponentialCoefficientSlider;
@property(nonatomic, retain) IBOutlet UISlider *glossReflectionPowerSlider;
@property(nonatomic, retain) IBOutlet UISlider *glossStartingWhiteSlider;
@property(nonatomic, retain) IBOutlet UISlider *glossEndingWhiteSlider;
@property(nonatomic, retain) IBOutlet UILabel *exponentialCoefficientLabel;
@property(nonatomic, retain) IBOutlet UILabel *glossReflectionPowerLabel;
@property(nonatomic, retain) IBOutlet UILabel *glossStartingWhiteLabel;
@property(nonatomic, retain) IBOutlet UILabel *glossEndingWhiteLabel;

@property(nonatomic, retain) IBOutlet UISlider *causticHueSlider;
@property(nonatomic, retain) IBOutlet UISlider *graySaturationThresholdSlider;
@property(nonatomic, retain) IBOutlet UISlider *causticSaturationForGraysSlider;
@property(nonatomic, retain) IBOutlet UISlider *redHueThresholdSlider;
@property(nonatomic, retain) IBOutlet UISlider *blueHueThresholdSlider;
@property(nonatomic, retain) IBOutlet UISlider *blueCausticHueSlider;
@property(nonatomic, retain) IBOutlet UISlider *causticFractionDomainFactorSlider;
@property(nonatomic, retain) IBOutlet UISlider *causticFractionRangeFactorSlider;
@property(nonatomic, retain) IBOutlet UILabel *causticHueLabel;
@property(nonatomic, retain) IBOutlet UILabel *graySaturationThresholdLabel;
@property(nonatomic, retain) IBOutlet UILabel *causticSaturationForGraysLabel;
@property(nonatomic, retain) IBOutlet UILabel *redHueThresholdLabel;
@property(nonatomic, retain) IBOutlet UILabel *blueHueThresholdLabel;
@property(nonatomic, retain) IBOutlet UILabel *blueCausticHueLabel;
@property(nonatomic, retain) IBOutlet UILabel *causticFractionDomainFactorLabel;
@property(nonatomic, retain) IBOutlet UILabel *causticFractionRangeFactorLabel;

- (IBAction)printToConsoleButtonTapped:(id)sender;
- (IBAction)resetToDefaultsButtonTapped:(id)sender;

- (IBAction)colorSliderChanged:(id)sender;
- (IBAction)sliderChanged:(id)sender;

@end
171 changes: 146 additions & 25 deletions SampleAppiPhone/Classes/GlossCausticShaderViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,173 @@
//------------------------------------------------------------------------------

#import "GlossCausticShaderViewController.h"
#import "GlossCausticShaderView.h"

#import "RRGlossCausticShader.h"
#import "UIColor+RRUIKit.h"

#import <QuartzCore/QuartzCore.h>

@implementation GlossCausticShaderViewController

// designated initialiser
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
@synthesize shaderView;
@synthesize controlsView;
@synthesize scrollView;
@synthesize colorButton;
@synthesize settingsTab;

@synthesize redSlider;
@synthesize greenSlider;
@synthesize blueSlider;
@synthesize redLabel;
@synthesize greenLabel;
@synthesize blueLabel;

@synthesize exponentialCoefficientSlider;
@synthesize glossReflectionPowerSlider;
@synthesize glossStartingWhiteSlider;
@synthesize glossEndingWhiteSlider;
@synthesize exponentialCoefficientLabel;
@synthesize glossReflectionPowerLabel;
@synthesize glossStartingWhiteLabel;
@synthesize glossEndingWhiteLabel;

@synthesize causticHueSlider;
@synthesize graySaturationThresholdSlider;
@synthesize causticSaturationForGraysSlider;
@synthesize redHueThresholdSlider;
@synthesize blueHueThresholdSlider;
@synthesize blueCausticHueSlider;
@synthesize causticFractionDomainFactorSlider;
@synthesize causticFractionRangeFactorSlider;
@synthesize causticHueLabel;
@synthesize graySaturationThresholdLabel;
@synthesize causticSaturationForGraysLabel;
@synthesize redHueThresholdLabel;
@synthesize blueHueThresholdLabel;
@synthesize blueCausticHueLabel;
@synthesize causticFractionDomainFactorLabel;
@synthesize causticFractionRangeFactorLabel;

static NSString *const keyPaths[] =
{
@"exponentialCoefficient",
@"glossReflectionPower",
@"glossStartingWhite",
@"glossEndingWhite",
@"matcher.causticHue",
@"matcher.graySaturationThreshold",
@"matcher.causticSaturationForGrays",
@"matcher.redHueThreshold",
@"matcher.blueHueThreshold",
@"matcher.blueCausticHue",
@"matcher.causticFractionDomainFactor",
@"matcher.causticFractionRangeFactor",
};

- (void)syncUIWithShaderSettings
{
UIColor *color = [self.shaderView.shader noncausticColor];
self.colorButton.backgroundColor = color;
self.redLabel.text = [NSString stringWithFormat:@"%.3f", self.redSlider.value = [color redComponent]];
self.greenLabel.text = [NSString stringWithFormat:@"%.3f", self.greenSlider.value = [color greenComponent]];
self.blueLabel.text = [NSString stringWithFormat:@"%.3f", self.blueSlider.value = [color blueComponent]];
for (NSUInteger i = 0; i < sizeof(keyPaths)/sizeof(keyPaths[0]); i++)
{
float value = [[self.shaderView.shader valueForKeyPath:keyPaths[i]] floatValue];
NSString *key = [[keyPaths[i] componentsSeparatedByString:@"."] lastObject];
UISlider *slider = [self valueForKeyPath:[key stringByAppendingString:@"Slider"]];
UILabel *label = [self valueForKeyPath:[key stringByAppendingString:@"Label"]];
label.text = [NSString stringWithFormat:@"%.3f", slider.value = value];
}
[self.shaderView update];
}

- (void)loadSettingsFromUserDefaults
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"Shader"];
if (data)
{
// Custom initialisation. Override here to perform set-up that executes
// before the view loads.
RRGlossCausticShader *shader = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (shader)
{
shaderView.shader = shader;
}
}
return self;
}
- (void)saveSettingsToUserDefaults
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:shaderView.shader];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"Shader"];
}

- (void)viewDidLoad
{
// Called after the view has been loaded. For view controllers created in
// code, this is after -loadView. For view controllers unarchived from a
// nib, this is after the view is set.
[super viewDidLoad];

self.scrollView.contentSize = self.controlsView.bounds.size;
[self.scrollView addSubview:self.controlsView];

self.colorButton.layer.borderColor = [[UIColor blackColor] CGColor];
self.colorButton.layer.borderWidth = 1.0f;

self.settingsTab.layer.borderColor = [[UIColor blackColor] CGColor];
self.settingsTab.layer.borderWidth = 1.0f;
self.settingsTab.layer.cornerRadius = 5.0f;

[self loadSettingsFromUserDefaults];
[self syncUIWithShaderSettings];
}

//------------------------------------------------------------------------------
#pragma mark Actions
//------------------------------------------------------------------------------

- (IBAction)printToConsoleButtonTapped:(id)sender
{

}

// (starting with iPhone OS 3.0)
- (void)viewDidUnload
- (IBAction)resetToDefaultsButtonTapped:(id)sender
{
// Called after the view controller's view is released and set to nil. For
// example, a memory warning which causes the view to be purged. Not invoked
// as a result of -dealloc.
self.shaderView.shader = [[[RRGlossCausticShader alloc] init] autorelease];
[self syncUIWithShaderSettings];
[self saveSettingsToUserDefaults];
}

- (void)didReceiveMemoryWarning
// non-caustic a.k.a. base colour slider

- (IBAction)colorSliderChanged:(id)sender
{
// Called when the parent application receives a memory warning. Default
// implementation releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
UIColor *newColor = [UIColor colorWithRed:self.redSlider.value
green:self.greenSlider.value
blue:self.blueSlider.value
alpha:1.0f];

self.colorButton.backgroundColor = newColor;

self.redLabel.text = [NSString stringWithFormat:@"%.3f", [newColor redComponent]];
self.greenLabel.text = [NSString stringWithFormat:@"%.3f", [newColor greenComponent]];
self.blueLabel.text = [NSString stringWithFormat:@"%.3f", [newColor blueComponent]];

[self.shaderView.shader setNoncausticColor:newColor];
[self.shaderView update];
[self saveSettingsToUserDefaults];
}

//------------------------------------------------------------------------------
#pragma mark UI View Controller Rotation
//------------------------------------------------------------------------------
// non-colour sliders

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
- (IBAction)sliderChanged:(id)sender
{
// Override to allow rotation. Default returns YES only for
// UIDeviceOrientationPortrait.
return toInterfaceOrientation == UIInterfaceOrientationPortrait;
UISlider *slider = sender;
NSString *keyPath = keyPaths[slider.tag];
NSString *key = [[keyPath componentsSeparatedByString:@"."] lastObject];
UILabel *label = [self valueForKeyPath:[key stringByAppendingString:@"Label"]];
label.text = [NSString stringWithFormat:@"%.3f", slider.value];

[self.shaderView.shader setValue:[NSNumber numberWithFloat:slider.value] forKeyPath:keyPath];
[self.shaderView update];
[self saveSettingsToUserDefaults];
}

@end

0 comments on commit 07dfbe4

Please sign in to comment.