/
SettingsViewController.m
187 lines (147 loc) · 6.34 KB
/
SettingsViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// SettingsViewController.m
// ClassicInvaders
//
// Created by Todd Steinackle on 7/5/10.
// Copyright 2010 The No Quarter Arcade. All rights reserved.
//
#import "SettingsViewController.h"
#import "SoundManager.h"
#import "GameController.h"
#import "AbstractScene.h"
@interface SettingsViewController (Private)
- (void)show;
// Update the controls on the view with the current values
- (void)updateControlValues;
@end
@implementation SettingsViewController
#pragma mark -
#pragma mark Deallocation
- (void)dealloc {
// Remove observers that have been set up
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"showSettings" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateSettingsSliders" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceOrientationDidChangeNotification" object:nil];
[super dealloc];
}
#pragma mark -
#pragma mark Init view
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Set up the settings view
sharedSoundManager = [SoundManager sharedSoundManager];
sharedGameController = [GameController sharedGameController];
// Set up a notification observers
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(show) name:@"showSettings" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateControlValues) name:@"updateSettingsSliders" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set the initial alpha of the view
self.view.alpha = 0;
// Make sure the controls on the view are updated with the current values
[self updateControlValues];
if (sharedGameController.interfaceOrientation_ == UIInterfaceOrientationLandscapeRight) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.view.center = CGPointMake(384, 512);
} else {
self.view.center = CGPointMake(160, 240);
}
}
if (sharedGameController.interfaceOrientation_ == UIInterfaceOrientationLandscapeLeft) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.view.center = CGPointMake(384, 512);
} else {
self.view.center = CGPointMake(160, 240);
}
}
}
- (void) orientationChanged:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.view.center = CGPointMake(384, 512);
} else {
self.view.center = CGPointMake(160, 240);
}
}
if (orientation == UIDeviceOrientationLandscapeRight) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.view.center = CGPointMake(384, 512);
} else {
self.view.center = CGPointMake(160, 240);
}
}
}
#pragma mark -
#pragma mark UI Actions
- (IBAction)backgroundValueChanged:(UISlider*)sender {
sharedSoundManager.bgVolume = [sender value];
}
- (IBAction)fxValueChanged:(UISlider*)sender {
sharedSoundManager.fxVolume = [sender value];
}
- (IBAction)buttonPositionsChanged:(UISegmentedControl*)sender {
sharedGameController.buttonPositions_ = sender.selectedSegmentIndex;
}
- (IBAction)grahicsChoiceChanged:(UISegmentedControl*)sender {
sharedGameController.graphicsChoice_ = sender.selectedSegmentIndex;
}
#pragma mark -
#pragma mark Rotating and hiding
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
- (IBAction)hide:(id)sender {
[sharedSoundManager playSoundWithKey:@"guiTouch" gain:0.3f pitch:1.0f location:CGPointMake(0, 0) shouldLoop:NO ];
[sharedGameController saveSettings];
// Fade out the view using core animation. We do not want to remove this view from EAGLView
// until the fade has ended, so we use the animation delegate and AnimationDidStopSelector
// to call the hideFinished method when the animation is done. This then removes this
// view from EAGLView
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(hideFinished)];
self.view.alpha = 0.0f;
[UIView commitAnimations];
}
- (void)hideFinished {
// Remove this view from its superview i.e. EAGLView. This allows the next view that is added
// to be the topmost view and therefore react to orientation events
[self.view removeFromSuperview];
}
@end
#pragma mark -
#pragma mark Private implementation
@implementation SettingsViewController (Private)
- (void)show {
// Add this view as a subview of EAGLView
[sharedGameController.eaglView_ addSubview:self.view];
// ...then fade it in using core animation
[UIView beginAnimations:nil context:NULL];
self.view.alpha = 1.0f;
[UIView commitAnimations];
}
- (void)updateControlValues {
// Set the views control values based on the game controllers values
bgVolume.value = sharedSoundManager.bgVolume;
fxVolume.value = sharedSoundManager.fxVolume;
buttonPositions.selectedSegmentIndex = sharedGameController.buttonPositions_;
graphicsChoice.selectedSegmentIndex = sharedGameController.graphicsChoice_;
}
@end