project_files/HedgewarsMobile/Classes/MasterViewController.m
author smaxx
Wed, 04 Aug 2010 17:35:45 +0200
changeset 3724 b8678667e7dc
parent 3701 8c449776ebe6
child 3737 2ba6ac8a114b
permissions -rw-r--r--
Engine: * Added Lua's table module
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  MasterViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 27/03/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "MasterViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "DetailViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import "GeneralSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
#import "TeamSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
#import "WeaponSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
#import "SchemeSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
@implementation MasterViewController
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    18
@synthesize targetController, controllerNames, lastIndexPath;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
    [super viewDidLoad];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    30
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    // the list of selectable controllers
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    32
    NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"General",@""),
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    33
                                                      NSLocalizedString(@"Teams",@""),
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    34
                                                      NSLocalizedString(@"Weapons",@""),
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    35
                                                      NSLocalizedString(@"Schemes",@""),
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    36
                                                      nil];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    37
    self.controllerNames = array;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    38
    [array release];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    39
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    40
    // targetControllers tells whether we're on the right or left side of the splitview -- on iphone we only use the right side
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    41
    if (targetController == nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    42
        if (nil == generalSettingsViewController)
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    43
            generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    44
        generalSettingsViewController.navigationItem.hidesBackButton = YES;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    45
        [self.navigationController pushViewController:generalSettingsViewController animated:NO];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    46
    } else {
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    47
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    48
                                                                                              target:self
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    49
                                                                                              action:@selector(dismissSplitView)];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    50
    }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    return [controllerNames count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    66
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    71
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    72
    cell.textLabel.text = [controllerNames objectAtIndex:[indexPath row]];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    73
    if (nil == targetController) {
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    74
        UIImage *icon = [[UIImage alloc] initWithContentsOfFile:@"Icon-Small.png"];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    75
        cell.imageView.image = icon;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    76
        [icon release];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    77
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    78
    } else
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    79
        cell.accessoryType = UITableViewCellAccessoryNone;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    80
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    UIViewController *nextController = nil;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    90
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
        [self.tableView deselectRowAtIndexPath:lastIndexPath animated:YES];
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    93
        [targetController.navigationController popToRootViewControllerAnimated:NO];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    94
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
        switch (newRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
            case 0:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
                if (nil == generalSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
                    generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
                nextController = generalSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
            case 1:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
                if (nil == teamSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
                    teamSettingsViewController = [[TeamSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
                nextController = teamSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
            case 2:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
                if (nil == weaponSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
                    weaponSettingsViewController = [[WeaponSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
                nextController = weaponSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
            case 3:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
                if (nil == schemeSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
                    schemeSettingsViewController = [[SchemeSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
                nextController = schemeSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   117
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
        nextController.title = [controllerNames objectAtIndex:newRow];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   121
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   122
        if (nil == targetController) {
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   123
            nextController.navigationItem.hidesBackButton = NO;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   124
            [self.navigationController pushViewController:nextController animated:YES];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   125
        } else {
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   126
            nextController.navigationItem.hidesBackButton = YES;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   127
            [targetController.navigationController pushViewController:nextController animated:NO];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   128
        }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
    [super didReceiveMemoryWarning];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   138
    // Relinquish ownership any cached data, images, etc that aren't in use.
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    if (generalSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
        generalSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    if (teamSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
        teamSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
    if (weaponSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
        weaponSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    if (schemeSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
        schemeSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    MSG_MEMCLEAN();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
-(void) viewDidUnload {
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   151
    self.targetController = nil;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    self.controllerNames = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    generalSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    teamSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    weaponSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    schemeSettingsViewController = 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
   158
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
-(void) dealloc {
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   163
    targetController = nil;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
    [controllerNames release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
    [generalSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
    [teamSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    [weaponSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
    [schemeSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
-(IBAction) dismissSplitView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissModalView" object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178