2015-12-30 06:41:46 +00:00
|
|
|
#if defined(Hiro_Timer)
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
@implementation CocoaTimer : NSObject
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
-(id) initWith:(hiro::mTimer&)timerReference {
|
2013-03-15 13:11:33 +00:00
|
|
|
if(self = [super init]) {
|
|
|
|
timer = &timerReference;
|
|
|
|
instance = nil;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSTimer*) instance {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) update {
|
|
|
|
if(instance) {
|
|
|
|
[instance invalidate];
|
|
|
|
instance = nil;
|
|
|
|
}
|
2016-01-08 09:23:46 +00:00
|
|
|
if(timer->enabled()) {
|
|
|
|
instance = [NSTimer
|
|
|
|
scheduledTimerWithTimeInterval:timer->state.interval / 1000.0
|
|
|
|
target:self selector:@selector(run:) userInfo:nil repeats:YES
|
|
|
|
];
|
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) run:(NSTimer*)instance {
|
2016-01-08 09:23:46 +00:00
|
|
|
if(timer->enabled()) {
|
|
|
|
timer->doActivate();
|
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
namespace hiro {
|
2013-03-15 13:11:33 +00:00
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pTimer::construct() -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
cocoaTimer = [[CocoaTimer alloc] initWith:self()];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pTimer::destruct() -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
if([cocoaTimer instance]) [[cocoaTimer instance] invalidate];
|
|
|
|
[cocoaTimer release];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pTimer::setEnabled(bool enabled) -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
[cocoaTimer update];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pTimer::setInterval(uint interval) -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
[cocoaTimer update];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-12-30 06:41:46 +00:00
|
|
|
|
|
|
|
#endif
|