/Volumes/Plantain/MyDocuments/Projects/MacTrek/MacTrek/App/MTKeyMap.m

00001 //
00002 //  KeyMap.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 21/04/2006.
00006 //  Copyright 2006 __MyCompanyName__. All rights reserved.
00007 //
00008 
00009 #import "MTKeyMap.h"
00010 
00011 
00012 @implementation MTKeyMap
00013 
00014 - (void) fillWithDefaults {
00015     MTKeyMapEntry *entry;
00016     
00017     // combat
00018     entry = [[MTKeyMapEntry alloc] initAction: ACTION_CLOAK 
00019                                     withKey:'c' 
00020                               modifierFlags: 0 
00021                                 description: @"Toggle cloak"];
00022     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00023     
00024     entry = [[MTKeyMapEntry alloc] initAction: ACTION_DET_ENEMY 
00025                                     withKey:'d' 
00026                               modifierFlags: 0 
00027                                 description: @"Detonate enemy topedos"];
00028     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00029     
00030     entry = [[MTKeyMapEntry alloc] initAction: ACTION_DET_OWN 
00031                                     withKey:'D' 
00032                               modifierFlags: 0 
00033                                 description: @"Detonate own topedos"];
00034     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00035         
00036     entry = [[MTKeyMapEntry alloc] initAction: ACTION_FIRE_PLASMA 
00037                                     withKey:'f' 
00038                               modifierFlags: 0 
00039                                 description: @"Fire plasma torpedo"];
00040     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00041     
00042     entry = [[MTKeyMapEntry alloc] initAction: ACTION_FIRE_TORPEDO
00043                                     withKey:'t' 
00044                               modifierFlags: 0 
00045                                 description: @"Fire photon torpedo"];
00046     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00047     
00048     entry = [[MTKeyMapEntry alloc] initAction: ACTION_FIRE_PHASER
00049                                     withKey:'p' 
00050                               modifierFlags: 0 
00051                                 description: @"Fire phaser"];
00052     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00053                 
00054     
00055     entry = [[MTKeyMapEntry alloc] initAction: ACTION_SHIELDS
00056                                     withKey:'s' 
00057                               modifierFlags: 0 
00058                                 description: @"Toggle shields"];
00059     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00060     
00061     entry = [[MTKeyMapEntry alloc] initAction: ACTION_TRACTOR
00062                                     withKey:'y' 
00063                               modifierFlags: 0 
00064                                 description: @"Toggle tractor beam"];
00065     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00066     
00067     entry = [[MTKeyMapEntry alloc] initAction: ACTION_PRESSOR
00068                                     withKey:'u' 
00069                               modifierFlags: 0 
00070                                 description: @"Toggle pressor beam"];
00071     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00072     
00073     // Movement And Navigation Functions:
00074     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_0 
00075                                     withKey:'0' 
00076                               modifierFlags: 0 
00077                                 description: @"Set speed to warp 0 (stop)"];
00078     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00079     
00080     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_1 
00081                                     withKey:'1' 
00082                               modifierFlags: 0 
00083                                 description: @"Set speed to warp 1"];
00084     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00085     
00086     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_2
00087                                     withKey:'2' 
00088                               modifierFlags: 0 
00089                                 description: @"Set speed to warp 2"];
00090     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00091     
00092     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_3 
00093                                     withKey:'3' 
00094                               modifierFlags: 0 
00095                                 description: @"Set speed to warp 3"];
00096     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00097     
00098     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_4 
00099                                     withKey:'4' 
00100                               modifierFlags: 0 
00101                                 description: @"Set speed to warp 4"];
00102     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00103     
00104     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_5
00105                                     withKey:'5' 
00106                               modifierFlags: 0 
00107                                 description: @"Set speed to warp 5"];
00108     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00109     
00110     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_6
00111                                     withKey:'6' 
00112                               modifierFlags: 0 
00113                                 description: @"Set speed to warp 6"];
00114     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00115     
00116     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_7
00117                                     withKey:'7' 
00118                               modifierFlags: 0 
00119                                 description: @"Set speed to warp 7"];
00120     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00121     
00122     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_8 
00123                                     withKey:'8' 
00124                               modifierFlags: 0 
00125                                 description: @"Set speed to warp 8"];
00126     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00127     
00128     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_9 
00129                                     withKey:'9' 
00130                               modifierFlags: 0 
00131                                 description: @"Set speed to warp 9"];
00132     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00133     
00134     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_10 
00135                                     withKey:')' 
00136                               modifierFlags: 0 
00137                                 description: @"Set speed to warp 10"];
00138     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00139     
00140     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_11 
00141                                     withKey:'!' 
00142                               modifierFlags: 0 
00143                                 description: @"Set speed to warp 11"];
00144     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00145     
00146     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_12
00147                                     withKey:'@' 
00148                               modifierFlags: 0 
00149                                 description: @"Set speed to warp 12"];
00150     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00151     
00152     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_MAX
00153                                     withKey:'%' 
00154                               modifierFlags: 0 
00155                                 description: @"Set speed to maximum warp"];
00156     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00157     
00158     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_HALF_MAX
00159                                     withKey:'#' 
00160                               modifierFlags: 0 
00161                                 description: @"Set speed to half of maximum warp"];
00162     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00163     
00164     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_INCREASE
00165                                     withKey:'>' 
00166                               modifierFlags: 0 
00167                                 description: @"Increase speed by 1"];
00168     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00169     
00170     entry = [[MTKeyMapEntry alloc] initAction: ACTION_WARP_DECREASE
00171                                     withKey:'<' 
00172                               modifierFlags: 0 
00173                                 description: @"Decrease speed by 1"];
00174     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00175     
00176     entry = [[MTKeyMapEntry alloc] initAction: ACTION_SET_COURSE
00177                                     withKey:'k' 
00178                               modifierFlags: 0 
00179                                 description: @"Set course"];
00180     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00181         
00182     entry = [[MTKeyMapEntry alloc] initAction: ACTION_LOCK
00183                                     withKey:'l' 
00184                               modifierFlags: 0 
00185                                 description: @"Lock on target"];
00186     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00187                         
00188     entry = [[MTKeyMapEntry alloc] initAction: ACTION_PRACTICE_BOT
00189                                     withKey:'*' 
00190                               modifierFlags: 0 
00191                                 description: @"Send in practice bot"];
00192     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00193     
00194     entry = [[MTKeyMapEntry alloc] initAction: ACTION_TRANSWARP
00195                                     withKey:'*' 
00196                               modifierFlags: 0 
00197                                 description: @"Engage transwarp drive"];
00198     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00199     
00200     // Planet Functions                                
00201     entry = [[MTKeyMapEntry alloc] initAction: ACTION_BOMB
00202                                     withKey:'b' 
00203                               modifierFlags: 0 
00204                                 description: @"Bombard planet"];
00205     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00206     
00207     entry = [[MTKeyMapEntry alloc] initAction: ACTION_ORBIT
00208                                     withKey:'o' 
00209                               modifierFlags: 0 
00210                                 description: @"Orbit planet"];
00211     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00212                                     
00213     entry = [[MTKeyMapEntry alloc] initAction: ACTION_BEAM_DOWN
00214                                     withKey:'x' 
00215                               modifierFlags: 0 
00216                                 description: @"Beam armies down"];
00217     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00218     
00219     entry = [[MTKeyMapEntry alloc] initAction: ACTION_BEAM_UP
00220                                     withKey:'z' 
00221                               modifierFlags: 0 
00222                                 description: @"Beam armies up"];
00223     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00224                                     
00225     //Message Functions
00226     entry = [[MTKeyMapEntry alloc] initAction: ACTION_DISTRESS_CALL
00227                                     withKey:'E' 
00228                               modifierFlags: 0 
00229                                 description: @"Send distress call"];
00230     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];       
00231     
00232     entry = [[MTKeyMapEntry alloc] initAction: ACTION_ARMIES_CARRIED_REPORT
00233                                     withKey:'F' 
00234                               modifierFlags: 0 
00235                                 description: @"Send armies carried report"];
00236     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]]; 
00237 
00238     entry = [[MTKeyMapEntry alloc] initAction: ACTION_MESSAGE
00239                                     withKey:'F' 
00240                               modifierFlags: 0 
00241                                 description: @"Start sending message"];
00242     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]]; 
00243           
00244     // Macro and RCD left out for now
00245     
00246     // Misc. Functions
00247     entry = [[MTKeyMapEntry alloc] initAction: ACTION_DOCK_PERMISSION
00248                                     withKey:'e' 
00249                               modifierFlags: 0 
00250                                 description: @"Toggle docking permission"];
00251     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];                                     
00252                                       
00253     entry = [[MTKeyMapEntry alloc] initAction: ACTION_INFO
00254                                     withKey:'i' 
00255                               modifierFlags: 0 
00256                                 description: @"Get information on object near mouse"];
00257     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];       
00258                                             
00259     entry = [[MTKeyMapEntry alloc] initAction: ACTION_REFIT
00260                                     withKey:'r' 
00261                               modifierFlags: 0 
00262                                 description: @"Refit to different shiptype"];    
00263     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00264         
00265     entry = [[MTKeyMapEntry alloc] initAction: ACTION_REPAIR
00266                                     withKey:'R' 
00267                               modifierFlags: 0 
00268                                 description: @"Shut down for repairs"];    
00269     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]]; 
00270      
00271     entry = [[MTKeyMapEntry alloc] initAction: ACTION_QUIT
00272                                     withKey:'q' 
00273                               modifierFlags: 0 
00274                                 description: @"Quit"];    
00275     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];                                                        
00276 
00277     //Window And Display Functions:
00278     entry = [[MTKeyMapEntry alloc] initAction: ACTION_HELP
00279                                     withKey:'h' 
00280                               modifierFlags: 0 
00281                                 description: @"Show help window"];    
00282     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00283     
00284     entry = [[MTKeyMapEntry alloc] initAction: ACTION_DEBUG
00285                                       withKey:'#' 
00286                                 modifierFlags: 0 
00287                                   description: @"Activate debug labels"];    
00288     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry key]]];
00289 
00290 }
00291 
00292 - (id) init {
00293     self = [super init];
00294     if (self != nil) {
00295         // create a default
00296         keyMap = [[NSMutableDictionary alloc] init];        
00297         [self fillWithDefaults];
00298         // set vars
00299         changedSinceLastWrite = NO;
00300         NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00301         pathToKeyMap = [NSString stringWithFormat:@"%@/keymap.xml", pathToResources];
00302         [pathToKeyMap retain];
00303     }
00304 
00305     return self;
00306 }
00307 
00308 - (id) initWithDefaultFile {
00309     NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00310     pathToKeyMap = [NSString stringWithFormat:@"%@/keymap.xml", pathToResources];
00311     
00312     self = [self initWithFile:pathToKeyMap];
00313     if (self != nil) {
00314         if ([keyMap count] == 0) {
00315             // something went wrong
00316             NSLog(@"MTKeyMap.initWithDefaultFile keymap file is empty");
00317             [self fillWithDefaults];
00318         }
00319     }
00320     return self;
00321 }
00322 
00323 - (id) initWithFile:(NSString *) file {
00324     self = [super init];
00325     if (self != nil) {
00326         // load keymap
00327         keyMap = [[NSMutableDictionary alloc] initWithContentsOfFile:file];
00328         
00329         // set vars
00330         changedSinceLastWrite = NO;
00331         NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00332         pathToKeyMap = [NSString stringWithFormat:@"%@/keymap.xml", pathToResources];
00333         [pathToKeyMap retain];
00334     }
00335     return self;
00336 }
00337 
00338 - (void) readDefaultKeyMap {
00339     // create a private pool for this thread
00340     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00341     
00342     // load keymap
00343     NSMutableDictionary *newKeyMap;
00344     newKeyMap = [[NSMutableDictionary alloc] initWithContentsOfFile:pathToKeyMap];
00345     if ([newKeyMap count] == 0) {
00346         // something went wrong
00347         NSLog(@"MTKeyMap.readDefaultKeyMap keymap file is empty");
00348         return;
00349     }
00350     // swap maps
00351     [keyMap release];
00352     keyMap = newKeyMap;
00353 
00354     [pool release];
00355 }
00356 
00357 - (void) writeToFile:(NSString *)file {
00358     if ([keyMap writeToFile:file atomically:NO]) {
00359         NSLog(@"MTKeyMap.writeToFile %@ successfull", file);
00360     } else {
00361         NSLog(@"MTKeyMap.writeToFile %@ failed", file); 
00362     }
00363 
00364     changedSinceLastWrite = NO;
00365 }
00366 
00367 - (void)writeToDefaultFileIfChanged {
00368     if (changedSinceLastWrite) {
00369         [self writeToFile:pathToKeyMap];
00370     }
00371 }
00372 
00373 // not very efficient this code will be invoked a lot, so maybe
00374 // create a hashtable on keys as well and not only on actions.
00375 - (int) actionForKey:(char) key withModifierFlags:(unsigned int) flags {
00376     NSEnumerator *enumerator = [keyMap keyEnumerator];
00377     MTKeyMapEntry *keyEntry;
00378       
00379     while ((keyEntry = [keyMap objectForKey:[enumerator nextObject]])) {
00380         if (keyEntry != nil) { 
00381             if (([keyEntry key] == key)  
00382                 // && ([keyEntry modifierFlags] == flags)  // ignoring modifiers for now $$
00383                 ) {
00384                 return [keyEntry action];
00385             } 
00386         }
00387     }
00388     return ACTION_UNKNOWN;
00389 }
00390 
00391 - (NSString *) descriptionForAction:(int) action {
00392     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00393     return [keyEntry description];
00394 }
00395 
00396 - (char) keyForAction:(int) action {
00397     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00398     return [keyEntry key]; 
00399 }
00400 - (unsigned int) flagsForAction:(int) action {
00401     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00402     return [keyEntry modifierFlags];
00403 }
00404 - (void) setKey: (char) key forAction:(int) action {
00405     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00406     [keyEntry setKey:key];
00407     changedSinceLastWrite = YES;
00408 }
00409 - (void) setFlags: (unsigned int) flags ForAction:(int) action {
00410     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00411     [keyEntry setModifierFlags:flags];
00412     changedSinceLastWrite = YES;
00413 }
00414 
00415 - (int) count {
00416     return [keyMap count];
00417 }
00418 
00419 - (NSArray *) allKeys {
00420     return [keyMap allKeys];
00421 }
00422 
00423 @end

Generated on Fri Jul 28 19:15:14 2006 for MacTrek by  doxygen 1.4.7