yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 1 | #import "SkEventNotifier.h" |
| 2 | #include "SkEvent.h" |
| 3 | #define SkEventClass @"SkEvenClass" |
| 4 | @implementation SkEventNotifier |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 5 | - (id)init { |
| 6 | self = [super init]; |
| 7 | if (self) { |
| 8 | //Register as an observer for SkEventClass events and call |
| 9 | //receiveSkEvent: upon receiving the event |
| 10 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 11 | selector:@selector(receiveSkEvent:) |
| 12 | name:SkEventClass |
| 13 | object:nil]; |
| 14 | } |
| 15 | return self; |
| 16 | } |
| 17 | |
| 18 | - (void)dealloc { |
| 19 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 20 | [super dealloc]; |
| 21 | } |
| 22 | |
| 23 | -(BOOL) acceptsFirstResponder { |
| 24 | return YES; |
| 25 | } |
| 26 | |
| 27 | //SkEvent Handers |
| 28 | - (void)receiveSkEvent:(NSNotification *)notification { |
| 29 | if(SkEvent::ProcessEvent()) |
| 30 | SkEvent::SignalNonEmptyQueue(); |
| 31 | } |
| 32 | |
| 33 | + (void)postTimedSkEvent:(NSTimeInterval)timeInterval { |
| 34 | [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self |
| 35 | selector:@selector(timerFireMethod:) |
| 36 | userInfo:nil repeats:NO]; |
| 37 | } |
| 38 | |
| 39 | + (void)timerFireMethod:(NSTimer*)theTimer { |
| 40 | SkEvent::ServiceQueueTimer(); |
| 41 | } |
| 42 | |
| 43 | @end |
| 44 | //////////////////////////////////////////////////////////////////////////////// |
| 45 | void SkEvent::SignalNonEmptyQueue() { |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 46 | //post a SkEventClass event to the default notification queue |
| 47 | NSNotification* notification = [NSNotification notificationWithName:SkEventClass object:nil]; |
| 48 | [[NSNotificationQueue defaultQueue] enqueueNotification:notification |
| 49 | postingStyle:NSPostWhenIdle |
| 50 | coalesceMask:NSNotificationNoCoalescing |
| 51 | forModes:nil]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void SkEvent::SignalQueueTimer(SkMSec delay) { |
| 55 | if (delay) { |
| 56 | //Convert to seconds |
| 57 | NSTimeInterval ti = delay/(float)SK_MSec1; |
| 58 | [SkEventNotifier postTimedSkEvent:ti]; |
| 59 | } |
| 60 | } |