cocoaTouch/HogHatViewController.m
author nemo
Sun, 16 May 2010 23:02:49 +0000
changeset 3466 78d9fa9a700e
parent 3374 0d522416d97f
child 3490 016b3172b645
permissions -rw-r--r--
Comment out the desyncing block, with a suggestion for possible fix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     1
//
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     2
//  HogHatViewController.m
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     3
//  HedgewarsMobile
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     4
//
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     5
//  Created by Vittorio on 02/04/10.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     7
//
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     8
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
     9
#import "HogHatViewController.h"
3325
652a8ebdf667 moved around team creation
koda
parents: 3317
diff changeset
    10
#import "CommodityFunctions.h"
3352
ac5d14a35482 complete previews on the team settings
koda
parents: 3340
diff changeset
    11
#import "UIImageExtra.h"
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    12
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    13
@implementation HogHatViewController
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    14
@synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    15
3325
652a8ebdf667 moved around team creation
koda
parents: 3317
diff changeset
    16
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3328
diff changeset
    17
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3328
diff changeset
    18
	return rotationManager(interfaceOrientation);
3325
652a8ebdf667 moved around team creation
koda
parents: 3317
diff changeset
    19
}
652a8ebdf667 moved around team creation
koda
parents: 3317
diff changeset
    20
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3328
diff changeset
    21
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    22
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    23
#pragma mark View lifecycle
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    24
- (void)viewDidLoad {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    25
    [super viewDidLoad];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    26
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
    27
    // load all the hat file names and store them into hatArray
3325
652a8ebdf667 moved around team creation
koda
parents: 3317
diff changeset
    28
    NSString *hatsDirectory = HATS_DIRECTORY();
652a8ebdf667 moved around team creation
koda
parents: 3317
diff changeset
    29
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL];
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
    30
    self.hatArray = array;
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
    31
    
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    32
    // load the base hog image, drawing will occure in cellForRow...
3357
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    33
    NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    34
    UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
3357
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    35
    [normalHogFile release];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    36
    self.normalHogSprite = hogSprite;
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    37
    [hogSprite release];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    38
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    39
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    40
- (void)viewWillAppear:(BOOL)animated {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    41
    [super viewWillAppear:animated];
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
    42
    self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"];
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
    43
3308
b6dcae4b6d2c make the tables work also on iphone
koda
parents: 3305
diff changeset
    44
    // this updates the hog name and its hat
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    45
    [self.tableView reloadData];
3308
b6dcae4b6d2c make the tables work also on iphone
koda
parents: 3305
diff changeset
    46
    // this moves the tableview to the top
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3308
diff changeset
    47
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    48
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    49
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    50
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    51
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    52
#pragma mark Table view data source
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    53
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    54
    return 1;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    55
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    56
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    57
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    58
    return [self.hatArray count];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    59
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    60
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    61
// Customize the appearance of table view cells.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    62
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    63
    
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    64
    static NSString *CellIdentifier = @"Cell";
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    65
    
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    66
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    67
    if (cell == nil) 
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    68
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    69
    
3325
652a8ebdf667 moved around team creation
koda
parents: 3317
diff changeset
    70
    NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    71
    NSString *hat = [hatArray objectAtIndex:[indexPath row]];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    72
    cell.textLabel.text = [hat stringByDeletingPathExtension];
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    73
    
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    74
    NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    75
    UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    76
    [hatFile release];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    77
    cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    78
    [hatSprite release];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    79
        
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    80
    if ([hat isEqualToString:[hog objectForKey:@"hat"]]) {
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    81
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    82
        self.lastIndexPath = indexPath;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    83
    } else {
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    84
        cell.accessoryType = UITableViewCellAccessoryNone;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    85
    }
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    86
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    87
    return cell;
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    88
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    89
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    90
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    91
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
    92
#pragma mark Table view delegate
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
    93
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    94
    int newRow = [indexPath row];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    95
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    96
    
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    97
    if (newRow != oldRow) {
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
    98
        // if the two selected rows differ update data on the hog dictionary and reload table content
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    99
        // TODO: maybe this section could be cleaned up
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   100
        NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   101
        
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   102
        NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   103
        [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   104
        [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   105
        [newHog release];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   106
        
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   107
        // tell our boss to write this new stuff on disk
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   108
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   109
        
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   110
        UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   111
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   112
        UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   113
        oldCell.accessoryType = UITableViewCellAccessoryNone;
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   114
        self.lastIndexPath = indexPath;
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   115
        [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   116
    } 
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   117
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3325
diff changeset
   118
    [self.navigationController popViewControllerAnimated:YES];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   119
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   120
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   121
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   122
#pragma mark -
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   123
#pragma mark Memory management
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   124
- (void)didReceiveMemoryWarning {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   125
    // Releases the view if it doesn't have a superview.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   126
    [super didReceiveMemoryWarning];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   127
    // Relinquish ownership any cached data, images, etc that aren't in use.
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   128
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   129
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   130
- (void)viewDidUnload {
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   131
    [super viewDidUnload];
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   132
    self.lastIndexPath = nil;
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   133
    self.normalHogSprite = nil;
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   134
    self.teamDictionary = nil;
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   135
    self.hatArray = nil;
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   136
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   137
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   138
- (void)dealloc {
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   139
    [hatArray release];
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   140
    [teamDictionary release];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   141
    [normalHogSprite release];
3315
4e2813713358 usa a cached dictionary and write only when needed
koda
parents: 3312
diff changeset
   142
    [lastIndexPath release];
3305
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   143
    [super dealloc];
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   144
}
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   145
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   146
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   147
@end
91074496d5c9 merged code from ipad and iphone frontends
koda
parents:
diff changeset
   148