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