blob: f4f8a637f497266795410662ad9265679c88d245 [file] [log] [blame]
yangsu@google.com12d177d2011-08-01 17:07:12 +00001#import "SkEventNotifier.h"
2#include "SkEvent.h"
3#define SkEventClass @"SkEvenClass"
4@implementation SkEventNotifier
yangsu@google.com12d177d2011-08-01 17:07:12 +00005- (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////////////////////////////////////////////////////////////////////////////////
45void SkEvent::SignalNonEmptyQueue() {
yangsu@google.comf3493f02011-08-08 15:12:05 +000046 //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.com12d177d2011-08-01 17:07:12 +000052}
53
54void SkEvent::SignalQueueTimer(SkMSec delay) {
55 if (delay) {
56 //Convert to seconds
57 NSTimeInterval ti = delay/(float)SK_MSec1;
58 [SkEventNotifier postTimedSkEvent:ti];
59 }
60}