author | koda |
Sat, 19 Feb 2011 02:21:09 +0100 | |
changeset 4950 | 941f17b2dd9f |
parent 4939 | 6d512ba87f72 |
child 4976 | 088d40d8aba2 |
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 25/03/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "SDL_uikitappdelegate.h" |
|
3647
0d0df215fb52
making chat work... (keyboard support is heavily broken in sdl upstream)
koda
parents:
3598
diff
changeset
|
23 |
#import "InGameMenuViewController.h" |
3547 | 24 |
#import "PascalImports.h" |
25 |
#import "CommodityFunctions.h" |
|
26 |
#import "SDL_sysvideo.h" |
|
3922
44804043b691
iPad Video Out support (+less warnings +code update for latest SDL)
koda
parents:
3829
diff
changeset
|
27 |
#import "SDL_uikitkeyboard.h" |
4935 | 28 |
#import "OpenGLES/ES1/gl.h" |
29 |
||
30 |
#define VIEW_HEIGHT 200 |
|
3547 | 31 |
|
3647
0d0df215fb52
making chat work... (keyboard support is heavily broken in sdl upstream)
koda
parents:
3598
diff
changeset
|
32 |
@implementation InGameMenuViewController |
3547 | 33 |
@synthesize menuList; |
34 |
||
35 |
||
36 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
37 |
return rotationManager(interfaceOrientation); |
|
38 |
} |
|
39 |
||
40 |
-(void) didReceiveMemoryWarning { |
|
3971 | 41 |
self.menuList = nil; |
3547 | 42 |
[super didReceiveMemoryWarning]; |
43 |
} |
|
44 |
||
45 |
-(void) viewDidLoad { |
|
46 |
NSArray *array = [[NSArray alloc] initWithObjects: |
|
3701 | 47 |
NSLocalizedString(@"Show Help", @""), |
3737
2ba6ac8a114b
reworked the initialization functions, now it should be safe to update and no more need of spinning wheel at first launch
koda
parents:
3701
diff
changeset
|
48 |
NSLocalizedString(@"Tag", @""), |
4935 | 49 |
NSLocalizedString(@"Snapshot",@""), |
3547 | 50 |
NSLocalizedString(@"End Game", @""), |
51 |
nil]; |
|
52 |
self.menuList = array; |
|
53 |
[array release]; |
|
3697 | 54 |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
55 |
[super viewDidLoad]; |
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3924
diff
changeset
|
56 |
} |
3697 | 57 |
|
3547 | 58 |
-(void) viewDidUnload { |
59 |
self.menuList = nil; |
|
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
|
60 |
MSG_DIDUNLOAD(); |
3547 | 61 |
[super viewDidUnload]; |
62 |
} |
|
63 |
||
64 |
-(void) dealloc { |
|
65 |
[menuList release]; |
|
66 |
[super dealloc]; |
|
67 |
} |
|
68 |
||
69 |
#pragma mark - |
|
3648 | 70 |
#pragma mark animating |
71 |
-(void) present { |
|
72 |
CGRect screen = [[UIScreen mainScreen] bounds]; |
|
73 |
self.view.backgroundColor = [UIColor clearColor]; |
|
4935 | 74 |
self.view.frame = CGRectMake(screen.size.height, 0, 200, VIEW_HEIGHT); |
3697 | 75 |
|
3648 | 76 |
[UIView beginAnimations:@"showing popover" context:NULL]; |
77 |
[UIView setAnimationDuration:0.35]; |
|
4935 | 78 |
self.view.frame = CGRectMake(screen.size.height-200, 0, 200, VIEW_HEIGHT); |
3648 | 79 |
[UIView commitAnimations]; |
80 |
} |
|
81 |
||
82 |
-(void) dismiss { |
|
4912
862b134284b2
change order of statspage/gamerun when running in dual screen
koda
parents:
4861
diff
changeset
|
83 |
if (IS_IPAD() == NO) { |
862b134284b2
change order of statspage/gamerun when running in dual screen
koda
parents:
4861
diff
changeset
|
84 |
CGRect screen = [[UIScreen mainScreen] bounds]; |
862b134284b2
change order of statspage/gamerun when running in dual screen
koda
parents:
4861
diff
changeset
|
85 |
[UIView beginAnimations:@"hiding popover" context:NULL]; |
862b134284b2
change order of statspage/gamerun when running in dual screen
koda
parents:
4861
diff
changeset
|
86 |
[UIView setAnimationDuration:0.35]; |
4935 | 87 |
self.view.frame = CGRectMake(screen.size.height, 0, 200, VIEW_HEIGHT); |
4912
862b134284b2
change order of statspage/gamerun when running in dual screen
koda
parents:
4861
diff
changeset
|
88 |
[UIView commitAnimations]; |
4924 | 89 |
[self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35]; |
4912
862b134284b2
change order of statspage/gamerun when running in dual screen
koda
parents:
4861
diff
changeset
|
90 |
} |
3697 | 91 |
|
4920 | 92 |
HW_chatEnd(); |
93 |
SDL_iPhoneKeyboardHide((SDL_Window *)HW_getSDLWindow()); |
|
4935 | 94 |
|
95 |
if (shouldTakeScreenshot) { |
|
96 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please wait" |
|
97 |
message:nil |
|
98 |
delegate:nil |
|
99 |
cancelButtonTitle:nil |
|
100 |
otherButtonTitles:nil]; |
|
101 |
[alert show]; |
|
102 |
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] |
|
103 |
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; |
|
104 |
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 45); |
|
105 |
[indicator startAnimating]; |
|
106 |
[alert addSubview:indicator]; |
|
107 |
[indicator release]; |
|
108 |
||
109 |
// all these hacks because of the PAUSE caption on top of everything... |
|
110 |
[self performSelector:@selector(saveCurrentScreenToPhotoAlbum:) withObject:alert afterDelay:0.3]; |
|
111 |
} |
|
112 |
shouldTakeScreenshot = NO; |
|
113 |
||
3648 | 114 |
} |
115 |
||
116 |
#pragma mark - |
|
3547 | 117 |
#pragma mark tableView methods |
118 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
119 |
return 1; |
|
120 |
} |
|
121 |
||
122 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
4935 | 123 |
return 4; |
3547 | 124 |
} |
125 |
||
126 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
127 |
static NSString *cellIdentifier = @"CellIdentifier"; |
|
3697 | 128 |
|
3547 | 129 |
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier]; |
130 |
if (nil == cell) { |
|
131 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
|
132 |
reuseIdentifier:cellIdentifier] autorelease]; |
|
133 |
} |
|
4920 | 134 |
cell.textLabel.text = [self.menuList objectAtIndex:[indexPath row]]; |
3697 | 135 |
|
3996 | 136 |
if (IS_IPAD()) |
3668
3f7a95234d8a
tap to play piano notes, fix for audio and pause glitch
koda
parents:
3667
diff
changeset
|
137 |
cell.textLabel.textAlignment = UITextAlignmentCenter; |
3697 | 138 |
|
3547 | 139 |
return cell; |
140 |
} |
|
141 |
||
142 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
143 |
UIActionSheet *actionSheet; |
|
4935 | 144 |
UIAlertView *alert; |
3697 | 145 |
|
3547 | 146 |
switch ([indexPath row]) { |
147 |
case 0: |
|
3791
98072b3871c1
help page for ingame, some other fixes here and there
koda
parents:
3737
diff
changeset
|
148 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"show help ingame" object:nil]; |
4935 | 149 |
|
3547 | 150 |
break; |
151 |
case 1: |
|
4920 | 152 |
HW_chat(); |
153 |
SDL_iPhoneKeyboardShow((SDL_Window *)HW_getSDLWindow()); |
|
4935 | 154 |
|
3547 | 155 |
break; |
156 |
case 2: |
|
4935 | 157 |
alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Going to take a screenshot",@"") |
158 |
message:NSLocalizedString(@"The game snapshot will be placed in your Photo Album and it will be taken as soon as the pause menu is dismissed",@"") |
|
159 |
delegate:nil |
|
160 |
cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
161 |
otherButtonTitles:nil]; |
|
162 |
[alert show]; |
|
163 |
[alert release]; |
|
164 |
shouldTakeScreenshot = YES; |
|
165 |
||
166 |
break; |
|
167 |
case 3: |
|
3547 | 168 |
// expand the view (and table) so that the actionsheet can be selected on the iPhone |
4008
d87619de120d
fix an annoying bug that didn't allow returning to the ifrontend
koda
parents:
3996
diff
changeset
|
169 |
if (IS_IPAD() == NO) { |
3648 | 170 |
CGRect screen = [[UIScreen mainScreen] bounds]; |
3547 | 171 |
[self.tableView deselectRowAtIndexPath:indexPath animated:NO]; |
172 |
[UIView beginAnimations:@"table width more" context:NULL]; |
|
173 |
[UIView setAnimationDuration:0.2]; |
|
3648 | 174 |
self.view.frame = CGRectMake(0, 0, screen.size.height, screen.size.width); |
3547 | 175 |
[UIView commitAnimations]; |
176 |
} |
|
177 |
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"") |
|
178 |
delegate:self |
|
179 |
cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"") |
|
180 |
destructiveButtonTitle:NSLocalizedString(@"Of course!", @"") |
|
181 |
otherButtonTitles:nil]; |
|
182 |
[actionSheet showInView:self.view]; |
|
183 |
[actionSheet release]; |
|
3697 | 184 |
|
3547 | 185 |
break; |
186 |
default: |
|
3660 | 187 |
DLog(@"Warning: unset case value in section!"); |
3547 | 188 |
break; |
189 |
} |
|
3697 | 190 |
|
3547 | 191 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
192 |
} |
|
193 |
||
194 |
#pragma mark - |
|
195 |
#pragma mark actionSheet methods |
|
196 |
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
|
4912
862b134284b2
change order of statspage/gamerun when running in dual screen
koda
parents:
4861
diff
changeset
|
197 |
if (IS_IPAD() == NO) { |
3649 | 198 |
CGRect screen = [[UIScreen mainScreen] bounds]; |
3547 | 199 |
[UIView beginAnimations:@"table width less" context:NULL]; |
200 |
[UIView setAnimationDuration:0.2]; |
|
4935 | 201 |
self.view.frame = CGRectMake(screen.size.height-200, 0, 200, VIEW_HEIGHT); |
3547 | 202 |
[UIView commitAnimations]; |
203 |
} |
|
3697 | 204 |
|
4924 | 205 |
if ([actionSheet cancelButtonIndex] != buttonIndex) { |
206 |
if (IS_DUALHEAD()) |
|
207 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"remove overlay" object:nil]; |
|
4861
91f889289a47
(ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents:
4358
diff
changeset
|
208 |
HW_terminate(NO); |
4924 | 209 |
} |
3547 | 210 |
} |
211 |
||
4935 | 212 |
#pragma mark save screenshot |
213 |
//by http://www.bit-101.com/blog/?p=1861 |
|
214 |
// callback for CGDataProviderCreateWithData |
|
215 |
void releaseData(void *info, const void *data, size_t dataSize) { |
|
216 |
DLog(@"freeing raw data\n"); |
|
217 |
free((void *)data); |
|
218 |
} |
|
219 |
||
220 |
// callback for UIImageWriteToSavedPhotosAlbum |
|
221 |
-(void) image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { |
|
222 |
DLog(@"Save finished\n"); |
|
223 |
[image release]; |
|
224 |
UIAlertView *alert = (UIAlertView *)contextInfo; |
|
225 |
[alert dismissWithClickedButtonIndex:0 animated:YES]; |
|
226 |
[alert release]; |
|
227 |
} |
|
228 |
||
229 |
// the resolution of the buffer is always equal to the hardware device even if scaled |
|
230 |
-(void) saveCurrentScreenToPhotoAlbum:(UIAlertView *)alert { |
|
231 |
CGRect screenRect = [[UIScreen mainScreen] bounds]; |
|
232 |
int width = screenRect.size.width; |
|
233 |
int height = screenRect.size.height; |
|
234 |
||
235 |
NSInteger size = width * height * 4; |
|
236 |
GLubyte *buffer = (GLubyte *) malloc(size * sizeof(GLubyte)); |
|
237 |
GLubyte *buffer_flipped = (GLubyte *) malloc(size * sizeof(GLubyte)); |
|
238 |
||
239 |
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
|
4939
6d512ba87f72
screenshot binding for library (on ios it just plays the sound)
koda
parents:
4935
diff
changeset
|
240 |
HW_screenshot(); |
4935 | 241 |
// flip the data as glReadPixels here reads upside down |
242 |
for(int y = 0; y <height; y++) |
|
243 |
for(int x = 0; x <width * 4; x++) |
|
244 |
buffer_flipped[(int)((height - 1 - y) * width * 4 + x)] = buffer[(int)(y * 4 * width + x)]; |
|
245 |
free(buffer); |
|
246 |
||
247 |
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer_flipped, size, releaseData); |
|
248 |
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); |
|
249 |
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; |
|
250 |
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; |
|
251 |
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); |
|
252 |
||
253 |
CGColorSpaceRelease(colorSpaceRef); |
|
254 |
CGDataProviderRelease(provider); |
|
255 |
||
256 |
UIImage *image; |
|
257 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
258 |
image = [[UIImage alloc] initWithCGImage:imageRef scale:1 orientation:UIImageOrientationRight]; |
|
259 |
else |
|
260 |
image = [[UIImage alloc] initWithCGImage:imageRef]; |
|
261 |
CGImageRelease(imageRef); |
|
262 |
||
263 |
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (void *)alert); // add callback for finish saving |
|
264 |
} |
|
265 |
||
266 |
||
3547 | 267 |
@end |