00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPBundle.j>
00024 @import <Foundation/CPNotificationCenter.j>
00025 @import <Foundation/CPObject.j>
00026 @import <Foundation/CPRunLoop.j>
00027 @import <Foundation/CPString.j>
00028
00029 @import "CPGeometry.j"
00030
00031 CPImageLoadStatusInitialized = 0;
00032 CPImageLoadStatusLoading = 1;
00033 CPImageLoadStatusCompleted = 2;
00034 CPImageLoadStatusCancelled = 3;
00035 CPImageLoadStatusInvalidData = 4;
00036 CPImageLoadStatusUnexpectedEOF = 5;
00037 CPImageLoadStatusReadError = 6;
00038
00039 CPImageDidLoadNotification = @"CPImageDidLoadNotification";
00040
00041 @implementation CPBundle (CPImageAdditions)
00042
00043 - (CPString)pathForResource:(CPString)aFilename
00044 {
00045 return [self resourcePath] + '/' + aFilename;
00046 }
00047
00048 @end
00049
00069 @implementation CPImage : CPObject
00070 {
00071 CGSize _size;
00072 CPString _filename;
00073
00074 id _delegate;
00075 unsigned _loadStatus;
00076
00077 Image _image;
00078 }
00079
00088 - (CPImage)initByReferencingFile:(CPString)aFilename size:(CGSize)aSize
00089 {
00090 self = [super init];
00091
00092 if (self)
00093 {
00094 _size = CPSizeCreateCopy(aSize);
00095 _filename = aFilename;
00096 _loadStatus = CPImageLoadStatusInitialized;
00097 }
00098
00099 return self;
00100 }
00101
00108 - (CPImage)initWithContentsOfFile:(CPString)aFilename size:(CGSize)aSize
00109 {
00110 self = [self initByReferencingFile:aFilename size:aSize];
00111
00112 if (self)
00113 [self load];
00114
00115 return self;
00116 }
00117
00124 - (CPImage)initWithContentsOfFile:(CPString)aFilename
00125 {
00126 self = [self initByReferencingFile:aFilename size: CGSizeMake(-1, -1)];
00127
00128 if (self)
00129 [self load];
00130
00131 return self;
00132 }
00133
00137 - (CPString)filename
00138 {
00139 return _filename;
00140 }
00141
00146 - (void)setSize:(CGSize)aSize
00147 {
00148 _size = CGSizeMakeCopy(aSize);
00149 }
00150
00154 - (CGSize)size
00155 {
00156 return _size;
00157 }
00158
00163 - (void)setDelegate:(id)aDelegate
00164 {
00165 _delegate = aDelegate;
00166 }
00167
00171 - (id)delegate
00172 {
00173 return _delegate;
00174 }
00175
00179 - (BOOL)loadStatus
00180 {
00181 return _loadStatus;
00182 }
00183
00190 - (void)load
00191 {
00192 if (_loadStatus == CPImageLoadStatusLoading || _loadStatus == CPImageLoadStatusCompleted)
00193 return;
00194
00195 _loadStatus = CPImageLoadStatusLoading;
00196
00197 _image = new Image();
00198
00199 var isSynchronous = YES;
00200
00201
00202 _image.onload = function ()
00203 {
00204 if (isSynchronous)
00205 window.setTimeout(function() { [self _imageDidLoad]; }, 0);
00206 else
00207 {
00208 [self _imageDidLoad];
00209 [[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
00210 }
00211 }
00212
00213 _image.onerror = function ()
00214 {
00215 if (isSynchronous)
00216 window.setTimeout(function() { [self _imageDidError]; }, 0);
00217 else
00218 {
00219 [self _imageDidError];
00220 [[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
00221 }
00222 }
00223
00224 _image.onabort = function ()
00225 {
00226 if (isSynchronous)
00227 window.setTimeout(function() { [self _imageDidAbort]; }, 0);
00228 else
00229 {
00230 [self _imageDidAbort];
00231 [[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
00232 }
00233 }
00234
00235 _image.src = _filename;
00236
00237 isSynchronous = NO;
00238 }
00239
00240 - (BOOL)isThreePartImage
00241 {
00242 return NO;
00243 }
00244
00245 - (BOOL)isNinePartImage
00246 {
00247 return NO;
00248 }
00249
00250
00251 - (void)_imageDidLoad
00252 {
00253 _loadStatus = CPImageLoadStatusCompleted;
00254
00255
00256 if (!_size || (_size.width == -1 && _size.height == -1))
00257 _size = CGSizeMake(_image.width, _image.height);
00258
00259 [[CPNotificationCenter defaultCenter]
00260 postNotificationName:CPImageDidLoadNotification
00261 object:self];
00262
00263 if ([_delegate respondsToSelector:@selector(imageDidLoad:)])
00264 [_delegate imageDidLoad:self];
00265 }
00266
00267
00268 - (void)_imageDidError
00269 {
00270 _loadStatus = CPImageLoadStatusReadError;
00271
00272 if ([_delegate respondsToSelector:@selector(imageDidError:)])
00273 [_delegate imageDidError:self];
00274 }
00275
00276
00277 - (void)_imageDidAbort
00278 {
00279 _loadStatus = CPImageLoadStatusCancelled;
00280
00281 if ([_delegate respondsToSelector:@selector(imageDidAbort:)])
00282 [_delegate imageDidAbort:self];
00283 }
00284
00285 @end
00286
00287 @implementation CPImage (CPCoding)
00288
00294 - (id)initWithCoder:(CPCoder)aCoder
00295 {
00296 return [self initWithContentsOfFile:[aCoder decodeObjectForKey:@"CPFilename"] size:[aCoder decodeSizeForKey:@"CPSize"]];
00297 }
00298
00303 - (void)encodeWithCoder:(CPCoder)aCoder
00304 {
00305 [aCoder encodeObject:_filename forKey:@"CPFilename"];
00306 [aCoder encodeSize:_size forKey:@"CPSize"];
00307 }
00308
00309 @end
00310
00311 @implementation CPThreePartImage : CPObject
00312 {
00313 CPArray _imageSlices;
00314 BOOL _isVertical;
00315 }
00316
00317 - (id)initWithImageSlices:(CPArray)imageSlices isVertical:(BOOL)isVertical
00318 {
00319 self = [super init];
00320
00321 if (self)
00322 {
00323 _imageSlices = imageSlices;
00324 _isVertical = isVertical;
00325 }
00326
00327 return self;
00328 }
00329
00330 - (CPArray)imageSlices
00331 {
00332 return _imageSlices;
00333 }
00334
00335 - (BOOL)isVertical
00336 {
00337 return _isVertical;
00338 }
00339
00340 - (BOOL)isThreePartImage
00341 {
00342 return YES;
00343 }
00344
00345 - (BOOL)isNinePartImage
00346 {
00347 return NO;
00348 }
00349
00350 @end
00351
00352 var CPThreePartImageImageSlicesKey = @"CPThreePartImageImageSlicesKey",
00353 CPThreePartImageIsVerticalKey = @"CPThreePartImageIsVerticalKey";
00354
00355 @implementation CPThreePartImage (CPCoding)
00356
00357 - (id)initWithCoder:(CPCoder)aCoder
00358 {
00359 self = [super init];
00360
00361 if (self)
00362 {
00363 _imageSlices = [aCoder decodeObjectForKey:CPThreePartImageImageSlicesKey];
00364 _isVertical = [aCoder decodeBoolForKey:CPThreePartImageIsVerticalKey];
00365 }
00366
00367 return self;
00368 }
00369
00370 - (void)encodeWithCoder:(CPCoder)aCoder
00371 {
00372 [aCoder encodeObject:_imageSlices forKey:CPThreePartImageImageSlicesKey];
00373 [aCoder encodeBool:_isVertical forKey:CPThreePartImageIsVerticalKey];
00374 }
00375
00376 @end
00377
00378
00379 @implementation CPNinePartImage : CPObject
00380 {
00381 CPArray _imageSlices;
00382 }
00383
00384 - (id)initWithImageSlices:(CPArray)imageSlices
00385 {
00386 self = [super init];
00387
00388 if (self)
00389 _imageSlices = imageSlices;
00390
00391 return self;
00392 }
00393
00394 - (CPArray)imageSlices
00395 {
00396 return _imageSlices;
00397 }
00398
00399 - (BOOL)isThreePartImage
00400 {
00401 return NO;
00402 }
00403
00404 - (BOOL)isNinePartImage
00405 {
00406 return YES;
00407 }
00408
00409 @end
00410
00411 var CPNinePartImageImageSlicesKey = @"CPNinePartImageImageSlicesKey";
00412
00413 @implementation CPNinePartImage (CPCoding)
00414
00415 - (id)initWithCoder:(CPCoder)aCoder
00416 {
00417 self = [super init];
00418
00419 if (self)
00420 _imageSlices = [aCoder decodeObjectForKey:CPNinePartImageImageSlicesKey];
00421
00422 return self;
00423 }
00424
00425 - (void)encodeWithCoder:(CPCoder)aCoder
00426 {
00427 [aCoder encodeObject:_imageSlices forKey:CPNinePartImageImageSlicesKey];
00428 }
00429
00430 @end