#import "AppDelegate.h"

#import "main.h"


@implementation Tipping_PointAppDelegate

@synthesize window=window_, glView=glView_;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{


[window_ setDelegate:self];

float fScreenWidth = [[NSScreen mainScreen] visibleFrame].size.width;

float fScreenHeight = [[NSScreen mainScreen] visibleFrame].size.height;


//Save the initial window size...

//float fWindowWidthSave = window_.frame.size.width;

//float fWindowHeightSave = window_.frame.size.height;

//Hardcode the window size to 1024 x 768

//The reason we do this is because the nodeToWorldTransform function doesn't perform correctly unless the initial window size is 1024x768

NSRect rWindowSize = NSMakeRect(window_.frame.origin.x, window_.frame.origin.y, 1024.0f, 768.0f + 22.0f);

[window_ setFrame:rWindowSize display:true];

CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];

// enable FPS and SPF

[director setDisplayStats:NO];

// connect the OpenGL view with the director

[director setView:glView_];


// EXPERIMENTAL stuff.

// 'Effects' don't work correctly when autoscale is turned on.

// Use kCCDirectorResize_NoScale if you don't want auto-scaling.

[director setResizeMode:kCCDirectorResize_AutoScale];

float fFrameWidth = window_.frame.size.width;

float fFrameHeight = window_.frame.size.height - 22;

[window_ setContentAspectRatio:NSMakeSize(4,3)];

// Enable "moving" mouse event. Default no.

[window_ setAcceptsMouseMovedEvents:NO];

// Center main window

[window_ center];


// create the main scene

mcMain = [MainScene node];

[mcMain FirstTime: fFrameWidth WindowHeight:fFrameHeight];

// and run it!

[director runWithScene: mcMain];


//Setup initial window size...

float fWindowX;

float fWindowY;

float fWindowWidth;

float fWindowHeight;


/*

//Restore the window back to it's initial size...

fWindowWidth = fWindowWidthSave;

fWindowHeight = fWindowHeightSave;

fWindowX = window_.frame.origin.x;

fWindowY = window_.frame.origin.y;

*/

//Set the window size to the maximum and center it horizontally...

//NSLog(@"AppDelegate  fScreenWidth=%f, fScreenHeight=%f", fScreenWidth, fScreenHeight);

fWindowHeight = fScreenHeight - 20.0f;

fWindowWidth = (fWindowHeight - 22.0f) / 3.0f * 4.0f;

fWindowX = (fScreenWidth - fWindowWidth) * 0.5f;

fWindowY = fScreenHeight * 0.5f;

fWindowY = 70.0f + 20.0f;

//NSLog(@"AppDelegate  fWindowX=%f, fWindowY=%f, fWindowHeight=%f, fWindowWidth=%f", fWindowX, fWindowY, fWindowHeight, fWindowWidth);


rWindowSize = NSMakeRect(fWindowX, fWindowY, fWindowWidth, fWindowHeight);

[window_ setFrame:rWindowSize display:true];

[mcMain Command:@"WindowSizeChange" Param1:[NSString stringWithFormat: @"%f", fWindowWidth] Param2:[NSString stringWithFormat: @"%f", fWindowHeight]];

}


- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) theApplication

{

return YES;

}


- (void)dealloc

{

[[CCDirector sharedDirector] end];

[window_ release];

[super dealloc];

}


#pragma mark AppDelegate - IBActions


- (IBAction)toggleFullScreen: (id)sender

{

CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];

[director setFullScreen: ! [director isFullScreen] ];

}


-(CGPoint) WindowSize

{

CGPoint pReturn = CGPointMake(window_.frame.size.width, window_.frame.size.height);

return pReturn;

}



-(NSSize)windowWillResize:(NSWindow *)sender

  toSize:(NSSize)framesize;

{

//NSLog(@"windowWillResize");

//NSSize currentSize = [sender frame].size;

[mcMain Command:@"WindowSizeChange" Param1:[NSString stringWithFormat: @"%f", framesize.width] Param2:[NSString stringWithFormat: @"%f", framesize.height]];

return framesize;


}



-(void)windowDidResize:(NSNotification *)notification

{

//NSLog(@"windowDidResize");

NSSize currentSize = [(NSWindow*)[notification object] frame].size;

//NSLog(@"windowDidResize  currentSize.width=%f, currentSize.height=%f", currentSize.width, currentSize.height);

[mcMain Command:@"WindowSizeChange" Param1:[NSString stringWithFormat: @"%f", currentSize.width] Param2:[NSString stringWithFormat: @"%f", currentSize.height]];

}



@end