author | koda |
Sat, 28 Aug 2010 02:35:26 +0200 | |
changeset 3780 | 7c704e69242e |
parent 3705 | e1959819a542 |
child 3783 | 8e9daf967406 |
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 |
||
151 |
-(IBAction) updatePreview { |
|
152 |
// don't generate a new preview while it's already generating one |
|
153 |
if (busy) |
|
154 |
return; |
|
3697 | 155 |
|
3547 | 156 |
// generate a seed |
157 |
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); |
|
158 |
NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid); |
|
159 |
CFRelease(uuid); |
|
160 |
NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
|
161 |
[seed release]; |
|
162 |
self.seedCommand = seedCmd; |
|
163 |
[seedCmd release]; |
|
3697 | 164 |
|
3547 | 165 |
NSIndexPath *theIndex; |
166 |
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
|
167 |
// remove the current preview and title |
3547 | 168 |
[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
|
169 |
[self.previewButton setTitle:nil forState:UIControlStateNormal]; |
3697 | 170 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
171 |
// 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
|
172 |
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
|
173 |
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
|
174 |
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
|
175 |
[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
|
176 |
} else { |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
177 |
// 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
|
178 |
[self turnOffWidgets]; |
3697 | 179 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
180 |
// add a very nice spinning wheel |
3697 | 181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
[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
|
186 |
[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
|
187 |
[indicator release]; |
3697 | 188 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
189 |
// 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
|
190 |
[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
|
191 |
} |
3697 | 192 |
|
3547 | 193 |
theIndex = [NSIndexPath indexPathForRow:(random()%[self.themeArray count]) inSection:0]; |
194 |
} else { |
|
195 |
theIndex = [NSIndexPath indexPathForRow:(random()%[self.mapArray count]) inSection:0]; |
|
196 |
} |
|
197 |
[self.tableView reloadData]; |
|
198 |
[self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; |
|
199 |
[self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionNone animated:YES]; |
|
200 |
} |
|
201 |
||
3642 | 202 |
// instead of drawing a random map we load an image; this function is called by didSelectRowAtIndexPath only |
3547 | 203 |
-(void) updatePreviewWithMap:(NSInteger) index { |
204 |
// change the preview button |
|
205 |
NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]]; |
|
206 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage]; |
|
207 |
[fileImage release]; |
|
208 |
[self.previewButton setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal]; |
|
209 |
[image release]; |
|
3697 | 210 |
|
3547 | 211 |
// update label |
212 |
maxHogs = 18; |
|
213 |
NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]]; |
|
214 |
NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
|
215 |
[fileCfg release]; |
|
216 |
NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
|
3642 | 217 |
[contents release]; |
3547 | 218 |
|
3642 | 219 |
// set the theme and map here |
220 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", [split objectAtIndex:0]]; |
|
221 |
self.staticMapCommand = [NSString stringWithFormat:@"emap %@", [self.mapArray objectAtIndex:index]]; |
|
3697 | 222 |
|
223 |
// if the number is not set we keep 18 standard; |
|
3547 | 224 |
// sometimes it's not set but there are trailing characters, we get around them with the second equation |
225 |
if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0) |
|
226 |
maxHogs = [[split objectAtIndex:1] intValue]; |
|
227 |
NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs]; |
|
228 |
self.maxLabel.text = max; |
|
229 |
[max release]; |
|
230 |
} |
|
231 |
||
232 |
-(void) turnOffWidgets { |
|
233 |
busy = YES; |
|
234 |
self.previewButton.alpha = 0.5f; |
|
235 |
self.previewButton.enabled = NO; |
|
236 |
self.maxLabel.text = @"..."; |
|
237 |
self.segmentedControl.enabled = NO; |
|
238 |
self.slider.enabled = NO; |
|
239 |
} |
|
240 |
||
241 |
-(void) turnOnWidgets { |
|
242 |
self.previewButton.alpha = 1.0f; |
|
243 |
self.previewButton.enabled = YES; |
|
244 |
self.segmentedControl.enabled = YES; |
|
245 |
self.slider.enabled = YES; |
|
246 |
busy = NO; |
|
3697 | 247 |
|
3547 | 248 |
UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG]; |
249 |
if (indicator) { |
|
250 |
[indicator stopAnimating]; |
|
251 |
[indicator removeFromSuperview]; |
|
252 |
} |
|
253 |
} |
|
3697 | 254 |
|
3547 | 255 |
-(void) setLabelText:(NSString *)str { |
256 |
self.maxLabel.text = str; |
|
257 |
} |
|
258 |
||
259 |
-(void) setButtonImage:(UIImage *)img { |
|
260 |
[self.previewButton setBackgroundImage:img forState:UIControlStateNormal]; |
|
261 |
} |
|
262 |
||
263 |
-(void) restoreBackgroundImage { |
|
264 |
// white rounded rectangle as background image for previewButton |
|
3697 | 265 |
UIGraphicsBeginImageContext(CGSizeMake(256,128)); |
266 |
CGContextRef context = UIGraphicsGetCurrentContext(); |
|
267 |
UIGraphicsPushContext(context); |
|
3547 | 268 |
|
269 |
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); |
|
270 |
CGContextFillRect(context,CGRectMake(0,0,256,128)); |
|
3697 | 271 |
|
3547 | 272 |
UIGraphicsPopContext(); |
273 |
UIImage *bkgImg = UIGraphicsGetImageFromCurrentImageContext(); |
|
274 |
UIGraphicsEndImageContext(); |
|
275 |
[self.previewButton setBackgroundImage:[bkgImg makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal]; |
|
276 |
} |
|
277 |
||
278 |
#pragma mark - |
|
279 |
#pragma mark Table view data source |
|
280 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
281 |
return 1; |
|
282 |
} |
|
283 |
||
284 |
-(NSInteger) tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger) section { |
|
285 |
if (self.segmentedControl.selectedSegmentIndex != 1) |
|
286 |
return [themeArray count]; |
|
287 |
else |
|
288 |
return [mapArray count]; |
|
289 |
} |
|
290 |
||
291 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
292 |
static NSString *CellIdentifier = @"Cell"; |
|
293 |
NSInteger row = [indexPath row]; |
|
3697 | 294 |
|
3547 | 295 |
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 296 |
if (cell == nil) |
3547 | 297 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 298 |
|
3623 | 299 |
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
|
300 |
cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
3623 | 301 |
} |
3697 | 302 |
|
3547 | 303 |
if (self.segmentedControl.selectedSegmentIndex != 1) { |
304 |
// the % prevents a strange bug that occurs sporadically |
|
305 |
NSString *themeName = [self.themeArray objectAtIndex:row % [self.themeArray count]]; |
|
306 |
cell.textLabel.text = themeName; |
|
307 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]]; |
|
308 |
cell.imageView.image = image; |
|
309 |
[image release]; |
|
310 |
} else { |
|
311 |
cell.textLabel.text = [self.mapArray objectAtIndex:row]; |
|
312 |
cell.imageView.image = nil; |
|
313 |
} |
|
3697 | 314 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
315 |
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
|
316 |
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
|
317 |
cell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
318 |
[checkbox release]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
319 |
} else |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
320 |
cell.accessoryView = nil; |
3547 | 321 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
322 |
cell.backgroundColor = [UIColor blackColor]; |
3547 | 323 |
return cell; |
324 |
} |
|
325 |
||
326 |
||
327 |
#pragma mark - |
|
328 |
#pragma mark Table view delegate |
|
329 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
330 |
int newRow = [indexPath row]; |
|
331 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 332 |
|
3547 | 333 |
if (newRow != oldRow) { |
334 |
if (self.segmentedControl.selectedSegmentIndex != 1) { |
|
3697 | 335 |
NSString *theme = [self.themeArray objectAtIndex:newRow]; |
3642 | 336 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", theme]; |
337 |
} else { |
|
338 |
// theme and map are set in the function below |
|
3547 | 339 |
[self updatePreviewWithMap:newRow]; |
3642 | 340 |
} |
3697 | 341 |
|
342 |
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
|
343 |
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
|
344 |
newCell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
345 |
[checkbox release]; |
3547 | 346 |
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
|
347 |
oldCell.accessoryView = nil; |
3547 | 348 |
|
349 |
self.lastIndexPath = indexPath; |
|
350 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
351 |
} |
|
352 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
353 |
} |
|
354 |
||
355 |
#pragma mark - |
|
356 |
#pragma mark slider & segmentedControl |
|
357 |
// this updates the label and the command keys when the slider is moved, depending of the selection in segmentedControl |
|
358 |
// no methods are called by this routine and you can pass nil to it |
|
359 |
-(IBAction) sliderChanged:(id) sender { |
|
360 |
NSString *labelText; |
|
361 |
NSString *templateCommand; |
|
362 |
NSString *mazeCommand; |
|
3697 | 363 |
|
3547 | 364 |
switch ((int)(self.slider.value*100)) { |
365 |
case 0: |
|
366 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
367 |
labelText = NSLocalizedString(@"Wacky",@""); |
|
368 |
} else { |
|
369 |
labelText = NSLocalizedString(@"Large Floating Islands",@""); |
|
370 |
} |
|
371 |
templateCommand = @"e$template_filter 5"; |
|
372 |
mazeCommand = @"e$maze_size 5"; |
|
373 |
break; |
|
374 |
case 1: |
|
375 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
376 |
labelText = NSLocalizedString(@"Cavern",@""); |
|
377 |
} else { |
|
378 |
labelText = NSLocalizedString(@"Medium Floating Islands",@""); |
|
379 |
} |
|
380 |
templateCommand = @"e$template_filter 4"; |
|
381 |
mazeCommand = @"e$maze_size 4"; |
|
382 |
break; |
|
383 |
case 2: |
|
384 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
385 |
labelText = NSLocalizedString(@"Small",@""); |
|
386 |
} else { |
|
387 |
labelText = NSLocalizedString(@"Small Floating Islands",@""); |
|
388 |
} |
|
389 |
templateCommand = @"e$template_filter 1"; |
|
390 |
mazeCommand = @"e$maze_size 3"; |
|
391 |
break; |
|
392 |
case 3: |
|
393 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
394 |
labelText = NSLocalizedString(@"Medium",@""); |
|
395 |
} else { |
|
396 |
labelText = NSLocalizedString(@"Large Tunnels",@""); |
|
397 |
} |
|
398 |
templateCommand = @"e$template_filter 2"; |
|
399 |
mazeCommand = @"e$maze_size 2"; |
|
400 |
break; |
|
401 |
case 4: |
|
402 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
403 |
labelText = NSLocalizedString(@"Large",@""); |
|
404 |
} else { |
|
405 |
labelText = NSLocalizedString(@"Medium Tunnels",@""); |
|
406 |
} |
|
407 |
templateCommand = @"e$template_filter 3"; |
|
408 |
mazeCommand = @"e$maze_size 1"; |
|
409 |
break; |
|
410 |
case 5: |
|
411 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
412 |
labelText = NSLocalizedString(@"All",@""); |
|
413 |
} else { |
|
414 |
labelText = NSLocalizedString(@"Small Tunnels",@""); |
|
415 |
} |
|
416 |
templateCommand = @"e$template_filter 0"; |
|
417 |
mazeCommand = @"e$maze_size 0"; |
|
418 |
break; |
|
419 |
default: |
|
420 |
labelText = nil; |
|
421 |
templateCommand = nil; |
|
422 |
mazeCommand = nil; |
|
423 |
break; |
|
424 |
} |
|
3697 | 425 |
|
3547 | 426 |
self.sizeLabel.text = labelText; |
427 |
self.templateFilterCommand = templateCommand; |
|
428 |
self.mazeSizeCommand = mazeCommand; |
|
429 |
} |
|
430 |
||
431 |
// update preview (if not busy and if its value really changed) as soon as the user lifts its finger up |
|
432 |
-(IBAction) sliderEndedChanging:(id) sender { |
|
433 |
int num = (int) (self.slider.value * 100); |
|
434 |
if (oldValue != num) { |
|
435 |
[self updatePreview]; |
|
436 |
oldValue = num; |
|
437 |
} |
|
438 |
} |
|
439 |
||
3697 | 440 |
// perform actions based on the activated section, then call updatePreview to visually update the selection |
3547 | 441 |
// updatePreview will call didSelectRowAtIndexPath which will call the right update routine) |
442 |
// and if necessary update the table with a slide animation |
|
443 |
-(IBAction) segmentedControlChanged:(id) sender { |
|
3642 | 444 |
NSString *mapgen, *staticmap; |
3547 | 445 |
NSInteger newPage = self.segmentedControl.selectedSegmentIndex; |
3697 | 446 |
|
3547 | 447 |
switch (newPage) { |
448 |
case 0: // Random |
|
449 |
mapgen = @"e$mapgen 0"; |
|
3642 | 450 |
staticmap = @""; |
3547 | 451 |
[self sliderChanged:nil]; |
452 |
self.slider.enabled = YES; |
|
453 |
break; |
|
3697 | 454 |
|
3547 | 455 |
case 1: // Map |
456 |
mapgen = @"e$mapgen 0"; |
|
3642 | 457 |
// dummy value, everything is set by -updatePreview -> -didSelectRowAtIndexPath -> -updatePreviewWithMap |
458 |
staticmap = @"map Bamboo"; |
|
3547 | 459 |
self.slider.enabled = NO; |
3705 | 460 |
self.sizeLabel.text = @""; |
3547 | 461 |
[self restoreBackgroundImage]; |
462 |
break; |
|
3697 | 463 |
|
3547 | 464 |
case 2: // Maze |
465 |
mapgen = @"e$mapgen 1"; |
|
3642 | 466 |
staticmap = @""; |
3547 | 467 |
[self sliderChanged:nil]; |
468 |
self.slider.enabled = YES; |
|
469 |
break; |
|
3697 | 470 |
|
3547 | 471 |
default: |
472 |
mapgen = nil; |
|
3642 | 473 |
staticmap = nil; |
3547 | 474 |
break; |
475 |
} |
|
476 |
self.mapGenCommand = mapgen; |
|
3642 | 477 |
self.staticMapCommand = staticmap; |
3547 | 478 |
[self updatePreview]; |
3697 | 479 |
|
3547 | 480 |
// nice animation for updating the table when appropriate (on iphone) |
481 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) |
|
482 |
if (((oldPage == 0 || oldPage == 2) && newPage == 1) || |
|
483 |
(oldPage == 1 && (newPage == 0 || newPage == 2))) { |
|
484 |
[UIView beginAnimations:@"moving out table" context:NULL]; |
|
485 |
self.tableView.frame = CGRectMake(480, 0, 185, 276); |
|
486 |
[UIView commitAnimations]; |
|
487 |
[self performSelector:@selector(moveTable) withObject:nil afterDelay:0.2]; |
|
488 |
} |
|
489 |
oldPage = newPage; |
|
490 |
} |
|
491 |
||
492 |
// update data when table is not visible and then show it |
|
493 |
-(void) moveTable { |
|
494 |
[self.tableView reloadData]; |
|
3697 | 495 |
|
3547 | 496 |
[UIView beginAnimations:@"moving in table" context:NULL]; |
497 |
self.tableView.frame = CGRectMake(295, 0, 185, 276); |
|
498 |
[UIView commitAnimations]; |
|
499 |
} |
|
500 |
||
501 |
#pragma mark - |
|
502 |
#pragma mark view management |
|
503 |
-(void) viewDidLoad { |
|
504 |
[super viewDidLoad]; |
|
3697 | 505 |
|
3547 | 506 |
srandom(time(NULL)); |
3697 | 507 |
|
3547 | 508 |
CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
509 |
self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
510 |
||
511 |
// themes.cfg contains all the user-selectable themes |
|
512 |
NSString *string = [[NSString alloc] initWithContentsOfFile:[THEMES_DIRECTORY() stringByAppendingString:@"/themes.cfg"] |
|
3697 | 513 |
encoding:NSUTF8StringEncoding |
3547 | 514 |
error:NULL]; |
515 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[string componentsSeparatedByString:@"\n"]]; |
|
516 |
[string release]; |
|
517 |
// remove a trailing "" element |
|
518 |
[array removeLastObject]; |
|
519 |
self.themeArray = array; |
|
520 |
[array release]; |
|
521 |
self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
|
522 |
||
523 |
busy = NO; |
|
3697 | 524 |
|
3547 | 525 |
// draw a white background |
526 |
[self restoreBackgroundImage]; |
|
3697 | 527 |
|
3547 | 528 |
// initialize some "default" values |
529 |
self.sizeLabel.text = NSLocalizedString(@"All",@""); |
|
530 |
self.slider.value = 0.05f; |
|
3697 | 531 |
|
3780
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3705
diff
changeset
|
532 |
// select a map at first because it's faster |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3705
diff
changeset
|
533 |
self.segmentedControl.selectedSegmentIndex = 1; |
3547 | 534 |
|
535 |
self.templateFilterCommand = @"e$template_filter 0"; |
|
536 |
self.mazeSizeCommand = @"e$maze_size 0"; |
|
537 |
self.mapGenCommand = @"e$mapgen 0"; |
|
3642 | 538 |
self.staticMapCommand = @""; |
3697 | 539 |
|
3547 | 540 |
self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; |
3697 | 541 |
|
3547 | 542 |
oldValue = 5; |
543 |
oldPage = 0; |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
544 |
|
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
545 |
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
|
546 |
[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
|
547 |
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
|
548 |
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
|
549 |
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
|
550 |
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
|
551 |
} |
3547 | 552 |
} |
553 |
||
554 |
-(void) viewDidAppear:(BOOL) animated { |
|
555 |
[super viewDidAppear:animated]; |
|
556 |
[self updatePreview]; |
|
557 |
} |
|
558 |
||
559 |
#pragma mark - |
|
3705 | 560 |
#pragma mark delegate functions for iPad |
561 |
-(IBAction) buttonPressed:(id) sender { |
|
562 |
if (self.delegate != nil && [delegate respondsToSelector:@selector(buttonPressed:)]) |
|
563 |
[self.delegate buttonPressed:(UIButton *)sender]; |
|
564 |
} |
|
565 |
||
566 |
#pragma mark - |
|
3547 | 567 |
-(void) didReceiveMemoryWarning { |
568 |
[super didReceiveMemoryWarning]; |
|
3705 | 569 |
//[previewButton setImage:nil forState:UIControlStateNormal]; |
3547 | 570 |
} |
571 |
||
572 |
-(void) viewDidUnload { |
|
3705 | 573 |
self.delegate = nil; |
574 |
||
3547 | 575 |
self.previewButton = nil; |
576 |
self.seedCommand = nil; |
|
577 |
self.templateFilterCommand = nil; |
|
578 |
self.mapGenCommand = nil; |
|
579 |
self.mazeSizeCommand = nil; |
|
580 |
self.themeCommand = nil; |
|
3642 | 581 |
self.staticMapCommand = nil; |
3697 | 582 |
|
3547 | 583 |
self.previewButton = nil; |
584 |
self.tableView = nil; |
|
585 |
self.maxLabel = nil; |
|
586 |
self.sizeLabel = nil; |
|
587 |
self.segmentedControl = nil; |
|
588 |
self.slider = nil; |
|
3697 | 589 |
|
3547 | 590 |
self.lastIndexPath = nil; |
591 |
self.themeArray = nil; |
|
592 |
self.mapArray = nil; |
|
3697 | 593 |
|
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
|
594 |
MSG_DIDUNLOAD(); |
3547 | 595 |
[super viewDidUnload]; |
596 |
} |
|
597 |
||
598 |
-(void) dealloc { |
|
3705 | 599 |
self.delegate = nil; |
600 |
||
3547 | 601 |
[seedCommand release]; |
602 |
[templateFilterCommand release]; |
|
603 |
[mapGenCommand release]; |
|
604 |
[mazeSizeCommand release]; |
|
605 |
[themeCommand release]; |
|
3642 | 606 |
[staticMapCommand release]; |
3697 | 607 |
|
3547 | 608 |
[previewButton release]; |
609 |
[tableView release]; |
|
610 |
[maxLabel release]; |
|
611 |
[sizeLabel release]; |
|
612 |
[segmentedControl release]; |
|
613 |
[slider release]; |
|
3697 | 614 |
|
3547 | 615 |
[lastIndexPath release]; |
616 |
[themeArray release]; |
|
617 |
[mapArray release]; |
|
3697 | 618 |
|
3547 | 619 |
[super dealloc]; |
620 |
} |
|
621 |
||
622 |
||
623 |
@end |