-
Notifications
You must be signed in to change notification settings - Fork 24
/
GameListViewController.m
302 lines (259 loc) · 10.9 KB
/
GameListViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// GameListViewController.m
// Puzzles
//
// Created by Greg Hewgill on 8/03/13.
// Copyright (c) 2013 Greg Hewgill. All rights reserved.
//
#import "GameListViewController.h"
#import "GameViewController.h"
#import "GameHelpController.h"
#include "puzzles.h"
#include "descriptions.h"
//#define LAUNCH_IMAGE
NSMutableSet *g_InProgress;
static NSString *CellIdentifier = @"Cell";
@interface GameListViewCell: UICollectionViewCell
@end
@implementation GameListViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
if (@available(iOS 13.0, *)) {
self.contentView.backgroundColor = [UIColor tertiarySystemBackgroundColor];
} else {
self.contentView.backgroundColor = [UIColor whiteColor];
}
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 31)];
label.tag = 1;
label.font = [UIFont boldSystemFontOfSize:16];
label.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:label];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((self.contentView.frame.size.width-96)/2, 31, 96, 96)];
image.tag = 2;
[self.contentView addSubview:image];
UILabel *detail = [[UILabel alloc] initWithFrame:CGRectMake(5, 31+96, self.contentView.frame.size.width-10, 50)];
detail.tag = 3;
detail.font = [UIFont systemFontOfSize:14];
detail.numberOfLines = 0;
[self.contentView addSubview:detail];
UIImageView *inprogress = [[UIImageView alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width-50, 50, 40, 40)];
inprogress.tag = 4;
inprogress.image = [UIImage imageNamed:@"inprogress.png"];
[self.contentView addSubview:inprogress];
} else {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, self.contentView.frame.size.width-100, 26)];
label.tag = 1;
label.font = [UIFont boldSystemFontOfSize:20];
[self.contentView addSubview:label];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 96, 96)];
image.tag = 2;
[self.contentView addSubview:image];
UILabel *detail = [[UILabel alloc] initWithFrame:CGRectMake(100, 30, self.contentView.frame.size.width-100, self.contentView.frame.size.height-30)];
detail.tag = 3;
detail.font = [UIFont systemFontOfSize:14];
detail.numberOfLines = 0;
[self.contentView addSubview:detail];
UIImageView *inprogress = [[UIImageView alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width-40, 5, 40, 40)];
inprogress.tag = 4;
inprogress.image = [UIImage imageNamed:@"inprogress.png"];
[self.contentView addSubview:inprogress];
}
}
return self;
}
@end
@interface GameListViewController ()
@end
@implementation GameListViewController {
NSString *path;
NSDictionary *descriptions;
}
- (id)init
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
layout.itemSize = CGSizeMake(246, 31+96+50);
} else {
layout.itemSize = CGSizeMake(320, 100);
}
self = [super initWithCollectionViewLayout:layout];
if (self) {
// Custom initialization
self.title = @"Puzzles";
g_InProgress = [[NSMutableSet alloc] init];
path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
for (NSString *fn in files) {
if ([fn hasSuffix:@".save"]) {
[g_InProgress addObject:[fn substringToIndex:fn.length-5]];
}
}
NSMutableDictionary *descs = [[NSMutableDictionary alloc] init];
for (int i = 0; i < sizeof(GameDescriptions)/sizeof(GameDescriptions[0]); i++) {
descs[GameDescriptions[i][0]] = GameDescriptions[i][1];
}
descriptions = descs;
}
return self;
}
- (GameViewController *)savedGameViewController
{
GameViewController *gvc = nil;
NSString *lastgame = [[NSUserDefaults standardUserDefaults] stringForKey:@"lastgame"];
if (lastgame) {
int i = gamecount-1;
while (i >= 0) {
if ([[NSString stringWithUTF8String:gamelist[i]->name] isEqualToString:lastgame]) {
break;
}
i--;
}
if (i >= 0) {
gvc = [self gameViewControllerForGame:gamelist[i]];
}
}
return gvc;
}
- (GameViewController *)gameViewControllerForGame:(const game *)game
{
NSString *name = [NSString stringWithUTF8String:game->name];
BOOL inprogress = YES;
NSString *saved = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.save", path, name] encoding:NSUTF8StringEncoding error:NULL];
if (saved == nil) {
saved = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.new", path, name] encoding:NSUTF8StringEncoding error:NULL];
inprogress = NO;
}
return [[GameViewController alloc] initWithGame:game saved:saved inprogress:inprogress saver:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[GameListViewCell class] forCellWithReuseIdentifier:CellIdentifier];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
#ifndef LAUNCH_IMAGE
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Help" style:UIBarButtonItemStylePlain target:self action:@selector(showHelp)];
#endif
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"lastgame"];
[self.collectionView reloadData];
}
- (void)saveGame:(NSString *)name state:(NSString *)save inprogress:(BOOL)inprogress
{
if (inprogress) {
[g_InProgress addObject:name];
} else {
[g_InProgress removeObject:name];
}
[save writeToFile:[NSString stringWithFormat:@"%@/%@.%@", path, name, (inprogress ? @"save" : @"new")] atomically:YES encoding:NSUTF8StringEncoding error:NULL];
if (!inprogress) {
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.save", path, name] error:NULL];
}
}
- (void)showHelp
{
[self.navigationController pushViewController:[[GameHelpController alloc] initWithFile:@"help.html"] animated:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
// Return the number of sections.
#ifdef LAUNCH_IMAGE
return 0;
#endif
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return gamecount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
UIImageView *image = (UIImageView *)[cell viewWithTag:2];
UILabel *detail = (UILabel *)[cell viewWithTag:3];
UIImageView *inprogress = (UIImageView *)[cell viewWithTag:4];
// Configure the cell...
NSString *name = [NSString stringWithUTF8String:gamelist[indexPath.row]->name];
label.text = name;
detail.text = descriptions[name];
NSString *iconname = [[name stringByReplacingOccurrencesOfString:@" " withString:@""] lowercaseString];
if ([iconname isEqualToString:@"rectangles"]) {
iconname = @"rect";
}
image.image = [UIImage imageNamed:[iconname stringByAppendingString:@"-96d24.png"]];
inprogress.hidden = ![g_InProgress containsObject:name];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
const game *game = gamelist[indexPath.row];
NSString *name = [NSString stringWithUTF8String:game->name];
GameViewController *gvc = [self gameViewControllerForGame:game];
[self.navigationController pushViewController:gvc animated:YES];
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"lastgame"];
}
@end