cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

DetailViewController.mm (11970B)


      1/*
      2 * Gearboy - Nintendo Game Boy Emulator
      3 * Copyright (C) 2012  Ignacio Sanchez
      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, either version 3 of the License, or
      8 * any later version.
      9 
     10 * This program is distributed in the hope that it will be useful,
     11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13 * GNU General Public License for more details.
     14 
     15 * You should have received a copy of the GNU General Public License
     16 * along with this program.  If not, see http://www.gnu.org/licenses/
     17 *
     18 */
     19
     20#import "DetailViewController.h"
     21#include "inputmanager.h"
     22
     23@interface DetailViewController ()
     24
     25@end
     26
     27@implementation DetailViewController
     28
     29#pragma mark - Managing the detail item
     30
     31- (void)viewDidLoad {
     32    [super viewDidLoad];
     33    
     34    self.view.multipleTouchEnabled = YES;
     35    
     36    UIBarButtonItem *menu = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStylePlain target:self action:@selector(menuButtonPressed)];
     37    
     38    self.navigationItem.rightBarButtonItems = @[menu];
     39
     40    CGRect screenBounds = [[UIScreen mainScreen] bounds];
     41    
     42    int scrW = 480;
     43    int scrH = 640;
     44    NSString* imageName = @"gb_832.jpg";
     45    
     46    switch ((int)screenBounds.size.height)
     47    {
     48        case 480:
     49        {
     50            // iPhone 4
     51            scrW = 320;
     52            scrH = 416;
     53            imageName = @"gb_832.jpg";
     54            break;
     55        }
     56        case 568:
     57        {
     58            // iPhone 5
     59            scrW = 320;
     60            scrH = 504;
     61            imageName = @"gb_1008.jpg";
     62            break;
     63        }
     64        case 667:
     65        {
     66            // iPhone 6
     67            scrW = 375;
     68            scrH = 603;
     69            imageName = @"gb_1206.jpg";
     70            break;
     71        }
     72        case 736:
     73        {
     74            // iPhone 6 Plus
     75            scrW = 414;
     76            scrH = 672;
     77            imageName = @"gb_2016.jpg";
     78            break;
     79        }
     80        case 1024:
     81        {
     82            scrW = 768;
     83            scrH = 960;
     84            if ([[UIScreen mainScreen] scale] != 1)
     85            {
     86                // iPad Air
     87                imageName = @"gb_ipad_1920.jpg";
     88            }
     89            else
     90            {
     91                // iPad 2
     92                imageName = @"gb_ipad_960.jpg";
     93            }
     94            break;
     95        }
     96    }
     97    
     98    UIImageView *imgView = [[UIImageView alloc] init];
     99    imgView.image = [UIImage imageNamed:imageName];
    100    imgView.frame = CGRectMake(0, 0, scrW, scrH);
    101    
    102    [self.view addSubview:imgView];
    103    [self.view addSubview:self.theGLViewController.view];
    104    
    105    if (self.detailItem)
    106    {
    107        [self.theGLViewController loadRomWithName:self.detailItem];
    108    }
    109}
    110
    111// Makes it so that swiping left wont exit the detail view
    112
    113id savedGestureRecognizerDelegate1;
    114id savedGestureRecognizerDelegate2;
    115
    116- (void)viewDidAppear:(BOOL)animated {
    117    [super viewDidAppear:animated];
    118    
    119    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    120        savedGestureRecognizerDelegate1 = self.navigationController.interactivePopGestureRecognizer.delegate;
    121        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    122    }
    123    if ([self.navigationController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    124        savedGestureRecognizerDelegate2 = self.navigationController.navigationController.interactivePopGestureRecognizer.delegate;
    125        self.navigationController.navigationController.interactivePopGestureRecognizer.delegate = self;
    126    }
    127}
    128
    129- (void)viewWillDisappear:(BOOL)animated {
    130    [super viewWillDisappear:animated];
    131    
    132    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    133        self.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate1;
    134    }
    135    if ([self.navigationController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    136        self.navigationController.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate2;
    137    }
    138}
    139
    140- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    141{
    142    if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer || gestureRecognizer == self.navigationController.navigationController.interactivePopGestureRecognizer) {
    143        return NO;
    144    }
    145    return YES;
    146}
    147
    148- (BOOL)prefersHomeIndicatorAutoHidden
    149{
    150    return YES;
    151}
    152
    153- (BOOL)shouldAutorotate
    154{
    155    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    156    
    157    return (orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown);;
    158}
    159
    160-(void) _handleTouch : (UITouch *) touch
    161{
    162    InputManager::Instance().HandleTouch(touch, self.view);
    163}
    164
    165-(void) touchesBegan : (NSSet *) touches withEvent : (UIEvent *) event
    166{
    167    for (UITouch *touch in touches)
    168    {
    169        [self _handleTouch : touch];
    170    }
    171}
    172
    173-(void) touchesMoved : (NSSet *) touches withEvent : (UIEvent *) event
    174{
    175    for (UITouch *touch in touches)
    176    {
    177        [self _handleTouch : touch];
    178    }
    179}
    180
    181-(void) touchesEnded : (NSSet *) touches withEvent : (UIEvent *) event
    182{
    183    for (UITouch *touch in touches)
    184    {
    185        [self _handleTouch : touch];
    186    }
    187}
    188
    189-(void) touchesCancelled : (NSSet *) touches withEvent : (UIEvent *) event
    190{
    191    for (UITouch *touch in touches)
    192    {
    193        [self _handleTouch : touch];
    194    }
    195}
    196
    197-(void)menuButtonPressed {
    198    
    199    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Menu"
    200                                                            message:@"Gearboy options"
    201                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    202    
    203    UIAlertAction *save = [UIAlertAction actionWithTitle:@"Save State"
    204                                                style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    205                                                    [self saveState];
    206                                                }];
    207    UIAlertAction *load = [UIAlertAction actionWithTitle:@"Load State"
    208                                                style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    209                                                    [self loadState];
    210                                                }];
    211    UIAlertAction *shareROM = [UIAlertAction actionWithTitle:@"Share ROM"
    212                                                style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    213                                                    [self airdropROM];
    214                                                }];
    215    UIAlertAction *shareSaveFile = [UIAlertAction actionWithTitle:@"Share Save File"
    216                                                style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    217                                                    [self airdropSaveFile];
    218                                                }];
    219    UIAlertAction *toggleMute = [UIAlertAction actionWithTitle:@"Toggle Mute"
    220                                                            style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    221                                                                [self toggleMute];
    222                                                }];
    223    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel"
    224                                                            style:UIAlertActionStyleCancel handler:NULL];
    225    [alert addAction:save];
    226    [alert addAction:load];
    227    [alert addAction:shareROM];
    228    [alert addAction:shareSaveFile];
    229    [alert addAction:toggleMute];
    230    [alert addAction:cancel];
    231    
    232    alert.popoverPresentationController.sourceView = self.view;
    233    alert.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems[0];
    234    
    235    [self presentViewController:alert animated:YES completion:nil];
    236}
    237
    238-(void)saveState
    239{
    240    [self.theGLViewController.theEmulator saveState];
    241}
    242
    243-(void)loadState
    244{
    245     [self.theGLViewController.theEmulator loadState];
    246}
    247
    248-(void)airdropROM
    249{
    250    if (self.detailItem)
    251    {
    252        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    253        NSString* documentsDirectoryPath = [paths objectAtIndex:0];
    254        NSString* romPath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, self.detailItem];
    255        NSURL* romFile = [NSURL fileURLWithPath:romPath];
    256        NSArray* objectsToAirdrop = @[romFile];
    257        UIActivityViewController* controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToAirdrop applicationActivities:nil];
    258        
    259        NSArray* excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
    260                                        UIActivityTypePostToWeibo,
    261                                        UIActivityTypeMessage,
    262                                        UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
    263                                        UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
    264                                        UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
    265                                        UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
    266        controller.excludedActivityTypes = excludedActivities;
    267        controller.popoverPresentationController.sourceView = self.view;
    268        controller.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems[0];
    269        
    270        [self presentViewController:controller animated:YES completion:nil];
    271    }
    272}
    273
    274-(void)airdropSaveFile
    275{
    276    if (self.detailItem)
    277    {
    278        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    279        NSString* documentsDirectoryPath = [paths objectAtIndex:0];
    280        NSString* saveFilename = [self.detailItem stringByDeletingPathExtension];
    281        NSString* saveFilePath = [NSString stringWithFormat:@"%@/%@.sav", documentsDirectoryPath, saveFilename];
    282        NSURL* saveFile = [NSURL fileURLWithPath:saveFilePath];
    283        NSArray* objectsToAirdrop = @[saveFile];
    284        
    285        if (objectsToAirdrop && objectsToAirdrop.count)
    286        {
    287            UIActivityViewController* controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToAirdrop applicationActivities:nil];
    288            
    289            NSArray* excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
    290                                            UIActivityTypePostToWeibo,
    291                                            UIActivityTypeMessage,
    292                                            UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
    293                                            UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
    294                                            UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
    295                                            UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
    296            controller.excludedActivityTypes = excludedActivities;
    297            controller.popoverPresentationController.sourceView = self.view;
    298            controller.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems[0];
    299            
    300            [self presentViewController:controller animated:YES completion:nil];
    301        }
    302    }
    303}
    304
    305-(void)toggleMute
    306{
    307    if (_theGLViewController.theEmulator.audioEnabled == YES) {
    308        [_theGLViewController.theEmulator setAudioEnabled:NO];
    309    } else {
    310        [_theGLViewController.theEmulator setAudioEnabled:YES];
    311    }
    312}
    313
    314@end