project_files/HedgewarsMobile/Classes/HogHatViewController.m
author koda
Sun, 10 Oct 2010 22:32:01 +0200
changeset 3948 24daa33a3114
parent 3829 81db3c85784b
child 3971 5c82ee165ed5
permissions -rw-r--r--
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3697
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: 3697
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    18
 * File created on 02/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3697
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 "HogHatViewController.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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
@implementation HogHatViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
@synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
- (void)viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    // load all the hat file names and store them into hatArray
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    NSString *hatsDirectory = HATS_DIRECTORY();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    self.hatArray = array;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    43
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    // load the base hog image, drawing will occure in cellForRow...
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    [normalHogFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    self.normalHogSprite = hogSprite;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    [hogSprite release];
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
    50
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
    51
    self.title = NSLocalizedString(@"Change hedgehog's hat",@"");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
- (void)viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    [super viewWillAppear:animated];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    56
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    // this updates the hog name and its hat
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    // this moves the tableview to the top
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    return [self.hatArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    76
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    78
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    80
    if (cell == nil)
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    82
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    NSString *hat = [hatArray objectAtIndex:[indexPath row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    cell.textLabel.text = [hat stringByDeletingPathExtension];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    86
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    [hatFile release];
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3829
diff changeset
    90
    cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    [hatSprite release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    92
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    if ([hat isEqualToString:[hog objectForKey:@"hat"]]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   109
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
        // 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
   112
        // TODO: maybe this section could be cleaned up
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   114
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
        NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
        [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
        [newHog release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   119
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   122
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
        UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
        UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
        oldCell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
        [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   129
    }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    [self.navigationController popViewControllerAnimated:YES];
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
- (void)didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
- (void)viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    self.normalHogSprite = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    self.hatArray = 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
   148
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
- (void)dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    [hatArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    [teamDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    [normalHogSprite release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162