forked from menssen/panoramagl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLJSONLoader.m
324 lines (297 loc) · 13.2 KB
/
PLJSONLoader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* PanoramaGL library
* Version 0.1
* Copyright (c) 2010 Javier Baez <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "PLObjectBaseProtected.h"
#import "PLJSONLoader.h"
#import "PLView.h"
#import "PLIView.h"
@interface PLJSONLoader(Private)
-(void)loadJSON:(NSString *)jsonString;
-(NSString *)getResourcePath;
-(NSString *)getFilePath:(NSString *)filename urlbase:(NSString *)urlbase;
-(PLTexture *)createTexture:(NSString *)filename urlbase:(NSString *)urlbase;
-(void)loadCubicPanoramaTexture:(NSObject<PLIPanorama> *)panorama face:(PLCubeFaceOrientation)face images:(NSDictionary *)images property:(NSString *)property urlbase:(NSString *)urlbase;
-(PLTexture *)createHotspotTexture:(NSString *)filename urlbase:(NSString *)urlbase;
@end
@implementation PLJSONLoader
#pragma mark -
#pragma mark init methods
-(id)init
{
return nil;
}
-(id)initWithString:(NSString *)string
{
if(self = [super init])
[self loadJSON:string];
return self;
}
-(id)initWithPath:(NSString *)path
{
if(self = [super init])
{
NSError *error = nil;
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if(jsonString)
{
[self loadJSON:jsonString];
[jsonString release];
}
else
[NSException raise:@"PanoramaGL" format:@"path is incorrect"];
}
return self;
}
-(void)initializeValues
{
[super initializeValues];
hotspotTextures = [[NSMutableDictionary alloc] init];
}
+(id)loaderWithString:(NSString *)string
{
return [[[PLJSONLoader alloc] initWithString:string] autorelease];
}
+(id)loaderWithPath:(NSString *)path
{
return [[[PLJSONLoader alloc] initWithPath:path] autorelease];
}
#pragma mark -
#pragma mark utility methods
-(void)loadJSON:(NSString *)jsonString
{
if(jsonString)
{
NSObject *jsonObject = [jsonString objectFromJSONString];
if(jsonObject && [jsonObject isKindOfClass:[NSDictionary class]])
{
json = (NSDictionary *)[jsonObject retain];
return;
}
}
else
[NSException raise:@"PanoramaGL" format:@"JSON string is empty"];
[NSException raise:@"PanoramaGL" format:@"JSON parse failed"];
}
-(NSString *)getResourcePath
{
return [[NSBundle mainBundle] resourcePath];
}
-(NSString *)getFilePath:(NSString *)filename urlbase:(NSString *)urlbase
{
BOOL isFullURL = ([filename rangeOfString:@"res://"].location != NSNotFound);
return (isFullURL ? filename : [NSString stringWithFormat:@"%@/%@", urlbase, filename]);
}
-(PLTexture *)createTexture:(NSString *)filename urlbase:(NSString *)urlbase
{
if(filename)
return [PLTexture textureWithImage:[PLImage imageWithPath:[self getFilePath:filename urlbase:urlbase]]];
return nil;
}
-(void)loadCubicPanoramaTexture:(NSObject<PLIPanorama> *)panorama face:(PLCubeFaceOrientation)face images:(NSDictionary *)images property:(NSString *)property urlbase:(NSString *)urlbase
{
if([[images allKeys] containsObject:property])
{
PLTexture *texture = [self createTexture:[images objectForKey:property] urlbase:urlbase];
if(texture)
[(PLCubicPanorama *)panorama setTexture:texture face:face];
else
[NSException raise:@"PanoramaGL" format:@"images.%@ property wrong value", property];
}
else
[NSException raise:@"PanoramaGL" format:@"images.%@ property not exists", property];
}
-(PLTexture *)createHotspotTexture:(NSString *)filename urlbase:(NSString *)urlbase
{
if(filename)
{
BOOL isFullURL = ([filename rangeOfString:@"res://"].location != NSNotFound);
NSString *url = (isFullURL ? filename : [NSString stringWithFormat:@"%@/%@", urlbase, filename]);
if([[hotspotTextures allKeys] containsObject:url])
return [hotspotTextures objectForKey:url];
else
{
PLTexture *texture = [PLTexture textureWithImage:[PLImage imageWithPath:url]];
[hotspotTextures setValue:texture forKey:url];
return texture;
}
}
return nil;
}
#pragma mark -
#pragma mark load methods
-(void)load:(UIView<PLIView> *)view
{
if(json)
{
NSString *urlbase = [json objectForKey:@"urlBase"];
if(urlbase && [urlbase isKindOfClass:[NSString class]] && [urlbase rangeOfString:@"res://"].location != NSNotFound)
{
NSString *path = [urlbase stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
path = [path stringByReplacingOccurrencesOfString:@"res://" withString:@""];
if(path && [path length] > 0)
urlbase = [NSString stringWithFormat:@"%@/%@", [self getResourcePath], path];
else
urlbase = [self getResourcePath];
}
else
[NSException raise:@"PanoramaGL" format:@"urlBase property not exists"];
if(!urlbase)
[NSException raise:@"PanoramaGL" format:@"urlBase property is wrong"];
NSString *type = [json objectForKey:@"type"];
NSObject<PLIPanorama> *panorama = nil;
PLPanoramaType panoramaType = PLPanoramaTypeUnknow;
if(type && [type isKindOfClass:[NSString class]])
{
if([type isEqualToString:@"spherical"])
{
panoramaType = PLPanoramaTypeSpherical;
panorama = [[PLSphericalPanorama alloc] init];
}
else if([type isEqualToString:@"spherical2"])
{
panoramaType = PLPanoramaTypeSpherical2;
panorama = [[PLSpherical2Panorama alloc] init];
}
else if([type isEqualToString:@"cubic"])
{
panoramaType = PLPanoramaTypeCubic;
panorama = [[PLCubicPanorama alloc] init];
}
if(!panorama)
[NSException raise:@"PanoramaGL" format:@"Panorama type is wrong"];
}
else
[NSException raise:@"PanoramaGL" format:@"type property not exists"];
NSDictionary *images = [json objectForKey:@"images"];
if(images && [images isKindOfClass:[NSDictionary class]])
{
if([[images allKeys] containsObject:@"preview"])
{
NSString *preview = [images objectForKey:@"preview"];
[panorama setPreviewImage:[PLImage imageWithPath:[NSString stringWithFormat:@"%@/%@", urlbase, preview]]];
}
if(panoramaType == PLPanoramaTypeSpherical)
{
if([[images allKeys] containsObject:@"image"])
{
PLTexture *imageTexture = [self createTexture:[images objectForKey:@"image"] urlbase:urlbase];
if(imageTexture)
[(PLSphericalPanorama *)panorama setTexture:imageTexture];
else
[NSException raise:@"PanoramaGL" format:@"images.image property wrong value"];
}
else
[NSException raise:@"PanoramaGL" format:@"images.image property not exists"];
}
else if(panoramaType == PLPanoramaTypeSpherical2)
{
if([[images allKeys] containsObject:@"image"])
{
[(PLSpherical2Panorama *)panorama setImage:[PLImage imageWithPath:[self getFilePath:[images objectForKey:@"image"] urlbase:urlbase]]];
}
else
[NSException raise:@"PanoramaGL" format:@"images.image property not exists"];
}
else if(panoramaType == PLPanoramaTypeCubic)
{
[self loadCubicPanoramaTexture:panorama face:PLCubeFaceOrientationFront images:images property:@"front" urlbase:urlbase];
[self loadCubicPanoramaTexture:panorama face:PLCubeFaceOrientationBack images:images property:@"back" urlbase:urlbase];
[self loadCubicPanoramaTexture:panorama face:PLCubeFaceOrientationLeft images:images property:@"left" urlbase:urlbase];
[self loadCubicPanoramaTexture:panorama face:PLCubeFaceOrientationRight images:images property:@"right" urlbase:urlbase];
[self loadCubicPanoramaTexture:panorama face:PLCubeFaceOrientationUp images:images property:@"up" urlbase:urlbase];
[self loadCubicPanoramaTexture:panorama face:PLCubeFaceOrientationDown images:images property:@"down" urlbase:urlbase];
}
else if(panoramaType == PLPanoramaTypeCylindrical)
{
if([[images allKeys] containsObject:@"image"])
{
PLTexture *imageTexture = [self createTexture:[images objectForKey:@"image"] urlbase:urlbase];
if(imageTexture)
[(PLCylindricalPanorama *)panorama setTexture:imageTexture];
else
[NSException raise:@"PanoramaGL" format:@"images.image property wrong value"];
}
else
[NSException raise:@"PanoramaGL" format:@"images.image property not exists"];
}
}
else
[NSException raise:@"PanoramaGL" format:@"images property not exists"];
NSDictionary *camera = [json objectForKey:@"camera"];
if(camera && [camera isKindOfClass:[NSDictionary class]])
{
NSArray *cameraKeys = [camera allKeys];
if([cameraKeys containsObject:@"athmin"] && [cameraKeys containsObject:@"athmax"] && [cameraKeys containsObject:@"atvmin"] && [cameraKeys containsObject:@"atvmax"] && [cameraKeys containsObject:@"hlookat"] && [cameraKeys containsObject:@"vlookat"])
{
PLCamera *currentCamera = panorama.currentCamera;
int athmin = [(NSNumber *)[camera objectForKey:@"athmin"] intValue];
int athmax = [(NSNumber *)[camera objectForKey:@"athmax"] intValue];
int atvmin = [(NSNumber *)[camera objectForKey:@"atvmin"] intValue];
int atvmax = [(NSNumber *)[camera objectForKey:@"atvmax"] intValue];
int hlookat = [(NSNumber *)[camera objectForKey:@"hlookat"] intValue];
int vlookat = [(NSNumber *)[camera objectForKey:@"vlookat"] intValue];
currentCamera.pitchRange = PLRangeMake(atvmin, atvmax);
currentCamera.yawRange = PLRangeMake(athmin, athmax);
[currentCamera setInitialLookAtWithPitch:vlookat yaw:hlookat];
}
else
[NSException raise:@"PanoramaGL" format:@"camera properties are wrong"];
}
NSArray *hotspots = [json objectForKey:@"hotspots"];
if(hotspots && [hotspots isKindOfClass:[NSArray class]])
{
NSUInteger hotspotsCount = [hotspots count];
for(NSUInteger i = 0; i < hotspotsCount; i++)
{
NSDictionary *hotspot = [hotspots objectAtIndex:i];
if(hotspot && [hotspot isKindOfClass:[NSDictionary class]])
{
NSArray *hotspotKeys = [hotspot allKeys];
if([hotspotKeys containsObject:@"id"] && [hotspotKeys containsObject:@"image"] && [hotspotKeys containsObject:@"atv"] && [hotspotKeys containsObject:@"ath"] && [hotspotKeys containsObject:@"width"] && [hotspotKeys containsObject:@"height"])
{
PLTexture *hotspotTexture = [self createHotspotTexture:[hotspot objectForKey:@"image"] urlbase:urlbase];
int identifier = [(NSNumber *)[hotspot objectForKey:@"id"] intValue];
int atv = [(NSNumber *)[hotspot objectForKey:@"atv"] intValue];
int ath = [(NSNumber *)[hotspot objectForKey:@"ath"] intValue];
float width = [(NSNumber *)[hotspot objectForKey:@"width"] floatValue];
float height = [(NSNumber *)[hotspot objectForKey:@"height"] floatValue];
PLHotspot *currentHotspot = [PLHotspot hotspotWithId:identifier texture:hotspotTexture atv:atv ath:ath];
[currentHotspot setWidth:width];
[currentHotspot setHeight:height];
[panorama addHotspot:currentHotspot];
}
}
}
}
[view setPanorama:panorama];
if([[json allKeys] containsObject:@"sensorialRotation"] && [(NSNumber *)[json objectForKey:@"sensorialRotation"] boolValue])
[view startSensorialRotation];
[view startAnimation];
[panorama release];
}
}
#pragma mark -
#pragma mark dealloc methods
-(void)dealloc
{
if(json)
[json release];
if(hotspotTextures)
[hotspotTextures release];
[super dealloc];
}
@end