Skip to content

Commit 8316664

Browse files
committed
made to work in macOS
1 parent 2714152 commit 8316664

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

Modules/@babylonjs/react-native-macos/macos/BabylonModule.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ @implementation BabylonModule
3131
resolve([NSNull null]);
3232
});
3333
}
34-
3534
@end

Modules/@babylonjs/react-native-macos/macos/BabylonNativeInterop.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
+ (void)resetView;
1212
+ (void)updateXRView:(MTKView*)mtkView;
1313
+ (bool)isXRActive;
14+
#ifndef TARGET_OS_OSX
1415
+ (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches event:(UIEvent*)event;
16+
#endif
1517
@end

Modules/@babylonjs/react-native-macos/macos/BabylonNativeInterop.mm

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,8 @@ + (void)onBridgeWillInvalidate:(NSNotification*)notification
5555
}
5656

5757
+ (void)updateView:(MTKView*)mtkView {
58-
const CGFloat scale = mtkView.contentScaleFactor;
59-
const int width = static_cast<int>(mtkView.bounds.size.width * scale);
60-
const int height = static_cast<int>(mtkView.bounds.size.height * scale);
61-
if (width != 0 && height != 0) {
62-
BabylonNative::UpdateView(mtkView, width, height);
63-
}
58+
const CGSize monitor = [[NSScreen mainScreen] frame].size;
59+
BabylonNative::UpdateView(mtkView, monitor.width, monitor.height);
6460
}
6561

