Debian Codegraph @

phoenix/cocoa/timer.cpp

raw
1 @implementation CocoaTimer : NSObject
2
3 -(id) initWith:(phoenix::Timer&)timerReference {
4 if(self = [super init]) {
5 timer = &timerReference;
6 instance = nil;
7 }
8 return self;
9 }
10
11 -(NSTimer*) instance {
12 return instance;
13 }
14
15 -(void) update {
16 if(instance) {
17 [instance invalidate];
18 instance = nil;
19 }
20 if(timer->state.enabled == false) return;
21 instance = [NSTimer
22 scheduledTimerWithTimeInterval:timer->state.interval / 1000.0
23 target:self selector:@selector(run:) userInfo:nil repeats:YES
24 ];
25 }
26
27 -(void) run:(NSTimer*)instance {
28 if(timer->onActivate) timer->onActivate();
29 }
30
31 @end
32
33 namespace phoenix {
34
35 void pTimer::setEnabled(bool enabled) {
36 @autoreleasepool {
37 [cocoaTimer update];
38 }
39 }
40
41 void pTimer::setInterval(unsigned interval) {
42 @autoreleasepool {
43 [cocoaTimer update];
44 }
45 }
46
47 void pTimer::constructor() {
48 @autoreleasepool {
49 cocoaTimer = [[CocoaTimer alloc] initWith:timer];
50 }
51 }
52
53 void pTimer::destructor() {
54 @autoreleasepool {
55 if([cocoaTimer instance]) [[cocoaTimer instance] invalidate];
56 [cocoaTimer release];
57 }
58 }
59
60 }