summaryrefslogtreecommitdiff
path: root/platform/ios/src/EAGLContext+MGLAddtions.m
blob: 0012d00c57dac5dbccd1c427e8a8d3909070f71c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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