Source/iPhoneAppDelegate.m
author Jens Alfke <jens@mooseyard.com>
Wed May 28 12:47:10 2008 -0700 (2008-05-28)
changeset 8 45c82a071aca
parent 4 d781b00f3ed4
child 9 a59acc683080
permissions -rwxr-xr-x
* Got it working with latest iPhone SDK.
* Fixed some text alignment issues that showed up on PlayingCards.
* Working on persistence and move-tracking for Game.
jens@1
     1
//
jens@2
     2
//  iPhoneAppDelegate.m
jens@1
     3
//  GGB-iPhone
jens@1
     4
//
jens@1
     5
//  Created by Jens Alfke on 3/7/08.
jens@1
     6
//  Copyright __MyCompanyName__ 2008. All rights reserved.
jens@1
     7
//
jens@1
     8
jens@2
     9
#import "iPhoneAppDelegate.h"
jens@1
    10
#import "BoardUIView.h"
jens@1
    11
#import "Game.h"
jens@1
    12
#import "QuartzUtils.h"
jens@1
    13
#import "GGBUtils.h"
jens@1
    14
jens@1
    15
jens@1
    16
@implementation GGB_iPhoneAppDelegate
jens@1
    17
jens@1
    18
jens@1
    19
@synthesize window=_window;
jens@1
    20
@synthesize contentView=_contentView;
jens@1
    21
@synthesize headline=_headline;
jens@1
    22
jens@1
    23
jens@1
    24
- (void)applicationDidFinishLaunching:(UIApplication *)application 
jens@1
    25
{	
jens@4
    26
    for( NSString *family in [UIFont familyNames] )
jens@4
    27
        NSLog(@"%@: (%@)", family, [[UIFont fontNamesForFamilyName: family] componentsJoinedByString: @", "]);
jens@4
    28
        
jens@1
    29
    // Create window
jens@1
    30
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
jens@1
    31
    _window.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
jens@1
    32
    
jens@1
    33
    // Set up content view
jens@1
    34
    CGRect rHeadline,rContent;
jens@1
    35
    CGRectDivide([[UIScreen mainScreen] applicationFrame],
jens@1
    36
                 &rHeadline, &rContent, 35, CGRectMinYEdge);
jens@1
    37
    
jens@1
    38
    self.contentView = [[[BoardUIView alloc] initWithFrame: rContent] autorelease];
jens@1
    39
    [_window addSubview: _contentView];
jens@1
    40
    
jens@1
    41
    self.headline = [[[UILabel alloc] initWithFrame: rHeadline] autorelease];
jens@1
    42
    _headline.backgroundColor = nil;
jens@1
    43
    _headline.opaque = NO;
jens@1
    44
    _headline.textAlignment = UITextAlignmentCenter;
jens@1
    45
    _headline.font = [UIFont boldSystemFontOfSize: 20];
jens@1
    46
    _headline.minimumFontSize = 14;
jens@8
    47
    _headline.adjustsFontSizeToFitWidth = YES;
jens@1
    48
    [_window addSubview: _headline];
jens@1
    49
    
jens@1
    50
    // Start game:
jens@4
    51
    [self startGameNamed: @"KlondikeGame"];
jens@1
    52
    
jens@1
    53
    // Show window
jens@1
    54
    [_window makeKeyAndVisible];
jens@1
    55
}
jens@1
    56
jens@1
    57
jens@1
    58
- (void)dealloc 
jens@1
    59
{
jens@1
    60
    [_contentView release];
jens@1
    61
    [_headline release];
jens@1
    62
    [_window release];
jens@1
    63
    [super dealloc];
jens@1
    64
}
jens@1
    65
jens@1
    66
jens@1
    67
- (void) startGameNamed: (NSString*)gameClassName
jens@1
    68
{
jens@1
    69
    Game *game = _contentView.game;
jens@1
    70
    [game removeObserver: self  forKeyPath: @"currentPlayer"];
jens@1
    71
    [game removeObserver: self forKeyPath: @"winner"];
jens@1
    72
    
jens@1
    73
    if( gameClassName == nil )
jens@8
    74
        gameClassName = [[game class] description];
jens@1
    75
    
jens@1
    76
    [_contentView startGameNamed: gameClassName];
jens@1
    77
    
jens@1
    78
    game = _contentView.game;
jens@1
    79
    [game addObserver: self 
jens@1
    80
           forKeyPath: @"currentPlayer"
jens@1
    81
              options: NSKeyValueObservingOptionInitial
jens@1
    82
              context: NULL];
jens@1
    83
    [game addObserver: self
jens@1
    84
           forKeyPath: @"winner"
jens@1
    85
              options: 0 
jens@1
    86
              context: NULL];
jens@1
    87
}
jens@1
    88
jens@1
    89
jens@1
    90
- (void)observeValueForKeyPath:(NSString *)keyPath 
jens@1
    91
                      ofObject:(id)object 
jens@1
    92
                        change:(NSDictionary *)change
jens@1
    93
                       context:(void *)context
jens@1
    94
{
jens@1
    95
    Game *game = _contentView.game;
jens@1
    96
    if( object == game ) {
jens@1
    97
        Player *p = game.winner;
jens@1
    98
        NSString *msg;
jens@1
    99
        if( p ) {
jens@1
   100
            PlaySound(@"Sosumi");
jens@1
   101
            msg = @"%@ wins!";
jens@1
   102
        } else {
jens@1
   103
            p = game.currentPlayer;
jens@1
   104
            msg = @"Your turn, %@";
jens@1
   105
        }
jens@1
   106
        _headline.text = [NSString stringWithFormat: msg, p.name];
jens@1
   107
        
jens@1
   108
        if( game.winner ) {
jens@1
   109
            UIAlertView *alert;
jens@1
   110
            alert = [[UIAlertView alloc] initWithTitle: msg
jens@1
   111
                                               message: @"Congratulations!"
jens@1
   112
                                              delegate:self
jens@8
   113
                                     cancelButtonTitle:nil 
jens@8
   114
                                     otherButtonTitles:nil];
jens@1
   115
            [alert show];
jens@1
   116
            [alert release];
jens@1
   117
        }            
jens@1
   118
    }
jens@1
   119
}
jens@1
   120
jens@1
   121
jens@8
   122
- (void)alertView:(UIAlertView *)modalView didDismissWithButtonIndex:(NSInteger)buttonIndex;
jens@1
   123
{
jens@1
   124
    // Start new game:
jens@1
   125
    [self startGameNamed: nil];
jens@1
   126
}
jens@1
   127
jens@1
   128
jens@1
   129
@end