BrowserItem.mm
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:9k
源码类别:

OpenGL

开发平台:

Visual C++

  1. //
  2. //  BrowserItem.mm
  3. //  celestia
  4. //
  5. //  Created by Da Woon Jung on 2007-11-26
  6. //  Copyright (C) 2007, Celestia Development Team
  7. //
  8. #import "BrowserItem.h"
  9. #import "CelestiaStar.h"
  10. #import "CelestiaStar_PrivateAPI.h"
  11. #import "CelestiaBody.h"
  12. #import "CelestiaBody_PrivateAPI.h"
  13. #import "CelestiaDSO.h"
  14. #import "CelestiaLocation.h"
  15. #import "CelestiaAppCore.h"
  16. #import "CelestiaUniverse_PrivateAPI.h"
  17. #include "solarsys.h"
  18. @implementation BrowserItem
  19. - (id)initWithCelestiaDSO: (CelestiaDSO *)aDSO
  20. {
  21.     self = [super init];
  22.     if (self) data = [aDSO retain];
  23.     return self;
  24. }
  25. - (id)initWithCelestiaStar: (CelestiaStar *)aStar
  26. {
  27.     self = [super init];
  28.     if (self) data = [aStar retain];
  29.     return self;
  30. }
  31. - (id)initWithCelestiaBody: (CelestiaBody *)aBody
  32. {
  33.     self = [super init];
  34.     if (self) data = [aBody retain];
  35.     return self;
  36. }
  37. - (id)initWithCelestiaLocation: (CelestiaLocation *)aLocation
  38. {
  39.     self = [super init];
  40.     if (self) data = [aLocation retain];
  41.     return self;
  42. }
  43. - (id)initWithName: (NSString *)aName
  44. {
  45.     self = [super init];
  46.     if (self) data = [aName retain];
  47.     return self;
  48. }
  49. - (id)initWithName: (NSString *)aName
  50.           children: (NSDictionary *)aChildren
  51. {
  52.     self = [super init];
  53.     if (self)
  54.     {
  55.         data = [aName retain];
  56.         if (nil == children)
  57.         {
  58.             children = [[NSMutableDictionary alloc] initWithDictionary: aChildren];
  59.             childrenChanged = YES;
  60.         }
  61.     }
  62.     return self;
  63. }
  64. + (void)addChildrenToStar: (BrowserItem *) aStar
  65. {
  66.     SolarSystem *ss = [[[[CelestiaAppCore sharedAppCore] simulation] universe] universe]->getSolarSystem([((CelestiaStar *)[aStar body]) star]);
  67.     PlanetarySystem* sys = NULL;
  68.     if (ss) sys = ss->getPlanets();
  69.     
  70.     if (sys)
  71.     {
  72.         int sysSize = sys->getSystemSize();
  73.         BrowserItem *subItem = nil;
  74.         BrowserItem *planets = nil;
  75.         BrowserItem *dwarfPlanets = nil;
  76.         BrowserItem *minorMoons = nil;
  77.         BrowserItem *asteroids = nil;
  78.         BrowserItem *comets = nil;
  79.         BrowserItem *spacecrafts = nil;
  80.         int i;
  81.         for ( i=0; i<sysSize; i++)
  82.         {
  83.             Body* body = sys->getBody(i);
  84.             if (body->getName().empty())
  85.                 continue;
  86.             BrowserItem *item = [[[BrowserItem alloc] initWithCelestiaBody:
  87.                 [[[CelestiaBody alloc] initWithBody: body] autorelease]] autorelease];
  88.             int bodyClass  = body->getClassification();
  89.             switch (bodyClass)
  90.             {
  91.                 case Body::Invisible:
  92.                     continue;
  93.                 case Body::Planet:
  94.                     if (!planets)
  95.                         planets = [[[BrowserItem alloc] initWithName: NSLocalizedStringFromTable(@"Planets",@"po",@"")] autorelease];
  96.                     subItem = planets;
  97.                     break;
  98.                 case Body::DwarfPlanet:
  99.                     if (!dwarfPlanets)
  100.                         dwarfPlanets = [[[BrowserItem alloc] initWithName: NSLocalizedStringFromTable(@"Dwarf Planets",@"po",@"")] autorelease];
  101.                     subItem = dwarfPlanets;
  102.                     break;
  103.                 case Body::Moon:
  104.                 case Body::MinorMoon:
  105.                     if (body->getRadius() < 100.0f || Body::MinorMoon == bodyClass)
  106.                     {
  107.                         if (!minorMoons)
  108.                             minorMoons = [[[BrowserItem alloc] initWithName: NSLocalizedString(@"Minor Moons",@"")] autorelease];
  109.                         subItem = minorMoons;
  110.                     }
  111.                     else
  112.                     {
  113.                         subItem = aStar;
  114.                     }
  115.                     break;
  116.                 case Body::Asteroid:
  117.                     if (!asteroids)
  118.                         asteroids = [[[BrowserItem alloc] initWithName: NSLocalizedStringFromTable(@"Asteroids",@"po",@"")] autorelease];
  119.                     subItem = asteroids;
  120.                     break;
  121.                 case Body::Comet:
  122.                     if (!comets)
  123.                         comets = [[[BrowserItem alloc] initWithName: NSLocalizedStringFromTable(@"Comets",@"po",@"")] autorelease];
  124.                     subItem = comets;
  125.                     break;
  126.                 case Body::Spacecraft:
  127.                     if (!spacecrafts)
  128.                         spacecrafts = [[[BrowserItem alloc] initWithName: NSLocalizedString(@"Spacecrafts",@"")] autorelease];
  129.                     subItem = spacecrafts;
  130.                     break;
  131.                 default:
  132.                     subItem = aStar;
  133.                     break;
  134.             }
  135.             [subItem addChild: item];
  136.         }
  137.         
  138.         if (planets)      [aStar addChild: planets];
  139.         if (dwarfPlanets) [aStar addChild: dwarfPlanets];
  140.         if (minorMoons)   [aStar addChild: minorMoons];
  141.         if (asteroids)    [aStar addChild: asteroids];
  142.         if (comets)       [aStar addChild: comets];
  143.         if (spacecrafts)  [aStar addChild: spacecrafts];
  144.     }
  145. }
  146. + (void)addChildrenToBody: (BrowserItem *) aBody
  147. {
  148.     PlanetarySystem* sys = [(CelestiaBody *)[aBody body] body]->getSatellites();
  149.     
  150.     if (sys)
  151.     { 
  152.         int sysSize = sys->getSystemSize();
  153.         BrowserItem *subItem = nil;
  154.         BrowserItem *minorMoons = nil;
  155.         BrowserItem *comets = nil;
  156.         BrowserItem *spacecrafts = nil;
  157.         int i;
  158.         for ( i=0; i<sysSize; i++)
  159.         {
  160.             Body* body = sys->getBody(i);
  161.             if (body->getName().empty())
  162.                 continue;
  163.             BrowserItem *item = [[[BrowserItem alloc] initWithCelestiaBody:
  164.                 [[[CelestiaBody alloc] initWithBody: body] autorelease]] autorelease];
  165.             int bodyClass  = body->getClassification();
  166.             if (bodyClass==Body::Asteroid) bodyClass = Body::Moon;
  167.             
  168.             switch (bodyClass)
  169.             {
  170.                 case Body::Invisible:
  171.                     continue;
  172.                 case Body::Moon:
  173.                 case Body::MinorMoon:
  174.                     if (body->getRadius() < 100.0f || Body::MinorMoon == bodyClass)
  175.                     {
  176.                         if (!minorMoons)
  177.                             minorMoons = [[[BrowserItem alloc] initWithName: NSLocalizedString(@"Minor Moons",@"")] autorelease];
  178.                         subItem = minorMoons;
  179.                     }
  180.                     else
  181.                     {
  182.                         subItem = aBody;
  183.                     }
  184.                     break;
  185.                 case Body::Comet:
  186.                     if (!comets)
  187.                         comets = [[[BrowserItem alloc] initWithName: NSLocalizedStringFromTable(@"Comets",@"po",@"")] autorelease];
  188.                     subItem = comets;
  189.                     break;
  190.                 case Body::Spacecraft:
  191.                     if (!spacecrafts)
  192.                         spacecrafts = [[[BrowserItem alloc] initWithName: NSLocalizedString(@"Spacecrafts",@"")] autorelease];
  193.                     subItem = spacecrafts;
  194.                     break;
  195.                 default:
  196.                     subItem = aBody;
  197.                     break;
  198.             }
  199.             [subItem addChild: item];
  200.         }
  201.         
  202.         if (minorMoons)  [aBody addChild: minorMoons];
  203.         if (comets)      [aBody addChild: comets];
  204.         if (spacecrafts) [aBody addChild: spacecrafts];
  205.     }
  206.     
  207.     std::vector<Location*>* locations = [(CelestiaBody *)[aBody body] body]->getLocations();
  208.     if (locations != NULL)
  209.     {
  210.         BrowserItem *locationItems = [[[BrowserItem alloc] initWithName: NSLocalizedStringFromTable(@"Locations",@"po",@"")] autorelease];
  211.         for (vector<Location*>::const_iterator iter = locations->begin();
  212.              iter != locations->end(); iter++)
  213.         {
  214.             [locationItems addChild: [[[BrowserItem alloc] initWithCelestiaLocation: [[[CelestiaLocation alloc] initWithLocation: *iter] autorelease]] autorelease]];
  215.         }
  216.         [aBody addChild: locationItems];
  217.     }
  218. }
  219. - (void)dealloc
  220. {
  221.     [childNames release];
  222.     [children release];
  223.     [data release];
  224.     [super dealloc];
  225. }
  226. - (NSString *)name
  227. {
  228.     return ([data respondsToSelector:@selector(name)]) ? [data name] : data;
  229. }
  230. - (id)body
  231. {
  232.     return ([data isKindOfClass: [NSString class]]) ? nil : data;
  233. }
  234. - (void)addChild: (BrowserItem *)aChild
  235. {
  236.     if (nil == children)
  237.         children = [[NSMutableDictionary alloc] init];
  238.     
  239.     [children setObject: aChild forKey: [aChild name]];
  240.     childrenChanged = YES;
  241. }
  242. - (id)childNamed: (NSString *)aName
  243. {
  244.     return [children objectForKey: aName];
  245. }
  246. - (NSArray *)allChildNames
  247. {
  248.     if (childrenChanged)
  249.     {
  250.         [childNames release]; childNames = nil;
  251.     }
  252.     
  253.     if (nil == childNames)
  254.     {
  255.         childNames = [[[children allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] retain];
  256.         childrenChanged = NO;
  257.     }
  258.     
  259.     return childNames;
  260. }
  261. - (unsigned)childCount
  262. {
  263.     return [children count];
  264. }
  265. @end