2001-05-08 08:38:34 +00:00
|
|
|
/*
|
|
|
|
Preferences.m
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
Preferences class for Forge
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
Copyright (C) 2001 Jeff Teunissen <deek@quakeforge.net>
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
See the GNU General Public License for more details.
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
You should have received a copy of the GNU General Public
|
|
|
|
License along with this program; if not, write to:
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "qedefs.h"
|
|
|
|
|
|
|
|
#import "Preferences.h"
|
|
|
|
|
|
|
|
static NSDictionary *defaultValues (void) {
|
|
|
|
static NSDictionary *dict = nil;
|
|
|
|
if (!dict) {
|
|
|
|
dict = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
@"/Local/Forge/Projects", ProjectPath,
|
|
|
|
@"/Local/Forge/Sounds", BspSoundPath,
|
|
|
|
[NSNumber numberWithInt: 0], StartWad,
|
|
|
|
[NSNumber numberWithFloat: 1.0], XLight,
|
|
|
|
[NSNumber numberWithFloat: 0.6], YLight,
|
|
|
|
[NSNumber numberWithFloat: 0.75], ZLight,
|
|
|
|
[NSNumber numberWithBool: NO], ShowBSPOutput,
|
|
|
|
[NSNumber numberWithBool: NO], OffsetBrushCopy,
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
return dict;
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
@implementation Preferences
|
|
|
|
|
|
|
|
static Preferences *sharedInstance = nil;
|
|
|
|
|
|
|
|
- (id) objectForKey: (id) key
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
return [[[[self class] sharedInstance] preferences] objectForKey: key];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
2001-05-08 08:38:34 +00:00
|
|
|
|
|
|
|
+ (void) saveDefaults
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
if (sharedInstance) {
|
|
|
|
[Preferences savePreferencesToDefaults: [sharedInstance preferences]];
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) loadDefaults
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
if (currentValues)
|
|
|
|
[currentValues release];
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
currentValues = [[[self class] preferencesFromDefaults] copyWithZone: [self zone]];
|
|
|
|
[self discardDisplayedValues];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
+ (Preferences *) sharedInstance
|
|
|
|
{
|
|
|
|
return (sharedInstance ? sharedInstance : [[self alloc] init]);
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (id) init
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
if (sharedInstance) {
|
|
|
|
[self dealloc];
|
|
|
|
} else {
|
|
|
|
[super init];
|
|
|
|
currentValues = [[[self class] preferencesFromDefaults] copyWithZone:[self zone]];
|
|
|
|
[self discardDisplayedValues];
|
|
|
|
sharedInstance = self;
|
|
|
|
}
|
|
|
|
return sharedInstance;
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (NSDictionary *) preferences
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
return currentValues;
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) dealloc
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
/*
|
|
|
|
updateUI
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
Update the user interface with new preferences
|
|
|
|
*/
|
|
|
|
- (void) updateUI
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
qprintf ("defaults updated");
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
[map_i makeGlobalPerform: @selector(flushTextures)];
|
|
|
|
[quakeed_i updateAll];
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) prefsChanged: (id) sender {
|
|
|
|
static NSNumber *yes = nil;
|
|
|
|
static NSNumber *no = nil;
|
|
|
|
int anInt;
|
|
|
|
float aFloat;
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
if (!yes) {
|
|
|
|
yes = [[NSNumber alloc] initWithBool: YES];
|
|
|
|
no = [[NSNumber alloc] initWithBool: NO];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
[displayedValues setObject: [projectPathField stringValue] forKey: ProjectPath];
|
|
|
|
[displayedValues setObject: [bspSoundPathField stringValue] forKey: BspSoundPath];
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
if ((anInt = [startWadField intValue]) < 0 || anInt > 2) {
|
|
|
|
if ((anInt = [[displayedValues objectForKey: StartWad] intValue]) < 0 || anInt > 2) {
|
|
|
|
anInt = [[defaultValues () objectForKey: StartWad] intValue];
|
|
|
|
}
|
|
|
|
[startWadField setIntValue: anInt];
|
|
|
|
} else {
|
|
|
|
[displayedValues setObject: [NSNumber numberWithInt: anInt] forKey: StartWad];
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
if ((aFloat = [xLightField floatValue]) < 0.0 || aFloat > 1.0) {
|
|
|
|
if ((aFloat = [[displayedValues objectForKey: XLight] floatValue]) < 0.0 || aFloat > 1.0) {
|
|
|
|
aFloat = [[defaultValues () objectForKey: XLight] floatValue];
|
|
|
|
}
|
|
|
|
[xLightField setFloatValue: aFloat];
|
|
|
|
} else {
|
|
|
|
[displayedValues setObject: [NSNumber numberWithFloat: aFloat] forKey: XLight];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
if ((aFloat = [yLightField floatValue]) < 0.0 || aFloat > 1.0) {
|
|
|
|
if ((aFloat = [[displayedValues objectForKey: YLight] floatValue]) < 0.0 || aFloat > 1.0) {
|
|
|
|
aFloat = [[defaultValues () objectForKey: YLight] floatValue];
|
|
|
|
}
|
|
|
|
[yLightField setFloatValue: aFloat];
|
|
|
|
} else {
|
|
|
|
[displayedValues setObject: [NSNumber numberWithFloat: aFloat] forKey: YLight];
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
if ((aFloat = [zLightField floatValue]) < 0.0 || aFloat > 1.0) {
|
|
|
|
if ((aFloat = [[displayedValues objectForKey: YLight] floatValue]) < 0.0 || aFloat > 1.0) {
|
|
|
|
aFloat = [[defaultValues () objectForKey: YLight] floatValue];
|
|
|
|
}
|
|
|
|
[zLightField setFloatValue: aFloat];
|
|
|
|
} else {
|
|
|
|
[displayedValues setObject: [NSNumber numberWithFloat: aFloat] forKey: ZLight];
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
[displayedValues setObject: ([showBSPOutputButton state] ? yes : no) forKey: ShowBSPOutput];
|
|
|
|
[displayedValues setObject: ([offsetBrushCopyButton state] ? yes : no) forKey: OffsetBrushCopy];
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
[self commitDisplayedValues];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) commitDisplayedValues
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
if (currentValues != displayedValues) {
|
|
|
|
[currentValues release];
|
|
|
|
currentValues = [displayedValues copyWithZone: [self zone]];
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) discardDisplayedValues
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
if (currentValues != displayedValues) {
|
|
|
|
[displayedValues release];
|
|
|
|
displayedValues = [currentValues mutableCopyWithZone: [self zone]];
|
|
|
|
[self updateUI];
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) ok: (id) sender
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
[self commitDisplayedValues];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) revert: (id) sender
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
[self discardDisplayedValues];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
- (void) revertToDefault: (id) sender
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
if (currentValues)
|
|
|
|
[currentValues release];
|
|
|
|
|
|
|
|
currentValues = [defaultValues () copyWithZone: [self zone]];
|
|
|
|
[self discardDisplayedValues];
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
/***
|
|
|
|
Code to deal with defaults
|
|
|
|
***/
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
#define getBoolDefault(name) \
|
|
|
|
{ \
|
|
|
|
NSString *str = [defaults stringForKey: name]; \
|
|
|
|
[dict setObject: (str ? [NSNumber numberWithBool: [str hasPrefix: @"Y"]] : [defaultValues() objectForKey: name]) forKey: name]; \
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
2001-05-08 08:38:34 +00:00
|
|
|
|
|
|
|
#define getFloatDefault(name) \
|
|
|
|
{ \
|
|
|
|
NSString *str = [defaults stringForKey: name]; \
|
|
|
|
[dict setObject: (str ? [NSNumber numberWithFloat: [str floatValue]] : [defaultValues() objectForKey: name]) forKey: name]; \
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
2001-05-08 08:38:34 +00:00
|
|
|
|
|
|
|
#define getIntDefault(name) \
|
|
|
|
{ \
|
|
|
|
NSString *str = [defaults stringForKey: name]; \
|
|
|
|
[dict setObject: (str ? [NSNumber numberWithInt: [str intValue]] : [defaultValues() objectForKey: name]) forKey: name]; \
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
#define getStringDefault(name) \
|
|
|
|
{ \
|
|
|
|
NSString *str = [defaults stringForKey: name]; \
|
|
|
|
[dict setObject: (str ? str : [defaultValues() objectForKey: name]) forKey: name]; \
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
2001-05-08 08:38:34 +00:00
|
|
|
|
|
|
|
+ (NSDictionary *) preferencesFromDefaults
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 10];
|
|
|
|
|
|
|
|
getStringDefault(ProjectPath);
|
|
|
|
getStringDefault(BspSoundPath);
|
|
|
|
getIntDefault(StartWad);
|
|
|
|
getFloatDefault(XLight);
|
|
|
|
getFloatDefault(YLight);
|
|
|
|
getFloatDefault(ZLight);
|
|
|
|
getBoolDefault(ShowBSPOutput);
|
|
|
|
getBoolDefault(OffsetBrushCopy);
|
|
|
|
|
|
|
|
return dict;
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
2001-05-08 08:38:34 +00:00
|
|
|
|
|
|
|
#define setBoolDefault(name) \
|
|
|
|
{ \
|
|
|
|
if ([[defaultValues() objectForKey: name] isEqual: [dict objectForKey: name]]) \
|
|
|
|
[defaults removeObjectForKey: name]; \
|
|
|
|
else \
|
|
|
|
[defaults setBool:[[dict objectForKey:name] boolValue] forKey: name]; \
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
#define setFloatDefault(name) \
|
|
|
|
{ \
|
|
|
|
if ([[defaultValues() objectForKey: name] isEqual: [dict objectForKey: name]]) \
|
|
|
|
[defaults removeObjectForKey: name]; \
|
|
|
|
else \
|
|
|
|
[defaults setFloat:[[dict objectForKey:name] floatValue] forKey: name]; \
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
#define setIntDefault(name) \
|
|
|
|
{ \
|
|
|
|
if ([[defaultValues() objectForKey:name] isEqual:[dict objectForKey:name]]) \
|
|
|
|
[defaults removeObjectForKey:name]; \
|
|
|
|
else \
|
|
|
|
[defaults setInteger:[[dict objectForKey:name] intValue] forKey:name]; \
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
#define setStringDefault(name) \
|
|
|
|
{ \
|
|
|
|
if ([[defaultValues() objectForKey:name] isEqual: [dict objectForKey: name]]) \
|
|
|
|
[defaults removeObjectForKey: name]; \
|
|
|
|
else \
|
|
|
|
[defaults setObject: [[dict objectForKey: name] stringValue] forKey: name]; \
|
|
|
|
}
|
2001-02-09 01:52:44 +00:00
|
|
|
|
2001-05-08 08:38:34 +00:00
|
|
|
+ (void) savePreferencesToDefaults: (NSDictionary *) dict
|
2001-02-09 01:52:44 +00:00
|
|
|
{
|
2001-05-08 08:38:34 +00:00
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
setStringDefault(ProjectPath);
|
|
|
|
setStringDefault(BspSoundPath);
|
|
|
|
setIntDefault(StartWad);
|
|
|
|
setFloatDefault(XLight);
|
|
|
|
setFloatDefault(YLight);
|
|
|
|
setFloatDefault(ZLight);
|
|
|
|
setBoolDefault(ShowBSPOutput);
|
|
|
|
setBoolDefault(OffsetBrushCopy);
|
2001-02-09 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|