From 9585b6af925b44f7125301cbada6f47f37bc325d Mon Sep 17 00:00:00 2001 From: Jeff Teunissen Date: Fri, 25 Jan 2002 08:45:28 +0000 Subject: [PATCH] Forge updates. Preferences is no more, since each bundle will have its own prefs anyway. The "sample" bundle MainPrefs is more complete, and is now loading its interface from a .gorm file -- this would be a .nib on NeXTstep or Mac OS X, and it should be ready to run on those OSes already -- since it doesn't do much. :) --- tools/Forge/Bundles/MainPrefs/GNUmakefile | 4 + .../Forge/Bundles/MainPrefs/MainPrefs.classes | 84 +++++ tools/Forge/Bundles/MainPrefs/MainPrefs.gorm | Bin 0 -> 3279 bytes tools/Forge/Bundles/MainPrefs/MainPrefs.h | 10 +- tools/Forge/Bundles/MainPrefs/MainPrefs.m | 147 +++++++- tools/Forge/Bundles/MainPrefs/MainPrefsView.h | 10 + tools/Forge/Bundles/MainPrefs/MainPrefsView.m | 47 ++- tools/Forge/Forge.classes | 17 +- tools/Forge/GNUmakefile | 2 - tools/Forge/Preferences.h | 89 ----- tools/Forge/Preferences.m | 335 ------------------ 11 files changed, 284 insertions(+), 461 deletions(-) create mode 100644 tools/Forge/Bundles/MainPrefs/MainPrefs.classes create mode 100644 tools/Forge/Bundles/MainPrefs/MainPrefs.gorm delete mode 100644 tools/Forge/Preferences.h delete mode 100644 tools/Forge/Preferences.m diff --git a/tools/Forge/Bundles/MainPrefs/GNUmakefile b/tools/Forge/Bundles/MainPrefs/GNUmakefile index 392b57d98..7fb1feab2 100644 --- a/tools/Forge/Bundles/MainPrefs/GNUmakefile +++ b/tools/Forge/Bundles/MainPrefs/GNUmakefile @@ -4,6 +4,10 @@ BUNDLE_NAME= MainPrefs BUNDLE_EXTENSION= .forgeb BUNDLE_INSTALL_DIR= $(GNUSTEP_LOCAL_ROOT)/Library/Forge +MainPrefs_MAIN_MODEL_FILE= MainPrefs.gorm +MainPrefs_RESOURCE_FILES= \ + MainPrefs.gorm + MainPrefs_OBJC_FILES= \ MainPrefsView.m \ MainPrefs.m diff --git a/tools/Forge/Bundles/MainPrefs/MainPrefs.classes b/tools/Forge/Bundles/MainPrefs/MainPrefs.classes new file mode 100644 index 000000000..3e791e446 --- /dev/null +++ b/tools/Forge/Bundles/MainPrefs/MainPrefs.classes @@ -0,0 +1,84 @@ +{ + IBInspector = { + Actions = ( + ok:, + revert:, + touch: + ); + Outlets = ( + window + ); + Super = NSObject; + }; + MainPrefs = { + Actions = ( + loadPrefs:, + projectPathChanged:, + projectPathFindButtonClicked:, + resetPrefsToDefault:, + savePrefs: + ); + Outlets = ( + projectPathField, + projectPathFindButton, + view, + window + ); + Super = NSObject; + }; + NSApplication = { + Outlets = ( + delegate + ); + Super = NSResponder; + }; + NSButton = { + Super = NSControl; + }; + NSControl = { + Actions = ( + takeDoubleValueFrom:, + takeFloatValueFrom:, + takeIntValueFrom:, + takeObjectValueFrom:, + takeStringValueFrom: + ); + Outlets = ( + target + ); + Super = NSView; + }; + NSMenu = { + Super = NSObject; + }; + NSMenuItem = { + Outlets = ( + target + ); + Super = NSObject; + }; + NSPanel = { + Super = NSWindow; + }; + NSResponder = { + Super = NSObject; + }; + NSSlider = { + Super = NSControl; + }; + NSTextField = { + Outlets = ( + delegate + ); + Super = NSControl; + }; + NSView = { + Super = NSResponder; + }; + NSWindow = { + Outlets = ( + delegate + ); + Super = NSResponder; + }; +} \ No newline at end of file diff --git a/tools/Forge/Bundles/MainPrefs/MainPrefs.gorm b/tools/Forge/Bundles/MainPrefs/MainPrefs.gorm new file mode 100644 index 0000000000000000000000000000000000000000..004edfe335d8ab750a61daab3d591d8937d2cebb GIT binary patch literal 3279 zcmcguOLHPs6uxwT5Fmj(z==*Ad`zOF!P6O>Xw(GgNPHwrYKXc`2ii$1MW;F)GwxF5 zALyF3Ys+P`aG$L!{V(n<&$$O~8UhwtmQZ!;+;bo2-0%DP-m_Q#;@q>&3Z~mUwZF3f ze;b>dr7bc5^hZ6TWc3VyEr{q5F}K&K+b4$8@l3m8xdMN>CKC0=y3j8&pc^cyLQvFosQ{#PlI|?r0b1ApIF6#1Wf5M+Nt5WcIPR2BGNy7AI(?}n`sTh zTw+v+c4M?;Mh(qq2bj;HYkhqVF&<3PV3O7)qvA+d;+gP7P1tC)MN5<*uE#~H-Z-+J zc{ST=w{T`epB6Lq1|83J+R~8_@p|J++q#s!8CvngIzMwdEqVHgpfJME=TqftAyUSk z5c!nx8=4jDqnn0h6LM=p{v&h<;rlSHC&iostm3*RW0exq^+vzTaBw_G=xLD&C2v@5 z&UcpJtw_X`Z;sB;?+KV;faVo?^V*^75PgMx$7~f2P4Co)q@JN=HN=9l6p-R&F|{nF zRb1O_6U6aU<(MY%FKqk0v+{{Yv=Req7>#jV&G&tV`gstCw4Nns45#go2B!qfa(2rq zyJb|1VYclP*YvE`lf9~(+&D9vR%+)8Ik{;lYvxJ$V6K}DW^c$FY0+#tp8p&&6N&C2 zWV>-b1uCObz_8UnNifx8G%G(33r4wz1vB4GN}%Lku+UAzA}0`4=Z|WnY#)}o8OZ%T zb!DM`m5|-1E;2wN^?IIhTn#Z9rY;M439JH|X`uMS47@NY1DFjmaAzV0Hefl#K=nrq zYeXdjKM@1Rw;1TVhZ%TrfPt5gfxEXsKC->Gd^}_R<4y@_( z^wx2Yc)xi^jNeI=MJdWF66N(RQTE-#qI_cj$~W}|dJH(m&Gr9)lM279HM`R)7K?tc zG)k!h-~TwJ%D1}TL`7JsYw#9_6h6G&w=_97OWVb%LFvId4~Cz0^*Tc_q_*<4g2?#w zY7|7uEg_0%{2}2}`_2GFfNIk@YC_$d32onLnpCrd02u&5UV#7p5-h^Tq=I`mso?k$ zEb&>wg8K@&buQsueUSvmJqfpZhX{Rn`C^6g2LX$v5K)qmGtl`9Lc9xD0zB$Ulw`Rx zYSmD=)88yC(*@*UlR228%3&%f25b1DzsHC?xc(uvqZ$>X=exoj-XD9!t+68?j2p@G z^Haq@`Bd$0S;$77yDuE%nwgGZ<{xtAY05keAL&a}wsAq+!TL!SBSHiv*yf1gkE~a` z`mobo3f6;?4`nn~$*7=lkBllBcgQf%$dIv%MvRP)^_*Ce!5&}WtKwmi8VJ7xucjU>g(sFz>O6f#O + #import "BundleController.h" #import "PrefsView.h" +#define ProjectPath @"projectPath" + @interface MainPrefs: NSObject { - NSView *view; + IBOutlet id projectPathField; + IBOutlet id projectPathFindButton; + + IBOutlet id window; + IBOutlet id view; } @end diff --git a/tools/Forge/Bundles/MainPrefs/MainPrefs.m b/tools/Forge/Bundles/MainPrefs/MainPrefs.m index 00398d46e..31549d99a 100644 --- a/tools/Forge/Bundles/MainPrefs/MainPrefs.m +++ b/tools/Forge/Bundles/MainPrefs/MainPrefs.m @@ -39,11 +39,98 @@ static const char rcsid[] = # import #endif +#import + #import "PrefsPanel.h" #import "PrefsController.h" #import "MainPrefs.h" #import "MainPrefsView.h" +@interface MainPrefs (Private) + +- (NSDictionary *) preferencesFromDefaults; +- (void) savePreferencesToDefaults: (NSDictionary *) dict; + +- (void) commitDisplayedValues; +- (void) discardDisplayedValues; + +- (void) updateUI; + +@end + +@implementation MainPrefs (Private) + +static NSDictionary *currentValues = nil; +static NSMutableDictionary *displayedValues = nil; + +static NSMutableDictionary * +defaultValues (void) { + static NSMutableDictionary *dict = nil; + + if (!dict) { + dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: + @"/Local/Forge/Projects", ProjectPath, + nil]; + } + return dict; +} + +static NSString * +getStringDefault (NSMutableDictionary *dict, NSString *name) +{ + NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name]; + + if (!str) + str = [defaultValues() objectForKey: name]; + + [dict setObject: str forKey: name]; + + return str; +} + +- (NSDictionary *) preferencesFromDefaults +{ + NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 5]; + + getStringDefault (dict, ProjectPath); + return dict; +} + +- (void) savePreferencesToDefaults: (NSDictionary *) dict +{ + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + +#define setStringDefault(name) \ + [defaults setObject: [dict objectForKey: (name)] forKey: (name)] + + NSDebugLog (@"Updating Main Preferences..."); + setStringDefault (ProjectPath); + [defaults synchronize]; +} + +- (void) commitDisplayedValues +{ + [currentValues release]; + currentValues = [[displayedValues copy] retain]; + [self savePreferencesToDefaults: currentValues]; + [self updateUI]; +} + +- (void) discardDisplayedValues +{ + [displayedValues release]; + displayedValues = [[currentValues mutableCopy] retain]; + [self updateUI]; +} + +- (void) updateUI +{ + [projectPathField setStringValue: [displayedValues objectForKey: ProjectPath]]; + [projectPathField setNeedsDisplay: YES]; +} + +@end // MainPrefs (Private) + @implementation MainPrefs static MainPrefs *sharedInstance = nil; @@ -57,17 +144,19 @@ static id owner = nil; self = [super init]; owner = anOwner; [owner registerPrefsController: self]; - -#ifdef USING_NIBS if (![NSBundle loadNibNamed: @"MainPrefsView" owner: self]) { NSLog (@"MainPrefs: Could not load nib \"MainPrefsView\", using compiled version"); -#endif - view = [[MainPrefsView alloc] initWithFrame: PrefsRect]; -#ifdef USING_NIBS - } -#endif + view = [[MainPrefsView alloc] initWithOwner: self andFrame: PrefsRect]; + // hook up to our outlet(s) + projectPathField = [view directoryField]; + } else { + view = [window contentView]; + } [view retain]; + + [self loadPrefs: self]; + sharedInstance = self; } return sharedInstance; @@ -75,17 +164,26 @@ static id owner = nil; - (void) loadPrefs: (id) sender { - return; + if (currentValues) + [currentValues release]; + + currentValues = [[[self preferencesFromDefaults] copyWithZone: [self zone]] retain]; + [self discardDisplayedValues]; } - (void) savePrefs: (id) sender { - return; + [self commitDisplayedValues]; } - (void) resetPrefsToDefault: (id) sender { - return; + if (currentValues) + [currentValues release]; + + currentValues = [[defaultValues () copyWithZone: [self zone]] retain]; + + [self discardDisplayedValues]; } - (void) showView: (id) sender; @@ -114,4 +212,33 @@ static id owner = nil; return @selector(showView:); } +- (id) projectPath +{ + return [displayedValues objectForKey: ProjectPath]; +} + +- (id) projectPathFindButtonClicked: (id) sender +{ + int result; + NSOpenPanel *oPanel = [NSOpenPanel openPanel]; + + [oPanel setAllowsMultipleSelection: NO]; + [oPanel setCanChooseFiles: NO]; + [oPanel setCanChooseDirectories: YES]; + + result = [oPanel runModalForDirectory: NSHomeDirectory() file: nil types: nil]; + if (result == NSOKButton) { // got a new dir + NSArray *pathArray = [oPanel filenames]; + + [displayedValues setObject: [pathArray objectAtIndex: 0] forKey: ProjectPath]; + [self updateUI]; + } +} + +- (void) projectPathChanged: (id) sender +{ + [displayedValues setObject: [sender stringValue] forKey: ProjectPath]; + [self updateUI]; +} + @end diff --git a/tools/Forge/Bundles/MainPrefs/MainPrefsView.h b/tools/Forge/Bundles/MainPrefs/MainPrefsView.h index 67d2cf4a9..d065514b4 100644 --- a/tools/Forge/Bundles/MainPrefs/MainPrefsView.h +++ b/tools/Forge/Bundles/MainPrefs/MainPrefsView.h @@ -34,4 +34,14 @@ #import "PrefsView.h" @interface MainPrefsView: NSView +{ + id directoryField; + id findButton; + id owner; +} + +- (id) initWithOwner: (id) anOwner andFrame: (NSRect) frameRect; + +- (id) directoryField; + @end diff --git a/tools/Forge/Bundles/MainPrefs/MainPrefsView.m b/tools/Forge/Bundles/MainPrefs/MainPrefsView.m index 62f132e28..04c7d0738 100644 --- a/tools/Forge/Bundles/MainPrefs/MainPrefsView.m +++ b/tools/Forge/Bundles/MainPrefs/MainPrefsView.m @@ -35,26 +35,57 @@ static const char rcsid[] = #import #import +#import #import #import "MainPrefsView.h" +#import "MainPrefs.h" @implementation MainPrefsView -- (id) initWithFrame: (NSRect) frameRect +- (id) initWithOwner: (id) anOwner andFrame: (NSRect) frameRect { - id button; + id label = nil; + + owner = anOwner; if ((self = [super initWithFrame: frameRect])) { - button = [[NSButton alloc] initWithFrame: NSMakeRect (0, 0, 60, 24)]; - [button autorelease]; + label = [[NSTextField alloc] initWithFrame: NSMakeRect (3, 201, 480, 24)]; + [label setEditable: NO]; + [label setSelectable: NO]; + [label setAllowsEditingTextAttributes: NO]; + [label setImportsGraphics: NO]; + [label setTextColor: [NSColor blackColor]]; + [label setBackgroundColor: [NSColor controlColor]]; + [label setBezeled: NO]; + [label setStringValue: @"Project load path"]; + [self addSubview: [label autorelease]]; - [button setTitle: @"Default"]; - [button setTarget: self]; - [button setAction: @selector(resetToDefaults:)]; - [self addSubview: button]; + directoryField = [[NSTextField alloc] initWithFrame: NSMakeRect (3, 174, 410, 24)]; + [directoryField setEditable: YES]; + [directoryField setSelectable: YES]; + [directoryField setAllowsEditingTextAttributes: NO]; + [directoryField setImportsGraphics: NO]; + [directoryField setTextColor: [NSColor blackColor]]; + [directoryField setBackgroundColor: [NSColor whiteColor]]; + [directoryField setBezeled: YES]; + [directoryField setTarget: owner]; + [directoryField setAction: @selector(projectPathChanged:)]; + [self addSubview: [directoryField autorelease]]; + + findButton = [[NSButton alloc] initWithFrame: NSMakeRect (419, 174, 64, 24)]; + [findButton setTitle: @"Find..."]; + [findButton setTarget: owner]; + [findButton setAction: @selector(projectPathFindButtonClicked:)]; + [self addSubview: [findButton autorelease]]; } return self; } + +- (id) directoryField +{ + return directoryField; +} + @end diff --git a/tools/Forge/Forge.classes b/tools/Forge/Forge.classes index 348aeef4e..e999c0ff3 100644 --- a/tools/Forge/Forge.classes +++ b/tools/Forge/Forge.classes @@ -74,21 +74,6 @@ ); Super = NSResponder; }; - Preferences = { - Actions = ( - ok, - revert, - revertToDefault, - prefsChanged, - setObject - ); - Outlets = ( - "e\n\tfloat\t\tyLight", - "e\n\tfloat\t\tzLight", - "e\n\tBOOL\t\tshowBSPOutput" - ); - Super = NSObject; - }; PrefsPanel = { Actions = ( ); @@ -96,4 +81,4 @@ ); Super = NSPanel; }; -} \ No newline at end of file +} diff --git a/tools/Forge/GNUmakefile b/tools/Forge/GNUmakefile index 830871865..8dd26cdd0 100644 --- a/tools/Forge/GNUmakefile +++ b/tools/Forge/GNUmakefile @@ -70,7 +70,6 @@ Forge_HEADERS= \ Config.h \ BundleController.h \ Controller.h \ - Preferences.h \ PrefsPanel.h \ PrefsController.h @@ -80,7 +79,6 @@ Forge_HEADERS= \ Forge_OBJC_FILES= \ BundleController.m \ Controller.m \ - Preferences.m \ PrefsPanel.m \ PrefsController.m \ main.m diff --git a/tools/Forge/Preferences.h b/tools/Forge/Preferences.h deleted file mode 100644 index f261eac45..000000000 --- a/tools/Forge/Preferences.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - Preferences.h - - Preferences class - - Copyright (C) 2001 Dusk to Dawn Computing, Inc. - - Author: Jeff Teunissen - Date: 9 Feb 2001 - - 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. - - 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. - - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public - License along with this program; if not, write to: - - Free Software Foundation, Inc. - 59 Temple Place - Suite 330 - Boston, MA 02111-1307, USA -*/ - -#ifdef HAVE_CONFIG_H -# include "Config.h" -#endif - -#import -#import -#import - -/* - Keys in the dictionary -*/ -#define ProjectPath @"projectPath" -#define BspSoundPath @"bspSoundPath" -#define ShowBSPOutput @"showBSPOutput" -#define OffsetBrushCopy @"offsetBrushCopy" -#define StartWad @"startWad" -#define XLight @"xLight" -#define YLight @"yLight" -#define ZLight @"zLight" - -@interface Preferences: NSObject -{ - // UI targets - NSString *projectPath; // path to the project to load on startup - NSString *bspSoundPath; // location of BSP sounds - NSString *startWad; // which wadfile to load on startup - float xLight; // Lighting for X side - float yLight; // Lighting for Y side - float zLight; // Lighting for Z side - BOOL showBSPOutput; // "Show BSP Output" checkbox - BOOL offsetBrushCopy; // "Brush offset" checkbox -} - -+ (Preferences *) sharedInstance; // Return the shared instance - -- (void) saveDefaults; -- (void) loadDefaults; - -- (NSDictionary *) preferences; // current prefs - -- (void) updateUI; // Update the displayed values in the UI -- (void) commitDisplayedValues; // Make displayed settings current -- (void) discardDisplayedValues; // Replace displayed settings with current - -// UI notifications -- (void) ok: (id) sender; // commit displayed values -- (void) revert: (id) sender; // revert to current values -- (void) revertToDefault: (id) sender; // revert current values to defaults and - // discard displayed values - -- (void) prefsChanged: (id) sender; // Notify the object to update the UI - - -- (id) objectForKey: (id) key; -- (void) setObject: (id) obj forKey: (id) key; - -- (NSDictionary *) preferencesFromDefaults; -- (void) savePreferencesToDefaults: (NSDictionary *) dict; - -@end diff --git a/tools/Forge/Preferences.m b/tools/Forge/Preferences.m deleted file mode 100644 index 4fa5f2ef6..000000000 --- a/tools/Forge/Preferences.m +++ /dev/null @@ -1,335 +0,0 @@ -/* - Preferences.m - - Preferences class - - Copyright (C) 2001 Dusk to Dawn Computing, Inc. - - Author: Jeff Teunissen - Date: 9 Feb 2001 - - 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. - - 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. - - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public - License along with this program; if not, write to: - - Free Software Foundation, Inc. - 59 Temple Place - Suite 330 - Boston, MA 02111-1307, USA -*/ -static const char rcsid[] = - "$Id$"; - -#ifdef HAVE_CONFIG_H -# include "Config.h" -#endif - -#import -#import -#import -#import -#import - -#import -#import - -#import "Preferences.h" - -static NSMutableDictionary * -defaultValues (void) { - static NSMutableDictionary *dict = nil; - - if (!dict) { - dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: - @"/Local/Forge/Projects", ProjectPath, - @"/Local/Library/Sounds", BspSoundPath, - @"none", 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; -} - -/*** - Code to deal with defaults -***/ -static BOOL -getBoolDefault (NSMutableDictionary *dict, NSString *name) -{ - NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name]; - NSNumber *num; - - if (!str) - str = [[defaultValues() objectForKey: name] stringValue]; - - num = [NSNumber numberWithBool: [str hasPrefix: @"Y"]]; - [dict setObject: num forKey: name]; - - return [num boolValue]; -} - -static float -getFloatDefault (NSMutableDictionary *dict, NSString *name) -{ - NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name]; - NSNumber *num; - - if (!str) - str = [[defaultValues() objectForKey: name] stringValue]; - - num = [NSNumber numberWithFloat: [str floatValue]]; - [dict setObject: num forKey: name]; - - return [num floatValue]; -} - -#if 0 // unneeded -static int -getIntDefault (NSMutableDictionary *dict, NSString *name) -{ - NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name]; - NSNumber *num; - - if (!str) - str = [[defaultValues() objectForKey: name] stringValue]; - - num = [NSNumber numberWithInt: [str intValue]]; - [dict setObject: num forKey: name]; - - return [num intValue]; -} -#endif - -static NSString * -getStringDefault (NSMutableDictionary *dict, NSString *name) -{ - NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey: name]; - - if (!str) - str = [defaultValues() objectForKey: name]; - - [dict setObject: str forKey: name]; - - return str; -} - -@implementation Preferences - -static Preferences *sharedInstance = nil; -static NSDictionary *currentValues = nil; -static NSMutableDictionary *displayedValues = nil; - -- (id) objectForKey: (id) key -{ - id obj = [[NSUserDefaults standardUserDefaults] objectForKey: key]; - - if (!obj) - return [defaultValues () objectForKey: key]; - else - return obj; -} - -- (void) setObject: (id) obj forKey: (id) key -{ - if ((!key) || (!obj)) - return; - - [displayedValues setObject: obj forKey: key]; - [self commitDisplayedValues]; -} - -- (void) saveDefaults -{ - [self savePreferencesToDefaults: currentValues]; -} - -- (void) loadDefaults -{ - if (currentValues) - [currentValues release]; - - currentValues = [[self preferencesFromDefaults] copyWithZone: [self zone]]; - [self discardDisplayedValues]; -} - -+ (Preferences *) sharedInstance -{ - return (sharedInstance ? sharedInstance : [[self alloc] init]); -} - -- (id) init -{ - if (sharedInstance) { - [self dealloc]; - } else { - self = [super init]; - currentValues = [[self preferencesFromDefaults] mutableCopy]; - [self discardDisplayedValues]; - sharedInstance = self; - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(saveDefaults) - name: @"NSApplicationWillTerminateNotification" - object: nil]; - } - [self prefsChanged: self]; - return sharedInstance; -} - -- (NSDictionary *) preferences -{ - return currentValues; -} - -- (void) dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver: self]; - [currentValues release]; - [displayedValues release]; - currentValues = displayedValues = nil; - [super dealloc]; -} - -/* - updateUI - - Update the user interface with new preferences -*/ -- (void) updateUI -{ - return; -} - -- (void) prefsChanged: (id) sender -{ - float aFloat; - - [displayedValues setObject: projectPath forKey: ProjectPath]; - [displayedValues setObject: bspSoundPath forKey: BspSoundPath]; - [displayedValues setObject: startWad forKey: StartWad]; - - if ((aFloat = [[displayedValues objectForKey: XLight] floatValue]) < 0.0 || aFloat > 1.0) { - aFloat = [[defaultValues () objectForKey: XLight] floatValue]; - } - xLight = aFloat; - [displayedValues setObject: [NSNumber numberWithFloat: xLight] forKey: XLight]; - - if ((aFloat = [[displayedValues objectForKey: YLight] floatValue]) < 0.0 || aFloat > 1.0) { - aFloat = [[defaultValues () objectForKey: YLight] floatValue]; - } - yLight = aFloat; - [displayedValues setObject: [NSNumber numberWithFloat: yLight] forKey: YLight]; - - if ((aFloat = [[displayedValues objectForKey: ZLight] floatValue]) < 0.0 || aFloat > 1.0) { - aFloat = [[defaultValues () objectForKey: ZLight] floatValue]; - } - zLight = aFloat; - [displayedValues setObject: [NSNumber numberWithFloat: zLight] forKey: ZLight]; - - [displayedValues setObject: [NSNumber numberWithBool: showBSPOutput] forKey: ShowBSPOutput]; - [displayedValues setObject: [NSNumber numberWithBool: offsetBrushCopy] forKey: OffsetBrushCopy]; - - [self commitDisplayedValues]; -} - -- (void) commitDisplayedValues -{ - [currentValues release]; - currentValues = [[displayedValues copy] retain]; - [self saveDefaults]; - [self updateUI]; -} - -- (void) discardDisplayedValues -{ - [displayedValues release]; - displayedValues = [[currentValues mutableCopy] retain]; - [self updateUI]; -} - -- (void) ok: (id) sender -{ - [self commitDisplayedValues]; -} - -- (void) revert: (id) sender -{ - [self discardDisplayedValues]; -} - -- (void) revertToDefault: (id) sender -{ - if (currentValues) - [currentValues release]; - - currentValues = defaultValues (); - - [self discardDisplayedValues]; -} - -- (NSDictionary *) preferencesFromDefaults -{ - NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 10]; - - projectPath = getStringDefault (dict, ProjectPath); - bspSoundPath = getStringDefault (dict, BspSoundPath); - startWad = getStringDefault (dict, StartWad); - xLight = getFloatDefault (dict, XLight); - yLight = getFloatDefault (dict, YLight); - zLight = getFloatDefault (dict, ZLight); - showBSPOutput = getBoolDefault (dict, ShowBSPOutput); - offsetBrushCopy = getBoolDefault (dict, OffsetBrushCopy); - - return dict; -} - -#define setBoolDefault(name) \ -{ \ - [defaults setBool:[[dict objectForKey:name] boolValue] forKey: name]; \ -} - -#define setFloatDefault(name) \ -{ \ - [defaults setFloat:[[dict objectForKey:name] floatValue] forKey: name]; \ -} - -#define setIntDefault(name) \ -{ \ - [defaults setInteger:[[dict objectForKey:name] intValue] forKey:name]; \ -} - -#define setStringDefault(name) \ -{ \ - [defaults setObject: [dict objectForKey: name] forKey: name]; \ -} - -- (void) savePreferencesToDefaults: (NSDictionary *) dict -{ - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - - NSLog (@"Updating defaults..."); - setStringDefault(ProjectPath); - setStringDefault(BspSoundPath); - setStringDefault(StartWad); - setFloatDefault (XLight); - setFloatDefault (YLight); - setFloatDefault (ZLight); - setBoolDefault (ShowBSPOutput); - setBoolDefault (OffsetBrushCopy); - [defaults synchronize]; -} - -@end