author | Wuzzy <Wuzzy2@mail.ru> |
Fri, 19 Apr 2019 15:54:44 +0200 | |
changeset 14811 | d65e25e211d4 |
parent 12872 | 00215a7ec5f5 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
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 | 17 |
*/ |
18 |
||
3547 | 19 |
|
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 | 22 |
|
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 | 25 |
|
26 |
||
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
27 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
3547 | 28 |
return rotationManager(interfaceOrientation); |
29 |
} |
|
30 |
||
31 |
#pragma mark - |
|
32 |
#pragma mark View lifecycle |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
33 |
- (void)viewDidLoad { |
3547 | 34 |
[super viewDidLoad]; |
35 |
||
36 |
// load all the hat file names and store them into hatArray |
|
37 |
NSString *hatsDirectory = HATS_DIRECTORY(); |
|
38 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
|
39 |
self.hatArray = array; |
|
3697 | 40 |
|
3547 | 41 |
// load the base hog image, drawing will occure in cellForRow... |
5983 | 42 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/basehat-hedgehog.png",[[NSBundle mainBundle] resourcePath]]; |
43 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile]; |
|
3547 | 44 |
self.normalHogSprite = hogSprite; |
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
|
45 |
|
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
|
46 |
self.title = NSLocalizedString(@"Change hedgehogs' hat",@""); |
3547 | 47 |
} |
48 |
||
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
49 |
- (void)viewWillAppear:(BOOL)animated { |
3547 | 50 |
[super viewWillAppear:animated]; |
3697 | 51 |
|
3547 | 52 |
// this updates the hog name and its hat |
53 |
[self.tableView reloadData]; |
|
54 |
// this moves the tableview to the top |
|
55 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
56 |
} |
|
57 |
||
58 |
||
59 |
#pragma mark - |
|
60 |
#pragma mark Table view data source |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
61 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
3547 | 62 |
return 1; |
63 |
} |
|
64 |
||
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
65 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3547 | 66 |
return [self.hatArray count]; |
67 |
} |
|
68 |
||
69 |
// Customize the appearance of table view cells. |
|
3971 | 70 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
3697 | 71 |
|
3547 | 72 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 73 |
|
3547 | 74 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 75 |
if (cell == nil) |
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
76 |
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; |
3697 | 77 |
|
11182
55f20efae2e4
- Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
78 |
NSString *hat = [self.hatArray objectAtIndex:[indexPath row]]; |
3547 | 79 |
cell.textLabel.text = [hat stringByDeletingPathExtension]; |
3697 | 80 |
|
3547 | 81 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
82 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3829
diff
changeset
|
83 |
cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)]; |
3697 | 84 |
|
11183
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
85 |
NSDictionary *hog = (self.selectedHog != -1) ? [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog] : nil; |
11182
55f20efae2e4
- Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
86 |
if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) { |
3547 | 87 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
88 |
} else { |
|
89 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
90 |
} |
|
91 |
||
92 |
return cell; |
|
93 |
} |
|
94 |
||
95 |
||
96 |
#pragma mark - |
|
97 |
#pragma mark Table view delegate |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
98 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
11182
55f20efae2e4
- Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
99 |
{ |
55f20efae2e4
- Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
100 |
NSInteger selectedRow = [indexPath row]; |
11183
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
101 |
NSString *newHat = [[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension]; |
11182
55f20efae2e4
- Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
102 |
|
11183
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
103 |
// update data on the hogs dictionary |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
104 |
if (self.selectedHog != -1) |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
105 |
{ |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
106 |
// update only selected hog with new hat |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
107 |
[self updateTeamDictionaryWithNewHat:newHat forStartHogIndex:self.selectedHog toEndHogIndex:self.selectedHog]; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
108 |
} |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
109 |
else |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
110 |
{ |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
111 |
// update all hogs with new hat |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
112 |
NSInteger startIndex = 0; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
113 |
NSInteger endIndex = [[self.teamDictionary objectForKey:@"hedgehogs"] count] - 1; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
114 |
[self updateTeamDictionaryWithNewHat:newHat forStartHogIndex:startIndex toEndHogIndex:endIndex]; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
115 |
} |
3697 | 116 |
|
11182
55f20efae2e4
- Fix for consequent selection of same hat for all hogs in settings
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
117 |
// 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
|
118 |
[[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
|
119 |
|
3547 | 120 |
[self.navigationController popViewControllerAnimated:YES]; |
121 |
} |
|
122 |
||
11183
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
123 |
- (void)updateTeamDictionaryWithNewHat:(NSString *)newHat forStartHogIndex:(NSInteger)startIndex toEndHogIndex:(NSInteger)endIndex |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
124 |
{ |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
125 |
NSMutableArray *hogsArray = [self.teamDictionary objectForKey:@"hedgehogs"]; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
126 |
|
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
127 |
for (NSInteger i=startIndex; i <= endIndex; i++) |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
128 |
{ |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
129 |
NSDictionary *oldHog = [hogsArray objectAtIndex:i]; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
130 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog]; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
131 |
[newHog setObject:newHat forKey:@"hat"]; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
132 |
[hogsArray replaceObjectAtIndex:i withObject:newHog]; |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
133 |
} |
b2112ed988cb
- Added functionality to select one hat for all hogs at team settings
antonc27 <antonc27@mail.ru>
parents:
11182
diff
changeset
|
134 |
} |
3547 | 135 |
|
136 |
#pragma mark - |
|
137 |
#pragma mark Memory management |
|
12872
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
138 |
|
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11183
diff
changeset
|
139 |
- (void)didReceiveMemoryWarning { |
3971 | 140 |
MSG_MEMCLEAN(); |
3547 | 141 |
[super didReceiveMemoryWarning]; |
142 |
} |
|
143 |
||
144 |
@end |
|
145 |