project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m
changeset 6083 72c882c0fd0f
child 6084 e692c0348e74
equal deleted inserted replaced
6082:16ca7a7a6aa6 6083:72c882c0fd0f
       
     1 /*
       
     2  * Hedgewars-iOS, a Hedgewars port for iOS devices
       
     3  * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
       
     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
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
       
    17  *
       
    18  * File created on 03/10/2011.
       
    19  */
       
    20 
       
    21 #import "MissionTrainingViewController.h"
       
    22 #import <QuartzCore/QuartzCore.h>
       
    23 #import "GameInterfaceBridge.h"
       
    24 
       
    25 @implementation MissionTrainingViewController
       
    26 @synthesize listOfMissions, previewImage, tableView, descriptionLabel, missionFile;
       
    27 
       
    28 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    29     return rotationManager(interfaceOrientation);
       
    30 }
       
    31 
       
    32 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
       
    33 -(void) viewDidLoad {
       
    34     srand(time(NULL));
       
    35 
       
    36     NSString *imgName = (IS_IPAD()) ? @"mediumBackground~ipad.png" : @"smallerBackground~iphone.png";
       
    37     UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName];
       
    38     self.view.backgroundColor = [UIColor colorWithPatternImage:img];
       
    39     [img release];
       
    40     
       
    41     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL];
       
    42     self.listOfMissions = array;
       
    43     self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor];
       
    44     self.previewImage.layer.borderWidth = 3.8f;
       
    45     self.previewImage.layer.cornerRadius = 14;
       
    46 
       
    47     UIView *backView = [[UIView alloc] initWithFrame:self.tableView.frame];
       
    48     backView.backgroundColor = [UIColor darkBlueColorTransparent];
       
    49     [self.tableView setBackgroundView:backView];
       
    50     [backView release];
       
    51     self.tableView.backgroundColor = [UIColor clearColor];
       
    52     self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
       
    53     self.tableView.layer.borderWidth = 2;
       
    54     self.tableView.layer.cornerRadius = 8;
       
    55 
       
    56     self.descriptionLabel.textColor = [UIColor lightYellowColor];
       
    57     [super viewDidLoad];
       
    58 }
       
    59 
       
    60 -(void) viewWillAppear:(BOOL)animated {
       
    61     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:random()%[self.listOfMissions count] inSection:0];
       
    62     [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
    63     [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
       
    64     [super viewWillAppear:animated];
       
    65 }
       
    66 
       
    67 -(IBAction) buttonPressed:(id) sender {
       
    68     UIButton *button = (UIButton *)sender;
       
    69 
       
    70     if (button.tag == 0) {
       
    71         [AudioManagerController playBackSound];
       
    72         [[self parentViewController] dismissModalViewControllerAnimated:YES];
       
    73     } else {
       
    74 /*        GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] initWithController:self];
       
    75         [bridge startMissionGame:self.missionFile];
       
    76         [bridge release];*/
       
    77     }
       
    78 }
       
    79 
       
    80 #pragma mark Table view data source
       
    81 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
    82     return 1;
       
    83 }
       
    84 
       
    85 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    86     return [self.listOfMissions count];
       
    87 }
       
    88 
       
    89 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    90     static NSString *CellIdentifier = @"CellTr";
       
    91 
       
    92     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    93     if (cell == nil)
       
    94         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    95 
       
    96     cell.textLabel.text = [[[self.listOfMissions objectAtIndex:[indexPath row]] stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
       
    97     cell.textLabel.textColor = [UIColor lightYellowColor];
       
    98     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
       
    99     cell.textLabel.textAlignment = UITextAlignmentCenter;
       
   100     cell.textLabel.backgroundColor = [UIColor clearColor];
       
   101     cell.backgroundColor = [UIColor blackColorTransparent];
       
   102     return cell;
       
   103 }
       
   104 
       
   105 #pragma mark -
       
   106 #pragma mark Table view delegate
       
   107 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   108     NSString *missionName = [[self.listOfMissions objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
       
   109     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@@2x.png",GRAPHICS_DIRECTORY(),missionName];
       
   110     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
       
   111     [filePath release];
       
   112     [self.previewImage setImage:img];
       
   113     [img release];
       
   114 
       
   115     self.descriptionLabel.text = nil;
       
   116     NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
       
   117     NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL];
       
   118     [descLocation release];
       
   119     NSArray *descArray =  [descComplete componentsSeparatedByString:@"\n"];
       
   120     [descComplete release];
       
   121     for (NSString *str in descArray) {
       
   122         if ([str hasPrefix:missionName]) {
       
   123             NSArray *descriptionText = [str componentsSeparatedByString:@"\""];
       
   124             self.descriptionLabel.text = [descriptionText objectAtIndex:1];
       
   125         }
       
   126     }
       
   127 
       
   128     NSString *missionPath = [[NSString alloc] initWithFormat:@"%@/%@.lua",TRAININGS_DIRECTORY(),missionName];
       
   129     self.missionFile = missionPath;
       
   130     [missionPath release];
       
   131 }
       
   132 
       
   133 #pragma mark -
       
   134 #pragma mark Memory management
       
   135 -(void) didReceiveMemoryWarning {
       
   136     previewImage = nil;
       
   137     missionFile = nil;
       
   138     [super didReceiveMemoryWarning];
       
   139 }
       
   140 
       
   141 -(void) viewDidUnload {
       
   142     self.listOfMissions = nil;
       
   143     self.previewImage = nil;
       
   144     self.tableView = nil;
       
   145     self.descriptionLabel = nil;
       
   146     self.missionFile = nil;
       
   147     MSG_DIDUNLOAD();
       
   148     [super viewDidUnload];
       
   149 }
       
   150 
       
   151 
       
   152 -(void) dealloc {
       
   153     releaseAndNil(listOfMissions);
       
   154     releaseAndNil(previewImage);
       
   155     releaseAndNil(tableView);
       
   156     releaseAndNil(descriptionLabel);
       
   157     releaseAndNil(missionFile);
       
   158     [super dealloc];
       
   159 }
       
   160 
       
   161 
       
   162 @end