author | koda |
Mon, 30 Aug 2010 06:33:56 +0200 | |
changeset 3792 | dd9345e74b66 |
parent 3791 | 98072b3871c1 |
child 3829 | 81db3c85784b |
permissions | -rw-r--r-- |
3547 | 1 |
// |
2 |
// MapConfigViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 22/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "MapConfigViewController.h" |
|
10 |
#import "PascalImports.h" |
|
11 |
#import "CommodityFunctions.h" |
|
12 |
#import "UIImageExtra.h" |
|
13 |
#import "SDL_net.h" |
|
14 |
#import <pthread.h> |
|
15 |
||
16 |
#define INDICATOR_TAG 7654 |
|
17 |
||
18 |
@implementation MapConfigViewController |
|
3642 | 19 |
@synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand, |
3705 | 20 |
tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, themeArray, mapArray, busy, delegate; |
3547 | 21 |
|
22 |
||
23 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
24 |
return rotationManager(interfaceOrientation); |
|
25 |
} |
|
26 |
||
27 |
#pragma mark - |
|
28 |
#pragma mark Preview Handling |
|
29 |
-(int) sendToEngine: (NSString *)string { |
|
30 |
unsigned char length = [string length]; |
|
3697 | 31 |
|
3547 | 32 |
SDLNet_TCP_Send(csd, &length , 1); |
33 |
return SDLNet_TCP_Send(csd, [string UTF8String], length); |
|
34 |
} |
|
35 |
||
36 |
-(const uint8_t *)engineProtocol:(NSInteger) port { |
|
37 |
IPaddress ip; |
|
38 |
BOOL serverQuit = NO; |
|
39 |
static uint8_t map[128*32]; |
|
3697 | 40 |
|
3547 | 41 |
if (SDLNet_Init() < 0) { |
3660 | 42 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
3547 | 43 |
serverQuit = YES; |
44 |
} |
|
3697 | 45 |
|
3547 | 46 |
// Resolving the host using NULL make network interface to listen |
47 |
if (SDLNet_ResolveHost(&ip, NULL, port) < 0) { |
|
3660 | 48 |
DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
3547 | 49 |
serverQuit = YES; |
50 |
} |
|
3697 | 51 |
|
3547 | 52 |
// Open a connection with the IP provided (listen on the host's port) |
53 |
if (!(sd = SDLNet_TCP_Open(&ip))) { |
|
3660 | 54 |
DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port); |
3547 | 55 |
serverQuit = YES; |
56 |
} |
|
3697 | 57 |
|
3547 | 58 |
// launch the preview here so that we're sure the tcp channel is open |
59 |
pthread_t thread_id; |
|
60 |
pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port); |
|
61 |
pthread_detach(thread_id); |
|
3697 | 62 |
|
3547 | 63 |
DLog(@"Waiting for a client on port %d", port); |
64 |
while (!serverQuit) { |
|
65 |
/* This check the sd if there is a pending connection. |
|
66 |
* If there is one, accept that, and open a new socket for communicating */ |
|
67 |
csd = SDLNet_TCP_Accept(sd); |
|
3697 | 68 |
if (NULL != csd) { |
3547 | 69 |
DLog(@"Client found"); |
3697 | 70 |
|
3547 | 71 |
[self sendToEngine:self.seedCommand]; |
72 |
[self sendToEngine:self.templateFilterCommand]; |
|
73 |
[self sendToEngine:self.mapGenCommand]; |
|
74 |
[self sendToEngine:self.mazeSizeCommand]; |
|
75 |
[self sendToEngine:@"!"]; |
|
3697 | 76 |
|
3547 | 77 |
memset(map, 0, 128*32); |
78 |
SDLNet_TCP_Recv(csd, map, 128*32); |
|
79 |
SDLNet_TCP_Recv(csd, &maxHogs, sizeof(uint8_t)); |
|
80 |
||
81 |
SDLNet_TCP_Close(csd); |
|
82 |
serverQuit = YES; |
|
83 |
} |
|
84 |
} |
|
3697 | 85 |
|
3547 | 86 |
SDLNet_TCP_Close(sd); |
87 |
SDLNet_Quit(); |
|
88 |
return map; |
|
89 |
} |
|
90 |
||
91 |
-(void) drawingThread { |
|
92 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3697 | 93 |
|
3547 | 94 |
// select the port for IPC and launch the preview generation through engineProtocol: |
95 |
int port = randomPort(); |
|
96 |
const uint8_t *map = [self engineProtocol:port]; |
|
97 |
uint8_t mapExp[128*32*8]; |
|
98 |
||
99 |
// draw the buffer (1 pixel per component, 0= transparent 1= color) |
|
100 |
int k = 0; |
|
101 |
for (int i = 0; i < 32*128; i++) { |
|
102 |
unsigned char byte = map[i]; |
|
103 |
for (int j = 0; j < 8; j++) { |
|
104 |
// select the color based on the leftmost bit |
|
105 |
if ((byte & 0x80) != 0) |
|
106 |
mapExp[k] = 100; |
|
107 |
else |
|
108 |
mapExp[k] = 255; |
|
109 |
// shift to next bit |
|
110 |
byte <<= 1; |
|
111 |
k++; |
|
112 |
} |
|
113 |
} |
|
114 |
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray(); |
|
115 |
CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 256, 128, 8, 256, colorspace, kCGImageAlphaNone); |
|
116 |
CGColorSpaceRelease(colorspace); |
|
3697 | 117 |
|
3547 | 118 |
CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage); |
3598 | 119 |
CGContextRelease(bitmapImage); |
3547 | 120 |
UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage]; |
121 |
CGImageRelease(previewCGImage); |
|
122 |
previewCGImage = nil; |
|
123 |
||
124 |
// set the preview image (autoreleased) in the button and the maxhog label on the main thread to prevent a leak |
|
125 |
[self performSelectorOnMainThread:@selector(setButtonImage:) withObject:[previewImage makeRoundCornersOfSize:CGSizeMake(12, 12)] waitUntilDone:NO]; |
|
126 |
[previewImage release]; |
|
127 |
[self performSelectorOnMainThread:@selector(setLabelText:) withObject:[NSString stringWithFormat:@"%d", maxHogs] waitUntilDone:NO]; |
|
3697 | 128 |
|
3547 | 129 |
// restore functionality of button and remove the spinning wheel on the main thread to prevent a leak |
130 |
[self performSelectorOnMainThread:@selector(turnOnWidgets) withObject:nil waitUntilDone:NO]; |
|
3697 | 131 |
|
3547 | 132 |
[pool release]; |
133 |
//Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution. |
|
134 |
//[NSThread exit]; |
|
135 |
||
136 |
/* |
|
137 |
// http://developer.apple.com/mac/library/qa/qa2001/qa1037.html |
|
3697 | 138 |
UIGraphicsBeginImageContext(CGSizeMake(256,128)); |
139 |
CGContextRef context = UIGraphicsGetCurrentContext(); |
|
140 |
UIGraphicsPushContext(context); |
|
3547 | 141 |
|
142 |
CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0); |
|
143 |
CGContextFillRect(context,CGRectMake(xc,yc,1,1)); |
|
3697 | 144 |
|
3547 | 145 |
UIGraphicsPopContext(); |
146 |
UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext(); |
|
147 |
UIGraphicsEndImageContext(); |
|
148 |
*/ |
|
149 |
} |
|
150 |
||
3783 | 151 |
-(IBAction) mapButtonPressed { |
152 |
playSound(@"clickSound"); |
|
153 |
[self updatePreview]; |
|
154 |
} |
|
155 |
||
156 |
-(void) updatePreview { |
|
3547 | 157 |
// don't generate a new preview while it's already generating one |
158 |
if (busy) |
|
159 |
return; |
|
3697 | 160 |
|
3547 | 161 |
// generate a seed |
162 |
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); |
|
163 |
NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid); |
|
164 |
CFRelease(uuid); |
|
165 |
NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
|
166 |
[seed release]; |
|
167 |
self.seedCommand = seedCmd; |
|
168 |
[seedCmd release]; |
|
3697 | 169 |
|
3547 | 170 |
NSIndexPath *theIndex; |
171 |
if (segmentedControl.selectedSegmentIndex != 1) { |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
172 |
// remove the current preview and title |
3547 | 173 |
[self.previewButton setImage:nil forState:UIControlStateNormal]; |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
174 |
[self.previewButton setTitle:nil forState:UIControlStateNormal]; |
3697 | 175 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
176 |
// don't display preview on slower device, too slow and memory hog |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
177 |
NSString *modelId = modelType(); |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
178 |
if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) { |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
179 |
busy = NO; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
180 |
[self.previewButton setTitle:NSLocalizedString(@"Preview not available",@"") forState:UIControlStateNormal]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
181 |
} else { |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
182 |
// prevent other events and add an activity while the preview is beign generated |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
183 |
[self turnOffWidgets]; |
3697 | 184 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
185 |
// add a very nice spinning wheel |
3697 | 186 |
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
187 |
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
188 |
indicator.center = CGPointMake(previewButton.bounds.size.width / 2, previewButton.bounds.size.height / 2); |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
189 |
indicator.tag = INDICATOR_TAG; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
190 |
[indicator startAnimating]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
191 |
[self.previewButton addSubview:indicator]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
192 |
[indicator release]; |
3697 | 193 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
194 |
// let's draw in a separate thread so the gui can work; at the end it restore other widgets |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
195 |
[NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
196 |
} |
3697 | 197 |
|
3547 | 198 |
theIndex = [NSIndexPath indexPathForRow:(random()%[self.themeArray count]) inSection:0]; |
199 |
} else { |
|
200 |
theIndex = [NSIndexPath indexPathForRow:(random()%[self.mapArray count]) inSection:0]; |
|
201 |
} |
|
202 |
[self.tableView reloadData]; |
|
203 |
[self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; |
|
204 |
[self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionNone animated:YES]; |
|
205 |
} |
|
206 |
||
3642 | 207 |
// instead of drawing a random map we load an image; this function is called by didSelectRowAtIndexPath only |
3547 | 208 |
-(void) updatePreviewWithMap:(NSInteger) index { |
209 |
// change the preview button |
|
210 |
NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]]; |
|
211 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage]; |
|
212 |
[fileImage release]; |
|
213 |
[self.previewButton setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal]; |
|
214 |
[image release]; |
|
3697 | 215 |
|
3547 | 216 |
// update label |
217 |
maxHogs = 18; |
|
218 |
NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]]; |
|
219 |
NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
|
220 |
[fileCfg release]; |
|
221 |
NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
|
3642 | 222 |
[contents release]; |
3547 | 223 |
|
3642 | 224 |
// set the theme and map here |
225 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", [split objectAtIndex:0]]; |
|
226 |
self.staticMapCommand = [NSString stringWithFormat:@"emap %@", [self.mapArray objectAtIndex:index]]; |
|
3697 | 227 |
|
228 |
// if the number is not set we keep 18 standard; |
|
3547 | 229 |
// sometimes it's not set but there are trailing characters, we get around them with the second equation |
230 |
if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0) |
|
231 |
maxHogs = [[split objectAtIndex:1] intValue]; |
|
232 |
NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs]; |
|
233 |
self.maxLabel.text = max; |
|
234 |
[max release]; |
|
235 |
} |
|
236 |
||
237 |
-(void) turnOffWidgets { |
|
238 |
busy = YES; |
|
239 |
self.previewButton.alpha = 0.5f; |
|
240 |
self.previewButton.enabled = NO; |
|
241 |
self.maxLabel.text = @"..."; |
|
242 |
self.segmentedControl.enabled = NO; |
|
243 |
self.slider.enabled = NO; |
|
244 |
} |
|
245 |
||
246 |
-(void) turnOnWidgets { |
|
247 |
self.previewButton.alpha = 1.0f; |
|
248 |
self.previewButton.enabled = YES; |
|
249 |
self.segmentedControl.enabled = YES; |
|
250 |
self.slider.enabled = YES; |
|
251 |
busy = NO; |
|
3697 | 252 |
|
3547 | 253 |
UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG]; |
254 |
if (indicator) { |
|
255 |
[indicator stopAnimating]; |
|
256 |
[indicator removeFromSuperview]; |
|
257 |
} |
|
258 |
} |
|
3697 | 259 |
|
3547 | 260 |
-(void) setLabelText:(NSString *)str { |
261 |
self.maxLabel.text = str; |
|
262 |
} |
|
263 |
||
264 |
-(void) setButtonImage:(UIImage *)img { |
|
265 |
[self.previewButton setBackgroundImage:img forState:UIControlStateNormal]; |
|
266 |
} |
|
267 |
||
268 |
-(void) restoreBackgroundImage { |
|
269 |
// white rounded rectangle as background image for previewButton |
|
3697 | 270 |
UIGraphicsBeginImageContext(CGSizeMake(256,128)); |
271 |
CGContextRef context = UIGraphicsGetCurrentContext(); |
|
272 |
UIGraphicsPushContext(context); |
|
3547 | 273 |
|
274 |
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); |
|
275 |
CGContextFillRect(context,CGRectMake(0,0,256,128)); |
|
3697 | 276 |
|
3547 | 277 |
UIGraphicsPopContext(); |
278 |
UIImage *bkgImg = UIGraphicsGetImageFromCurrentImageContext(); |
|
279 |
UIGraphicsEndImageContext(); |
|
280 |
[self.previewButton setBackgroundImage:[bkgImg makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal]; |
|
281 |
} |
|
282 |
||
283 |
#pragma mark - |
|
284 |
#pragma mark Table view data source |
|
285 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
286 |
return 1; |
|
287 |
} |
|
288 |
||
289 |
-(NSInteger) tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger) section { |
|
290 |
if (self.segmentedControl.selectedSegmentIndex != 1) |
|
291 |
return [themeArray count]; |
|
292 |
else |
|
293 |
return [mapArray count]; |
|
294 |
} |
|
295 |
||
296 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
297 |
static NSString *CellIdentifier = @"Cell"; |
|
298 |
NSInteger row = [indexPath row]; |
|
3697 | 299 |
|
3547 | 300 |
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 301 |
if (cell == nil) |
3547 | 302 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 303 |
|
3792 | 304 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
305 |
cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
3697 | 306 |
|
3547 | 307 |
if (self.segmentedControl.selectedSegmentIndex != 1) { |
308 |
// the % prevents a strange bug that occurs sporadically |
|
309 |
NSString *themeName = [self.themeArray objectAtIndex:row % [self.themeArray count]]; |
|
310 |
cell.textLabel.text = themeName; |
|
311 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]]; |
|
312 |
cell.imageView.image = image; |
|
313 |
[image release]; |
|
314 |
} else { |
|
315 |
cell.textLabel.text = [self.mapArray objectAtIndex:row]; |
|
316 |
cell.imageView.image = nil; |
|
317 |
} |
|
3697 | 318 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
319 |
if (row == [self.lastIndexPath row]) { |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
320 |
UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
321 |
cell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
322 |
[checkbox release]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
323 |
} else |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
324 |
cell.accessoryView = nil; |
3547 | 325 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
326 |
cell.backgroundColor = [UIColor blackColor]; |
3547 | 327 |
return cell; |
328 |
} |
|
329 |
||
330 |
||
331 |
#pragma mark - |
|
332 |
#pragma mark Table view delegate |
|
333 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
334 |
int newRow = [indexPath row]; |
|
335 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 336 |
|
3547 | 337 |
if (newRow != oldRow) { |
338 |
if (self.segmentedControl.selectedSegmentIndex != 1) { |
|
3697 | 339 |
NSString *theme = [self.themeArray objectAtIndex:newRow]; |
3642 | 340 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", theme]; |
341 |
} else { |
|
342 |
// theme and map are set in the function below |
|
3547 | 343 |
[self updatePreviewWithMap:newRow]; |
3642 | 344 |
} |
3697 | 345 |
|
346 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
347 |
UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
348 |
newCell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
349 |
[checkbox release]; |
3547 | 350 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:self.lastIndexPath]; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
351 |
oldCell.accessoryView = nil; |
3547 | 352 |
|
353 |
self.lastIndexPath = indexPath; |
|
354 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
355 |
} |
|
356 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
357 |
} |
|
358 |
||
359 |
#pragma mark - |
|
360 |
#pragma mark slider & segmentedControl |
|
361 |
// this updates the label and the command keys when the slider is moved, depending of the selection in segmentedControl |
|
362 |
// no methods are called by this routine and you can pass nil to it |
|
363 |
-(IBAction) sliderChanged:(id) sender { |
|
364 |
NSString *labelText; |
|
365 |
NSString *templateCommand; |
|
366 |
NSString *mazeCommand; |
|
3697 | 367 |
|
3547 | 368 |
switch ((int)(self.slider.value*100)) { |
369 |
case 0: |
|
370 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
371 |
labelText = NSLocalizedString(@"Wacky",@""); |
|
372 |
} else { |
|
373 |
labelText = NSLocalizedString(@"Large Floating Islands",@""); |
|
374 |
} |
|
375 |
templateCommand = @"e$template_filter 5"; |
|
376 |
mazeCommand = @"e$maze_size 5"; |
|
377 |
break; |
|
378 |
case 1: |
|
379 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
380 |
labelText = NSLocalizedString(@"Cavern",@""); |
|
381 |
} else { |
|
382 |
labelText = NSLocalizedString(@"Medium Floating Islands",@""); |
|
383 |
} |
|
384 |
templateCommand = @"e$template_filter 4"; |
|
385 |
mazeCommand = @"e$maze_size 4"; |
|
386 |
break; |
|
387 |
case 2: |
|
388 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
389 |
labelText = NSLocalizedString(@"Small",@""); |
|
390 |
} else { |
|
391 |
labelText = NSLocalizedString(@"Small Floating Islands",@""); |
|
392 |
} |
|
393 |
templateCommand = @"e$template_filter 1"; |
|
394 |
mazeCommand = @"e$maze_size 3"; |
|
395 |
break; |
|
396 |
case 3: |
|
397 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
398 |
labelText = NSLocalizedString(@"Medium",@""); |
|
399 |
} else { |
|
400 |
labelText = NSLocalizedString(@"Large Tunnels",@""); |
|
401 |
} |
|
402 |
templateCommand = @"e$template_filter 2"; |
|
403 |
mazeCommand = @"e$maze_size 2"; |
|
404 |
break; |
|
405 |
case 4: |
|
406 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
407 |
labelText = NSLocalizedString(@"Large",@""); |
|
408 |
} else { |
|
409 |
labelText = NSLocalizedString(@"Medium Tunnels",@""); |
|
410 |
} |
|
411 |
templateCommand = @"e$template_filter 3"; |
|
412 |
mazeCommand = @"e$maze_size 1"; |
|
413 |
break; |
|
414 |
case 5: |
|
415 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
416 |
labelText = NSLocalizedString(@"All",@""); |
|
417 |
} else { |
|
418 |
labelText = NSLocalizedString(@"Small Tunnels",@""); |
|
419 |
} |
|
420 |
templateCommand = @"e$template_filter 0"; |
|
421 |
mazeCommand = @"e$maze_size 0"; |
|
422 |
break; |
|
423 |
default: |
|
424 |
labelText = nil; |
|
425 |
templateCommand = nil; |
|
426 |
mazeCommand = nil; |
|
427 |
break; |
|
428 |
} |
|
3697 | 429 |
|
3547 | 430 |
self.sizeLabel.text = labelText; |
431 |
self.templateFilterCommand = templateCommand; |
|
432 |
self.mazeSizeCommand = mazeCommand; |
|
433 |
} |
|
434 |
||
435 |
// update preview (if not busy and if its value really changed) as soon as the user lifts its finger up |
|
436 |
-(IBAction) sliderEndedChanging:(id) sender { |
|
437 |
int num = (int) (self.slider.value * 100); |
|
438 |
if (oldValue != num) { |
|
439 |
[self updatePreview]; |
|
440 |
oldValue = num; |
|
441 |
} |
|
3783 | 442 |
playSound(@"clickSound"); |
3547 | 443 |
} |
444 |
||
3697 | 445 |
// perform actions based on the activated section, then call updatePreview to visually update the selection |
3547 | 446 |
// updatePreview will call didSelectRowAtIndexPath which will call the right update routine) |
447 |
// and if necessary update the table with a slide animation |
|
448 |
-(IBAction) segmentedControlChanged:(id) sender { |
|
3642 | 449 |
NSString *mapgen, *staticmap; |
3547 | 450 |
NSInteger newPage = self.segmentedControl.selectedSegmentIndex; |
3697 | 451 |
|
3783 | 452 |
playSound(@"selSound"); |
3547 | 453 |
switch (newPage) { |
454 |
case 0: // Random |
|
455 |
mapgen = @"e$mapgen 0"; |
|
3642 | 456 |
staticmap = @""; |
3547 | 457 |
[self sliderChanged:nil]; |
458 |
self.slider.enabled = YES; |
|
459 |
break; |
|
3697 | 460 |
|
3547 | 461 |
case 1: // Map |
462 |
mapgen = @"e$mapgen 0"; |
|
3642 | 463 |
// dummy value, everything is set by -updatePreview -> -didSelectRowAtIndexPath -> -updatePreviewWithMap |
464 |
staticmap = @"map Bamboo"; |
|
3547 | 465 |
self.slider.enabled = NO; |
3791
98072b3871c1
help page for ingame, some other fixes here and there
koda
parents:
3789
diff
changeset
|
466 |
self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
3547 | 467 |
[self restoreBackgroundImage]; |
468 |
break; |
|
3697 | 469 |
|
3547 | 470 |
case 2: // Maze |
471 |
mapgen = @"e$mapgen 1"; |
|
3642 | 472 |
staticmap = @""; |
3547 | 473 |
[self sliderChanged:nil]; |
474 |
self.slider.enabled = YES; |
|
475 |
break; |
|
3697 | 476 |
|
3547 | 477 |
default: |
478 |
mapgen = nil; |
|
3642 | 479 |
staticmap = nil; |
3547 | 480 |
break; |
481 |
} |
|
482 |
self.mapGenCommand = mapgen; |
|
3642 | 483 |
self.staticMapCommand = staticmap; |
3547 | 484 |
[self updatePreview]; |
3697 | 485 |
|
3547 | 486 |
// nice animation for updating the table when appropriate (on iphone) |
487 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) |
|
488 |
if (((oldPage == 0 || oldPage == 2) && newPage == 1) || |
|
489 |
(oldPage == 1 && (newPage == 0 || newPage == 2))) { |
|
490 |
[UIView beginAnimations:@"moving out table" context:NULL]; |
|
491 |
self.tableView.frame = CGRectMake(480, 0, 185, 276); |
|
492 |
[UIView commitAnimations]; |
|
493 |
[self performSelector:@selector(moveTable) withObject:nil afterDelay:0.2]; |
|
494 |
} |
|
495 |
oldPage = newPage; |
|
496 |
} |
|
497 |
||
498 |
// update data when table is not visible and then show it |
|
499 |
-(void) moveTable { |
|
500 |
[self.tableView reloadData]; |
|
3697 | 501 |
|
3547 | 502 |
[UIView beginAnimations:@"moving in table" context:NULL]; |
503 |
self.tableView.frame = CGRectMake(295, 0, 185, 276); |
|
504 |
[UIView commitAnimations]; |
|
505 |
} |
|
506 |
||
507 |
#pragma mark - |
|
508 |
#pragma mark view management |
|
509 |
-(void) viewDidLoad { |
|
510 |
[super viewDidLoad]; |
|
3697 | 511 |
|
3547 | 512 |
srandom(time(NULL)); |
3697 | 513 |
|
3547 | 514 |
CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
515 |
self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
516 |
||
517 |
// themes.cfg contains all the user-selectable themes |
|
518 |
NSString *string = [[NSString alloc] initWithContentsOfFile:[THEMES_DIRECTORY() stringByAppendingString:@"/themes.cfg"] |
|
3697 | 519 |
encoding:NSUTF8StringEncoding |
3547 | 520 |
error:NULL]; |
521 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[string componentsSeparatedByString:@"\n"]]; |
|
522 |
[string release]; |
|
523 |
// remove a trailing "" element |
|
524 |
[array removeLastObject]; |
|
525 |
self.themeArray = array; |
|
526 |
[array release]; |
|
527 |
self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
|
528 |
||
529 |
busy = NO; |
|
3697 | 530 |
|
3547 | 531 |
// draw a white background |
532 |
[self restoreBackgroundImage]; |
|
3697 | 533 |
|
3547 | 534 |
// initialize some "default" values |
535 |
self.sizeLabel.text = NSLocalizedString(@"All",@""); |
|
536 |
self.slider.value = 0.05f; |
|
3697 | 537 |
|
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3783
diff
changeset
|
538 |
// select a map at first because it's faster - done in IB |
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3783
diff
changeset
|
539 |
//self.segmentedControl.selectedSegmentIndex = 1; |
3791
98072b3871c1
help page for ingame, some other fixes here and there
koda
parents:
3789
diff
changeset
|
540 |
if (self.segmentedControl.selectedSegmentIndex == 1) { |
98072b3871c1
help page for ingame, some other fixes here and there
koda
parents:
3789
diff
changeset
|
541 |
self.slider.enabled = NO; |
98072b3871c1
help page for ingame, some other fixes here and there
koda
parents:
3789
diff
changeset
|
542 |
self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
98072b3871c1
help page for ingame, some other fixes here and there
koda
parents:
3789
diff
changeset
|
543 |
} |
3547 | 544 |
|
545 |
self.templateFilterCommand = @"e$template_filter 0"; |
|
546 |
self.mazeSizeCommand = @"e$maze_size 0"; |
|
547 |
self.mapGenCommand = @"e$mapgen 0"; |
|
3642 | 548 |
self.staticMapCommand = @""; |
3697 | 549 |
|
3792 | 550 |
self.lastIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0]; |
3697 | 551 |
|
3547 | 552 |
oldValue = 5; |
553 |
oldPage = 0; |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
554 |
|
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
555 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
556 |
[self.tableView setBackgroundView:nil]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
557 |
self.view.backgroundColor = [UIColor clearColor]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
558 |
self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
559 |
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
560 |
self.tableView.rowHeight = 45; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
561 |
} |
3547 | 562 |
} |
563 |
||
564 |
-(void) viewDidAppear:(BOOL) animated { |
|
565 |
[super viewDidAppear:animated]; |
|
566 |
[self updatePreview]; |
|
567 |
} |
|
568 |
||
569 |
#pragma mark - |
|
3705 | 570 |
#pragma mark delegate functions for iPad |
571 |
-(IBAction) buttonPressed:(id) sender { |
|
572 |
if (self.delegate != nil && [delegate respondsToSelector:@selector(buttonPressed:)]) |
|
573 |
[self.delegate buttonPressed:(UIButton *)sender]; |
|
574 |
} |
|
575 |
||
576 |
#pragma mark - |
|
3547 | 577 |
-(void) didReceiveMemoryWarning { |
578 |
[super didReceiveMemoryWarning]; |
|
3705 | 579 |
//[previewButton setImage:nil forState:UIControlStateNormal]; |
3791
98072b3871c1
help page for ingame, some other fixes here and there
koda
parents:
3789
diff
changeset
|
580 |
MSG_MEMCLEAN(); |
3547 | 581 |
} |
582 |
||
583 |
-(void) viewDidUnload { |
|
3705 | 584 |
self.delegate = nil; |
585 |
||
3547 | 586 |
self.previewButton = nil; |
587 |
self.seedCommand = nil; |
|
588 |
self.templateFilterCommand = nil; |
|
589 |
self.mapGenCommand = nil; |
|
590 |
self.mazeSizeCommand = nil; |
|
591 |
self.themeCommand = nil; |
|
3642 | 592 |
self.staticMapCommand = nil; |
3697 | 593 |
|
3547 | 594 |
self.previewButton = nil; |
595 |
self.tableView = nil; |
|
596 |
self.maxLabel = nil; |
|
597 |
self.sizeLabel = nil; |
|
598 |
self.segmentedControl = nil; |
|
599 |
self.slider = nil; |
|
3697 | 600 |
|
3547 | 601 |
self.lastIndexPath = nil; |
602 |
self.themeArray = nil; |
|
603 |
self.mapArray = nil; |
|
3697 | 604 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
605 |
MSG_DIDUNLOAD(); |
3547 | 606 |
[super viewDidUnload]; |
607 |
} |
|
608 |
||
609 |
-(void) dealloc { |
|
3705 | 610 |
self.delegate = nil; |
611 |
||
3547 | 612 |
[seedCommand release]; |
613 |
[templateFilterCommand release]; |
|
614 |
[mapGenCommand release]; |
|
615 |
[mazeSizeCommand release]; |
|
616 |
[themeCommand release]; |
|
3642 | 617 |
[staticMapCommand release]; |
3697 | 618 |
|
3547 | 619 |
[previewButton release]; |
620 |
[tableView release]; |
|
621 |
[maxLabel release]; |
|
622 |
[sizeLabel release]; |
|
623 |
[segmentedControl release]; |
|
624 |
[slider release]; |
|
3697 | 625 |
|
3547 | 626 |
[lastIndexPath release]; |
627 |
[themeArray release]; |
|
628 |
[mapArray release]; |
|
3697 | 629 |
|
3547 | 630 |
[super dealloc]; |
631 |
} |
|
632 |
||
633 |
||
634 |
@end |