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