@babylonjs/react-native 0.65.0-alpha.47 → 1.0.0-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/EngineHook.d.ts +0 -1
  2. package/EngineHook.js +0 -1
  3. package/EngineHook.js.map +1 -1
  4. package/EngineView.js +13 -37
  5. package/EngineView.js.map +1 -1
  6. package/NativeEngineHook.d.ts +4 -0
  7. package/NativeEngineHook.js +43 -0
  8. package/NativeEngineHook.js.map +1 -0
  9. package/NativeEngineView.d.ts +10 -0
  10. package/NativeEngineView.js +3 -0
  11. package/NativeEngineView.js.map +1 -0
  12. package/package.json +5 -5
  13. package/shared/BabylonNative.h +4 -4
  14. package/android/build.gradle +0 -173
  15. package/android/include/IXrContextARCore.h +0 -10
  16. package/android/src/main/AndroidManifest.xml +0 -4
  17. package/android/src/main/java/com/babylonreactnative/BabylonModule.java +0 -37
  18. package/android/src/main/java/com/babylonreactnative/BabylonNativeInterop.java +0 -125
  19. package/android/src/main/java/com/babylonreactnative/BabylonPackage.java +0 -21
  20. package/android/src/main/java/com/babylonreactnative/EngineView.java +0 -230
  21. package/android/src/main/java/com/babylonreactnative/EngineViewManager.java +0 -71
  22. package/android/src/main/java/com/babylonreactnative/SnapshotDataReturnedEvent.java +0 -28
  23. package/android/src/main/jniLibs/arm64-v8a/libBabylonNative.so +0 -0
  24. package/android/src/main/jniLibs/arm64-v8a/libturbomodulejsijni.so +0 -0
  25. package/android/src/main/jniLibs/armeabi-v7a/libBabylonNative.so +0 -0
  26. package/android/src/main/jniLibs/armeabi-v7a/libturbomodulejsijni.so +0 -0
  27. package/android/src/main/jniLibs/x86/libBabylonNative.so +0 -0
  28. package/android/src/main/jniLibs/x86/libturbomodulejsijni.so +0 -0
  29. package/ios/BabylonModule.mm +0 -35
  30. package/ios/BabylonNativeInterop.h +0 -14
  31. package/ios/BabylonNativeInterop.mm +0 -130
  32. package/ios/EngineViewManager.mm +0 -140
  33. package/ios/ReactNativeBabylon.xcodeproj/project.pbxproj +0 -14881
  34. package/ios/ReactNativeBabylon.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +0 -8
  35. package/ios/include/IXrContextARKit.h +0 -10
  36. package/ios/libs/libBabylonNative.a +0 -0
  37. package/ios/libs/libCanvas.a +0 -0
  38. package/ios/libs/libGenericCodeGen.a +0 -0
  39. package/ios/libs/libGraphics.a +0 -0
  40. package/ios/libs/libJsRuntime.a +0 -0
  41. package/ios/libs/libMachineIndependent.a +0 -0
  42. package/ios/libs/libNativeCapture.a +0 -0
  43. package/ios/libs/libNativeEngine.a +0 -0
  44. package/ios/libs/libNativeInput.a +0 -0
  45. package/ios/libs/libNativeOptimizations.a +0 -0
  46. package/ios/libs/libNativeTracing.a +0 -0
  47. package/ios/libs/libNativeXr.a +0 -0
  48. package/ios/libs/libOGLCompiler.a +0 -0
  49. package/ios/libs/libOSDependent.a +0 -0
  50. package/ios/libs/libSPIRV.a +0 -0
  51. package/ios/libs/libUrlLib.a +0 -0
  52. package/ios/libs/libWindow.a +0 -0
  53. package/ios/libs/libXMLHttpRequest.a +0 -0
  54. package/ios/libs/libastc-codec.a +0 -0
  55. package/ios/libs/libastc.a +0 -0
  56. package/ios/libs/libbgfx.a +0 -0
  57. package/ios/libs/libbimg.a +0 -0
  58. package/ios/libs/libbx.a +0 -0
  59. package/ios/libs/libglslang.a +0 -0
  60. package/ios/libs/libnapi.a +0 -0
  61. package/ios/libs/libspirv-cross-core.a +0 -0
  62. package/ios/libs/libspirv-cross-glsl.a +0 -0
  63. package/ios/libs/libspirv-cross-msl.a +0 -0
  64. package/ios/libs/libtinyexr.a +0 -0
  65. package/ios/libs/libxr.a +0 -0
  66. package/react-native-babylon.podspec +0 -25
