
#import "PacManPreferencesBrain.h"
#import "PacManView.h"			// Need to send it the new -setScale: method
#import <stdio.h>
#import <string.h>

// Size of the play window when the game comes up if no default is set.
#define DEFAULTscale 1		// default is a 1 or 2

// The screen must be at least this size to use the "Big" option.
// Technically, the big window _does_ fit on 800x600, but you have to
// play with the window positioning, and it looks kind of ugly when
// you're done.  Thus I require the 1024x768 or larger settings.
// If you've got an 800x600 screen and _really_ want this option,
// then change these numbers...but it's probably not worth it.
#define MINWIDTHFORBIG 900.0
#define MINHEIGHTFORBIG 650.0

static BOOL screenTooSmall(id gameScreen)
{ // return YES if screen is too small for big size option, NO otherwise
	const NXScreen *theScreen;
	theScreen = [[gameScreen window] screen]; // screen with game window
	if (!theScreen) theScreen = [NXApp mainScreen]; // if window not on screen
	if ((NX_WIDTH(&(theScreen->screenBounds)) < MINWIDTHFORBIG) ||
		(NX_HEIGHT(&(theScreen->screenBounds)) < MINHEIGHTFORBIG)) return YES;
	return NO;
}

@implementation PacManPreferencesBrain

// Methods for the Preference we add (the screen size "scale", int: 1 or 2)
//  -(int)scale to query, - setScale:(int)newScale to set programmatically,
//  and - scaleChange:sender to change from the panel's controls.
- (int)scale { return scale; }

- setScale:(int)newScale
{	// keep the value in the accepted range
	// you should follow up any call to this method with a call to the
	// gamescreen's -setScale: method -- I don't call it from here!
	if (screenTooSmall(gameScreen)) newScale = 1;
	if ((newScale < 1) || (newScale > 2)) return self;
	scale = newScale;
	return self;
}

- scaleChange:sender
{	// sender must be a Matrix of radio buttons with the tags 1=small, 2=big
	scale = [[sender selectedCell] tag];
	if (screenTooSmall(gameScreen)) scale = 1;
	[gameScreen setScale:scale];
	return self;
}

- readDefaults:sender		// get preferences from defaults database
{	// This shows how to add other prefs values beyond what the GameKit
	// normally provides *and* how to override GameKit restrictions on
	// the size of these variables (speed in this case; GameKit restricts
	// it to the range 0-2, but I want 0-3 for PacMan.)
	int speedTemp = getIntPreference("GameSpeed", 0, 3, 1); // load w/my MAX
	scale = getIntPreference("Scale", 1, 2, DEFAULTscale); // a new pref
	if (screenTooSmall(gameScreen)) scale = 1; // keep with screen size.
	[super readDefaults:sender]; // get the GameKit versions
	speed = speedTemp;	// override the GameKit restrictions
    return self;
}

- writeDefaults:sender		// save preferences in defaults database
{
	[super writeDefaults:sender];
	// don't need to override speed here, just add the new pref.
	putIntPreference("Scale",	scale);
    return self;
}

- revert:sender	// return to default values
{
	[super revert:sender];
	scale = DEFAULTscale;	// this is the default value of the scale
	return [self preferences:self]; // update the panel
}

- refresh
{
	[super refresh]; // puts current prefs values into the panel
	// disable big/small if screen too small and turn on the right radio switch
	[scaleMatrix setEnabled:!screenTooSmall(gameScreen)];
    [scaleMatrix selectCellWithTag:scale];
	return self;
}

// The next three methods are added to load specific pictures into the
// background...  the pix must be in the app wrapper.  The methods all
// just ask the GameView to load the background image and then display it.

- back1:sender { return [gameScreen loadAnImage:"Gradation.eps"]; }
- back2:sender { return [gameScreen loadAnImage:"lush.tiff"]; }
- back3:sender { return [gameScreen loadAnImage:"Sunset.tiff"]; }

// This one sets the background to a solid black.  It's here by
// user request, even though it is functionally redundant.  That's
// why I put in the alert panel.  If people get used to dragging colors
// into game windows -- and it always works in the gamekit -- then I
// won't have to stick this into every game I make; instead they'll
// try the drag/drop first, rather than looking for a menu buried on
// the prefs panel.  :-)
- solidBlack:sender
{	// The user can get this by dragging a color chip into the
	// game window, but this isn't completely obvious, so we
	// provide this.  We'll pop up an alert the first time around
	// to let them know about drag and drop...
	NXPoint tempPoint = { 0.0, 0.0 }; // for temp use...
	if (alert && !getBOOLPreference("KnowDrag", NO)) {
		NXRunAlertPanel(
				[strings valueForStringKey:"DidjaKnow"],
				[strings valueForStringKey:"YouCanDrag"],
				[strings valueForStringKey:"Neato"], NULL, NULL);
		putBOOLPreference("KnowDrag", YES);
	}
	// now, simulate drag of a black chip into the gameview:
	[gameScreen acceptColor:NX_COLORBLACK atPoint:&tempPoint];
	return self;
}

@end
