ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / iPhone / FreeFlight / Classes / AppDelegate.m
1 //
2 //  AppDelegate.m
3 //  FreeFlight
4 //
5 //  Created by Frédéric D'HAEYER on 16/10/09.
6 //  Copyright Parrot SA 2009. All rights reserved.
7 //
8 #import "AppDelegate.h"
9 #import "EAGLView.h"
10 #import "MenuUpdater.h"
11 #import "ES1Renderer.h"
12
13 #define SKIP_BUTTON_X_LANDSCAPE_LEFT .9f
14 #define SKIP_BUTTON_Y_LANDSCAPE_LEFT 0.f
15
16 #define SKIP_BUTTON_X_LANDSCAPE_RIGHT 0.f
17 #define SKIP_BUTTON_Y_LANDSCAPE_RIGHT .7f
18
19 #define SKIP_BUTTON_WIDTH .1f
20 #define SKIP_BUTTON_HEIGHT .3f
21
22 #define SKIP_BUTTON_TEXT NSLocalizedString(@"SKIP",)
23
24 @interface Movie : MPMoviePlayerViewController
25 {
26 @public
27         id delegate;
28         SEL rotateToInterfaceOrientationCallback;
29 }
30
31 @end
32
33 @implementation Movie
34
35 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
36 {
37         switch (toInterfaceOrientation)
38         {
39                 case UIInterfaceOrientationPortrait:
40                 case UIInterfaceOrientationPortraitUpsideDown: 
41                         return NO;
42                 default:
43                         [delegate performSelector:rotateToInterfaceOrientationCallback withObject:[NSNumber numberWithUnsignedInt:toInterfaceOrientation]];
44                         return YES;
45         }
46 }
47
48 @end
49
50
51 @implementation AppDelegate
52 @synthesize window;
53 @synthesize menuController;
54
55 - (void) applicationDidFinishLaunching:(UIApplication *)application
56 {
57         NSLog(@"app did finish launching");
58         application.idleTimerDisabled = YES;
59         
60         window.backgroundColor = [UIColor blackColor];
61         
62 #if     (!TARGET_CPU_X86) && (!DEBUG)
63         // Display the movie:
64         movie = [[Movie alloc] initWithContentURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"ARFreeFlight" ofType:@"mp4"]]];
65         
66         ((Movie *)movie)->delegate = self;
67         ((Movie *)movie)->rotateToInterfaceOrientationCallback = @selector(rotateToInterfaceOrientation:);
68         
69         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initGame) name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
70         
71         movie.moviePlayer.shouldAutoplay = NO;
72         movie.moviePlayer.controlStyle = MPMovieControlStyleNone;
73         
74         [window addSubview:[movie view]];
75         [[movie moviePlayer] play];
76         
77         CGRect frame = [window frame];
78         frame.origin.x = SKIP_BUTTON_X_LANDSCAPE_RIGHT*frame.size.width;
79         frame.origin.y = SKIP_BUTTON_Y_LANDSCAPE_RIGHT*frame.size.height;
80         
81         frame.size.width *= SKIP_BUTTON_WIDTH;
82         frame.size.height *= SKIP_BUTTON_HEIGHT;
83         
84         skipMovie = [UIButton buttonWithType:UIButtonTypeCustom];
85         
86         [skipMovie setTransform:CGAffineTransformMakeRotation(.5f*M_PI)];
87         [skipMovie setFrame:frame];
88         [skipMovie setTitle:SKIP_BUTTON_TEXT forState:UIControlStateNormal];
89         [skipMovie setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
90         
91         skipMovie.titleLabel.font = [UIFont boldSystemFontOfSize:20.f];
92         
93         [skipMovie addTarget:movie.moviePlayer action:@selector(stop) forControlEvents:UIControlEventTouchDown];
94         
95         [window addSubview:skipMovie];
96         [window makeKeyAndVisible];
97 #else
98         [window makeKeyAndVisible];
99         [self initGame];
100 #endif
101         
102 }
103
104 - (void) rotateToInterfaceOrientation:(id)toInterfaceOrientation
105 {
106         UIInterfaceOrientation orientation = [(NSNumber *)toInterfaceOrientation unsignedIntValue];
107         
108         CGRect frame = [window frame];
109         
110         switch (orientation)
111         {
112                 case UIInterfaceOrientationLandscapeLeft:
113                         frame.origin.x = SKIP_BUTTON_X_LANDSCAPE_LEFT*frame.size.width;
114                         frame.origin.y = SKIP_BUTTON_Y_LANDSCAPE_LEFT*frame.size.height;
115                         
116                         frame.size.width *= SKIP_BUTTON_WIDTH;
117                         frame.size.height *= SKIP_BUTTON_HEIGHT;
118                         
119                         [skipMovie setTransform:CGAffineTransformMakeRotation(-.5f*M_PI)];
120                         break;
121                 case UIInterfaceOrientationLandscapeRight:
122                         frame.origin.x = SKIP_BUTTON_X_LANDSCAPE_RIGHT*frame.size.width;
123                         frame.origin.y = SKIP_BUTTON_Y_LANDSCAPE_RIGHT*frame.size.height;
124                         
125                         frame.size.width *= SKIP_BUTTON_WIDTH;
126                         frame.size.height *= SKIP_BUTTON_HEIGHT;
127                         
128                         [skipMovie setTransform:CGAffineTransformMakeRotation(.5f*M_PI)];
129                         break;
130                 default:
131                         break;
132         }
133         
134         [skipMovie setFrame:frame];
135 }
136
137 - (void) initGame
138 {
139         [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
140         
141         [skipMovie removeTarget:movie.moviePlayer action:@selector(stop) forControlEvents:UIControlEventTouchDown];
142         [skipMovie removeFromSuperview];
143         skipMovie = nil;
144         
145         [[movie view] removeFromSuperview];
146         [[movie moviePlayer] stop];
147         [movie release];
148         movie = nil;
149         
150         // Setup the menu controller
151         menuController.delegate = self;
152         NSLog(@"menu controller view %@", menuController.view);
153         
154         was_in_game = NO;
155         
156         // Setup the ARDrone
157         ARDroneHUDConfiguration hudconfiguration = {YES, NO, YES};
158         drone = [[ARDrone alloc] initWithFrame:menuController.view.frame withState:was_in_game withDelegate:menuController withHUDConfiguration:&hudconfiguration];
159         
160         // Setup the OpenGL view
161         glView = [[EAGLView alloc] initWithFrame:menuController.view.frame];
162         [glView setDrone:drone];
163         [glView changeState:was_in_game];
164         
165         CGRect frame = drone.view.frame;
166         
167         if (frame.size.width < frame.size.height)
168         {
169                 NSLog(@"Here, the ARDrone frame isn't initialized");
170                 frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.height, frame.size.width);
171                 [drone.view setFrame:frame];
172         }
173
174         [menuController setDrone:drone];
175         [menuController changeState:was_in_game];
176
177         [window addSubview:menuController.view];
178         [window addSubview:glView];
179         [window bringSubviewToFront:menuController.view];
180         [window makeKeyAndVisible];
181 }
182
183 #pragma mark -
184 #pragma mark Drone protocol implementation
185 - (void)changeState:(BOOL)inGame
186 {
187         was_in_game = inGame;
188         
189         if(inGame)
190         {
191                 int value;
192                 [drone setScreenOrientationRight:(menuController.interfaceOrientation == UIInterfaceOrientationLandscapeRight)];
193 #ifdef DISPLAY_DART
194                 value = ARDRONE_VIDEO_CHANNEL_HORI;
195                 [drone setDefaultConfigurationForKey:ARDRONE_CONFIG_KEY_VIDEO_CHANNEL withValue:&value];
196                 
197                 value = ARDRONE_CAMERA_DETECTION_H_ORIENTED_COCARDE;
198 #else
199                 value = ARDRONE_CAMERA_DETECTION_NONE;
200 #endif
201                 [drone setDefaultConfigurationForKey:ARDRONE_CONFIG_KEY_DETECT_TYPE withValue:&value];
202                 
203                 value = 0;
204                 [drone setDefaultConfigurationForKey:ARDRONE_CONFIG_KEY_CONTROL_LEVEL withValue:&value];
205                 
206         }
207         
208         [drone changeState:inGame];
209         [glView changeState:inGame];
210 }
211
212 - (void) applicationWillResignActive:(UIApplication *)application
213 {
214         // Become inactive
215         if(was_in_game)
216         {
217                 [drone changeState:NO];
218                 [glView changeState:NO];
219         }
220         else
221         {
222                 [menuController changeState:NO];
223         }
224 }
225
226 - (void) applicationDidBecomeActive:(UIApplication *)application
227 {
228         if(was_in_game)
229         {
230                 [drone changeState:YES];
231                 [glView changeState:YES];
232         }
233 }
234
235 - (void)applicationWillTerminate:(UIApplication *)application
236 {
237         printf("%s : %d\n", __FUNCTION__, was_in_game);
238         
239         if(was_in_game)
240         {
241                 [drone changeState:NO];
242                 [glView changeState:NO];
243         }
244         else
245         {
246                 [menuController changeState:NO];
247         }
248 }
249
250 - (void)executeCommandIn:(ARDRONE_COMMAND_IN_WITH_PARAM)commandIn fromSender:(id)sender refreshSettings:(BOOL)refresh
251 {
252         
253 }
254
255 - (void)executeCommandIn:(ARDRONE_COMMAND_IN)commandId withParameter:(void*)parameter fromSender:(id)sender
256 {
257         
258 }
259
260 - (void)setDefaultConfigurationForKey:(ARDRONE_CONFIG_KEYS)key withValue:(void *)value
261 {
262         
263 }
264
265 - (BOOL)checkState
266 {
267         BOOL result = NO;
268         
269         if(was_in_game)
270         {
271                 result = [drone checkState];
272         }
273         else
274         {
275                 //result = [menuController checkState];
276         }
277         
278         return result;
279 }
280
281 @end