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 |
| 5 | //Overwritten from NSObject |
| 6 | - (id)init { |
| 7 | self = [super init]; |
| 8 | if (self) { |
| 9 | //Register as an observer for SkEventClass events and call |
| 10 | //receiveSkEvent: upon receiving the event |
| 11 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 12 | selector:@selector(receiveSkEvent:) |
| 13 | name:SkEventClass |
| 14 | object:nil]; |
| 15 | } |
| 16 | return self; |
| 17 | } |
| 18 | |
| 19 | - (void)dealloc { |
| 20 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 21 | [super dealloc]; |
| 22 | } |
| 23 | |
| 24 | -(BOOL) acceptsFirstResponder { |
| 25 | return YES; |
| 26 | } |
| 27 | |
| 28 | //SkEvent Handers |
| 29 | - (void)receiveSkEvent:(NSNotification *)notification { |
| 30 | if(SkEvent::ProcessEvent()) |
| 31 | SkEvent::SignalNonEmptyQueue(); |
| 32 | } |
| 33 | |
| 34 | + (void)postTimedSkEvent:(NSTimeInterval)timeInterval { |
| 35 | [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self |
| 36 | selector:@selector(timerFireMethod:) |
| 37 | userInfo:nil repeats:NO]; |
| 38 | } |
| 39 | |
| 40 | + (void)timerFireMethod:(NSTimer*)theTimer { |
| 41 | SkEvent::ServiceQueueTimer(); |
| 42 | } |
| 43 | |
| 44 | @end |
| 45 | //////////////////////////////////////////////////////////////////////////////// |
| 46 | void SkEvent::SignalNonEmptyQueue() { |
| 47 | //post a SkEventClass event to the default notification center |
| 48 | [[NSNotificationCenter defaultCenter] postNotificationName:SkEventClass |
| 49 | object:nil]; |
| 50 | } |
| 51 | |
| 52 | void SkEvent::SignalQueueTimer(SkMSec delay) { |
| 53 | if (delay) { |
| 54 | //Convert to seconds |
| 55 | NSTimeInterval ti = delay/(float)SK_MSec1; |
| 56 | [SkEventNotifier postTimedSkEvent:ti]; |
| 57 | } |
| 58 | } |