-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSLSMoleculeRootViewController.m
293 lines (238 loc) · 9.33 KB
/
SLSMoleculeRootViewController.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// SLSMoleculeRootViewController.m
// Molecules
//
// The source code for Molecules is available under a BSD license. See License.txt for details.
//
// Created by Brad Larson on 6/30/2008.
//
// This controller manages a root view into which the 3D view and the molecule table selection views and animated for the neat flipping effect
#import "SLSMoleculeAppDelegate.h"
#import "SLSMoleculeRootViewController.h"
#import "SLSMoleculeTableViewController.h"
#import "SLSMoleculeGLViewController.h"
#import "SLSMoleculeGLView.h"
#import "SLSMolecule.h"
@implementation SLSMoleculeRootViewController
#pragma mark -
#pragma mark Initialiation and breakdown
- (id)init;
{
if (self = [super init])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toggleView:) name:@"ToggleView" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toggleRotationButton:) name:@"ToggleRotationSelected" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(customURLSelectedForMoleculeDownload:) name:@"CustomURLForMoleculeSelected" object:nil];
}
return self;
}
- (void)dealloc
{
[rotationButton release];
[tableViewController release];
[glViewController release];
[tableNavigationController release];
[super dealloc];
}
- (void)loadView
{
CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];
UIView *backgroundView = [[UIView alloc] initWithFrame:mainScreenFrame];
backgroundView.backgroundColor = [UIColor whiteColor];
self.view = backgroundView;
[backgroundView release];
toggleViewDisabled = NO;
SLSMoleculeGLViewController *viewController = [[SLSMoleculeGLViewController alloc] initWithNibName:nil bundle:nil];
self.glViewController = viewController;
[viewController release];
[self.view addSubview:glViewController.view];
UIButton *infoButton = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoButton.frame = CGRectMake(320.0f - 70.0f, 460.0f - 70.0f, 70.0f, 70.0f);
[infoButton addTarget:glViewController action:@selector(switchToTableView) forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
[glViewController.view addSubview:infoButton];
[infoButton release];
rotationButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rotationImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"RotationIcon" ofType:@"png"]];
[rotationButton setImage:rotationImage forState:UIControlStateNormal];
[rotationImage release];
UIImage *selectedRotationImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"RotationIconSelected" ofType:@"png"]];
[rotationButton setImage:selectedRotationImage forState:UIControlStateSelected];
[selectedRotationImage release];
rotationButton.showsTouchWhenHighlighted = YES;
[rotationButton addTarget:glViewController action:@selector(startOrStopAutorotation:) forControlEvents:UIControlEventTouchUpInside];
rotationButton.frame = CGRectMake(0.0f, 460.0f - 70.0f, 70.0f, 70.0f);
rotationButton.clipsToBounds = NO;
[glViewController.view addSubview:rotationButton];
}
- (void)toggleView:(NSNotification *)note;
{
if (molecules == nil)
return;
UIView *tableView = self.tableNavigationController.view;
SLSMoleculeGLView *glView = (SLSMoleculeGLView *)glViewController.view;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([glView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
if ([glView superview] != nil)
{
[self cancelMoleculeLoading];
[tableNavigationController viewWillAppear:YES];
[glViewController viewWillDisappear:YES];
[glView removeFromSuperview];
[self.view addSubview:tableView];
[glViewController viewDidDisappear:YES];
[tableNavigationController viewDidAppear:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
else
{
[glViewController viewWillAppear:YES];
[tableNavigationController viewWillDisappear:YES];
[tableView removeFromSuperview];
[self.view addSubview:glView];
[tableNavigationController viewDidDisappear:YES];
[glViewController viewDidAppear:YES];
if (bufferedMolecule != previousMolecule)
{
previousMolecule = bufferedMolecule;
glViewController.moleculeToDisplay = bufferedMolecule;
}
else
previousMolecule.isBeingDisplayed = YES;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
}
[UIView commitAnimations];
}
#pragma mark -
#pragma mark Passthroughs for managing molecules
- (void)loadInitialMolecule;
{
NSInteger indexOfInitialMolecule = [[NSUserDefaults standardUserDefaults] integerForKey:@"indexOfLastSelectedMolecule"];
if (indexOfInitialMolecule >= [molecules count])
indexOfInitialMolecule = 0;
if ([molecules count] > 0)
glViewController.moleculeToDisplay = [molecules objectAtIndex:indexOfInitialMolecule];
}
- (void)selectedMoleculeDidChange:(NSInteger)newMoleculeIndex;
{
if (newMoleculeIndex >= [molecules count])
newMoleculeIndex = 0;
[[NSUserDefaults standardUserDefaults] setInteger:newMoleculeIndex forKey:@"indexOfLastSelectedMolecule"];
[[NSUserDefaults standardUserDefaults] synchronize];
tableViewController.selectedIndex = newMoleculeIndex;
// Defer sending the change message to the OpenGL view until the view is loaded, to make sure that rendering occurs only then
if ([molecules count] == 0)
bufferedMolecule = nil;
else
bufferedMolecule = [molecules objectAtIndex:newMoleculeIndex];
}
#pragma mark -
#pragma mark UIViewController methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Only allow free autorotation on the iPad
if ([SLSMoleculeAppDelegate isRunningOniPad])
{
return YES;
}
else
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
- (void)didReceiveMemoryWarning
{
}
- (void)cancelMoleculeLoading;
{
if (!glViewController.moleculeToDisplay.isDoneRendering)
{
glViewController.moleculeToDisplay.isRenderingCancelled = YES;
[NSThread sleepForTimeInterval:0.1];
}
}
- (void)updateTableListOfMolecules;
{
UITableView *tableView = (UITableView *)tableViewController.view;
[tableView reloadData];
}
#pragma mark -
#pragma mark Manage the switching of rotation state
- (void)toggleRotationButton:(NSNotification *)note;
{
if ([[note object] boolValue])
{
rotationButton.selected = YES;
}
else
{
rotationButton.selected = NO;
}
}
- (void)customURLSelectedForMoleculeDownload:(NSNotification *)note;
{
NSURL *customURLForMoleculeDownload = [note object];
bufferedMolecule = nil;
if (![SLSMoleculeAppDelegate isRunningOniPad])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ToggleView" object:nil];
}
//molecules://www.sunsetlakesoftware.com/sites/default/files/xenonPump.pdb
//html://www.sunsetlakesoftware.com/sites/default/files/xenonPump.pdb
NSString *pathComponentForCustomURL = [[customURLForMoleculeDownload host] stringByAppendingString:[customURLForMoleculeDownload path]];
NSString *customMoleculeHandlingURL = [NSString stringWithFormat:@"molecules://%@", pathComponentForCustomURL];
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customMoleculeHandlingURL]];
[(SLSMoleculeAppDelegate *)[[UIApplication sharedApplication] delegate] handleCustomURLScheme:[NSURL URLWithString:customMoleculeHandlingURL]];
}
#pragma mark -
#pragma mark Accessors
@synthesize tableNavigationController;
@synthesize tableViewController;
@synthesize glViewController;
@synthesize database;
@synthesize molecules;
- (void)setDatabase:(sqlite3 *)newValue
{
database = newValue;
tableViewController.database = database;
}
- (void)setMolecules:(NSMutableArray *)newValue;
{
if (molecules == newValue)
return;
[molecules release];
molecules = [newValue retain];
tableViewController.molecules = molecules;
NSInteger indexOfInitialMolecule = [[NSUserDefaults standardUserDefaults] integerForKey:@"indexOfLastSelectedMolecule"];
if (indexOfInitialMolecule >= [molecules count])
indexOfInitialMolecule = 0;
tableViewController.selectedIndex = indexOfInitialMolecule;
}
- (UINavigationController *)tableNavigationController;
{
if (tableNavigationController == nil)
{
bufferedMolecule = nil;
tableNavigationController = [[UINavigationController alloc] init];
if ([SLSMoleculeAppDelegate isRunningOniPad])
{
tableNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
}
NSInteger indexOfInitialMolecule = [[NSUserDefaults standardUserDefaults] integerForKey:@"indexOfLastSelectedMolecule"];
if (indexOfInitialMolecule >= [molecules count])
indexOfInitialMolecule = 0;
tableViewController = [[SLSMoleculeTableViewController alloc] initWithStyle:UITableViewStylePlain initialSelectedMoleculeIndex:indexOfInitialMolecule];
tableViewController.database = database;
tableViewController.molecules = molecules;
[tableNavigationController pushViewController:tableViewController animated:NO];
tableViewController.delegate = self;
// Need to correct the view rectangle of the navigation view to correct for the status bar gap
UIView *tableView = tableNavigationController.view;
CGRect tableFrame = tableView.frame;
tableFrame.origin.y -= 20;
tableView.frame = tableFrame;
toggleViewDisabled = NO;
}
return tableNavigationController;
}
@end