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

OpenGL

开发平台:

Visual C++

  1. //
  2. //  CelestiaFavorite.mm
  3. //  celestia
  4. //
  5. //  Created by Bob Ippolito on Fri Jun 07 2002.
  6. //  Copyright (c) 2002 Chris Laurel. All rights reserved.
  7. //
  8. #import "CelestiaFavorite.h"
  9. #import "CelestiaFavorite_PrivateAPI.h"
  10. #import "CelestiaVector_PrivateAPI.h"
  11. #import "CelestiaUniversalCoord_PrivateAPI.h"
  12. #import "NSString_ObjCPlusPlus.h"
  13. #import "Astro.h"
  14. #import "Observer.h"
  15. #import "CelestiaAppCore.h"
  16. #import "CelestiaSimulation.h"
  17. #import "CelestiaSimulation_PrivateAPI.h"
  18. #ifdef URL_FAVORITES
  19. #include "url.h"
  20. #endif
  21. @implementation CelestiaFavorite(PrivateAPI)
  22. -(CelestiaFavorite *)initWithFavorite:(FavoritesEntry *)fav 
  23. {
  24.     self = [super init];
  25.     _data = [[NSValue alloc] initWithBytes:reinterpret_cast<void*>(&fav) objCType:@encode(FavoritesEntry*)];
  26.     _freeWhenDone = NO;
  27.     return self;
  28. }
  29. -(FavoritesEntry *)favorite
  30. {
  31.     return reinterpret_cast<FavoritesEntry*>([_data pointerValue]);
  32. }
  33. -(void)setSelectionName:(NSString*)name
  34. {
  35.     if (name == nil)
  36.         name = @"";
  37.     [self favorite]->selectionName = [name stdString];
  38. }
  39. -(void)setPosition:(CelestiaUniversalCoord*)position
  40. {
  41.     [self favorite]->position = [position universalCoord];
  42. }
  43. -(void)setOrientation:(CelestiaVector*)orientation
  44. {
  45.     [self favorite]->orientation = [orientation quatf];
  46. }
  47. -(void)setJd:(NSNumber*)jd
  48. {
  49.     [self favorite]->jd = [jd doubleValue];
  50. }
  51. -(void)setCoordinateSystem:(NSString*)coordSys
  52. {
  53.     [self favorite]->coordSys = (ObserverFrame::CoordinateSystem)[[Astro coordinateSystem:coordSys] intValue];
  54. }
  55. @end
  56. @implementation CelestiaFavorite
  57. -(void)encodeWithCoder:(NSCoder*)coder
  58. {
  59.     //[super encodeWithCoder:coder];
  60.     [coder encodeObject:[NSNumber numberWithBool:[self isFolder]]];
  61.     [coder encodeObject:[self name]];
  62.     [coder encodeObject:[self parentFolder]];
  63.     if ([self isFolder])
  64.         return;
  65.     [coder encodeObject:[self selectionName]];
  66.     [coder encodeObject:[self position]];
  67.     [coder encodeObject:[self orientation]];
  68.     [coder encodeObject:[self jd]];
  69.     [coder encodeObject:[self coordinateSystem]];
  70. }
  71. -(id)initWithCoder:(NSCoder*)coder
  72. {
  73.     FavoritesEntry* fav = new FavoritesEntry();
  74.     //self = [super initWithCoder:coder];
  75.     self = [self init];
  76.     _data = [[NSValue alloc] initWithBytes:reinterpret_cast<void*>(&fav) objCType:@encode(FavoritesEntry*)];
  77.     _freeWhenDone = YES;
  78.     fav->isFolder = (bool)[[coder decodeObject] boolValue];
  79.     [self setName:[coder decodeObject]];
  80.     [self setParentFolder:[coder decodeObject]];
  81.     if ([self isFolder])
  82.         return self;
  83.     [self setSelectionName:[coder decodeObject]];
  84.     [self setPosition:[coder decodeObject]];
  85.     [self setOrientation:[coder decodeObject]];
  86.     [self setJd:[coder decodeObject]];
  87.     [self setCoordinateSystem:[coder decodeObject]];
  88.     return self;
  89. }
  90. -(void)activate
  91. {
  92. #ifdef URL_FAVORITES
  93.     if (url)
  94.         [[CelestiaAppCore sharedAppCore] goToUrl: url];
  95.     else
  96. #endif
  97.         [[CelestiaAppCore sharedAppCore] activateFavorite:self];
  98. }
  99. -(id)initWithName:(NSString*)name
  100. {
  101.     return [self initWithName:name parentFolder:nil];
  102. }
  103. -(id)initWithFolderName:(NSString*)name
  104. {
  105.     return [self initWithFolderName:name parentFolder:nil];
  106. }
  107. -(id)initWithFolderName:(NSString*)name parentFolder:(CelestiaFavorite*)folder
  108. {
  109.     FavoritesEntry* fav = new FavoritesEntry();
  110.     NSString *parentFolder = (folder == nil) ? @"" : [folder name];
  111.     if (name == nil || [name isEqualToString:@""])
  112.         name = NSLocalizedString(@"untitled folder",@"");
  113.     fav->name = [name stdString];
  114.     fav->isFolder = true;
  115.     fav->parentFolder = [parentFolder stdString];
  116.     self = [self initWithFavorite:fav];
  117.     _freeWhenDone = YES;
  118. #ifdef URL_FAVORITES
  119.     [self setName: name];
  120. #endif
  121.     return self;
  122. }
  123. -(id)initWithName:(NSString*)name parentFolder:(CelestiaFavorite*)folder
  124. {
  125.     FavoritesEntry* fav = new FavoritesEntry();
  126.     Simulation* sim = [[[CelestiaAppCore sharedAppCore] simulation] simulation];
  127.     NSString *parentFolder = (folder == nil) ? @"" : [folder name];
  128.     if (name == nil)
  129.         name = [[[[CelestiaAppCore sharedAppCore] simulation] selection] name];
  130.     if (name == nil)
  131.         name = [[[[CelestiaAppCore sharedAppCore] simulation] julianDate] description];
  132.     fav->jd = sim->getTime();
  133.     fav->position = sim->getObserver().getPosition();
  134.     fav->orientation = sim->getObserver().getOrientationf();
  135.     fav->name = [name stdString];
  136.     fav->isFolder = false;
  137.     fav->parentFolder = [parentFolder stdString];
  138.     Selection sel = sim->getSelection();
  139.     if (sel.deepsky() != NULL)
  140.         fav->selectionName = sim->getUniverse()->getDSOCatalog()->getDSOName(sel.deepsky());
  141.     else
  142.         fav->selectionName = sel.getName();
  143.     fav->coordSys = sim->getFrame()->getCoordinateSystem();
  144.     self = [self initWithFavorite:fav];
  145.     _freeWhenDone = YES;
  146. #ifdef URL_FAVORITES
  147.     Url currentUrl = Url([[CelestiaAppCore sharedAppCore] appCore], Url::Absolute);
  148.     [self setUrl:  [NSString stringWithStdString: currentUrl.getAsString()]];
  149.     [self setName: [NSString stringWithStdString: currentUrl.getName()]];
  150.     if (_name == nil)
  151.         [self setName: [[[[CelestiaAppCore sharedAppCore] simulation] selection] name]];
  152.     if (_name == nil)
  153.         [self setName: [[[[CelestiaAppCore sharedAppCore] simulation] julianDate] description]];
  154. #endif
  155.     return self;
  156. }
  157. -(unsigned)hash
  158. {
  159. #ifdef URL_FAVORITES
  160.     if (url)
  161.         return [url hash];
  162. #endif
  163.     return (unsigned)[_data pointerValue];
  164. }
  165. -(void)dealloc
  166. {
  167. #ifdef URL_FAVORITES
  168.     [url release];   url = nil;
  169.     [_name release]; _name = nil;
  170. #endif
  171.     if (_data != nil) {
  172.         if (_freeWhenDone) {
  173.             FavoritesEntry* fav = [self favorite];
  174.             delete fav;
  175.         }
  176.         [_data release];
  177.         _data = nil;
  178.     }
  179.     [super dealloc];
  180. }
  181. -(BOOL)isEqualToFavorite:(CelestiaFavorite*)fav
  182. {
  183.     // pointer equality is enough in this case
  184.     return ([self favorite] == [fav favorite]);
  185. }
  186. -(BOOL)isEqualToString:(NSString*)str
  187. {
  188.     return ([[self name] isEqualToString:str]);
  189. }
  190. -(BOOL)isEqual:(id)obj
  191. {
  192.     if ([obj isKindOfClass:[NSString class]]) {
  193.         return [self isEqualToString:(NSString*)obj];
  194.     } else if ([obj isKindOfClass:[self class]]) {
  195.         return [self isEqualToFavorite:(CelestiaFavorite*)obj];
  196.     }
  197.     return NO;
  198. }
  199. -(NSDictionary*)dictionary
  200. {
  201.     if ([self isFolder])
  202.         return [NSDictionary dictionaryWithObjectsAndKeys:[self name],@"name",[NSNumber numberWithBool:YES],@"isFolder",nil,nil];
  203.     return [NSDictionary dictionaryWithObjectsAndKeys:
  204.         [self name], @"name",
  205.         [self parentFolder], @"parentFolder",
  206.         [self selectionName], @"selectionName",
  207.         [NSArray arrayWithArray:[self orientation]], @"orientation",
  208.         [[self position] data], @"position",
  209.         [self jd], @"jd",
  210.         [NSNumber numberWithBool:NO], @"isFolder",
  211.         [self coordinateSystem], @"coordinateSystem",
  212. #ifdef URL_FAVORITES
  213.         url, @"url",
  214. #endif
  215.         nil];
  216. }
  217. -(id)initWithDictionary:(NSDictionary*)dict
  218. {
  219.     FavoritesEntry* fav;
  220.     if ([[dict objectForKey:@"isFolder"] boolValue]) {
  221.         self = [self initWithFolderName:[dict objectForKey:@"name"]];
  222.         [self setParentFolder:[dict objectForKey:@"parentFolder"]];
  223.         return self;
  224.     }
  225.     fav = new FavoritesEntry();
  226.     self = [self init];
  227.     _data = [[NSValue alloc] initWithBytes:reinterpret_cast<void*>(&fav) objCType:@encode(FavoritesEntry*)];
  228.     _freeWhenDone = YES;
  229.     fav->isFolder = false;
  230.     [self setName:[dict objectForKey:@"name"]];
  231.     [self setParentFolder:[dict objectForKey:@"parentFolder"]];
  232.     [self setSelectionName:[dict objectForKey:@"selectionName"]];
  233.     [self setOrientation:[CelestiaVector vectorWithArray:[dict objectForKey:@"orientation"]]];
  234.     [self setPosition:[[[CelestiaUniversalCoord alloc] initWithData:[dict objectForKey:@"position"]] autorelease]];
  235.     [self setJd:[dict objectForKey:@"jd"]];
  236.     [self setCoordinateSystem:[dict objectForKey:@"coordinateSystem"]];
  237. #ifdef URL_FAVORITES
  238.     [self setUrl: [dict objectForKey: @"url"]];
  239. #endif
  240.     return self;
  241. }
  242. -(NSString*)description
  243. {
  244.     return [[self dictionary] description];
  245. }
  246. -(void)setName:(NSString*)name
  247. {
  248.     if (name == nil)
  249.         name = @"";
  250. #ifdef URL_FAVORITES
  251.     [_name release];
  252.     _name = [name retain];
  253. #else
  254.     [self favorite]->name = [name stdString];
  255. #endif
  256. }
  257. -(void)setParentFolder:(NSString*)parentFolder
  258. {
  259.     if (parentFolder == nil)
  260.         parentFolder = @"";
  261.     [self favorite]->parentFolder = [parentFolder stdString];
  262. }
  263. -(NSString*)name
  264. {
  265. #ifdef URL_FAVORITES
  266.     return [[_name retain] autorelease];
  267. #else
  268.     return [NSString stringWithStdString:[self favorite]->name];
  269. #endif
  270. }
  271. -(NSString*)selectionName
  272. {
  273.     return [NSString stringWithStdString:[self favorite]->selectionName];
  274. }
  275. -(CelestiaUniversalCoord*)position
  276. {
  277.     return [[[CelestiaUniversalCoord alloc] initWithUniversalCoord:[self favorite]->position] autorelease];
  278. }
  279. -(CelestiaVector*)orientation
  280. {
  281.     return [CelestiaVector vectorWithQuatf:[self favorite]->orientation];
  282. }
  283. -(NSNumber*)jd
  284. {
  285.     return [NSNumber numberWithDouble:[self favorite]->jd];
  286. }
  287. -(BOOL)isFolder
  288. {
  289.     return [self favorite]->isFolder;
  290. }
  291. -(NSString*)parentFolder
  292. {
  293.     return [NSString stringWithStdString:[self favorite]->parentFolder];
  294. }
  295. -(NSString*)coordinateSystem
  296. {
  297.     return [Astro stringWithCoordinateSystem:[NSNumber numberWithInt:(int)[self favorite]->coordSys]];
  298. }
  299. #ifdef URL_FAVORITES
  300. -(NSString*)url
  301. {
  302.     return url;
  303. }
  304. -(void)setUrl:(NSString *)aUrl
  305. {
  306.     url = [aUrl retain];
  307. }
  308. #endif
  309. @end