App/PlayerListDataSource.m

00001 //
00002 //  PlayerListDataSource.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 02/06/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "PlayerListDataSource.h"
00010 
00011 
00012 @implementation PlayerListDataSource
00013 
00014 
00015 - (id) init {
00016     self = [super init];
00017     if (self != nil) {
00018         universe = nil;
00019         players = [[NSMutableArray alloc] init];
00020         universe = [Universe defaultInstance];
00021         [self refreshData];
00022     }
00023     return self;
00024 }
00025 
00026 - (void) awakeFromNib {
00027     [notificationCenter addObserver:self selector:@selector(refreshData) name:@"SP_PLAYER_INFO" 
00028                              object:nil useLocks:NO useMainRunLoop:YES]; 
00029     
00030     // set up the toField
00031     [toField removeAllItems];
00032     [toField addItemWithTitle:@"ALL"];
00033     [toField addItemWithTitle:@"FED"];
00034     [toField addItemWithTitle:@"KLI"];
00035     [toField addItemWithTitle:@"ORI"];
00036     [toField addItemWithTitle:@"ROM"];
00037     [toField addItemWithTitle:@"GOD"];
00038     [toField selectItemWithTitle:@"ALL"];
00039 }
00040 
00041 // tie this to events that change players
00042 - (void) refreshData {
00043     
00044     // clean up
00045     //[players removeAllObjects];
00046     
00047     // test for new
00048     if (universe == nil) {        
00049         return;
00050     }
00051     
00052     bool changed = NO;
00053     
00054     // find players
00055     for (int i = 0; i < UNIVERSE_MAX_PLAYERS; i++) {
00056         Player *player = [universe playerWithId:i];
00057         if ([player status] != PLAYER_FREE) {
00058             if (![players containsObject:player]) {
00059                 [players addObject:player];
00060                 changed = YES;
00061                 [toField addItemWithTitle:[player mapChars]];
00062             } else { // player == FREE
00063                 if ([players containsObject: player]) {
00064                     [players removeObject:player];
00065                     changed = YES;
00066                     [toField removeItemWithTitle:[player mapChars]];
00067                 }
00068             }                       
00069         }        
00070     } 
00071     
00072     // show it
00073     // $$ performance hog, reload it not that often or make our own view....
00074     if (changed) {
00075         // refresh the player list
00076         // (it will come and ask for data)
00077         [playerList reloadData];  
00078     } else {
00079         NSLog(@"PlayerListDataSource.refresh skipping, no change detected");
00080     }
00081 
00082 }
00083 
00084 - (int)numberOfRowsInTableView:(NSTableView *)aTableView {
00085     return [players count];
00086 }
00087 
00088 - (id)tableView:(NSTableView *)aTableView
00089     objectValueForTableColumn:(NSTableColumn *)aTableColumn
00090             row:(int)rowIndex {
00091     // get the player
00092     Player *player = [players objectAtIndex:rowIndex];
00093     
00094     if ([[aTableColumn identifier] isEqualToString:@"id"]) {
00095         return [player mapChars];        
00096     } else { // must be name
00097         return [player longName];
00098     }    
00099 }
00100 
00101 @end

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