project_files/HedgewarsMobile/Classes/WeaponSettingsViewController.m
author unc0rr
Tue, 11 Apr 2023 09:30:56 +0200
changeset 15951 5aed6c0b63e2
parent 12872 00215a7ec5f5
permissions -rw-r--r--
Start PoC of pas2rust conversion routine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
6700
e04da46ee43c the most important commit of the year
koda
parents: 6103
diff changeset
     3
 * Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com>
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3737
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: 3737
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3737
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3737
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: 3737
diff changeset
    17
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3737
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 "WeaponSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
#import "SingleWeaponViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
6832
fae8fd118da9 dates are no more!
koda
parents: 6822
diff changeset
    23
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
@implementation WeaponSettingsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
@synthesize listOfWeapons;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
    27
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
3547
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
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
    33
- (void)viewDidLoad {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
    [super viewDidLoad];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    35
3916
e7d665a4ef42 implemented endless turns support and added Timeless scheme (also fixed a crasher)
koda
parents: 3829
diff changeset
    36
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"")
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
                                                                   style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
                                                                  target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
                                                                  action:@selector(toggleEdit:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    self.navigationItem.rightBarButtonItem = editButton;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    41
11229
b49dfdf628f6 - Some strings localizations for Settings
antonc27 <antonc27@mail.ru>
parents: 11217
diff changeset
    42
    self.navigationItem.title = NSLocalizedString(@"List of weapons", nil);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
    45
- (void)viewWillAppear:(BOOL) animated {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    [super viewWillAppear:animated];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    47
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    self.listOfWeapons = array;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    51
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
// modifies the navigation bar to add the "Add" and "Done" buttons
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
    56
- (void)toggleEdit:(id)sender {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    BOOL isEditing = self.tableView.editing;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    [self.tableView setEditing:!isEditing animated:YES];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    59
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    if (isEditing) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
        [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
        self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
                                                                      style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
                                                                     target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
                                                                     action:@selector(addWeapon:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
        self.navigationItem.leftBarButtonItem = addButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
    75
- (void)addWeapon:(id)sender {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    77
6103
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6074
diff changeset
    78
    [CreationChamber createWeaponNamed:[fileName stringByDeletingPathExtension]];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    79
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    [self.listOfWeapons addObject:fileName];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    81
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    // order the array alphabetically, so schemes will keep their position
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    [self.listOfWeapons sortUsingSelector:@selector(compare:)];
4287
7dbdc862097c some interface tricks for iphone as well
koda
parents: 4281
diff changeset
    84
    [self.tableView reloadData];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    85
4287
7dbdc862097c some interface tricks for iphone as well
koda
parents: 4281
diff changeset
    86
    NSInteger index = [self.listOfWeapons indexOfObject:fileName];
7dbdc862097c some interface tricks for iphone as well
koda
parents: 4281
diff changeset
    87
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    return 1;
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
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    return [self.listOfWeapons count];
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
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   102
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    if (cell == nil) {
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
   105
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   107
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   108
    NSUInteger row = [indexPath row];
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   109
    NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   110
    cell.textLabel.text = rowString;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   112
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
// delete the row and the file
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
   117
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    NSUInteger row = [indexPath row];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   119
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
    [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   122
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
    [self.listOfWeapons removeObjectAtIndex:row];
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: 5208
diff changeset
   124
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
#pragma mark Table view delegate
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
   129
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
11217
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   130
{
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   131
    SingleWeaponViewController *singleWeaponViewController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   132
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
    NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   135
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    // this must be set so childController can load the correct plist
11217
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   137
    singleWeaponViewController.weaponName = [selectedWeaponFile stringByDeletingPathExtension];
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   138
    [singleWeaponViewController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
11217
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   140
    [self.navigationController pushViewController:singleWeaponViewController animated:YES];
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   141
    
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: 5208
diff changeset
   142
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
#pragma mark Memory management
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
   148
11217
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   149
-(void)didReceiveMemoryWarning
e68b3e392091 - Big refactoring of front-end Settings for both iPhone and iPad:
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   150
{
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
    [super didReceiveMemoryWarning];
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11229
diff changeset
   152
    MSG_MEMCLEAN();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156