summary refs log tree commit diff stats
path: root/libs/cocos2d/CCNotifications.m
blob: 235a6a03593b7269d2b973bf9ae361d854f611b2 (plain) (blame)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * CCNotifications
 *
 * Copyright (c) 2010 ForzeField Studios S.L.
 * http://forzefield.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


#import "CCNotifications.h"
#import "CCArray.h"
#import "notificationDesign.h"


@implementation ccNotificationData
@synthesize title		= title_;
@synthesize message		= message_;
@synthesize media		= media_;
@synthesize mediaType	= mediaType_;
@synthesize tag			= tag_;
@synthesize animated	= animated_;


- (void) dealloc
{
	[self setTitle:nil];
	[self setMessage:nil];
	[self setMedia:nil];
	[super dealloc];
}


@end

@interface CCNotifications (Private)

- (void) _updateAnimationIn;
- (void) _updateAnimationOut;
- (CCActionInterval*) _animation:(char)type time:(ccTime)time;
- (void) _showNotification;
- (void) _addNotificationToArray:(ccNotificationData*)data cached:(BOOL)isCached;
- (void) _startScheduler;
- (void) _hideNotification;
- (void) _hideNotificationScheduler;
- (void) registerWithTouchDispatcher;
- (void) _setState:(char)states;

@end


@implementation CCNotifications
@synthesize position				= position_;
@synthesize notificationDesign		= template_;
@synthesize animationIn				= animationIn_;
@synthesize animationOut			= animationOut_;
@synthesize delegate				= delegate_;
@synthesize showingTime				= showingTime_;
@synthesize currentNotification		= currentNotification_;

static CCNotifications *sharedManager;

+ (CCNotifications *)sharedManager
{
	if (!sharedManager)
		sharedManager = [[CCNotifications alloc] init];
	
	return sharedManager;
}

+ (id) alloc
{
	NSAssert(sharedManager == nil, @"Attempted to allocate a second instance of a singleton.");
	return [super alloc];
}

+ (id) systemWithTemplate:(CCNode <CCNotificationDesignProtocol> *)notifications
{
	NSAssert(sharedManager == nil, @"Attempted to allocate a second instance of a singleton. You should use setTemplate");
	sharedManager = [[CCNotifications alloc] initWithTemplate:notifications];
	
	return sharedManager;
}

+ (void) purgeSharedManager
{
	[sharedManager release];
}

- (id) init
{
	CCNode <CCNotificationDesignProtocol> *templates = [[[CCNotificationDefaultDesign alloc] init] autorelease];
	self = [self initWithTemplate:templates];
	return self;
}

- (id) initWithTemplate:(CCNode <CCNotificationDesignProtocol> *)templates
{
	if( (self = [super init]) ) {
		self.notificationDesign = templates;
		
		delegate_			= nil;
		state_				= kCCNotificationStateHide;
		typeAnimationIn_	= kCCNotificationAnimationMovement;
		typeAnimationOut_	= kCCNotificationAnimationMovement;
		timeAnimationIn_	= 0.0f;
		timeAnimationOut_	= 0.0f;
		
		cachedNotifications_ = [[CCArray alloc] initWithCapacity:4];
		
		//Default settings
		showingTime_		= 4.0f;
		position_			= kCCNotificationPositionTop;
		
		[self setAnimation:kCCNotificationAnimationMovement time:0.5f];
		//[self setAnimationIn:kCCNotificationAnimationMovement time:0.5f];
		//[self setAnimationOut:kCCNotificationAnimationScale time:0.5f];
	}	
	return self;
}

- (void) _setState:(char)states
{
	if(state_==states) return;
	state_ = states;
	
	if([delegate_ respondsToSelector:@selector(notification:newState:)])
		[delegate_ notification:currentNotification_ newState:state_];
	
	if([delegate_ respondsToSelector:@selector(notificationChangeState:tag:)])
		[delegate_ notificationChangeState:state_ tag:[currentNotification_ tag]];
}

- (void) setPosition:(char)positions
{
	position_ = positions;
	[self updateAnimations];
}

