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