ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / iPhone / FreeFlight / Classes / Menus / MenuController.m
1 //
2 //  MenuController.m
3 //  ArDroneGameLib
4 //
5 //  Created by clement choquereau on 5/4/11.
6 //  Copyright 2011 Parrot. All rights reserved.
7 //
8
9 #import "MenuController.h"
10
11 #import "ARDrone.h"
12 #import "FiniteStateMachine.h"
13
14 @interface MenuController()
15
16 - (void) enterState:(id)state;
17 - (void) quitState:(id)state;
18
19 @end
20
21 @implementation MenuController
22
23 @synthesize fsm;
24 @synthesize drone;
25 @synthesize delegate;
26
27 - (void)viewDidLoad
28 {
29         [super viewDidLoad];
30     
31         currentMenu = nil;
32         
33         self.fsm = [FiniteStateMachine fsmWithXML:[[NSBundle mainBundle] pathForResource:@"menus_fsm" ofType:@"xml"]];
34         
35         fsm.currentState = MENU_STATE_UPDATER;
36 }
37
38 - (void) dealloc
39 {
40         // Release the current menu
41         if (currentMenu)
42         {
43                 [currentMenu.view removeFromSuperview];
44                 [currentMenu release];
45                 currentMenu = nil;
46         }
47         
48     [fsm release];
49     fsm = nil;
50         
51         [super dealloc];
52 }
53
54 /*
55  * Finite State Machine setter
56  *
57  * 
58  */
59 - (void)setFsm:(FiniteStateMachine *)_fsm
60 {
61     [_fsm retain];
62     [fsm release];
63     fsm = _fsm;
64     
65     [fsm setDelegate:self];
66     
67     unsigned int n = fsm.statesCount;
68     
69     for (unsigned int state = 0 ; state < n ; state++)
70     {
71         [fsm setEnterStateCallback:@selector(enterState:) forState:state];
72         [fsm setQuitStateCallback:@selector(quitState:) forState:state];
73     }
74 }
75
76 /*
77  * enterState
78  *
79  * 
80  */
81 - (void) enterState:(id)_fsm
82 {
83         if (![fsm currentObject])
84                 return;
85         
86         NSString *object = [fsm currentObject];
87         
88         if ([object compare:FSM_HUD] == NSOrderedSame)
89         {
90                 [delegate changeState:YES];
91                 [self changeState:YES];
92                 
93                 return;
94         }
95         
96     Class menuClass = NSClassFromString(object);
97     
98     currentMenu = [(UIViewController<MenuProtocol> *)[menuClass alloc] initWithController:self];
99         if (![delegate checkState])
100                 [self.view addSubview:currentMenu.view];
101 }
102
103 /*
104  * quitState
105  *
106  * 
107  */
108 - (void) quitState:(id)_fsm
109 {
110         NSString *object = nil;
111         
112         if (fsm)
113                 object = [fsm currentObject];
114         
115         if ( (object) && ([object compare:FSM_HUD] == NSOrderedSame) )
116         {
117                 [self changeState:NO];
118                 [delegate changeState:NO];
119                 
120                 return;
121         }
122         
123     [currentMenu.view removeFromSuperview];
124     [currentMenu release];
125         currentMenu = nil;
126 }
127
128 /*
129  * doAction
130  *
131  * 
132  */
133 - (void)doAction:(unsigned int)action
134 {
135     if (!fsm)
136         return;
137     
138     [fsm doAction:action];
139 }
140
141 /*
142  * currentState
143  *
144  *
145  */
146 - (unsigned int)currentState
147 {
148         if (!fsm)
149                 return NO_STATE;
150         
151         return [fsm currentState];
152 }
153
154 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
155 {       
156         if([delegate checkState] == NO)
157                 return UIInterfaceOrientationIsLandscape(interfaceOrientation);
158         else
159                 return FALSE;
160 }
161
162 // ARDrone protocols:
163 // ProtocolIn
164 - (void)changeState:(BOOL)inGame
165 {
166         if(!inGame)
167         {
168         // If not in game, the MenuController removes the drone view, and adds a menu view.
169                 if (drone)
170                         [drone.view removeFromSuperview];
171                 
172                 if (currentMenu)
173                         [self.view addSubview:currentMenu.view];
174         }
175         else
176         {
177         // If in game, the MenuController removes the active menu view, and adds the drone view.
178                 if (currentMenu)
179                         [currentMenu.view removeFromSuperview];
180                 
181                 if (drone)
182                         [self.view addSubview:drone.view];
183         }
184 }
185
186 - (void) executeCommandIn:(ARDRONE_COMMAND_IN_WITH_PARAM)commandIn fromSender:(id)sender refreshSettings:(BOOL)refresh
187 {
188 }
189
190 - (void)executeCommandIn:(ARDRONE_COMMAND_IN)commandId withParameter:(void *)parameter fromSender:(id)sender
191 {
192 }
193
194 - (BOOL)checkState
195 {
196         return YES;
197 }
198
199 - (void)setDefaultConfigurationForKey:(ARDRONE_CONFIG_KEYS)key withValue:(void *)value
200 {
201     
202 }
203
204 // ProtocolOut
205 - (void)executeCommandOut:(ARDRONE_COMMAND_OUT)commandId withParameter:(void *)parameter fromSender:(id)sender
206 {
207         if (currentMenu)
208                 [currentMenu executeCommandOut:commandId withParameter:parameter fromSender:sender];
209 }
210
211 @end