5 // Created by Frédéric D'HAEYER on 16/10/09.
6 // Copyright Parrot SA 2009. All rights reserved.
8 #import "AppDelegate.h"
10 #import "MenuUpdater.h"
11 #import "ES1Renderer.h"
13 #define SKIP_BUTTON_X_LANDSCAPE_LEFT .9f
14 #define SKIP_BUTTON_Y_LANDSCAPE_LEFT 0.f
16 #define SKIP_BUTTON_X_LANDSCAPE_RIGHT 0.f
17 #define SKIP_BUTTON_Y_LANDSCAPE_RIGHT .7f
19 #define SKIP_BUTTON_WIDTH .1f
20 #define SKIP_BUTTON_HEIGHT .3f
22 #define SKIP_BUTTON_TEXT NSLocalizedString(@"SKIP",)
24 @interface Movie : MPMoviePlayerViewController
28 SEL rotateToInterfaceOrientationCallback;
35 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
37 switch (toInterfaceOrientation)
39 case UIInterfaceOrientationPortrait:
40 case UIInterfaceOrientationPortraitUpsideDown:
43 [delegate performSelector:rotateToInterfaceOrientationCallback withObject:[NSNumber numberWithUnsignedInt:toInterfaceOrientation]];
51 @implementation AppDelegate
53 @synthesize menuController;
55 - (void) applicationDidFinishLaunching:(UIApplication *)application
57 NSLog(@"app did finish launching");
58 application.idleTimerDisabled = YES;
60 window.backgroundColor = [UIColor blackColor];
62 #if (!TARGET_CPU_X86) && (!DEBUG)
64 movie = [[Movie alloc] initWithContentURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"ARFreeFlight" ofType:@"mp4"]]];
66 ((Movie *)movie)->delegate = self;
67 ((Movie *)movie)->rotateToInterfaceOrientationCallback = @selector(rotateToInterfaceOrientation:);
69 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initGame) name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
71 movie.moviePlayer.shouldAutoplay = NO;
72 movie.moviePlayer.controlStyle = MPMovieControlStyleNone;
74 [window addSubview:[movie view]];
75 [[movie moviePlayer] play];
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;
81 frame.size.width *= SKIP_BUTTON_WIDTH;
82 frame.size.height *= SKIP_BUTTON_HEIGHT;
84 skipMovie = [UIButton buttonWithType:UIButtonTypeCustom];
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];
91 skipMovie.titleLabel.font = [UIFont boldSystemFontOfSize:20.f];
93 [skipMovie addTarget:movie.moviePlayer action:@selector(stop) forControlEvents:UIControlEventTouchDown];
95 [window addSubview:skipMovie];
96 [window makeKeyAndVisible];
98 [window makeKeyAndVisible];
104 - (void) rotateToInterfaceOrientation:(id)toInterfaceOrientation
106 UIInterfaceOrientation orientation = [(NSNumber *)toInterfaceOrientation unsignedIntValue];
108 CGRect frame = [window frame];
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;
116 frame.size.width *= SKIP_BUTTON_WIDTH;
117 frame.size.height *= SKIP_BUTTON_HEIGHT;
119 [skipMovie setTransform:CGAffineTransformMakeRotation(-.5f*M_PI)];
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;
125 frame.size.width *= SKIP_BUTTON_WIDTH;
126 frame.size.height *= SKIP_BUTTON_HEIGHT;
128 [skipMovie setTransform:CGAffineTransformMakeRotation(.5f*M_PI)];
134 [skipMovie setFrame:frame];
139 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
141 [skipMovie removeTarget:movie.moviePlayer action:@selector(stop) forControlEvents:UIControlEventTouchDown];
142 [skipMovie removeFromSuperview];
145 [[movie view] removeFromSuperview];
146 [[movie moviePlayer] stop];
150 // Setup the menu controller
151 menuController.delegate = self;
152 NSLog(@"menu controller view %@", menuController.view);
157 ARDroneHUDConfiguration hudconfiguration = {YES, NO, YES};
158 drone = [[ARDrone alloc] initWithFrame:menuController.view.frame withState:was_in_game withDelegate:menuController withHUDConfiguration:&hudconfiguration];
160 // Setup the OpenGL view
161 glView = [[EAGLView alloc] initWithFrame:menuController.view.frame];
162 [glView setDrone:drone];
163 [glView changeState:was_in_game];
165 CGRect frame = drone.view.frame;
167 if (frame.size.width < frame.size.height)
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];
174 [menuController setDrone:drone];
175 [menuController changeState:was_in_game];
177 [window addSubview:menuController.view];
178 [window addSubview:glView];
179 [window bringSubviewToFront:menuController.view];
180 [window makeKeyAndVisible];
184 #pragma mark Drone protocol implementation
185 - (void)changeState:(BOOL)inGame
187 was_in_game = inGame;
192 [drone setScreenOrientationRight:(menuController.interfaceOrientation == UIInterfaceOrientationLandscapeRight)];
194 value = ARDRONE_VIDEO_CHANNEL_HORI;
195 [drone setDefaultConfigurationForKey:ARDRONE_CONFIG_KEY_VIDEO_CHANNEL withValue:&value];
197 value = ARDRONE_CAMERA_DETECTION_H_ORIENTED_COCARDE;
199 value = ARDRONE_CAMERA_DETECTION_NONE;
201 [drone setDefaultConfigurationForKey:ARDRONE_CONFIG_KEY_DETECT_TYPE withValue:&value];
204 [drone setDefaultConfigurationForKey:ARDRONE_CONFIG_KEY_CONTROL_LEVEL withValue:&value];
208 [drone changeState:inGame];
209 [glView changeState:inGame];
212 - (void) applicationWillResignActive:(UIApplication *)application
217 [drone changeState:NO];
218 [glView changeState:NO];
222 [menuController changeState:NO];
226 - (void) applicationDidBecomeActive:(UIApplication *)application
230 [drone changeState:YES];
231 [glView changeState:YES];
235 - (void)applicationWillTerminate:(UIApplication *)application
237 printf("%s : %d\n", __FUNCTION__, was_in_game);
241 [drone changeState:NO];
242 [glView changeState:NO];
246 [menuController changeState:NO];
250 - (void)executeCommandIn:(ARDRONE_COMMAND_IN_WITH_PARAM)commandIn fromSender:(id)sender refreshSettings:(BOOL)refresh
255 - (void)executeCommandIn:(ARDRONE_COMMAND_IN)commandId withParameter:(void*)parameter fromSender:(id)sender
260 - (void)setDefaultConfigurationForKey:(ARDRONE_CONFIG_KEYS)key withValue:(void *)value
271 result = [drone checkState];
275 //result = [menuController checkState];