author | koda |
Tue, 06 Jul 2010 03:33:01 +0200 | |
changeset 3625 | 9f1d79e01a60 |
parent 3623 | f14db208f2fa |
child 3626 | 19f78afa0188 |
permissions | -rw-r--r-- |
3547 | 1 |
// |
2 |
// GameConfigViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 18/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "GameConfigViewController.h" |
|
10 |
#import "SDL_uikitappdelegate.h" |
|
11 |
#import "CommodityFunctions.h" |
|
12 |
#import "MapConfigViewController.h" |
|
13 |
#import "TeamConfigViewController.h" |
|
14 |
#import "SchemeWeaponConfigViewController.h" |
|
15 |
||
3619 | 16 |
// draw background image in uitoolbar |
3625 | 17 |
/*@implementation UIToolbar (CustomImage) |
3619 | 18 |
-(void) drawRect:(CGRect)rect { |
19 |
UIImage *image = [UIImage imageWithContentsOfFile:@"toolbarBackground.png"]; |
|
20 |
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; |
|
21 |
} |
|
22 |
@end |
|
3625 | 23 |
*/ |
3547 | 24 |
@implementation GameConfigViewController |
25 |
||
26 |
||
27 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
28 |
return rotationManager(interfaceOrientation); |
3547 | 29 |
} |
30 |
||
31 |
-(IBAction) buttonPressed:(id) sender { |
|
32 |
// works even if it's not actually a button |
|
33 |
UIButton *theButton = (UIButton *)sender; |
|
34 |
switch (theButton.tag) { |
|
35 |
case 0: |
|
3625 | 36 |
if ([mapConfigViewController busy]) { |
37 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Wait for the Preview",@"") |
|
38 |
message:NSLocalizedString(@"Before returning the preview needs to be generated",@"") |
|
39 |
delegate:nil |
|
40 |
cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
41 |
otherButtonTitles:nil]; |
|
42 |
[alert show]; |
|
43 |
[alert release]; |
|
44 |
} else { |
|
45 |
[[self parentViewController] dismissModalViewControllerAnimated:YES]; |
|
46 |
||
47 |
} |
|
3547 | 48 |
break; |
49 |
case 1: |
|
50 |
theButton.enabled = NO; |
|
51 |
[self performSelector:@selector(startGame:) |
|
52 |
withObject:theButton |
|
53 |
afterDelay:0.25]; |
|
54 |
break; |
|
55 |
default: |
|
56 |
break; |
|
57 |
} |
|
58 |
} |
|
59 |
||
60 |
-(IBAction) segmentPressed:(id) sender { |
|
61 |
UISegmentedControl *theSegment = (UISegmentedControl *)sender; |
|
62 |
||
63 |
switch (theSegment.selectedSegmentIndex) { |
|
64 |
case 0: |
|
65 |
// this init here is just aestetic as this controller was already set up in viewDidLoad |
|
66 |
if (mapConfigViewController == nil) { |
|
67 |
mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil]; |
|
68 |
} |
|
69 |
activeController = mapConfigViewController; |
|
70 |
break; |
|
71 |
case 1: |
|
72 |
if (teamConfigViewController == nil) { |
|
73 |
teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
74 |
// this message is compulsory otherwise the table won't be loaded at all |
|
75 |
} |
|
76 |
activeController = teamConfigViewController; |
|
77 |
break; |
|
78 |
case 2: |
|
79 |
if (schemeWeaponConfigViewController == nil) { |
|
80 |
schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
81 |
} |
|
82 |
activeController = schemeWeaponConfigViewController; |
|
83 |
break; |
|
84 |
} |
|
85 |
||
86 |
// this message is compulsory otherwise the table won't be loaded at all |
|
87 |
[activeController viewWillAppear:NO]; |
|
88 |
[self.view addSubview:activeController.view]; |
|
89 |
} |
|
90 |
||
91 |
-(void) startGame:(UIButton *)button { |
|
92 |
button.enabled = YES; |
|
93 |
||
94 |
// don't start playing if the preview is in progress |
|
95 |
if ([mapConfigViewController busy]) { |
|
96 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Wait for the Preview",@"") |
|
97 |
message:NSLocalizedString(@"Before playing the preview needs to be generated",@"") |
|
98 |
delegate:nil |
|
99 |
cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
100 |
otherButtonTitles:nil]; |
|
101 |
[alert show]; |
|
102 |
[alert release]; |
|
103 |
return; |
|
104 |
} |
|
105 |
||
106 |
// play only if there is more than one team |
|
107 |
if ([teamConfigViewController.listOfSelectedTeams count] < 2) { |
|
108 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too few teams playing",@"") |
|
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
109 |
message:NSLocalizedString(@"Select at least two teams to play a game",@"") |
3547 | 110 |
delegate:nil |
111 |
cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
112 |
otherButtonTitles:nil]; |
|
113 |
[alert show]; |
|
114 |
[alert release]; |
|
115 |
return; |
|
116 |
} |
|
117 |
||
118 |
// play if there's room for enough hogs in the selected map |
|
119 |
int hogs = 0; |
|
120 |
for (NSDictionary *teamData in teamConfigViewController.listOfSelectedTeams) |
|
121 |
hogs += [[teamData objectForKey:@"number"] intValue]; |
|
122 |
||
123 |
if (hogs > mapConfigViewController.maxHogs) { |
|
124 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too many hogs",@"") |
|
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
125 |
message:NSLocalizedString(@"The map is too small for that many hogs",@"") |
3547 | 126 |
delegate:nil |
127 |
cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
128 |
otherButtonTitles:nil]; |
|
129 |
[alert show]; |
|
130 |
[alert release]; |
|
131 |
return; |
|
132 |
} |
|
133 |
||
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
134 |
if ([schemeWeaponConfigViewController.selectedScheme length] == 0 || [schemeWeaponConfigViewController.selectedWeapon length] == 0 ) { |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
135 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Missing detail",@"") |
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
136 |
message:NSLocalizedString(@"Select one Scheme and one Weapon for this game",@"") |
3548
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
137 |
delegate:nil |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
138 |
cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
139 |
otherButtonTitles:nil]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
140 |
[alert show]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
141 |
[alert release]; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
142 |
return; |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
143 |
} |
4d220ee7c75f
server somewhat simplified and correct sporadic crasher
koda
parents:
3547
diff
changeset
|
144 |
|
3547 | 145 |
// create the configuration file that is going to be sent to engine |
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
146 |
NSDictionary *gameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:mapConfigViewController.seedCommand,@"seed_command", |
3547 | 147 |
mapConfigViewController.templateFilterCommand,@"templatefilter_command", |
148 |
mapConfigViewController.mapGenCommand,@"mapgen_command", |
|
149 |
mapConfigViewController.mazeSizeCommand,@"mazesize_command", |
|
150 |
mapConfigViewController.themeCommand,@"theme_command", |
|
151 |
teamConfigViewController.listOfSelectedTeams,@"teams_list", |
|
152 |
schemeWeaponConfigViewController.selectedScheme,@"scheme", |
|
153 |
schemeWeaponConfigViewController.selectedWeapon,@"weapon", |
|
154 |
nil]; |
|
155 |
||
156 |
// finally launch game and remove this controller |
|
157 |
[[self parentViewController] dismissModalViewControllerAnimated:YES]; |
|
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
158 |
[[SDLUIKitDelegate sharedAppDelegate] startSDLgame:gameDictionary]; |
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
159 |
[gameDictionary release]; |
3547 | 160 |
} |
161 |
||
162 |
-(void) viewDidLoad { |
|
163 |
CGRect screen = [[UIScreen mainScreen] bounds]; |
|
164 |
self.view.frame = CGRectMake(0, 0, screen.size.height, screen.size.width); |
|
165 |
||
166 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
|
167 |
if (mapConfigViewController == nil) |
|
168 |
mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPad" bundle:nil]; |
|
169 |
if (teamConfigViewController == nil) |
|
170 |
teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStylePlain]; |
|
3623 | 171 |
teamConfigViewController.view.frame = CGRectMake(0, 224, 300, 472); |
3547 | 172 |
teamConfigViewController.view.backgroundColor = [UIColor clearColor]; |
173 |
[mapConfigViewController.view addSubview:teamConfigViewController.view]; |
|
174 |
if (schemeWeaponConfigViewController == nil) |
|
175 |
schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
3623 | 176 |
schemeWeaponConfigViewController.view.frame = CGRectMake(362, 224, 300, 472); |
3547 | 177 |
[mapConfigViewController.view addSubview:schemeWeaponConfigViewController.view]; |
178 |
for (UIView *oneView in self.view.subviews) { |
|
179 |
if ([oneView isMemberOfClass:[UIToolbar class]]) { |
|
180 |
[[oneView viewWithTag:12345] setHidden:YES]; |
|
181 |
break; |
|
182 |
} |
|
183 |
} |
|
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
184 |
} else { |
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
185 |
// this is the visible controller |
3547 | 186 |
mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil]; |
3616
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
187 |
// this must be loaded to auto set default scheme and ammo |
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
188 |
schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
85d69ddb41b6
tackling the iphoneos todo (another trial for the beginning sporadic bug, remove curl animation, don't need to go to detail page)
koda
parents:
3548
diff
changeset
|
189 |
} |
3547 | 190 |
activeController = mapConfigViewController; |
191 |
||
192 |
[self.view addSubview:mapConfigViewController.view]; |
|
193 |
||
194 |
[super viewDidLoad]; |
|
195 |
} |
|
196 |
||
197 |
-(void) viewWillAppear:(BOOL)animated { |
|
198 |
[mapConfigViewController viewWillAppear:animated]; |
|
199 |
[teamConfigViewController viewWillAppear:animated]; |
|
200 |
[schemeWeaponConfigViewController viewWillAppear:animated]; |
|
201 |
// ADD other controllers here |
|
202 |
||
203 |
[super viewWillAppear:animated]; |
|
204 |
} |
|
205 |
||
206 |
-(void) viewDidAppear:(BOOL)animated { |
|
207 |
[mapConfigViewController viewDidAppear:animated]; |
|
208 |
[teamConfigViewController viewDidAppear:animated]; |
|
209 |
[schemeWeaponConfigViewController viewDidAppear:animated]; |
|
210 |
[super viewDidAppear:animated]; |
|
211 |
} |
|
212 |
||
213 |
-(void) didReceiveMemoryWarning { |
|
214 |
// Releases the view if it doesn't have a superview. |
|
215 |
[super didReceiveMemoryWarning]; |
|
216 |
// Release any cached data, images, etc that aren't in use. |
|
217 |
if (mapConfigViewController.view.superview == nil) |
|
218 |
mapConfigViewController = nil; |
|
219 |
if (teamConfigViewController.view.superview == nil) |
|
220 |
teamConfigViewController = nil; |
|
221 |
if (schemeWeaponConfigViewController.view.superview == nil) |
|
222 |
schemeWeaponConfigViewController = nil; |
|
223 |
activeController = nil; |
|
224 |
MSG_MEMCLEAN(); |
|
225 |
} |
|
226 |
||
227 |
-(void) viewDidUnload { |
|
228 |
activeController = nil; |
|
229 |
mapConfigViewController = nil; |
|
230 |
teamConfigViewController = nil; |
|
231 |
schemeWeaponConfigViewController = nil; |
|
232 |
[super viewDidUnload]; |
|
233 |
MSG_DIDUNLOAD(); |
|
234 |
} |
|
235 |
||
236 |
-(void) dealloc { |
|
237 |
[activeController release]; |
|
238 |
[mapConfigViewController release]; |
|
239 |
[teamConfigViewController release]; |
|
240 |
[schemeWeaponConfigViewController release]; |
|
241 |
[super dealloc]; |
|
242 |
} |
|
243 |
||
244 |
@end |