diff options
Diffstat (limited to 'platform/ios/src/EAGLContext+MGLAddtions.m')
-rw-r--r-- | platform/ios/src/EAGLContext+MGLAddtions.m | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/platform/ios/src/EAGLContext+MGLAddtions.m b/platform/ios/src/EAGLContext+MGLAddtions.m new file mode 100644 index 0000000000..0012d00c57 --- /dev/null +++ b/platform/ios/src/EAGLContext+MGLAddtions.m @@ -0,0 +1,58 @@ +#import "EAGLContext+MGLAddtions.h" +#import <objc/runtime.h> + + +NSString *const MapboxEAGLContextWillSwitchNotificationKey = @"MapboxEAGLContextWillSwitcheNotificationKey"; +NSString *const MapboxEAGLContextDidFinishSwitchedNotificationKey = @"MapboxEAGLContextDidFinishSwitchedNotificationKey"; + + +@implementation EAGLContext (MGLAddtions) + ++ (void)load{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + Class class = [self class]; + + SEL originalSelector = @selector(setCurrentContext:); + SEL swizzledSelector = @selector(mapbox_setCurrentContext:); + + Method originalMethod = class_getClassMethod(class, originalSelector); + Method swizzledMethod = class_getClassMethod(class, swizzledSelector); + + method_exchangeImplementations(originalMethod, swizzledMethod); + + }); +} + +#pragma mark - Method Swizzling + +static EAGLContext *mapboxContext = nil; +static BOOL currentContextShouldBeMapbox; + ++ (BOOL)mapbox_setCurrentContext:(EAGLContext *)context{ + if(context && context == mapboxContext && !currentContextShouldBeMapbox){ + [[NSNotificationCenter defaultCenter]postNotificationName:MapboxEAGLContextWillSwitchNotificationKey object:nil]; + return NO; + } + + BOOL bRet = [self mapbox_setCurrentContext:context]; + + if(mapboxContext && context && context != mapboxContext && !currentContextShouldBeMapbox){ + //other context has been current context + [[NSNotificationCenter defaultCenter]postNotificationName:MapboxEAGLContextDidFinishSwitchedNotificationKey object:nil]; + mapboxContext = nil; + } + return bRet; +} + + ++ (void)becomeMapboxContext:(EAGLContext *)context{ + mapboxContext = context; + currentContextShouldBeMapbox = YES; +} + ++ (void)destoryMapboxContext{ + currentContextShouldBeMapbox = NO; +} + +@end |