3547
|
1 |
//
|
|
2 |
// HogHatViewController.m
|
|
3 |
// HedgewarsMobile
|
|
4 |
//
|
|
5 |
// Created by Vittorio on 02/04/10.
|
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
7 |
//
|
|
8 |
|
|
9 |
#import "HogHatViewController.h"
|
|
10 |
#import "CommodityFunctions.h"
|
|
11 |
#import "UIImageExtra.h"
|
|
12 |
|
|
13 |
@implementation HogHatViewController
|
|
14 |
@synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog;
|
|
15 |
|
|
16 |
|
|
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
|
|
18 |
return rotationManager(interfaceOrientation);
|
|
19 |
}
|
|
20 |
|
|
21 |
|
|
22 |
#pragma mark -
|
|
23 |
#pragma mark View lifecycle
|
|
24 |
- (void)viewDidLoad {
|
|
25 |
[super viewDidLoad];
|
|
26 |
|
|
27 |
// load all the hat file names and store them into hatArray
|
|
28 |
NSString *hatsDirectory = HATS_DIRECTORY();
|
|
29 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL];
|
|
30 |
self.hatArray = array;
|
|
31 |
|
|
32 |
// load the base hog image, drawing will occure in cellForRow...
|
|
33 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
|
|
34 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
|
|
35 |
[normalHogFile release];
|
|
36 |
self.normalHogSprite = hogSprite;
|
|
37 |
[hogSprite release];
|
|
38 |
}
|
|
39 |
|
|
40 |
- (void)viewWillAppear:(BOOL)animated {
|
|
41 |
[super viewWillAppear:animated];
|
|
42 |
self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"];
|
|
43 |
|
|
44 |
// this updates the hog name and its hat
|
|
45 |
[self.tableView reloadData];
|
|
46 |
// this moves the tableview to the top
|
|
47 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
|
|
48 |
}
|
|
49 |
|
|
50 |
|
|
51 |
#pragma mark -
|
|
52 |
#pragma mark Table view data source
|
|
53 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
|
|
54 |
return 1;
|
|
55 |
}
|
|
56 |
|
|
57 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
58 |
return [self.hatArray count];
|
|
59 |
}
|
|
60 |
|
|
61 |
// Customize the appearance of table view cells.
|
|
62 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
63 |
|
|
64 |
static NSString *CellIdentifier = @"Cell";
|
|
65 |
|
|
66 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
67 |
if (cell == nil)
|
|
68 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
|
|
69 |
|
|
70 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
|
|
71 |
NSString *hat = [hatArray objectAtIndex:[indexPath row]];
|
|
72 |
cell.textLabel.text = [hat stringByDeletingPathExtension];
|
|
73 |
|
|
74 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
|
|
75 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
|
|
76 |
[hatFile release];
|
|
77 |
cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)];
|
|
78 |
[hatSprite release];
|
|
79 |
|
|
80 |
if ([hat isEqualToString:[hog objectForKey:@"hat"]]) {
|
|
81 |
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
82 |
self.lastIndexPath = indexPath;
|
|
83 |
} else {
|
|
84 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
85 |
}
|
|
86 |
|
|
87 |
return cell;
|
|
88 |
}
|
|
89 |
|
|
90 |
|
|
91 |
#pragma mark -
|
|
92 |
#pragma mark Table view delegate
|
|
93 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
94 |
int newRow = [indexPath row];
|
|
95 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
|
|
96 |
|
|
97 |
if (newRow != oldRow) {
|
|
98 |
// if the two selected rows differ update data on the hog dictionary and reload table content
|
|
99 |
// TODO: maybe this section could be cleaned up
|
|
100 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
|
|
101 |
|
|
102 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
|
|
103 |
[newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"];
|
|
104 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
|
|
105 |
[newHog release];
|
|
106 |
|
|
107 |
// tell our boss to write this new stuff on disk
|
|
108 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
|
|
109 |
|
|
110 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
|
|
111 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
112 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
|
|
113 |
oldCell.accessoryType = UITableViewCellAccessoryNone;
|
|
114 |
self.lastIndexPath = indexPath;
|
|
115 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
|
|
116 |
}
|
|
117 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
118 |
[self.navigationController popViewControllerAnimated:YES];
|
|
119 |
}
|
|
120 |
|
|
121 |
|
|
122 |
#pragma mark -
|
|
123 |
#pragma mark Memory management
|
|
124 |
- (void)didReceiveMemoryWarning {
|
|
125 |
// Releases the view if it doesn't have a superview.
|
|
126 |
[super didReceiveMemoryWarning];
|
|
127 |
// Relinquish ownership any cached data, images, etc that aren't in use.
|
|
128 |
}
|
|
129 |
|
|
130 |
- (void)viewDidUnload {
|
|
131 |
self.lastIndexPath = nil;
|
|
132 |
self.normalHogSprite = nil;
|
|
133 |
self.teamDictionary = nil;
|
|
134 |
self.hatArray = nil;
|
|
135 |
[super viewDidUnload];
|
|
136 |
MSG_DIDUNLOAD();
|
|
137 |
}
|
|
138 |
|
|
139 |
- (void)dealloc {
|
|
140 |
[hatArray release];
|
|
141 |
[teamDictionary release];
|
|
142 |
[normalHogSprite release];
|
|
143 |
[lastIndexPath release];
|
|
144 |
[super dealloc];
|
|
145 |
}
|
|
146 |
|
|
147 |
|
|
148 |
@end
|
|
149 |
|