@@ -1,230 +0,0 @@
1
- package com.babylonreactnative;
2
-
3
- import android.annotation.TargetApi;
4
- import android.graphics.Bitmap;
5
- import android.graphics.SurfaceTexture;
6
- import android.os.Build;
7
- import android.os.Handler;
8
- import android.os.HandlerThread;
9
- import android.util.Base64;
10
- import android.view.MotionEvent;
11
- import android.view.PixelCopy;
12
- import android.view.Surface;
13
- import android.view.SurfaceHolder;
14
- import android.view.SurfaceView;
15
- import android.view.TextureView;
16
- import android.view.View;
17
- import android.widget.FrameLayout;
18
-
19
- import androidx.annotation.NonNull;
20
-
21
- import com.facebook.react.bridge.ReactContext;
22
- import com.facebook.react.uimanager.UIManagerModule;
23
- import com.facebook.react.uimanager.events.EventDispatcher;
24
-
25
- import java.io.ByteArrayOutputStream;
26
-
27
- public final class EngineView extends FrameLayout implements SurfaceHolder.Callback, TextureView.SurfaceTextureListener, View.OnTouchListener {
28
- private static final FrameLayout.LayoutParams childViewLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
29
- private TextureView transparentTextureView;
30
- private Surface transparentSurface = null;
31
- private SurfaceView opaqueSurfaceView = null;
32
- private SurfaceView xrSurfaceView;
33
- private final EventDispatcher reactEventDispatcher;
34
- private Runnable renderRunnable;
35
-
36
- public EngineView(ReactContext reactContext) {
37
- super(reactContext);
38
-
39
- this.setIsTransparent(false);
40
-
41
- this.xrSurfaceView = new SurfaceView(reactContext);
42
- this.xrSurfaceView.setLayoutParams(childViewLayoutParams);
43
- this.xrSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
44
- @Override
45
- public void surfaceCreated(SurfaceHolder holder) {
46
- // surfaceChanged is also called when the surface is created, so just do all the handling there
47
- }
48
-
49
- @Override
50
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
51
- BabylonNativeInterop.updateXRView(holder.getSurface());
52
- }
53
-
54
- @Override
55
- public void surfaceDestroyed(SurfaceHolder holder) {
56
- BabylonNativeInterop.updateXRView(null);
57
- }
58
- });
59
- this.xrSurfaceView.setVisibility(View.INVISIBLE);
60
- this.addView(this.xrSurfaceView);
61
-
62
- this.setOnTouchListener(this);
63
-
64
- this.reactEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
65
- }
66
-
67
- // ------------------------------------
68
- // TextureView related
69
-
70
- public void setIsTransparent(Boolean isTransparent) {
71
- if (isTransparent) {
72
- if (this.opaqueSurfaceView != null) {
73
- this.opaqueSurfaceView.setVisibility(View.GONE);
74
- this.opaqueSurfaceView = null;
75
- }
76
- if (this.transparentTextureView == null) {
77
- this.transparentTextureView = new TextureView(this.getContext());
78
- this.transparentTextureView.setLayoutParams(EngineView.childViewLayoutParams);
79
- this.transparentTextureView.setSurfaceTextureListener(this);
80
- this.transparentTextureView.setOpaque(false);
81
- this.addView(this.transparentTextureView);
82
- }
83
- } else {
84
- if (this.transparentTextureView != null) {
85
- this.transparentTextureView.setVisibility(View.GONE);
86
- this.transparentTextureView = null;
87
- }
88
- if (this.opaqueSurfaceView == null) {
89
- this.opaqueSurfaceView = new SurfaceView(this.getContext());
90
- this.opaqueSurfaceView.setLayoutParams(EngineView.childViewLayoutParams);
91
- this.opaqueSurfaceView.getHolder().addCallback(this);
92
- this.addView(this.opaqueSurfaceView);
93
- }
94
- }
95
- // xr view needs to be on top of views that might be created after it.
96
- if (this.xrSurfaceView != null) {
97
- this.xrSurfaceView.bringToFront();
98
- }
99
- }
100
-
101
- @Override
102
- public void surfaceCreated(SurfaceHolder surfaceHolder) {
103
- this.startRenderLoop();
104
- }
105
-
106
- @Override
107
- public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int width, int height) {
108
- BabylonNativeInterop.updateView(surfaceHolder.getSurface());
109
- }
110
-
111
- @Override
112
- public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
113
- this.removeCallbacks(this.renderRunnable);
114
- this.renderRunnable = null;
115
- }
116
-
117
- @Override
118
- public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int i, int i1) {
119
- this.startRenderLoop();
120
- this.acquireNewTransparentSurface(surfaceTexture);
121
- BabylonNativeInterop.updateView(this.transparentSurface);
122
- }
123
-
124
- @Override
125
- public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, int i, int i1) {
126
- this.acquireNewTransparentSurface(surfaceTexture);
127
- BabylonNativeInterop.updateView(this.transparentSurface);
128
- }
129
-
130
- @Override
131
- public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
132
- this.stopRenderLoop();
133
- this.transparentSurface.release();
134
- this.transparentSurface = null;
135
- return false;
136
- }
137
-
138
- @Override
139
- public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {
140
- this.acquireNewTransparentSurface(surfaceTexture);
141
- BabylonNativeInterop.updateView(this.transparentSurface);
142
- }
143
-
144
- private void acquireNewTransparentSurface(@NonNull SurfaceTexture surfaceTexture) {
145
- if (this.transparentSurface != null) {
146
- this.transparentSurface.release();
147
- }
148
- this.transparentSurface = new Surface(surfaceTexture);
149
- }
150
-
151
- // ------------------------------------
152
- // Common
153
-
154
- @Override
155
- public boolean onTouch(View view, MotionEvent motionEvent) {
156
- BabylonNativeInterop.reportMotionEvent(motionEvent);
157
- return true;
158
- }
159
-
160
- @TargetApi(24)
161
- public void takeSnapshot() {
162
- // Only supported on API level 24 and up, return a blank image.
163
- if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
164
- SnapshotDataReturnedEvent snapshotEvent = new SnapshotDataReturnedEvent(this.getId(), "");
165
- reactEventDispatcher.dispatchEvent(snapshotEvent);
166
- }
167
-
168
- // Create a bitmap that matches the width and height of the EngineView.
169
- final Bitmap bitmap = Bitmap.createBitmap(
170
- getWidth(),
171
- getHeight(),
172
- Bitmap.Config.ARGB_8888);
173
-
174
- // Offload the snapshot worker to a helper thread.
175
- final HandlerThread helperThread = new HandlerThread("ScreenCapture",-1);
176
- helperThread.start();
177
- final Handler helperThreadHandler = new Handler(helperThread.getLooper());
178
-
179
- Surface sourceSurface = this.transparentSurface;
180
- if (BabylonNativeInterop.isXRActive()) {
181
- sourceSurface = this.xrSurfaceView.getHolder().getSurface();
182
- } else if (this.opaqueSurfaceView != null) {
183
- sourceSurface = this.opaqueSurfaceView.getHolder().getSurface();
184
- }
185
- PixelCopy.request(sourceSurface, bitmap, getOnPixelCopyFinishedListener(bitmap, helperThread), helperThreadHandler);
186
- }
187
-
188
- // ---------------------------------------------------------------------------------------------
189
- // Returns the listener for the PixelCopy.request function call
190
- @NonNull
191
- private PixelCopy.OnPixelCopyFinishedListener getOnPixelCopyFinishedListener(Bitmap bitmap, HandlerThread helperThread) {
192
- return (copyResult) -> {
193
- // If the pixel copy was a success then convert the image to a base 64 encoded jpeg and fire the event.
194
- String encoded = "";
195
- if (copyResult == PixelCopy.SUCCESS) {
196
- ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
197
- bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayStream);
198
- byte[] byteArray = byteArrayStream.toByteArray();
199
- bitmap.recycle();
200
- encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
201
- }
202
- SnapshotDataReturnedEvent snapshotEvent = new SnapshotDataReturnedEvent(this.getId(), encoded);
203
- reactEventDispatcher.dispatchEvent(snapshotEvent);
204
- helperThread.quitSafely();
205
- };
206
- }
207
-
208
- private void startRenderLoop() {
209
- if(this.renderRunnable == null){
210
- this.renderRunnable = new Runnable() {
211
- @Override
212
- public void run() {
213
- if (BabylonNativeInterop.isXRActive()) {
214
- EngineView.this.xrSurfaceView.setVisibility(View.VISIBLE);
215
- } else {
216
- EngineView.this.xrSurfaceView.setVisibility(View.INVISIBLE);
217
- }
218
- BabylonNativeInterop.renderView();
219
- EngineView.this.postOnAnimation(this);
220
- }
221
- };
222
- this.postOnAnimation(this.renderRunnable);
223
- }
224
- }
225
-
226
- private void stopRenderLoop() {
227
- this.removeCallbacks(this.renderRunnable);
228
- this.renderRunnable = null;
229
- }
230
- }
@@ -1,71 +0,0 @@
1
- package com.babylonreactnative;
2
-
3
- import android.widget.FrameLayout;
4
-
5
- import androidx.annotation.NonNull;
6
- import androidx.annotation.Nullable;
7
-
8
- import com.facebook.react.bridge.ReadableArray;
9
- import com.facebook.react.common.MapBuilder;
10
- import com.facebook.react.uimanager.SimpleViewManager;
11
- import com.facebook.react.uimanager.ThemedReactContext;
12
- import com.facebook.react.uimanager.annotations.ReactProp;
13
-
14
- import java.util.Map;
15
-
16
- public final class EngineViewManager extends SimpleViewManager<EngineView> {
17
- public static final int COMMAND_TAKE_SNAPSHOT = 0;
18
- public static final String COMMAND_TAKE_SNAPSHOT_NAME = "takeSnapshot";
19
-
20
- @NonNull
21
- @Override
22
- public String getName() {
23
- return "EngineView";
24
- }
25
-
26
- @ReactProp(name = "isTransparent")
27
- public void setIsTransparent(EngineView view, Boolean isTransparent) {
28
- view.setIsTransparent(isTransparent);
29
- }
30
-
31
- @NonNull
32
- @Override
33
- protected EngineView createViewInstance(@NonNull ThemedReactContext reactContext) {
34
- return new EngineView(reactContext);
35
- }
36
-
37
- @Override
38
- public void onDropViewInstance(@NonNull EngineView view) {
39
- super.onDropViewInstance(view);
40
- // TODO: Native view specific cleanup
41
- }
42
-
43
- @Override
44
- public Map<String,Integer> getCommandsMap() {
45
- return MapBuilder.of(
46
- COMMAND_TAKE_SNAPSHOT_NAME,
47
- COMMAND_TAKE_SNAPSHOT
48
- );
49
- }
50
-
51
- @Override
52
- public void receiveCommand(final EngineView view, String commandId, ReadableArray args) {
53
- // This will be called whenever a command is sent from react-native.
54
- switch (commandId) {
55
- case COMMAND_TAKE_SNAPSHOT_NAME:
56
- view.takeSnapshot();
57
- break;
58
- default:
59
- throw new IllegalArgumentException(
60
- String.format("Invalid command %s specified for EngineView. Supported Commands: takeSnapshot", commandId));
61
- }
62
- }
63
-
64
- @Override
65
- public Map getExportedCustomDirectEventTypeConstants() {
66
- return MapBuilder.builder()
67
- .put(SnapshotDataReturnedEvent.EVENT_NAME,
68
- MapBuilder.of("registrationName", SnapshotDataReturnedEvent.EVENT_NAME))
69
- .build();
70
- }
71
- }
@@ -1,28 +0,0 @@
1
- package com.babylonreactnative;
2
-
3
- import com.facebook.react.bridge.Arguments;
4
- import com.facebook.react.bridge.WritableMap;
5
- import com.facebook.react.uimanager.events.Event;
6
- import com.facebook.react.uimanager.events.RCTEventEmitter;
7
-
8
- public class SnapshotDataReturnedEvent extends Event<SnapshotDataReturnedEvent> {
9
- public static final String EVENT_NAME = "onSnapshotDataReturned";
10
- public static final String DATA_NAME = "data";
11
- private final WritableMap payload;
12
-
13
- public SnapshotDataReturnedEvent(int viewId, String imageData) {
14
- super(viewId);
15
- this.payload = Arguments.createMap();
16
- this.payload.putString(DATA_NAME, imageData);
17
- }
18
-
19
- @Override
20
- public String getEventName() {
21
- return EVENT_NAME;
22
- }
23
-
24
- @Override
25
- public void dispatch(RCTEventEmitter rctEventEmitter) {
26
- rctEventEmitter.receiveEvent(getViewTag(), getEventName(), this.payload);
27
- }
28
- }
@@ -1,35 +0,0 @@
1
- #import "BabylonNativeInterop.h"
2
-
3
- #import <React/RCTBridgeModule.h>
4
- #import <ReactCommon/CallInvoker.h>
5
-
6
- #import <Foundation/Foundation.h>
7
-
8
- @interface RCTBridge (RCTTurboModule)
9
- - (std::shared_ptr<facebook::react::CallInvoker>)jsCallInvoker;
10
- @end
11
-
12
- @interface BabylonModule : NSObject <RCTBridgeModule>
13
- @end
14
-
15
- @implementation BabylonModule
16
-
17
- RCT_EXPORT_MODULE();
18
-
19
- @synthesize bridge = _bridge;
20
-
21
- RCT_EXPORT_METHOD(initialize:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
22
- self.bridge.jsCallInvoker->invokeAsync([bridge{ self.bridge }, resolve]() {
23
- [BabylonNativeInterop initialize:bridge];
24
- resolve([NSNull null]);
25
- });
26
- }
27
-
28
- RCT_EXPORT_METHOD(resetView:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
29
- dispatch_async(dispatch_get_main_queue(), ^{
30
- [BabylonNativeInterop resetView];
31
- resolve([NSNull null]);
32
- });
33
- }
34
-
35
- @end
@@ -1,14 +0,0 @@
1
- #pragma once
2
-
3
- #import <MetalKit/MetalKit.h>
4
- #import <React/RCTBridge.h>
5
-
6
- @interface BabylonNativeInterop : NSObject
7
- + (void)initialize:(RCTBridge*)bridge;
8
- + (void)updateView:(MTKView*)mtkView;
9
- + (void)renderView;
10
- + (void)resetView;
11
- + (void)updateXRView:(MTKView*)mtkView;
12
- + (bool)isXRActive;
13
- + (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches event:(UIEvent*)event;
14
- @end
@@ -1,130 +0,0 @@
1
- #import "BabylonNativeInterop.h"
2
- #import "../shared/BabylonNative.h"
3
-
4
- #import <React/RCTBridge+Private.h>
5
- #import <jsi/jsi.h>
6
- #include <ReactCommon/CallInvoker.h>
7
-
8
- #import <Foundation/Foundation.h>
9
-
10
- #import <memory>
11
-
12
- using namespace facebook;
13
-
14
- @interface RCTBridge (RCTTurboModule)
15
- - (std::shared_ptr<facebook::react::CallInvoker>)jsCallInvoker;
16
- @end
17
-
18
- namespace {
19
- jsi::Runtime* GetJSIRuntime(RCTBridge* bridge) {
20
- RCTCxxBridge* cxxBridge = reinterpret_cast<RCTCxxBridge*>(bridge);
21
- return reinterpret_cast<jsi::Runtime*>(cxxBridge.runtime);
22
- }
23
- }
24
-
25
- @implementation BabylonNativeInterop
26
-
27
- static NSMutableArray* activeTouches = [NSMutableArray new];
28
-
29
- + (void)initialize:(RCTBridge*)bridge {
30
- auto jsCallInvoker{ bridge.jsCallInvoker };
31
- auto jsDispatcher{ [jsCallInvoker{ std::move(jsCallInvoker) }](std::function<void()> func)
32
- {
33
- jsCallInvoker->invokeAsync([func{ std::move(func) }]
34
- {
35
- func();
36
- });
37
- } };
38
-
39
- BabylonNative::Initialize(*GetJSIRuntime(bridge), std::move(jsDispatcher));
40
-
41
- [[NSNotificationCenter defaultCenter] removeObserver:self
42
- name:RCTBridgeWillInvalidateModulesNotification
43
- object:bridge.parentBridge];
44
-
45
- [[NSNotificationCenter defaultCenter] addObserver:self
46
- selector:@selector(onBridgeWillInvalidate:)
47
- name:RCTBridgeWillInvalidateModulesNotification
48
- object:bridge.parentBridge];
49
- }
50
-
51
- // NOTE: This happens during dev mode reload, when the JS engine is being shutdown and restarted.
52
- + (void)onBridgeWillInvalidate:(NSNotification*)notification
53
- {
54
- BabylonNative::Deinitialize();
55
- }
56
-
57
- + (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
- }
64
- }
65
-
66
- + (void)renderView {
67
- BabylonNative::RenderView();
68
- }
69
-
70
- + (void)resetView {
71
- BabylonNative::ResetView();
72
- }
73
-
74
- + (void)updateXRView:(MTKView*)mtkView {
75
- BabylonNative::UpdateXRView(mtkView);
76
- }
77
-
78
- + (bool)isXRActive {
79
- return BabylonNative::IsXRActive();
80
- }
81
-
82
- + (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches event:(UIEvent*)event {
83
- for (UITouch* touch in touches) {
84
- if (touch.view == mtkView) {
85
- const CGFloat scale = mtkView.contentScaleFactor;
86
- const CGPoint pointerPosition = [touch locationInView:mtkView];
87
- const uint32_t x = static_cast<uint32_t>(pointerPosition.x * scale);
88
- const uint32_t y = static_cast<uint32_t>(pointerPosition.y * scale);
89
-
90
- switch (touch.phase) {
91
- case UITouchPhaseBegan: {
92
- // The activeTouches array only grows, it does not shrink (to keep indices constant since they are used as pointer ids),
93
- // so look for an unused (null) array element and reuse it if found. Otherwise, add a new entry to the array.
94
- NSUInteger pointerId = [activeTouches indexOfObject:[NSNull null]];
95
- if (pointerId != NSNotFound) {
96
- [activeTouches replaceObjectAtIndex:pointerId withObject:touch];
97
- } else {
98
- pointerId = [activeTouches count];
99
- [activeTouches addObject:touch];
100
- }
101
- BabylonNative::SetTouchButtonState(static_cast<uint32_t>(pointerId), true, x, y);
102
- break;
103
- }
104
-
105
- case UITouchPhaseMoved: {
106
- NSUInteger pointerId = [activeTouches indexOfObject:touch];
107
- if (pointerId != NSNotFound) {
108
- BabylonNative::SetTouchPosition(static_cast<uint32_t>(pointerId), x, y);
109
- }
110
- break;
111
- }
112
-
113
- case UITouchPhaseEnded:
114
- case UITouchPhaseCancelled: {
115
- NSUInteger pointerId = [activeTouches indexOfObject:touch];
116
- if (pointerId != NSNotFound) {
117
- [activeTouches replaceObjectAtIndex:pointerId withObject:[NSNull null]];
118
- BabylonNative::SetTouchButtonState(static_cast<uint32_t>(pointerId), false, x, y);
119
- }
120
- break;
121
- }
122
-
123
- default:
124
- break;
125
- }
126
- }
127
- }
128
- }
129
-
130
- @end
@@ -1,140 +0,0 @@
1
- #import "BabylonNativeInterop.h"
2
-
3
- #import <React/RCTViewManager.h>
4
- #import <React/RCTUIManager.h>
5
- #import <ReactCommon/CallInvoker.h>
6
-
7
- #import <Foundation/Foundation.h>
8
- #import <UIKit/UIKit.h>
9
- #import <MetalKit/MetalKit.h>
10
-
11
- @interface EngineView : MTKView
12
-
13
- @property (nonatomic, copy) RCTDirectEventBlock onSnapshotDataReturned;
14
- @property (nonatomic, assign) BOOL isTransparent;
15
-
16
-
17
- @end
18
-
19
- @implementation EngineView {
20
- const RCTBridge* bridge;
21
- MTKView* xrView;
22
- }
23
-
24
- - (instancetype)init:(RCTBridge*)_bridge {
25
- if (self = [super initWithFrame:CGRectZero device:MTLCreateSystemDefaultDevice()]) {
26
- bridge = _bridge;
27
- super.translatesAutoresizingMaskIntoConstraints = false;
28
- super.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
29
- super.depthStencilPixelFormat = MTLPixelFormatDepth32Float;
30
- }
31
- return self;
32
- }
33
-
34
- - (void)setIsTransparentFlag:(NSNumber*)isTransparentFlag {
35
- BOOL isTransparent = [isTransparentFlag intValue] == 1;
36
- if(isTransparent){
37
- [self setOpaque:NO];
38
- } else {
39
- [self setOpaque:YES];
40
- }
41
- self.isTransparent = isTransparent;
42
- }
43
-
44
- - (void)setBounds:(CGRect)bounds {
45
- [super setBounds:bounds];
46
- [BabylonNativeInterop updateView:self];
47
- }
48
-
49
- - (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
50
- [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
51
- }
52
-
53
- - (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
54
- [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
55
- }
56
-
57
- - (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
58
- [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
59
- }
60
-
61
- - (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
62
- [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
63
- }
64
-
65
- - (void)drawRect:(CGRect)rect {
66
- if ([BabylonNativeInterop isXRActive]) {
67
- if (!xrView) {
68
- xrView = [[MTKView alloc] initWithFrame:self.bounds device:self.device];
69
- xrView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
70
- xrView.userInteractionEnabled = false;
71
- [self addSubview:xrView];
72
- [BabylonNativeInterop updateXRView:xrView];
73
- }
74
- } else if (xrView) {
75
- [BabylonNativeInterop updateXRView:nil];
76
- [xrView removeFromSuperview];
77
- xrView = nil;
78
- }
79
-
80
- [BabylonNativeInterop renderView];
81
- }
82
-
83
- -(void)dealloc {
84
- [BabylonNativeInterop updateXRView:nil];
85
- }
86
-
87
- - (void)takeSnapshot {
88
- // We must take the screenshot on the main thread otherwise we might fail to get a valid handle on the view's image.
89
- dispatch_async(dispatch_get_main_queue(), ^{
90
- // Start the graphics context.
91
- UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES /* opaque */, 0.0f);
92
-
93
- // Draw the current state of the view into the graphics context.
94
- [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
95
-
96
- // Grab the image from the graphics context, and convert into a base64 encoded JPG.
97
- UIImage* capturedImage = UIGraphicsGetImageFromCurrentImageContext();
98
- UIGraphicsEndImageContext();
99
- NSData* jpgData = UIImageJPEGRepresentation(capturedImage, .8f);
100
- NSString* encodedData = [jpgData base64EncodedStringWithOptions:0];
101
-
102
- // Fire the onSnapshotDataReturned event if hooked up.
103
- if (self.onSnapshotDataReturned != nil) {
104
- self.onSnapshotDataReturned(@{ @"data":encodedData});
105
- }
106
- });
107
- }
108
-
109
- @end
110
-
111
-
112
- @interface EngineViewManager : RCTViewManager
113
- @end
114
-
115
- @implementation EngineViewManager
116
-
117
- RCT_CUSTOM_VIEW_PROPERTY(isTransparent, NSNumber*, EngineView){
118
- [view setIsTransparentFlag:json];
119
- }
120
-
121
- RCT_EXPORT_MODULE(EngineViewManager)
122
-
123
- RCT_EXPORT_VIEW_PROPERTY(onSnapshotDataReturned, RCTDirectEventBlock)
124
-
125
- RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber*) reactTag) {
126
- // Marshal the takeSnapshot call to the appropriate EngineView.
127
- [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber*,UIView*>* viewRegistry) {
128
- EngineView* view = (EngineView*)viewRegistry[reactTag];
129
- if (!view || ![view isKindOfClass:[EngineView class]]) {
130
- return;
131
- }
132
- [view takeSnapshot];
133
- }];
134
- }
135
-
136
- - (UIView*)view {
137
- return [[EngineView alloc] init:self.bridge];
138
- }
139
-
140
- @end