project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11549 893722a2a1f9
child 13166 ba5c794adae3
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    19 
    19 
    20 #import "SavedGamesViewController.h"
    20 #import "SavedGamesViewController.h"
    21 #import "GameInterfaceBridge.h"
    21 #import "GameInterfaceBridge.h"
    22 
    22 
    23 @interface SavedGamesViewController ()
    23 @interface SavedGamesViewController ()
    24 @property (retain, nonatomic) IBOutlet UIBarButtonItem *clearAllButton;
    24 @property (strong, nonatomic) IBOutlet UIBarButtonItem *clearAllButton;
    25 @end
    25 @end
    26 
    26 
    27 @implementation SavedGamesViewController
    27 @implementation SavedGamesViewController
    28 @synthesize tableView, listOfSavegames;
    28 @synthesize tableView, listOfSavegames;
    29 
    29 
    30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    30 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    31     return rotationManager(interfaceOrientation);
    31     return rotationManager(interfaceOrientation);
    32 }
    32 }
    33 
    33 
    34 -(void) updateTable {
    34 - (void)updateTable {
    35     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL];
    35     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL];
    36     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    36     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    37     self.listOfSavegames = array;
    37     self.listOfSavegames = array;
    38     [array release];
       
    39 
    38 
    40     [self.tableView reloadData];
    39     [self.tableView reloadData];
    41 }
    40 }
    42 
    41 
    43 -(void) viewDidLoad
    42 - (void)viewDidLoad
    44 {
    43 {
    45     [super viewDidLoad];
    44     [super viewDidLoad];
    46     
    45     
    47     [self.tableView setBackgroundColorForAnyTable:[UIColor clearColor]];
    46     [self.tableView setBackgroundColorForAnyTable:[UIColor clearColor]];
    48 
    47 
    49     NSString *imgName = (IS_IPAD()) ? @"mediumBackground~ipad.png" : @"smallerBackground~iphone.png";
    48     NSString *imgName = (IS_IPAD()) ? @"mediumBackground~ipad.png" : @"smallerBackground~iphone.png";
    50     UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName];
    49     UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName];
    51     UIImageView *background = [[UIImageView alloc] initWithImage:img];
    50     UIImageView *background = [[UIImageView alloc] initWithImage:img];
    52     [img release];
       
    53     background.frame = self.view.frame;
    51     background.frame = self.view.frame;
    54     background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    52     background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    55     [self.view insertSubview:background atIndex:0];
    53     [self.view insertSubview:background atIndex:0];
    56     [background release];
       
    57     
    54     
    58     [self.clearAllButton setTitle:NSLocalizedString(@"Clear All", nil)];
    55     [self.clearAllButton setTitle:NSLocalizedString(@"Clear All", nil)];
    59     
    56     
    60     if (self.listOfSavegames == nil)
    57     if (self.listOfSavegames == nil)
    61         [self updateTable];
    58         [self updateTable];
    62 }
    59 }
    63 
    60 
    64 -(void) viewWillAppear:(BOOL)animated {
    61 - (void)viewWillAppear:(BOOL)animated {
    65     [self updateTable];
    62     [self updateTable];
    66     [super viewWillAppear:animated];
    63     [super viewWillAppear:animated];
    67 }
    64 }
    68 
    65 
    69 #pragma mark -
    66 #pragma mark -
    70 #pragma mark button functions
    67 #pragma mark button functions
    71 -(IBAction) buttonPressed:(id) sender {
    68 - (IBAction)buttonPressed:(id)sender {
    72     UIButton *button = (UIButton *)sender;
    69     UIButton *button = (UIButton *)sender;
    73 
    70 
    74     if (button.tag == 0) {
    71     if (button.tag == 0) {
    75         [[AudioManagerController mainManager] playBackSound];
    72         [[AudioManagerController mainManager] playBackSound];
    76         [self.tableView setEditing:NO animated:YES];
    73         [self.tableView setEditing:NO animated:YES];
    89 
    86 
    90         if (IS_IPAD())
    87         if (IS_IPAD())
    91             [actionSheet showFromBarButtonItem:(UIBarButtonItem *)sender animated:YES];
    88             [actionSheet showFromBarButtonItem:(UIBarButtonItem *)sender animated:YES];
    92         else
    89         else
    93             [actionSheet showInView:self.view];
    90             [actionSheet showInView:self.view];
    94         [actionSheet release];
    91     }
    95     }
    92 }
    96 }
    93 
    97 
    94 - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    98 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
       
    99     if ([actionSheet cancelButtonIndex] != buttonIndex) {
    95     if ([actionSheet cancelButtonIndex] != buttonIndex) {
   100         // remove all files and recreate the directory
    96         // remove all files and recreate the directory
   101         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
    97         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
   102         [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
    98         [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
   103 
    99 
   106         for (int i = 0; i < [self.listOfSavegames count]; i++)
   102         for (int i = 0; i < [self.listOfSavegames count]; i++)
   107             [array addObject:[NSIndexPath indexPathForRow:i inSection:0]];
   103             [array addObject:[NSIndexPath indexPathForRow:i inSection:0]];
   108         [self.listOfSavegames removeAllObjects];
   104         [self.listOfSavegames removeAllObjects];
   109         
   105         
   110         [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
   106         [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
   111         [array release];
       
   112     }
   107     }
   113 }
   108 }
   114 
   109 
   115 #pragma mark -
   110 #pragma mark -
   116 #pragma mark Table view data source
   111 #pragma mark Table view data source
   117 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   112 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   118     return 1;
   113     return 1;
   119 }
   114 }
   120 
   115 
   121 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   116 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   122     return [self.listOfSavegames count];
   117     return [self.listOfSavegames count];
   123 }
   118 }
   124 
   119 
   125 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   120 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   126     static NSString *CellIdentifier = @"Cell";
   121     static NSString *CellIdentifier = @"Cell";
   127 
   122 
   128     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   123     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   129     if (editableCell == nil) {
   124     if (editableCell == nil) {
   130         editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   125         editableCell = [[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   131         editableCell.delegate = nil;
   126         editableCell.delegate = nil;
   132         editableCell.textField.userInteractionEnabled = NO;
   127         editableCell.textField.userInteractionEnabled = NO;
   133     }
   128     }
   134     editableCell.tag = [indexPath row];
   129     editableCell.tag = [indexPath row];
   135     editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
   130     editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
   136     editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   131     editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   137 
   132 
   138     return (UITableViewCell *)editableCell;
   133     return (UITableViewCell *)editableCell;
   139 }
   134 }
   140 
   135 
   141 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section {
   136 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
   142     UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)];
   137     UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)];
   143     footer.backgroundColor = [UIColor clearColor];
   138     footer.backgroundColor = [UIColor clearColor];
   144 
   139 
   145     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*60/100, 60)];
   140     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*60/100, 60)];
   146     label.center = CGPointMake(self.tableView.frame.size.width/2, 30);
   141     label.center = CGPointMake(self.tableView.frame.size.width/2, 30);
   150     label.numberOfLines = 5;
   145     label.numberOfLines = 5;
   151     label.text = NSLocalizedString(@"Press to resume playing or swipe to delete the save file.",@"");
   146     label.text = NSLocalizedString(@"Press to resume playing or swipe to delete the save file.",@"");
   152 
   147 
   153     label.backgroundColor = [UIColor clearColor];
   148     label.backgroundColor = [UIColor clearColor];
   154     [footer addSubview:label];
   149     [footer addSubview:label];
   155     [label release];
   150     return footer;
   156     return [footer autorelease];
       
   157 }
   151 }
   158 
   152 
   159 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   153 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   160     return 60;
   154     return 60;
   161 }
   155 }
   162 
   156 
   163 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   157 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   164     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
   158     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
   165     [self fixTagsForStartTag:[indexPath row]];
   159     [self fixTagsForStartTag:[indexPath row]];
   166 
   160 
   167     NSString *saveName = [self.listOfSavegames objectAtIndex:[indexPath row]];
   161     NSString *saveName = [self.listOfSavegames objectAtIndex:[indexPath row]];
   168     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
   162     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
   187     }
   181     }
   188 }
   182 }
   189 
   183 
   190 #pragma mark -
   184 #pragma mark -
   191 #pragma mark Table view delegate
   185 #pragma mark Table view delegate
   192 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   186 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   193     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   187     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   194     if (self.listOfSavegames == nil)
   188     if (self.listOfSavegames == nil)
   195         [self updateTable];
   189         [self updateTable];
   196 
   190 
   197     // duplicate the entry
   191     // duplicate the entry
   201     NSString *currentFilePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName];
   195     NSString *currentFilePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName];
   202     NSString *newSaveName = [[NSString alloc] initWithFormat:@"[%@] %@",NSLocalizedString(@"Backup",@""),currentSaveName];
   196     NSString *newSaveName = [[NSString alloc] initWithFormat:@"[%@] %@",NSLocalizedString(@"Backup",@""),currentSaveName];
   203     NSString *newFilePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName];
   197     NSString *newFilePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName];
   204 
   198 
   205     [self.listOfSavegames addObject:newSaveName];
   199     [self.listOfSavegames addObject:newSaveName];
   206     [newSaveName release];
       
   207     [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
   200     [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
   208     [newFilePath release];
       
   209 
   201 
   210     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
   202     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
   211 
   203 
   212     [GameInterfaceBridge registerCallingController:self];
   204     [GameInterfaceBridge registerCallingController:self];
   213     [GameInterfaceBridge startSaveGame:currentFilePath];
   205     [GameInterfaceBridge startSaveGame:currentFilePath];
   214     [currentFilePath release];
       
   215 }
   206 }
   216 
   207 
   217 #pragma mark -
   208 #pragma mark -
   218 #pragma mark editableCellView delegate
   209 #pragma mark editableCellView delegate
   219 // rename old file if names differ
   210 // rename old file if names differ
   220 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   211 - (void)saveTextFieldValue:(NSString *)textString withTag:(NSInteger)tagValue {
   221     if (self.listOfSavegames == nil)
   212     if (self.listOfSavegames == nil)
   222         [self updateTable];
   213         [self updateTable];
   223     NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]];
   214     NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]];
   224     NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString];
   215     NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString];
   225 
   216 
   230 
   221 
   231 }
   222 }
   232 
   223 
   233 #pragma mark -
   224 #pragma mark -
   234 #pragma mark Memory Management
   225 #pragma mark Memory Management
   235 -(void) didReceiveMemoryWarning {
   226 
       
   227 - (void)didReceiveMemoryWarning {
   236     self.listOfSavegames = nil;
   228     self.listOfSavegames = nil;
   237     MSG_MEMCLEAN();
   229     MSG_MEMCLEAN();
   238     [super didReceiveMemoryWarning];
   230     [super didReceiveMemoryWarning];
   239 }
   231 }
   240 
   232 
   241 -(void) viewDidUnload {
       
   242     self.tableView = nil;
       
   243     self.listOfSavegames = nil;
       
   244     MSG_DIDUNLOAD();
       
   245     [super viewDidUnload];
       
   246 }
       
   247 
       
   248 -(void) dealloc {
       
   249     releaseAndNil(tableView);
       
   250     releaseAndNil(listOfSavegames);
       
   251     releaseAndNil(_clearAllButton);
       
   252     [super dealloc];
       
   253 }
       
   254 
       
   255 @end
   233 @end