From 1212116dfb9989332e99891d9bbd4ff154a9a862 Mon Sep 17 00:00:00 2001 From: meancoot Date: Thu, 21 Feb 2013 01:30:28 -0500 Subject: [PATCH] ios: Some cover view improvements: Don't allocate new views when reusing a cell. If a file item doesn't have an attached image, its filename will be printed in the cell instead. Images maintain aspect ratio when scaled. --- ios/RetroArch/RADirectoryGrid.m | 46 ++++++++++++++++++++++++++------- ios/RetroArch/browser.h | 1 + ios/RetroArch/browser.m | 12 +++++++-- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/ios/RetroArch/RADirectoryGrid.m b/ios/RetroArch/RADirectoryGrid.m index 6a6a7cca74..e209323cd2 100644 --- a/ios/RetroArch/RADirectoryGrid.m +++ b/ios/RetroArch/RADirectoryGrid.m @@ -39,8 +39,10 @@ self.navigationItem.rightBarButtonItem = [RetroArch_iOS get].settings_button; [self setTitle: [_path lastPathComponent]]; - - [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"filecell"]; + + [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dircell"]; + [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"textcell"]; + [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"imagecell"]; return self; } @@ -55,7 +57,6 @@ return [_list count]; } - - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { RADirectoryItem* path = [_list objectAtIndex: indexPath.row]; @@ -69,17 +70,42 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { RADirectoryItem* path = [_list objectAtIndex: indexPath.row]; + UICollectionViewCell* cell = nil; - UICollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"filecell" forIndexPath:indexPath]; if (path.isDirectory) - cell.backgroundView = [[UIImageView alloc] initWithImage:[RetroArch_iOS get].folder_icon]; + { + cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"dircell" forIndexPath:indexPath]; + + if (!cell.backgroundView) + { + cell.backgroundView = [[UIImageView alloc] initWithImage:[RetroArch_iOS get].folder_icon]; + ((UIImageView*)cell.backgroundView).contentMode = UIViewContentModeScaleAspectFit; + } + } + else if (path.coverPath) + { + cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"imagecell" forIndexPath:indexPath]; + + if (!cell.backgroundView) + { + cell.backgroundView = [UIImageView new]; + ((UIImageView*)cell.backgroundView).contentMode = UIViewContentModeScaleAspectFit; + } + + ((UIImageView*)cell.backgroundView).image = [UIImage imageWithContentsOfFile:path.coverPath]; + } else { - NSString* img = [NSString stringWithFormat:@"%@/.coverart/%@.png", _path, [[path.path lastPathComponent] stringByDeletingPathExtension]]; - if (ra_ios_is_file(img)) - cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:img]]; - else - cell.backgroundView = [[UIImageView alloc] initWithImage:_templateImage]; + cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"textcell" forIndexPath:indexPath]; + + if (!cell.backgroundView) + { + cell.backgroundView = [UILabel new]; + ((UILabel*)cell.backgroundView).numberOfLines = 0; + ((UILabel*)cell.backgroundView).textAlignment = NSTextAlignmentCenter; + } + + ((UILabel*)cell.backgroundView).text = [path.path lastPathComponent]; } return cell; diff --git a/ios/RetroArch/browser.h b/ios/RetroArch/browser.h index ce66c4c412..05bd932e38 100644 --- a/ios/RetroArch/browser.h +++ b/ios/RetroArch/browser.h @@ -20,6 +20,7 @@ extern NSString* ra_ios_get_browser_root(); @interface RADirectoryItem : NSObject @property (strong) NSString* path; +@property (strong) NSString* coverPath; @property bool isDirectory; @end diff --git a/ios/RetroArch/browser.m b/ios/RetroArch/browser.m index 3b39e1c4d0..dc096da460 100644 --- a/ios/RetroArch/browser.m +++ b/ios/RetroArch/browser.m @@ -18,7 +18,7 @@ #import "browser.h" @implementation RADirectoryItem -+ (RADirectoryItem*)directoryItemFromPath:(const char*)thePath ++ (RADirectoryItem*)directoryItemFromPath:(const char*)thePath checkForCovers:(BOOL)checkCovers { RADirectoryItem* result = [RADirectoryItem new]; result.path = [NSString stringWithUTF8String:thePath]; @@ -26,6 +26,14 @@ struct stat statbuf; if (stat(thePath, &statbuf) == 0) result.isDirectory = S_ISDIR(statbuf.st_mode); + + if (checkCovers && !result.isDirectory) + { + result.coverPath = [NSString stringWithFormat:@"%@/.coverart/%@.png", [result.path stringByDeletingLastPathComponent], [[result.path lastPathComponent] stringByDeletingPathExtension]]; + + if (!ra_ios_is_file(result.coverPath)) + result.coverPath = nil; + } return result; } @@ -65,7 +73,7 @@ NSArray* ra_ios_list_directory(NSString* path, NSRegularExpression* regex) cpath[cpath_end] = 0; strcat(cpath, item->d_name); - [result addObject:[RADirectoryItem directoryItemFromPath:cpath]]; + [result addObject:[RADirectoryItem directoryItemFromPath:cpath checkForCovers:YES]]; } closedir(dir);