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