5 // Created by clement choquereau on 5/4/11.
6 // Copyright 2011 Parrot. All rights reserved.
9 #import "MenuController.h"
12 #import "FiniteStateMachine.h"
14 @interface MenuController()
16 - (void) enterState:(id)state;
17 - (void) quitState:(id)state;
21 @implementation MenuController
33 self.fsm = [FiniteStateMachine fsmWithXML:[[NSBundle mainBundle] pathForResource:@"menus_fsm" ofType:@"xml"]];
35 fsm.currentState = MENU_STATE_UPDATER;
40 // Release the current menu
43 [currentMenu.view removeFromSuperview];
44 [currentMenu release];
55 * Finite State Machine setter
59 - (void)setFsm:(FiniteStateMachine *)_fsm
65 [fsm setDelegate:self];
67 unsigned int n = fsm.statesCount;
69 for (unsigned int state = 0 ; state < n ; state++)
71 [fsm setEnterStateCallback:@selector(enterState:) forState:state];
72 [fsm setQuitStateCallback:@selector(quitState:) forState:state];
81 - (void) enterState:(id)_fsm
83 if (![fsm currentObject])
86 NSString *object = [fsm currentObject];
88 if ([object compare:FSM_HUD] == NSOrderedSame)
90 [delegate changeState:YES];
91 [self changeState:YES];
96 Class menuClass = NSClassFromString(object);
98 currentMenu = [(UIViewController<MenuProtocol> *)[menuClass alloc] initWithController:self];
99 if (![delegate checkState])
100 [self.view addSubview:currentMenu.view];
108 - (void) quitState:(id)_fsm
110 NSString *object = nil;
113 object = [fsm currentObject];
115 if ( (object) && ([object compare:FSM_HUD] == NSOrderedSame) )
117 [self changeState:NO];
118 [delegate changeState:NO];
123 [currentMenu.view removeFromSuperview];
124 [currentMenu release];
133 - (void)doAction:(unsigned int)action
138 [fsm doAction:action];
146 - (unsigned int)currentState
151 return [fsm currentState];
154 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
156 if([delegate checkState] == NO)
157 return UIInterfaceOrientationIsLandscape(interfaceOrientation);
162 // ARDrone protocols:
164 - (void)changeState:(BOOL)inGame
168 // If not in game, the MenuController removes the drone view, and adds a menu view.
170 [drone.view removeFromSuperview];
173 [self.view addSubview:currentMenu.view];
177 // If in game, the MenuController removes the active menu view, and adds the drone view.
179 [currentMenu.view removeFromSuperview];
182 [self.view addSubview:drone.view];
186 - (void) executeCommandIn:(ARDRONE_COMMAND_IN_WITH_PARAM)commandIn fromSender:(id)sender refreshSettings:(BOOL)refresh
190 - (void)executeCommandIn:(ARDRONE_COMMAND_IN)commandId withParameter:(void *)parameter fromSender:(id)sender
199 - (void)setDefaultConfigurationForKey:(ARDRONE_CONFIG_KEYS)key withValue:(void *)value
205 - (void)executeCommandOut:(ARDRONE_COMMAND_OUT)commandId withParameter:(void *)parameter fromSender:(id)sender
208 [currentMenu executeCommandOut:commandId withParameter:parameter fromSender:sender];