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