project_files/HedgewarsMobile/Classes/FortsViewController.m
author koda
Tue, 19 Apr 2011 00:19:01 +0200
changeset 5157 a5a6ffc56141
parent 4976 088d40d8aba2
child 5208 878e551f0b4a
permissions -rw-r--r--
things are a little sanier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4476
diff changeset
     3
 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    18
 * File created on 08/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3789
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "FortsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
#import "UIImageExtra.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 3971
diff changeset
    26
#define IMGNUM_PER_FORT 4
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 3971
diff changeset
    27
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
@implementation FortsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
@synthesize teamDictionary, fortArray, lastIndexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL];
3789
c3eb56754e92 added a smaller version of forts, fixed a couple of regressions
koda
parents: 3697
diff changeset
    43
    NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 3)];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    // we need to remove the double entries and the L.png suffix
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    for (int i = 0; i < [directoryContents count]; i++) {
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 3971
diff changeset
    46
        if (i % IMGNUM_PER_FORT == 3) {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
            NSString *currentName = [directoryContents objectAtIndex:i];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
            NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
            [filteredContents addObject:correctName];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    50
        }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    self.fortArray = filteredContents;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
    [filteredContents release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    54
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    // statically set row height instead of using delegate method for performance reasons
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 3971
diff changeset
    56
    self.tableView.rowHeight = 128;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    57
3662
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3547
diff changeset
    58
    self.title = NSLocalizedString(@"Choose team fort",@"");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 3971
diff changeset
    62
-(void) viewWillAppear:(BOOL)animated {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    return [self.fortArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    82
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 3971
diff changeset
    84
    if (cell == nil)
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
                                       reuseIdentifier:CellIdentifier] autorelease];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    87
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    NSString *fortName = [fortArray objectAtIndex:[indexPath row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    cell.textLabel.text = fortName;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    90
3789
c3eb56754e92 added a smaller version of forts, fixed a couple of regressions
koda
parents: 3697
diff changeset
    91
    NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@-preview.png", FORTS_DIRECTORY(), fortName];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    [fortFile release];
3789
c3eb56754e92 added a smaller version of forts, fixed a couple of regressions
koda
parents: 3697
diff changeset
    94
    cell.imageView.image = fortSprite;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    [fortSprite release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    96
3789
c3eb56754e92 added a smaller version of forts, fixed a couple of regressions
koda
parents: 3697
diff changeset
    97
    //cell.detailTextLabel.text = @"Insert funny description here";
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
   104
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
   114
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        // if the two selected rows differ update data on the hog dictionary and reload table content
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
        [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
   121
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
        UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
        UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
        oldCell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
        [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    [self.navigationController popViewControllerAnimated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
-(void) didReceiveMemoryWarning {
3971
5c82ee165ed5 minor stuff
koda
parents: 3829
diff changeset
   137
    self.lastIndexPath = nil;
5c82ee165ed5 minor stuff
koda
parents: 3829
diff changeset
   138
    MSG_MEMCLEAN();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    self.fortArray = nil;
3662
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3547
diff changeset
   146
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
3663
8c28abf427f5 reduce the number of keywords used and switch to BMP format for screenshots
koda
parents: 3662
diff changeset
   151
-(void) dealloc {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    [teamDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    [fortArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160