- (void) setNotificationDesign:(CCNode <CCNotificationDesignProtocol>*) templates
{
	if(state_!=kCCNotificationStateHide)
		[template_ stopAllActions];
    
	if(state_==kCCNotificationStateShowing)
	{
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
		[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
#endif
		[[CCScheduler sharedScheduler] unscheduleSelector:@selector(_hideNotificationScheduler) forTarget:self];		
	}
	
	[templates retain];
	[template_ release];
	template_ = templates;
	[template_ setVisible:NO];
	[template_ setIsRelativeAnchorPoint:YES];
	
	[self _setState:kCCNotificationStateHide];
}
#pragma mark Notification Actions

- (CCActionInterval*) _animation:(char)type time:(ccTime)time
{
	CCActionInterval *action = nil;
	switch (type){
		case kCCNotificationAnimationMovement:
			if(position_==kCCNotificationPositionBottom)
				action = [CCMoveBy actionWithDuration:time position:ccp(0, template_.contentSize.height)];
			
			else if(position_ == kCCNotificationPositionTop)
				action = [CCMoveBy actionWithDuration:time position:ccp(0, -template_.contentSize.height)];
			
			break;
		case kCCNotificationAnimationScale:
			action = [CCScaleBy actionWithDuration:time scale:(1.0f-KNOTIFICATIONMIN_SCALE)/KNOTIFICATIONMIN_SCALE];
			
			break;
		default: break;
	}
	return action;
}

- (void) _updateAnimationIn
{
	self.animationIn = [CCSequence actionOne:[self _animation:typeAnimationIn_ time:timeAnimationIn_] two:[CCCallFunc actionWithTarget:self selector:@selector(_startScheduler)]];
}

- (void) _updateAnimationOut
{
	CCActionInterval *tempAction = [self _animation:typeAnimationOut_ time:timeAnimationOut_];
	self.animationOut = [CCSequence actionOne:[tempAction reverse] two:[CCCallFunc actionWithTarget:self selector:@selector(_hideNotification)]];
}

- (void) updateAnimations
{
	[self _updateAnimationIn];
	[self _updateAnimationOut];
}

- (void) setAnimationIn:(char)type time:(ccTime)time
{
	typeAnimationIn_ = type;
	timeAnimationIn_ = time;
	[self _updateAnimationIn];
}

- (void) setAnimationOut:(char)type time:(ccTime)time
{
	typeAnimationOut_ = type;
	timeAnimationOut_ = time;
	[self _updateAnimationOut];
}

- (void) setAnimation:(char)type time:(ccTime)time
{
	typeAnimationIn_ = typeAnimationOut_ = type;
	timeAnimationIn_ = timeAnimationOut_ = time;
	[self updateAnimations];
}

#pragma mark Notification steps

- (void) _startScheduler
{
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
	[self registerWithTouchDispatcher];
#endif
	[self _setState:kCCNotificationStateShowing];
	[template_ stopAllActions];
	[[CCScheduler sharedScheduler] scheduleSelector:@selector(_hideNotificationScheduler) forTarget:self interval:showingTime_ paused:NO];
}

- (void) _hideNotification
{
	[self _setState:kCCNotificationStateHide];
	[template_ setVisible:NO];
	[template_ stopAllActions];
	[template_ onExit];
	
	//Release current notification
	[cachedNotifications_ removeObject:currentNotification_];
	self.currentNotification = nil;
	
	//Check next notification
	[self _showNotification];
}

- (void) _hideNotificationScheduler
{	
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
	[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
#endif
	[[CCScheduler sharedScheduler] unscheduleSelector:@selector(_hideNotificationScheduler) forTarget:self];
	if([currentNotification_ animated])
	{
		[self _setState:kCCNotificationStateAnimationOut];
		[template_ runAction:animationOut_];
	}else
		[self _hideNotification];
}

#pragma mark Manager Notifications

- (void) _addNotificationToArray:(ccNotificationData*)data cached:(BOOL)isCached
{
	if(isCached)
	{
		[cachedNotifications_ addObject:data];
		if([cachedNotifications_ count]==1)
			[self _showNotification];
	}else{
		if(currentNotification_)
		{
			[cachedNotifications_ removeObject:currentNotification_];
		}
		[cachedNotifications_ insertObject:data atIndex:0];
		[self _showNotification];
	}
}

- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate waitUntilDone:(BOOL)isCached
{
	ccNotificationData *data = [[ccNotificationData alloc] init];
	data.title		= title;
	data.message	= message;
	data.media		= image;
	data.mediaType  = kCCNotificationMediaPath;
	data.tag		= tag;
	data.animated	= animate;
	
	[self _addNotificationToArray:data cached:isCached];
	[data release];
	return data;
}

- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture tag:(int)tag animate:(BOOL)animate waitUntilDone:(BOOL)isCached
{
	ccNotificationData *data = [[ccNotificationData alloc] init];
	data.title		= title;
	data.message	= message;
	data.media		= texture;
	data.mediaType  = kCCNotificationMediaTexture;
	data.tag		= tag;
	data.animated	= animate;
	
	[self _addNotificationToArray:data cached:isCached];
	[data release];
	return data;
}

- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate
{
	return [self addWithTitle:title message:message image:image tag:tag animate:animate waitUntilDone:YES];
}

- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture tag:(int)tag animate:(BOOL)animate
{
	return [self addWithTitle:title message:message texture:texture tag:tag animate:animate waitUntilDone:YES];
}

/* Fast methods */

- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image
{
	return [self addWithTitle:title message:message image:image tag:-1 animate:YES waitUntilDone:YES];
}

- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture
{
	return [self addWithTitle:title message:message texture:texture tag:-1 animate:YES waitUntilDone:YES];
}

/* Deprecated */
- (void) addSafelyWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate
{
	[self addWithTitle:title message:message image:image tag:tag animate:animate waitUntilDone:YES];
}

- (void) _showNotification
{
	if([cachedNotifications_ count]==0) return;
	//Get notification data
	self.currentNotification = [cachedNotifications_ objectAtIndex:0];
	
	//Stop system
	if(state_==kCCNotificationStateShowing)
	{
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
		[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
#else
		[[CCEventDispatcher sharedDispatcher] removeMouseDelegate:self];
#endif
	}
	
	if(state_!=kCCNotificationStateHide)
	{
		[self _setState:kCCNotificationStateHide];
		
		[template_ setVisible:NO];
		[template_ stopAllActions];
		[template_ onExit];
		[[CCScheduler sharedScheduler] unscheduleSelector:@selector(_hideNotificationScheduler) forTarget:self];
	}
	
	//Get variables
	CCTexture2D *texture = (currentNotification_.media) ? ((currentNotification_.mediaType==kCCNotificationMediaTexture) ? (CCTexture2D*)currentNotification_.media : [[CCTextureCache sharedTextureCache] addImage:(NSString*)currentNotification_.media]) : nil;
	
	//Prepare template
	[template_ setVisible:NO];
	[template_ stopAllActions];
	[template_ onExit];
	
	//Prepare animation
	CGSize winSize = CGSizeMake(480,320);
	if(currentNotification_.animated)
	{
		if(position_==kCCNotificationPositionBottom){
			[template_ setAnchorPoint:ccp(0.5f, 0)];
			switch (typeAnimationIn_) {
				case kCCNotificationAnimationMovement:
					[template_ setScale:1.0f];
					[template_ setPosition:ccp(winSize.width/2.0f, -template_.contentSize.height)];
					
					break;
				case kCCNotificationAnimationScale:
					[template_ setScale:KNOTIFICATIONMIN_SCALE];
					[template_ setPosition:ccp(winSize.width/2.0f, 0)];
					
					break;
				default: return;
			}
			
		}else if(position_==kCCNotificationPositionTop)
		{
			[template_ setAnchorPoint:ccp(0.5f, 1)];
			switch (typeAnimationIn_){
				case kCCNotificationAnimationMovement:
					[template_ setScale:1.0f];
					[template_ setPosition:ccp(winSize.width/2.0f, winSize.height+template_.contentSize.height)];
					
					break;
				case kCCNotificationAnimationScale:
					[template_ setScale:KNOTIFICATIONMIN_SCALE];
					[template_ setPosition:ccp(winSize.width/2.0f, winSize.height)];
					
					break;
				default: return;
			}
		}
		[self _setState:kCCNotificationStateAnimationIn];
		[template_ onEnter];
		[template_ runAction:animationIn_];
		
	}else{
		if(position_==kCCNotificationPositionBottom)
		{
			[template_ setAnchorPoint:ccp(0.5f, 0)];
			[template_ setPosition:ccp(winSize.width/2.0f, 0)];
		}else if(position_==kCCNotificationPositionTop)
		{
			[template_ setAnchorPoint:ccp(0.5f, 1)];
			[template_ setPosition:ccp(winSize.width/2.0f, winSize.height)];
		}
		[self _startScheduler];
	}
	
	//Update template
	[template_ setTitle:[currentNotification_ title] message:[currentNotification_ message] texture:texture];
	[template_ setVisible:YES];
}

#pragma mark Touch Events

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
- (void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:INT_MIN];
}

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	CGPoint point = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
	CGRect rect = [template_ boundingBox];
	if(CGRectContainsPoint(rect, point))
		if([delegate_ respondsToSelector:@selector(touched:)] && [delegate_ touched:[currentNotification_ tag]])
			[self _hideNotificationScheduler];
}
#endif

#pragma mark Other methods

- (void) visit
{	
	[template_ visit];
}

- (NSString*) description
{
	return [NSString stringWithFormat:@"<%@ = %08X>", [self class], self];
}

-(void) dealloc
{
	CCLOG(@"cocos2d: deallocing %@", self);
	
	sharedManager = nil;
	[cachedNotifications_ release];
	[self setCurrentNotification:nil];
	[self setNotificationDesign:nil];
	[self setDelegate:nil];
	[self setAnimationIn:nil];
	[self setAnimationOut:nil];
	[super dealloc];
}
@end