project_files/HedgewarsMobile/Classes/HogHatViewController.m
author antonc27 <antonc27@mail.ru>
Fri, 18 Sep 2015 04:33:37 +0200
branchios-revival
changeset 11182 55f20efae2e4
parent 11148 064a53861759
child 11183 b2112ed988cb
permissions -rw-r--r--
- Fix for consequent selection of same hat for all hogs in settings - Small refactoring of HogHatViewController
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
6700
e04da46ee43c the most important commit of the year
koda
parents: 6078
diff changeset
     3
 * Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com>
3829
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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 6832
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
3829
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
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
#import "HogHatViewController.h"
6078
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 6074
diff changeset
    21
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
@implementation HogHatViewController
11182
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
    24
@synthesize teamDictionary, hatArray, normalHogSprite, selectedHog;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
#pragma mark View lifecycle
3971
5c82ee165ed5 minor stuff
koda
parents: 3948
diff changeset
    33
-(void) viewDidLoad {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
    // load all the hat file names and store them into hatArray
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    NSString *hatsDirectory = HATS_DIRECTORY();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    self.hatArray = array;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    40
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    // load the base hog image, drawing will occure in cellForRow...
5983
f0925204f50e retina images, again
koda
parents: 5208
diff changeset
    42
    NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/basehat-hedgehog.png",[[NSBundle mainBundle] resourcePath]];
f0925204f50e retina images, again
koda
parents: 5208
diff changeset
    43
    UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    [normalHogFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    self.normalHogSprite = hogSprite;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    [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
    47
6074
047eaed35cbb ios major refactoring for ios settings, now they are presented differently on iphone/ipad, code is simplified and optimized, and ui is a little refreshed (eg. no more stuck selected fields)
koda
parents: 5983
diff changeset
    48
    self.title = NSLocalizedString(@"Change hedgehogs' hat",@"");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
3971
5c82ee165ed5 minor stuff
koda
parents: 3948
diff changeset
    51
-(void) viewWillAppear:(BOOL)animated {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    [super viewWillAppear:animated];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    53
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    // this updates the hog name and its hat
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    // this moves the tableview to the top
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
}
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    return [self.hatArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
// Customize the appearance of table view cells.
3971
5c82ee165ed5 minor stuff
koda
parents: 3948
diff changeset
    72
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    73
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    75
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    77
    if (cell == nil)
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    79
11182
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
    80
    NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
    81
    NSString *hat = [self.hatArray objectAtIndex:[indexPath row]];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    cell.textLabel.text = [hat stringByDeletingPathExtension];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    83
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    [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
    87
    cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    [hatSprite release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    89
11182
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
    90
    if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
}
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
#pragma mark Table view delegate
11182
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   102
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   103
{
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   104
    NSInteger selectedRow = [indexPath row];
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   105
    
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   106
    // update data on the hog dictionary
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   107
    NSDictionary *oldHog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   108
11182
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   109
    NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog];
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   110
    [newHog setObject:[[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension] forKey:@"hat"];
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   111
    [[self.teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:self.selectedHog withObject:newHog];
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   112
    [newHog release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   113
11182
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   114
    // tell our boss to write this new stuff on disk
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   115
    [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
55f20efae2e4 - Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   116
    
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    [self.navigationController popViewControllerAnimated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
#pragma mark Memory management
3971
5c82ee165ed5 minor stuff
koda
parents: 3948
diff changeset
   123
-(void) didReceiveMemoryWarning {
5c82ee165ed5 minor stuff
koda
parents: 3948
diff changeset
   124
    MSG_MEMCLEAN();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
3971
5c82ee165ed5 minor stuff
koda
parents: 3948
diff changeset
   128
-(void) viewDidUnload {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    self.normalHogSprite = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    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
   132
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
3971
5c82ee165ed5 minor stuff
koda
parents: 3948
diff changeset
   136
-(void) dealloc {
5208
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 4976
diff changeset
   137
    releaseAndNil(hatArray);
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 4976
diff changeset
   138
    releaseAndNil(teamDictionary);
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 4976
diff changeset
   139
    releaseAndNil(normalHogSprite);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    [super dealloc];
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145