Skip to content

Commit 96dc576

Browse files
committed
Merge pull request phoboslab#527 from sleepygarden/addXibSupportEJJavascriptView
support EJJavascriptViews placed inside of XIBs of other view controllers
2 parents 8fbc9ac + 6675426 commit 96dc576

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

Source/Ejecta/EJJavaScriptView.m

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,54 +49,62 @@ - (id)initWithFrame:(CGRect)frame {
4949

5050
- (id)initWithFrame:(CGRect)frame appFolder:(NSString *)folder {
5151
if( self = [super initWithFrame:frame] ) {
52-
oldSize = frame.size;
53-
appFolder = [folder retain];
54-
55-
isPaused = false;
56-
57-
// CADisplayLink (and NSNotificationCenter?) retains it's target, but this
58-
// is causing a retain loop - we can't completely release the scriptView
59-
// from the outside.
60-
// So we're using a "weak proxy" that doesn't retain the scriptView; we can
61-
// then just invalidate the CADisplayLink in our dealloc and be done with it.
62-
proxy = [[EJNonRetainingProxy proxyWithTarget:self] retain];
63-
64-
self.pauseOnEnterBackground = YES;
65-
66-
// Limit all background operations (image & sound loading) to one thread
67-
backgroundQueue = [[NSOperationQueue alloc] init];
68-
backgroundQueue.maxConcurrentOperationCount = 1;
69-
70-
timers = [[EJTimerCollection alloc] initWithScriptView:self];
71-
72-
displayLink = [[CADisplayLink displayLinkWithTarget:proxy selector:@selector(run:)] retain];
73-
[displayLink setFrameInterval:1];
74-
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
75-
76-
// Create the global JS context in its own group, so it can be released properly
77-
jsGlobalContext = JSGlobalContextCreateInGroup(NULL, NULL);
78-
jsUndefined = JSValueMakeUndefined(jsGlobalContext);
79-
JSValueProtect(jsGlobalContext, jsUndefined);
80-
81-
// Attach all native class constructors to 'Ejecta'
82-
classLoader = [[EJClassLoader alloc] initWithScriptView:self name:@"Ejecta"];
83-
84-
85-
// Retain the caches here, so even if they're currently unused in JavaScript,
86-
// they will persist until the last scriptView is released
87-
textureCache = [[EJSharedTextureCache instance] retain];
88-
openALManager = [[EJSharedOpenALManager instance] retain];
89-
openGLContext = [[EJSharedOpenGLContext instance] retain];
90-
91-
// Create the OpenGL context for Canvas2D
92-
glCurrentContext = openGLContext.glContext2D;
93-
[EAGLContext setCurrentContext:glCurrentContext];
94-
95-
[self loadScriptAtPath:EJECTA_BOOT_JS];
52+
[self setupWithAppFolder:folder];
9653
}
9754
return self;
9855
}
9956

57+
-(void)awakeFromNib {
58+
[self setupWithAppFolder:EJECTA_DEFAULT_APP_FOLDER];
59+
}
60+
61+
-(void)setupWithAppFolder:(NSString*)folder {
62+
oldSize = self.frame.size;
63+
appFolder = [folder retain];
64+
65+
isPaused = false;
66+
67+
// CADisplayLink (and NSNotificationCenter?) retains it's target, but this
68+
// is causing a retain loop - we can't completely release the scriptView
69+
// from the outside.
70+
// So we're using a "weak proxy" that doesn't retain the scriptView; we can
71+
// then just invalidate the CADisplayLink in our dealloc and be done with it.
72+
proxy = [[EJNonRetainingProxy proxyWithTarget:self] retain];
73+
74+
self.pauseOnEnterBackground = YES;
75+
76+
// Limit all background operations (image & sound loading) to one thread
77+
backgroundQueue = [[NSOperationQueue alloc] init];
78+
backgroundQueue.maxConcurrentOperationCount = 1;
79+
80+
timers = [[EJTimerCollection alloc] initWithScriptView:self];
81+
82+
displayLink = [[CADisplayLink displayLinkWithTarget:proxy selector:@selector(run:)] retain];
83+
[displayLink setFrameInterval:1];
84+
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
85+
86+
// Create the global JS context in its own group, so it can be released properly
87+
jsGlobalContext = JSGlobalContextCreateInGroup(NULL, NULL);
88+
jsUndefined = JSValueMakeUndefined(jsGlobalContext);
89+
JSValueProtect(jsGlobalContext, jsUndefined);
90+
91+
// Attach all native class constructors to 'Ejecta'
92+
classLoader = [[EJClassLoader alloc] initWithScriptView:self name:@"Ejecta"];
93+
94+
95+
// Retain the caches here, so even if they're currently unused in JavaScript,
96+
// they will persist until the last scriptView is released
97+
textureCache = [[EJSharedTextureCache instance] retain];
98+
openALManager = [[EJSharedOpenALManager instance] retain];
99+
openGLContext = [[EJSharedOpenGLContext instance] retain];
100+
101+
// Create the OpenGL context for Canvas2D
102+
glCurrentContext = openGLContext.glContext2D;
103+
[EAGLContext setCurrentContext:glCurrentContext];
104+
105+
[self loadScriptAtPath:EJECTA_BOOT_JS];
106+
}
107+
100108
- (void)dealloc {
101109
// Wait until all background operations are finished. If we would just release the
102110
// backgroundQueue it would cancel running operations (such as texture loading) and

0 commit comments

Comments
 (0)