project_files/HedgewarsMobile/Classes/InGameMenuViewController.m
changeset 4935 27fb500dd6b1
parent 4924 616b618814b5
child 4939 6d512ba87f72
equal deleted inserted replaced
4934:727da72f754a 4935:27fb500dd6b1
    23 #import "InGameMenuViewController.h"
    23 #import "InGameMenuViewController.h"
    24 #import "PascalImports.h"
    24 #import "PascalImports.h"
    25 #import "CommodityFunctions.h"
    25 #import "CommodityFunctions.h"
    26 #import "SDL_sysvideo.h"
    26 #import "SDL_sysvideo.h"
    27 #import "SDL_uikitkeyboard.h"
    27 #import "SDL_uikitkeyboard.h"
       
    28 #import "OpenGLES/ES1/gl.h"
       
    29 
       
    30 #define VIEW_HEIGHT 200
    28 
    31 
    29 @implementation InGameMenuViewController
    32 @implementation InGameMenuViewController
    30 @synthesize menuList;
    33 @synthesize menuList;
    31 
    34 
    32 
    35 
    41 
    44 
    42 -(void) viewDidLoad {
    45 -(void) viewDidLoad {
    43     NSArray *array = [[NSArray alloc] initWithObjects:
    46     NSArray *array = [[NSArray alloc] initWithObjects:
    44                       NSLocalizedString(@"Show Help", @""),
    47                       NSLocalizedString(@"Show Help", @""),
    45                       NSLocalizedString(@"Tag", @""),
    48                       NSLocalizedString(@"Tag", @""),
       
    49                       NSLocalizedString(@"Snapshot",@""),
    46                       NSLocalizedString(@"End Game", @""),
    50                       NSLocalizedString(@"End Game", @""),
    47                       nil];
    51                       nil];
    48     self.menuList = array;
    52     self.menuList = array;
    49     [array release];
    53     [array release];
    50 
    54 
    65 #pragma mark -
    69 #pragma mark -
    66 #pragma mark animating
    70 #pragma mark animating
    67 -(void) present {
    71 -(void) present {
    68     CGRect screen = [[UIScreen mainScreen] bounds];
    72     CGRect screen = [[UIScreen mainScreen] bounds];
    69     self.view.backgroundColor = [UIColor clearColor];
    73     self.view.backgroundColor = [UIColor clearColor];
    70     self.view.frame = CGRectMake(screen.size.height, 0, 200, 170);
    74     self.view.frame = CGRectMake(screen.size.height, 0, 200, VIEW_HEIGHT);
    71 
    75 
    72     [UIView beginAnimations:@"showing popover" context:NULL];
    76     [UIView beginAnimations:@"showing popover" context:NULL];
    73     [UIView setAnimationDuration:0.35];
    77     [UIView setAnimationDuration:0.35];
    74     self.view.frame = CGRectMake(screen.size.height-200, 0, 200, 170);
    78     self.view.frame = CGRectMake(screen.size.height-200, 0, 200, VIEW_HEIGHT);
    75     [UIView commitAnimations];
    79     [UIView commitAnimations];
    76 }
    80 }
    77 
    81 
    78 -(void) dismiss {
    82 -(void) dismiss {
    79     if (IS_IPAD() == NO) {
    83     if (IS_IPAD() == NO) {
    80         CGRect screen = [[UIScreen mainScreen] bounds];
    84         CGRect screen = [[UIScreen mainScreen] bounds];
    81         [UIView beginAnimations:@"hiding popover" context:NULL];
    85         [UIView beginAnimations:@"hiding popover" context:NULL];
    82         [UIView setAnimationDuration:0.35];
    86         [UIView setAnimationDuration:0.35];
    83         self.view.frame = CGRectMake(screen.size.height, 0, 200, 170);
    87         self.view.frame = CGRectMake(screen.size.height, 0, 200, VIEW_HEIGHT);
    84         [UIView commitAnimations];
    88         [UIView commitAnimations];
    85         [self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
    89         [self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
    86     }
    90     }
    87 
    91 
    88     HW_chatEnd();
    92     HW_chatEnd();
    89     SDL_iPhoneKeyboardHide((SDL_Window *)HW_getSDLWindow());
    93     SDL_iPhoneKeyboardHide((SDL_Window *)HW_getSDLWindow());
       
    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 
    90 }
   114 }
    91 
   115 
    92 #pragma mark -
   116 #pragma mark -
    93 #pragma mark tableView methods
   117 #pragma mark tableView methods
    94 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   118 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    95     return 1;
   119     return 1;
    96 }
   120 }
    97 
   121 
    98 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   122 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    99     return 3;
   123     return 4;
   100 }
   124 }
   101 
   125 
   102 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   126 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   103     static NSString *cellIdentifier = @"CellIdentifier";
   127     static NSString *cellIdentifier = @"CellIdentifier";
   104 
   128 
   115     return cell;
   139     return cell;
   116 }
   140 }
   117 
   141 
   118 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   142 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   119     UIActionSheet *actionSheet;
   143     UIActionSheet *actionSheet;
       
   144     UIAlertView *alert;
   120 
   145 
   121     switch ([indexPath row]) {
   146     switch ([indexPath row]) {
   122         case 0:
   147         case 0:
   123             [[NSNotificationCenter defaultCenter] postNotificationName:@"show help ingame" object:nil];
   148             [[NSNotificationCenter defaultCenter] postNotificationName:@"show help ingame" object:nil];
       
   149 
   124             break;
   150             break;
   125         case 1:
   151         case 1:
   126             HW_chat();
   152             HW_chat();
   127             SDL_iPhoneKeyboardShow((SDL_Window *)HW_getSDLWindow());
   153             SDL_iPhoneKeyboardShow((SDL_Window *)HW_getSDLWindow());
       
   154 
   128             break;
   155             break;
   129         case 2:
   156         case 2:
       
   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:
   130             // expand the view (and table) so that the actionsheet can be selected on the iPhone
   168             // expand the view (and table) so that the actionsheet can be selected on the iPhone
   131             if (IS_IPAD() == NO) {
   169             if (IS_IPAD() == NO) {
   132                 CGRect screen = [[UIScreen mainScreen] bounds];
   170                 CGRect screen = [[UIScreen mainScreen] bounds];
   133                 [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
   171                 [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
   134                 [UIView beginAnimations:@"table width more" context:NULL];
   172                 [UIView beginAnimations:@"table width more" context:NULL];
   158 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   196 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   159     if (IS_IPAD() == NO) {
   197     if (IS_IPAD() == NO) {
   160         CGRect screen = [[UIScreen mainScreen] bounds];
   198         CGRect screen = [[UIScreen mainScreen] bounds];
   161         [UIView beginAnimations:@"table width less" context:NULL];
   199         [UIView beginAnimations:@"table width less" context:NULL];
   162         [UIView setAnimationDuration:0.2];
   200         [UIView setAnimationDuration:0.2];
   163         self.view.frame = CGRectMake(screen.size.height-200, 0, 200, 170);
   201         self.view.frame = CGRectMake(screen.size.height-200, 0, 200, VIEW_HEIGHT);
   164         [UIView commitAnimations];
   202         [UIView commitAnimations];
   165     }
   203     }
   166 
   204 
   167     if ([actionSheet cancelButtonIndex] != buttonIndex) {
   205     if ([actionSheet cancelButtonIndex] != buttonIndex) {
   168         if (IS_DUALHEAD())
   206         if (IS_DUALHEAD())
   169             [[NSNotificationCenter defaultCenter] postNotificationName:@"remove overlay" object:nil];
   207             [[NSNotificationCenter defaultCenter] postNotificationName:@"remove overlay" object:nil];
   170         HW_terminate(NO);
   208         HW_terminate(NO);
   171     }
   209     }
   172 }
   210 }
   173 
   211 
       
   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);
       
   240 
       
   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 
   174 @end
   267 @end