6662
+ (void)updateMSAA:(NSNumber*)value {
@@ -83,6 +79,7 @@ + (bool)isXRActive {
8379
return BabylonNative::IsXRActive();
8480
}
8581

82+
#ifndef TARGET_OS_OSX
8683
+ (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches event:(UIEvent*)event {
8784
for (UITouch* touch in touches) {
8885
if (touch.view == mtkView) {
@@ -130,5 +127,6 @@ + (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches eve
130127
}
131128
}
132129
}
130+
#endif
133131

134132
@end

Modules/@babylonjs/react-native-macos/macos/EngineViewManager.mm

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#import <ReactCommon/CallInvoker.h>
66

77
#import <Foundation/Foundation.h>
8-
#import <UIKit/UIKit.h>
8+
99
#import <MetalKit/MetalKit.h>
1010

1111
@interface EngineView : MTKView
@@ -22,23 +22,39 @@ @implementation EngineView {
2222
}
2323

2424
- (instancetype)init:(RCTBridge*)_bridge {
25-
if (self = [super initWithFrame:CGRectZero device:MTLCreateSystemDefaultDevice()]) {
25+
#ifdef TARGET_OS_OSX
26+
const CGSize monitor = [[NSScreen mainScreen] frame].size;
27+
const CGSize appWin = [[NSApp mainWindow] frame].size;
28+
if (self = [super initWithFrame:CGRectMake((appWin.width - monitor.width) / 2, (appWin.height - monitor.height) / 2, monitor.width, monitor.height)
29+
device:MTLCreateSystemDefaultDevice()]) {
2630
bridge = _bridge;
2731
super.translatesAutoresizingMaskIntoConstraints = false;
2832
super.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
2933
super.depthStencilPixelFormat = MTLPixelFormatDepth32Float;
3034
}
35+
36+
[self setBounds:CGRectMake((appWin.width - monitor.width) / 2, (appWin.height - monitor.height) / 2, monitor.width, monitor.height)];
37+
#else
38+
if (self = [super initWithFrame:CGRectZero device:MTLCreateSystemDefaultDevice()]) {
39+
bridge = _bridge;
40+
super.translatesAutoresizingMaskIntoConstraints = false;
41+
super.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
42+
super.depthStencilPixelFormat = MTLPixelFormatDepth32Float;
43+
}
44+
#endif
3145
return self;
3246
}
3347

3448
- (void)setIsTransparentFlag:(NSNumber*)isTransparentFlag {
49+
#ifndef TARGET_OS_OSX
3550
BOOL isTransparent = [isTransparentFlag intValue] == 1;
3651
if(isTransparent){
3752
[self setOpaque:NO];
3853
} else {
3954
[self setOpaque:YES];
4055
}
4156
self.isTransparent = isTransparent;
57+
#endif
4258
}
4359

4460
- (void)setMSAA:(NSNumber*)value {
@@ -50,6 +66,7 @@ - (void)setBounds:(CGRect)bounds {
5066
[BabylonNativeInterop updateView:self];
5167
}
5268

69+
#ifndef TARGET_OS_OSX
5370
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
5471
[BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
5572
}
@@ -65,8 +82,10 @@ - (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
6582
- (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
6683
[BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
6784
}
85+
#endif
6886

6987
- (void)drawRect:(CGRect)rect {
88+
#ifndef TARGET_OS_OSX
7089
if ([BabylonNativeInterop isXRActive]) {
7190
if (!xrView) {
7291
xrView = [[MTKView alloc] initWithFrame:self.bounds device:self.device];
@@ -80,19 +99,20 @@ - (void)drawRect:(CGRect)rect {
8099
[xrView removeFromSuperview];
81100
xrView = nil;
82101
}
83-
102+
#endif
84103
[BabylonNativeInterop renderView];
85104
}
86105

87-
-(void)dealloc {
106+
- (void)dealloc {
88107
[BabylonNativeInterop updateXRView:nil];
89108
}
90109

91110
- (void)takeSnapshot {
111+
#ifndef TARGET_OS_OSX
92112
// We must take the screenshot on the main thread otherwise we might fail to get a valid handle on the view's image.
93113
dispatch_async(dispatch_get_main_queue(), ^{
94114
// Start the graphics context.
95-
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES /* opaque */, 0.0f);
115+
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0f);
96116

97117
// Draw the current state of the view into the graphics context.
98118
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
@@ -108,7 +128,17 @@ - (void)takeSnapshot {
108128
self.onSnapshotDataReturned(@{ @"data":encodedData});
109129
}
110130
});
131+
#endif
132+
}
133+
134+
#ifdef TARGET_OS_OSX
135+
- (void)layout {
136+
[super layout];
137+
const CGSize monitor = [[NSScreen mainScreen] frame].size;
138+
const CGSize appWin = [[NSApp mainWindow] frame].size;
139+
self.frame = CGRectMake((appWin.width - monitor.width) / 2, (appWin.height - monitor.height) / 2, monitor.width, monitor.height);
111140
}
141+
#endif
112142

113143
@end
114144

Modules/@babylonjs/react-native/shared/BabylonNative.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include <Babylon/Plugins/NativeInput.h>
99
#include <Babylon/Plugins/NativeOptimizations.h>
1010
#include <Babylon/Plugins/NativeTracing.h>
11+
#ifndef TARGET_OS_OSX
1112
#include <Babylon/Plugins/NativeXr.h>
13+
#endif
1214
#include <Babylon/Polyfills/Window.h>
1315
#include <Babylon/Polyfills/XMLHttpRequest.h>
1416
#include <Babylon/Polyfills/Canvas.h>
@@ -48,8 +50,10 @@ namespace BabylonNative
4850
Babylon::JsRuntime::CreateForJavaScript(m_env, Babylon::CreateJsRuntimeDispatcher(m_env, jsiRuntime, m_jsDispatcher, m_isRunning));
4951

5052
// Initialize Babylon Native plugins
53+
#ifndef TARGET_OS_OSX
5154
m_nativeXr.emplace(Babylon::Plugins::NativeXr::Initialize(m_env));
5255
m_nativeXr->SetSessionStateChangedCallback([isXRActive{ m_isXRActive }](bool isSessionActive) { *isXRActive = isSessionActive; });
56+
#endif
5357
Babylon::Plugins::NativeCapture::Initialize(m_env);
5458
m_nativeInput = &Babylon::Plugins::NativeInput::CreateForJavaScript(m_env);
5559
Babylon::Plugins::NativeOptimizations::Initialize(m_env);
@@ -78,6 +82,9 @@ namespace BabylonNative
7882
m_graphicsConfig.Window = window;
7983
m_graphicsConfig.Width = width;
8084
m_graphicsConfig.Height = height;
85+
#ifdef TARGET_OS_OSX
86+
[[[NSApp mainWindow] contentView] addSubview:window];
87+
#endif
8188
UpdateGraphicsConfiguration();
8289
}
8390

@@ -155,6 +162,12 @@ namespace BabylonNative
155162

156163
void Initialize()
157164
{
165+
#ifdef TARGET_OS_OSX
166+
id<MTLDevice> dev = MTLCreateSystemDefaultDevice();
167+
MTKView *mv = [[MTKView alloc] initWithFrame:[[NSApp mainWindow] frame] device:dev];
168+
m_graphicsConfig.Device = dev;
169+
m_graphicsConfig.Window = mv;
170+
#endif
158171
m_newEngine = true;
159172
}
160173

@@ -211,7 +224,9 @@ namespace BabylonNative
211224
#if defined(__APPLE__) || defined(ANDROID)
212225
void UpdateXRView(WindowType window)
213226
{
227+
#ifndef TARGET_OS_OSX
214228
m_nativeXr->UpdateWindow(window);
229+
#endif
215230
}
216231
#endif
217232

@@ -262,7 +277,11 @@ namespace BabylonNative
262277
bool m_isRenderingEnabled{};
263278
std::once_flag m_isGraphicsInitialized{};
264279
Babylon::Plugins::NativeInput* m_nativeInput{};
280+
#ifdef TARGET_OS_OSX
281+
std::optional<int> m_nativeXr{};
282+
#else
265283
std::optional<Babylon::Plugins::NativeXr> m_nativeXr{};
284+
#endif
266285

267286
Babylon::Graphics::Configuration m_graphicsConfig{};
268287

@@ -283,7 +302,7 @@ namespace BabylonNative
283302
if (!jsiRuntime.global().hasProperty(jsiRuntime, JS_INSTANCE_NAME))
284303
{
285304
auto nativeModule{ std::make_shared<ReactNativeModule>(jsiRuntime, jsDispatcher) };
286-
jsiRuntime.global().setProperty(jsiRuntime, JS_INSTANCE_NAME, jsi::Object::createFromHostObject(jsiRuntime, nativeModule));
305+
jsiRuntime.global().setProperty(jsiRuntime, JS_INSTANCE_NAME, jsi::Object::createFromHostObject(jsiRuntime, std::static_pointer_cast<facebook::jsi::HostObject>(nativeModule)));
287306
g_nativeModule = nativeModule;
288307
}
289308
if (auto nativeModule{ g_nativeModule.lock() })
@@ -388,21 +407,24 @@ namespace BabylonNative
388407

389408
bool IsXRActive()
390409
{
410+
#ifndef TARGET_OS_OSX
391411
if (auto nativeModule{ g_nativeModule.lock() })
392412
{
393413
return nativeModule->IsXRActive();
394414
}
395-
415+
#endif
396416
return false;
397417
}
398418

399419
#if defined(__APPLE__) || defined(ANDROID)
400420
void UpdateXRView(WindowType window)
401421
{
422+
#ifndef TARGET_OS_OSX
402423
if (auto nativeModule{ g_nativeModule.lock() })
403424
{
404425
nativeModule->UpdateXRView(window);
405426
}
427+
#endif
406428
}
407429
#endif
408430
}

0 commit comments

Comments
 (0)