Source/DemoBoardView.m
changeset 0 e9f7ba4718e1
child 1 3eb7be1dd7b6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Source/DemoBoardView.m	Fri Mar 07 11:43:02 2008 -0800
     1.3 @@ -0,0 +1,137 @@
     1.4 +/*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     1.5 +    http://developer.apple.com/samplecode/GeekGameBoard/
     1.6 +    Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     1.7 +
     1.8 +    Redistribution and use in source and binary forms, with or without modification, are permitted
     1.9 +    provided that the following conditions are met:
    1.10 +
    1.11 +    * Redistributions of source code must retain the above copyright notice, this list of conditions
    1.12 +      and the following disclaimer.
    1.13 +    * Redistributions in binary form must reproduce the above copyright notice, this list of
    1.14 +      conditions and the following disclaimer in the documentation and/or other materials provided
    1.15 +      with the distribution.
    1.16 +
    1.17 +    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    1.18 +    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    1.19 +    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    1.20 +    BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.21 +    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    1.22 +    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    1.23 +    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    1.24 +    THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.25 +*/
    1.26 +#import "DemoBoardView.h"
    1.27 +#import "Game.h"
    1.28 +#import "QuartzUtils.h"
    1.29 +
    1.30 +
    1.31 +/**  WARNING: THIS CODE REQUIRES GARBAGE COLLECTION!
    1.32 + **  This sample application uses Objective-C 2.0 garbage collection.
    1.33 + **  Therefore, the source code in this file does NOT perform manual object memory management.
    1.34 + **  If you reuse any of this code in a process that isn't garbage collected, you will need to
    1.35 + **  add all necessary retain/release/autorelease calls, and implement -dealloc methods,
    1.36 + **  otherwise unpleasant leakage will occur!
    1.37 + **/
    1.38 +
    1.39 +
    1.40 +@implementation DemoBoardView
    1.41 +
    1.42 +
    1.43 +/** Class names of available games */
    1.44 +static NSString* const kMenuGameNames[] = {@"KlondikeGame", @"CheckersGame", @"HexchequerGame",
    1.45 +                                           @"TicTacToeGame", @"GoGame"};
    1.46 +
    1.47 +/** Class name of the current game. */
    1.48 +static NSString* sCurrentGameName = @"KlondikeGame";
    1.49 +
    1.50 +
    1.51 +- (void) startGameNamed: (NSString*)gameClassName
    1.52 +{
    1.53 +    [super startGameNamed: gameClassName];
    1.54 +    
    1.55 +    Game *game = self.game;
    1.56 +    [game addObserver: self 
    1.57 +           forKeyPath: @"currentPlayer"
    1.58 +              options: NSKeyValueObservingOptionInitial
    1.59 +              context: NULL];
    1.60 +    [game addObserver: self
    1.61 +           forKeyPath: @"winner"
    1.62 +              options: 0 
    1.63 +              context: NULL];
    1.64 +    
    1.65 +    self.window.title = [(id)[game class] displayName];
    1.66 +}
    1.67 +
    1.68 +
    1.69 +- (CGRect) gameBoardFrame
    1.70 +{
    1.71 +    CGRect bounds = [super gameBoardFrame];
    1.72 +    bounds.size.height -= 32;                   // Leave room for headline
    1.73 +    return CGRectInset(bounds,4,4);
    1.74 +}
    1.75 +
    1.76 +
    1.77 +- (void) awakeFromNib
    1.78 +{
    1.79 +    srandomdev();
    1.80 +    
    1.81 +    [self registerForDraggedTypes: [NSImage imagePasteboardTypes]];
    1.82 +    [self registerForDraggedTypes: [NSArray arrayWithObject: NSFilenamesPboardType]];
    1.83 +    
    1.84 +    CGRect bounds = self.layer.bounds;
    1.85 +    self.layer.backgroundColor = GetCGPatternNamed(@"/Library/Desktop Pictures/Small Ripples graphite.png");
    1.86 +        
    1.87 +    bounds.size.height -= 32;
    1.88 +    _headline = AddTextLayer(self.layer,
    1.89 +                             nil, [NSFont boldSystemFontOfSize: 24], 
    1.90 +                             kCALayerWidthSizable | kCALayerMinYMargin);
    1.91 +    
    1.92 +    [self startGameNamed: sCurrentGameName];
    1.93 +}
    1.94 +
    1.95 +
    1.96 +- (void) startGameFromMenu: (id)sender
    1.97 +{
    1.98 +    sCurrentGameName = kMenuGameNames[ [sender tag] ];
    1.99 +    [self startGameNamed: sCurrentGameName];
   1.100 +}
   1.101 +
   1.102 +
   1.103 +- (void)observeValueForKeyPath:(NSString *)keyPath 
   1.104 +                      ofObject:(id)object 
   1.105 +                        change:(NSDictionary *)change
   1.106 +                       context:(void *)context
   1.107 +{
   1.108 +    Game *game = self.game;
   1.109 +    if( object == game ) {
   1.110 +        Player *p = game.winner;
   1.111 +        NSString *msg;
   1.112 +        if( p ) {
   1.113 +            [[NSSound soundNamed: @"Sosumi"] play];
   1.114 +            msg = @"%@ wins! Congratulations!";
   1.115 +        } else {
   1.116 +            p = game.currentPlayer;
   1.117 +            msg = @"Your turn, %@";
   1.118 +        }
   1.119 +        _headline.string = [NSString stringWithFormat: msg, p.name];
   1.120 +    }
   1.121 +}
   1.122 +
   1.123 +
   1.124 +- (IBAction) enterFullScreen: (id)sender
   1.125 +{
   1.126 +    [super enterFullScreen: sender];
   1.127 +    [self startGameNamed: sCurrentGameName];        // restart game so it'll use the new size
   1.128 +}
   1.129 +
   1.130 +
   1.131 +#pragma mark -
   1.132 +#pragma mark NSAPPLICATION DELEGATE:
   1.133 +
   1.134 +
   1.135 +- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
   1.136 +{
   1.137 +    return YES;
   1.138 +}
   1.139 +
   1.140 +@end