-
Notifications
You must be signed in to change notification settings - Fork 6
/
BFSceneViewController.m
379 lines (266 loc) · 11.2 KB
/
BFSceneViewController.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//
// SceneViewController.m
// Briefs
//
// Created by Rob Rhyne on 7/17/09.
// Copyright Digital Arch Design, 2009. See LICENSE file for details.
//
#import "BFSceneViewController.h"
#import "BFViewUtilityParser.h"
#import "BFRootView.h"
#import "BFActorView.h"
#import "BFConstants.h"
@implementation BFSceneViewController
@synthesize dataManager, current_scene, delegate;
///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIViewController overrides
- (id)initWithSceneManager:(BFSceneManager*)manager
{
if (self = [super init]) {
self.dataManager = manager;
self.view = [[BFRootView alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,480.0f) andViewController:self];
// load current view, according to data model
BFSceneView *scene_view = [[BFSceneView alloc] initWithScene:[self.dataManager currentScene]];
self.current_scene = scene_view;
[self.view addSubview:scene_view];
[scene_view release];
}
return self;
}
- (void)loadView
{
// TODO: do I need to add view allocation here?
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.current_scene = nil;
}
- (void)dealloc
{
if (current_scene != nil) {
[current_scene release];
}
[dataManager release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Local Dispatch Methods
- (BOOL)willLoadSceneWithIndex:(int)index
{
return [self willLoadSceneWithIndex:index usingTransition:nil];
}
- (BOOL)willLoadSceneWithIndex:(int)index usingTransition:(NSString *)transition
{
BFScene *scene = [dataManager sceneByNumber:index];
// ensure that the scene exists
if (scene == nil)
return false;
// display the view
else {
// remove from old scene
// TODO: need to remove this according to scene transition
BFSceneView *scene_view = [[BFSceneView alloc] initWithScene:scene];
if (self.current_scene != nil && transition != nil)
[self performTransition:transition onEnteringView:scene_view removingOldView:self.current_scene];
else
[self.view addSubview:scene_view];
self.current_scene = scene_view;
[scene_view release];
return true;
}
}
- (BOOL)willToggleActorWithIndex:(int)index
{
if (self.current_scene != nil) {
BFActorView *actorView = [self.current_scene.actor_views objectAtIndex:index];
UIImageView *stubView = [[UIImageView alloc] initWithImage:actorView.image];
stubView.frame = actorView.frame;
[self.current_scene addSubview:stubView];
// begin animation
[UIView beginAnimations:@"ToggleTransition" context:nil];
[UIView setAnimationDuration:0.5f];
stubView.alpha = 1.0f;
actorView.alpha = 0.0f;
// swap the images
[actorView.actor toggle];
actorView.image = [BFViewUtilityParser parseImageFromRepresentation:[actorView.actor background]];
// ease the new image back in
stubView.alpha = 0.0f;
actorView.alpha = 1.0f;
[stubView release];
// commit the animation stack
[UIView commitAnimations];
return true;
}
else return false;
}
- (BOOL)willShowActorWithIndex:(int)index
{
if (self.current_scene != nil) {
BFActorView *actorView = [self.current_scene.actor_views objectAtIndex:index];
// begin animation
[UIView beginAnimations:@"ShowTransition" context:nil];
[UIView setAnimationDuration:0.5f];
actorView.alpha = 1.0f;
// commit the animation stack
[UIView commitAnimations];
return true;
}
else return false;
}
- (BOOL)willHideActorWithIndex:(int)index
{
if (self.current_scene != nil) {
BFActorView *actorView = [self.current_scene.actor_views objectAtIndex:index];
// begin animation
[UIView beginAnimations:@"HideTransition" context:nil];
[UIView setAnimationDuration:0.5f];
actorView.alpha = 0.0f;
// commit the animation stack
[UIView commitAnimations];
return true;
}
else return false;
}
- (BOOL)willResizeActorWithIndex:(int)index toSize:(CGSize)size
{
if (self.current_scene != nil) {
BFActorView *actor = [self.current_scene.actor_views objectAtIndex:index];
// begin animation
[UIView beginAnimations:@"ResizeTransition" context:nil];
[UIView setAnimationDuration:0.5f];
CGPoint origin = actor.frame.origin;
actor.frame = CGRectMake(origin.x, origin.y, size.width, size.height);
// commit the animation stack
[UIView commitAnimations];
return true;
}
else return false;
}
- (BOOL)willMoveActorWithIndex:(int)index toPoint:(CGPoint)point
{
if (self.current_scene != nil) {
BFActorView *actor = [self.current_scene.actor_views objectAtIndex:index];
// begin animation
[UIView beginAnimations:@"MoveTransition" context:nil];
[UIView setAnimationDuration:0.5f];
CGSize size = actor.frame.size;
actor.frame = CGRectMake(point.x, point.y, size.width, size.height);
// commit the animation stack
[UIView commitAnimations];
return true;
}
else return false;
}
- (void)performTransition:(NSString *)transition onEnteringView:(BFSceneView *)entering removingOldView:(BFSceneView *)exiting
{
// P U S H T R A N S I T I O N
// supported directions: (left, right, up, down)
if ([transition hasPrefix:kBFSceneTransitionPush]) {
CGFloat tx = 0.0f;
CGFloat ty = 0.0f;
if([transition hasSuffix:kBFSceneTransitionDirectionLeft]) tx = -320.0f;
else if([transition hasSuffix:kBFSceneTransitionDirectionRight]) tx = 320.0f;
else if([transition hasSuffix:kBFSceneTransitionDirectionUp]) ty = -480.0f;
else if([transition hasSuffix:kBFSceneTransitionDirectionDown]) ty = 480.0f;
entering.transform = CGAffineTransformMakeTranslation(-tx, -ty);
[UIView beginAnimations:@"PushTransition" context:nil];
[UIView setAnimationDuration:0.4f];
[self.view insertSubview:entering belowSubview:exiting];
exiting.transform = CGAffineTransformMakeTranslation(tx, ty);
entering.transform = CGAffineTransformMakeTranslation(0, 0);
}
// F L I P T R A N S I T I O N
// supported directions: (left, right)
else if ([transition hasPrefix:kBFSceneTransitionFlip]) {
[UIView beginAnimations:@"FlipTransition" context:nil];
[UIView setAnimationDuration:0.8f];
UIViewAnimationTransition transitionType = [transition hasSuffix:kBFSceneTransitionDirectionRight] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight;
[UIView setAnimationTransition:transitionType forView:self.view cache:YES];
[self.view addSubview:entering];
[exiting removeFromSuperview];
}
// C U R L T R A N S I T I O N
// supported directions: (up, down)
else if ([transition hasPrefix:kBFSceneTransitionCurl]) {
[UIView beginAnimations:@"CurlTransition" context:nil];
[UIView setAnimationDuration:1.0f];
UIViewAnimationTransition transitionType = [transition hasSuffix:kBFSceneTransitionDirectionDown] ? UIViewAnimationTransitionCurlDown : UIViewAnimationTransitionCurlUp;
[UIView setAnimationTransition:transitionType forView:self.view cache:YES];
[self.view addSubview:entering];
[exiting removeFromSuperview];
}
// C O V E R T R A N S I T I O N
// supported directions: (left, right, up, down)
else if ([transition hasPrefix:kBFSceneTransitionCover]) {
CGFloat tx = 0.0f;
CGFloat ty = 0.0f;
if ([transition hasSuffix:kBFSceneTransitionDirectionLeft]) tx = -320.0f;
else if ([transition hasSuffix:kBFSceneTransitionDirectionRight]) tx = 320.0f;
else if ([transition hasSuffix:kBFSceneTransitionDirectionUp]) ty = -480.0f;
else if ([transition hasSuffix:kBFSceneTransitionDirectionDown]) ty = 480.0f;
entering.transform = CGAffineTransformMakeTranslation(-tx, -ty);
[UIView beginAnimations:@"SlideTransition" context:nil];
[UIView setAnimationDuration:0.5f];
[self.view insertSubview:entering aboveSubview:exiting];
entering.transform = CGAffineTransformMakeTranslation(0.0f, 0.0f);
}
// R E V E A L T R A N S I T I O N
// supported directions: (left, right, up, down)
else if ([transition hasPrefix:kBFSceneTransitionReveal]) {
CGFloat tx = 0.0f;
CGFloat ty = 0.0f;
if ([transition hasSuffix:kBFSceneTransitionDirectionLeft]) tx = -320.0f;
else if ([transition hasSuffix:kBFSceneTransitionDirectionRight]) tx = 320.0f;
else if ([transition hasSuffix:kBFSceneTransitionDirectionDown]) ty = 480.0f;
else if ([transition hasSuffix:kBFSceneTransitionDirectionUp]) ty = -480.0f;
exiting.transform = CGAffineTransformMakeTranslation(0.0f, 0.0f);
[UIView beginAnimations:@"SlideTransition" context:nil];
[UIView setAnimationDuration:0.5f];
[self.view insertSubview:entering belowSubview:exiting];
exiting.transform = CGAffineTransformMakeTranslation(tx, ty);
}
// Z O O M T R A N S I T I O N
// supported directions: (in, out)
else {
CGFloat d0 = ([transition hasSuffix:kBFSceneTransitionDirectionIn]) ? 0.01f : 3.0f;
CGFloat d1 = 1.0f;
exiting.alpha = 0.5f;
[UIView beginAnimations:@"ZoomTransition" context:nil];
[UIView setAnimationDuration:0.4f];
[self.view addSubview:entering];
entering.transform = CGAffineTransformMakeScale(d0, d0);
entering.alpha = 0.01f;
exiting.alpha = 0.0f;
if ([transition hasSuffix:kBFSceneTransitionDirectionIn])
exiting.transform = CGAffineTransformMakeScale(3.0f, 3.0f);
entering.transform = CGAffineTransformMakeScale(d1, d1);
entering.alpha = 1.0f;
}
// commit the animation stack
[UIView commitAnimations];
}
///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Global Dispatch Methods
- (BOOL)willShowKeyboard:(NSString *)type {
// TODO: implement keyboard display
return false;
}
- (void)willStopShowingScene
{
if (self.delegate) {
[self.delegate sceneView:self shouldDismissView:YES];
}
}
@end