cscg22-gearboy

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

MasterViewController.mm (9435B)


      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 "MasterViewController.h"
     21#import "DetailViewController.h"
     22
     23@interface MasterViewController ()
     24
     25@property NSMutableArray *objects;
     26@end
     27
     28@implementation MasterViewController
     29
     30- (void)awakeFromNib {
     31    [super awakeFromNib];
     32    self.title = @"Games";
     33    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
     34        self.clearsSelectionOnViewWillAppear = NO;
     35        self.preferredContentSize = CGSizeMake(320.0, 600.0);
     36    }
     37    
     38    self.theGLViewController = [[GLViewController alloc] initWithNibName:@"GLViewController" bundle:nil];
     39    self.theGLViewController.preferredFramesPerSecond = 60;
     40    self.theGLViewController.resumeOnDidBecomeActive = NO;
     41    self.theGLViewController.pauseOnWillResignActive = NO;
     42}
     43
     44- (void)viewDidLoad {
     45    [super viewDidLoad];
     46    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
     47    [self reloadTableView];
     48}
     49
     50- (BOOL)shouldAutorotate
     51{
     52    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
     53    
     54    return (orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown);;
     55}
     56
     57- (void)reloadTableView
     58{
     59    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     60    
     61    NSArray *files = nil;
     62    
     63    if ([paths count] > 0)
     64    {
     65        NSFileManager *fileManager = [[NSFileManager alloc] init];
     66        NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:nil];
     67        
     68        if ([dirContents count] > 0)
     69        {
     70            NSArray *extensions = [NSArray arrayWithObjects:@"zip", @"gb", @"sgb", @"gbc", @"rom", @"dmg", @"cgb", @"ZIP", @"GB", @"SGB", @"GBC", @"ROM", @"DMG", @"CGB", nil];
     71            files = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", extensions]];
     72        }
     73    }
     74    
     75    self.sections = [[NSMutableDictionary alloc] init];
     76    
     77    BOOL found;
     78    
     79    for (NSString* rom in files)
     80    {
     81        NSString* c = [[rom substringToIndex:1] uppercaseString];
     82        
     83        found = NO;
     84        
     85        for (NSString* str in [self.sections allKeys])
     86        {
     87            if ([str isEqualToString:c])
     88            {
     89                found = YES;
     90            }
     91        }
     92        
     93        if (!found)
     94        {
     95            [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
     96        }
     97    }
     98    
     99    for (NSString* rom in files)
    100    {
    101        [[self.sections objectForKey:[[rom substringToIndex:1] uppercaseString]] addObject:rom];
    102    }
    103    
    104    [self.tableView reloadData];
    105    
    106}
    107
    108- (void)loadWithROM:(NSString *)rom
    109{
    110    _openedFromOtherApp = YES;
    111    _openedFromOtherAppRom = rom;
    112    
    113    if (self.splitViewController.collapsed)
    114    {
    115        id vc = [[self.splitViewController.viewControllers lastObject] topViewController];
    116        if ([vc isMemberOfClass:[UINavigationController class]])
    117        {
    118            _openedFromOtherApp = NO;
    119            [self.theGLViewController loadRomWithName:_openedFromOtherAppRom];
    120            return;
    121        }
    122    }
    123    
    124    [self performSegueWithIdentifier: @"showDetail" sender: self];
    125}
    126
    127#pragma mark - Segues
    128
    129- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    130    if ([[segue identifier] isEqualToString:@"showDetail"]) {
    131        
    132        NSString* rom;
    133        
    134        if (_openedFromOtherApp)
    135        {
    136            _openedFromOtherApp = NO;
    137            rom = _openedFromOtherAppRom;
    138        }
    139        else
    140        {
    141            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    142            rom = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    143        }
    144
    145        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
    146        [controller setDetailItem:rom];
    147        [controller setTheGLViewController:self.theGLViewController];
    148        controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
    149        controller.navigationItem.leftItemsSupplementBackButton = YES;
    150        self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
    151    }
    152}
    153
    154#pragma mark - Table View
    155
    156- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    157    return [[self.sections allKeys] count];
    158}
    159
    160- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    161{
    162    return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
    163}
    164
    165- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    166    return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
    167}
    168
    169- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    170    return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    171}
    172
    173- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    174    static NSString *CellIdentifier = @"Cell";
    175    
    176    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    177    if (cell == nil) {
    178        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    179        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    180            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    181        }
    182    }
    183    
    184    NSString* rom = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    185    
    186    cell.textLabel.text = [rom stringByDeletingPathExtension];
    187    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    188    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    189    return cell;
    190}
    191
    192- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    193    return YES;
    194}
    195
    196- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    197    if (editingStyle == UITableViewCellEditingStyleDelete)
    198    {
    199        NSString* rom = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    200        
    201        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    202        NSError* error;
    203        NSFileManager *fileManager = [[NSFileManager alloc] init];
    204        
    205        NSString* deletePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, rom];
    206        
    207        if ([fileManager removeItemAtPath:deletePath error:&error])
    208        {
    209            [tableView beginUpdates];
    210            
    211            [[self.sections objectForKey:[[rom substringToIndex:1] uppercaseString]] removeObject:rom];
    212            
    213            if ([[self.sections objectForKey:[[rom substringToIndex:1] uppercaseString]] count] > 0)
    214            {
    215                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    216            }
    217            else
    218            {
    219                [self.sections removeObjectForKey:[[rom substringToIndex:1] uppercaseString]];
    220                
    221                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    222                [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationFade];
    223            }
    224            
    225            [tableView endUpdates];
    226        }
    227        else
    228        {
    229            NSLog(@"ERROR %@", [error localizedDescription]);
    230        }
    231        
    232        [tableView reloadData];
    233    }
    234}
    235
    236- (void)viewDidAppear:(BOOL)animated
    237{
    238    [super viewDidAppear:animated];
    239    [self.theGLViewController.theEmulator pause];
    240    // disable screen sleep when in-game
    241    [[UIApplication sharedApplication] setIdleTimerDisabled: NO];
    242}
    243
    244- (void)viewWillDisappear:(BOOL)animated
    245{
    246    [super viewWillDisappear:animated];
    247    [self.theGLViewController.theEmulator resume];
    248    // disable screen sleep when in-game
    249    [[UIApplication sharedApplication] setIdleTimerDisabled: YES];
    250}
    251
    252@end