App/MTKeyMap.m

00001 //
00002 //  KeyMap.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 21/04/2006.
00006 //  Copyright 2006 Luky Soft. 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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];
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 action]]];       
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 action]]]; 
00237 
00238     entry = [[MTKeyMapEntry alloc] initAction: ACTION_MESSAGE
00239                                     withKey:'m' 
00240                               modifierFlags: 0 
00241                                 description: @"Start sending message"];
00242     [keyMap setObject:entry forKey:[NSNumber numberWithInt:[entry action]]]; 
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 action]]];                                     
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 action]]];       
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 action]]];
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 action]]]; 
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 action]]];                                                        
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 action]]];
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 action]]];
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 /*
00309 - (id) initWithDefaultFile {
00310     NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00311     pathToKeyMap = [NSString stringWithFormat:@"%@/keymap.xml", pathToResources];
00312     
00313     self = [self initWithFile:pathToKeyMap];
00314     if (self != nil) {
00315         if ([keyMap count] == 0) {
00316             // something went wrong
00317             NSLog(@"MTKeyMap.initWithDefaultFile keymap file is empty, loading defaults");
00318             [self fillWithDefaults];
00319         } else {
00320             NSLog(@"MTKeyMap.initWithDefaultFile keymap file loaded %d", [keyMap count]);
00321         }
00322         
00323     }
00324     return self;
00325 }
00326 
00327 
00328 - (id) initWithFile:(NSString *) file {
00329     self = [super init];
00330     if (self != nil) {
00331         // load keymap as dict in dict
00332         NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile:file];
00333         
00334         // iterate temp and add all entries as keyMapEntries
00335         NSEnumerator *enumerator = [temp keyEnumerator];
00336         NSDictionary *entryAsDict;
00337         
00338         // convert all dicts to Keymap entries and add them
00339         while ((entryAsDict = [temp objectForKey:[enumerator nextObject]])) {
00340             MTKeyMapEntry *keyEntry = [[MTKeyMapEntry alloc] initWithDictionairy:entryAsDict];
00341             [keyMap setObject:keyEntry forKey:[NSNumber numberWithInt:[keyEntry action]]];
00342         }        
00343         
00344         // set vars
00345         changedSinceLastWrite = NO;
00346         NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00347         pathToKeyMap = [NSString stringWithFormat:@"%@/keymap.xml", pathToResources];
00348         [pathToKeyMap retain];
00349     }
00350     return self;
00351 }
00352 */
00353 
00354 - (void) readDefaultKeyMap {
00355     // create a private pool for this thread
00356     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00357     
00358     // load keymap
00359     NSMutableDictionary *newKeyMap = [[NSMutableDictionary alloc] init];
00360     
00361     // --- copy from init with file
00362     // load keymap as dict in dict
00363     NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile:pathToKeyMap];
00364     
00365     // iterate temp and add all entries as keyMapEntries
00366     NSEnumerator *enumerator = [temp keyEnumerator];
00367     NSDictionary *entryAsDict;
00368     
00369     // convert all dicts to Keymap entries and add them
00370     while ((entryAsDict = [temp objectForKey:[enumerator nextObject]])) {
00371         MTKeyMapEntry *keyEntry = [[MTKeyMapEntry alloc] initWithDictionairy:entryAsDict];
00372         NSLog(@"MTKeyMap.readDefaultKeyMap setting key [%c] for action: %@", [keyEntry key], [keyEntry description]);
00373         [newKeyMap setObject:keyEntry forKey:[NSNumber numberWithInt:[keyEntry action]]];
00374     }          
00375     // ---
00376     
00377     if ([newKeyMap count] == 0) {
00378         // something went wrong
00379         NSLog(@"MTKeyMap.readDefaultKeyMap keymap file is empty");
00380         return;
00381     } else {
00382         NSLog(@"MTKeyMap.readDefaultKeyMap loaded %d items", [newKeyMap count]);
00383     }
00384     // swap maps
00385     [keyMap release];
00386     keyMap = newKeyMap;
00387 
00388     [pool release];
00389 }
00390 
00391 - (void) writeToFile:(NSString *)file {
00392     
00393     NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
00394     
00395     // iterate keyMap and add all entries as dictionairies
00396     NSEnumerator *enumerator = [keyMap keyEnumerator];
00397     MTKeyMapEntry *keyEntry;
00398     
00399     while ((keyEntry = [keyMap objectForKey:[enumerator nextObject]])) {
00400         NSDictionary *dict = [keyEntry asDictionary];      
00401         [temp setObject:dict forKey:[NSString stringWithFormat:@"%d", [keyEntry action]]];
00402     }
00403     
00404     // dicts in dicts can be written to disk
00405     if ([temp writeToFile:file atomically:NO]) {
00406         NSLog(@"MTKeyMap.writeToFile %@ successfull", file);
00407     } else {
00408         NSLog(@"MTKeyMap.writeToFile %@ failed", file); 
00409     }
00410 
00411     changedSinceLastWrite = NO;
00412     [temp removeAllObjects];
00413     [temp release];
00414 }
00415 
00416 - (void)writeToDefaultFileIfChanged {
00417     if (changedSinceLastWrite) {
00418         [self writeToFile:pathToKeyMap];
00419     }
00420 }
00421 
00422 // not very efficient this code will be invoked a lot, so maybe
00423 // create a hashtable on keys as well and not only on actions.
00424 - (int) actionForKey:(char) key withModifierFlags:(unsigned int) flags {
00425     NSEnumerator *enumerator = [keyMap keyEnumerator];
00426     MTKeyMapEntry *keyEntry;
00427       
00428     while ((keyEntry = [keyMap objectForKey:[enumerator nextObject]])) {
00429         if (keyEntry != nil) { 
00430             if (([keyEntry key] == key)  
00431                 // && ([keyEntry modifierFlags] == flags)  // ignoring modifiers for now $$
00432                 ) {
00433                 return [keyEntry action];
00434             } 
00435         }
00436     }
00437     return ACTION_UNKNOWN;
00438 }
00439 
00440 - (NSString *) descriptionForAction:(int) action {
00441     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00442     return [keyEntry description];
00443 }
00444 
00445 - (char) keyForAction:(int) action {
00446     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00447     return [keyEntry key]; 
00448 }
00449 - (unsigned int) flagsForAction:(int) action {
00450     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00451     return [keyEntry modifierFlags];
00452 }
00453 - (void) setKey: (char) key forAction:(int) action {
00454     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00455     [keyEntry setKey:key];
00456     changedSinceLastWrite = YES;
00457 }
00458 - (void) setFlags: (unsigned int) flags ForAction:(int) action {
00459     MTKeyMapEntry *keyEntry = [keyMap objectForKey:[NSNumber numberWithInt: action]];
00460     [keyEntry setModifierFlags:flags];
00461     changedSinceLastWrite = YES;
00462 }
00463 
00464 - (int) count {
00465     return [keyMap count];
00466 }
00467 
00468 - (NSArray *) allKeys {
00469     return [keyMap allKeys];
00470 }
00471 
00472 @end

Generated on Sat Aug 26 21:14:07 2006 for MacTrek by  doxygen 1.4.7