Skip to content

Commit ddf1b0b

Browse files
C. M. Barthryantrem
andauthored
Fix native input usage for uwp (#145)
* fix native input usage for uwp * branch ready for native input * get app working with mouse input events * move to different submodule commits * avoid exposing device type * update content to reflect BabylonNative pr * work with babylon native changes * move to correct commit * update package references and validate uwp build * Update Modules/@babylonjs/react-native/ios/BabylonNativeInterop.mm Co-authored-by: Ryan Tremblay <[email protected]> * Update Modules/@babylonjs/react-native/ios/BabylonNativeInterop.mm Co-authored-by: Ryan Tremblay <[email protected]> * address review comments * try fix pr ci * move to master backslash fix Co-authored-by: Ryan Tremblay <[email protected]>
1 parent f8426da commit ddf1b0b

File tree

17 files changed

+235
-79
lines changed

17 files changed

+235
-79
lines changed

Apps/PackageTest/0.63.1/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Apps/PackageTest/0.63.1/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
1111
},
1212
"dependencies": {
13-
"@babylonjs/core": "^5.0.0-alpha.6",
13+
"@babylonjs/core": "^5.0.0-alpha.9",
1414
"@babylonjs/react-native": "file:../../../Package/Assembled/babylonjs-react-native-0.0.1.tgz",
1515
"react": "16.13.1",
1616
"react-native": "0.63.1",

Apps/PackageTest/0.64.0-rc.0/package-lock.json

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Apps/PackageTest/0.64.0-rc.0/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
1313
},
1414
"dependencies": {
15-
"@babylonjs/core": "^5.0.0-alpha.6",
15+
"@babylonjs/core": "^5.0.0-alpha.9",
1616
"@babylonjs/react-native": "file:../../../Package/Assembled/babylonjs-react-native-0.0.1.tgz",
1717
"react": "^17.0.1",
1818
"react-native": "^0.64.0-rc.0",

Apps/Playground/App.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import React, { useState, FunctionComponent, useEffect, useCallback } from 'reac
99
import { SafeAreaView, StatusBar, Button, View, Text, ViewProps, Image } from 'react-native';
1010

1111
import { EngineView, useEngine, EngineViewCallbacks } from '@babylonjs/react-native';
12-
import { Scene, Vector3, ArcRotateCamera, Camera, WebXRSessionManager, SceneLoader, TransformNode, DeviceSourceManager, DeviceType, DeviceSource, PointerInput, WebXRTrackingState } from '@babylonjs/core';
12+
import { Scene, Vector3, ArcRotateCamera, Camera, WebXRSessionManager, SceneLoader, TransformNode, DeviceSourceManager, DeviceType, DeviceSource, PointerInput, WebXRTrackingState, Nullable } from '@babylonjs/core';
1313
import '@babylonjs/loaders';
1414
import Slider from '@react-native-community/slider';
1515

@@ -40,18 +40,28 @@ const EngineScreen: FunctionComponent<ViewProps> = (props: ViewProps) => {
4040
setRootNode(rootNode);
4141

4242
const deviceSourceManager = new DeviceSourceManager(engine);
43+
const handlePointerInput = (inputIndex: PointerInput, previousState: Nullable<number>, currentState: Nullable<number>) => {
44+
if (inputIndex === PointerInput.Horizontal &&
45+
currentState && previousState) {
46+
rootNode.rotate(Vector3.Down(), (currentState - previousState) * 0.005);
47+
};
48+
};
49+
4350
deviceSourceManager.onDeviceConnectedObservable.add(device => {
4451
if (device.deviceType === DeviceType.Touch) {
4552
const touch: DeviceSource<DeviceType.Touch> = deviceSourceManager.getDeviceSource(device.deviceType, device.deviceSlot)!;
4653
touch.onInputChangedObservable.add(touchEvent => {
47-
if (touchEvent.inputIndex === PointerInput.Horizontal) {
48-
if (touchEvent.currentState && touchEvent.previousState) {
49-
rootNode.rotate(Vector3.Down(), (touchEvent.currentState - touchEvent.previousState) * 0.005);
50-
}
54+
handlePointerInput(touchEvent.inputIndex, touchEvent.previousState, touchEvent.currentState);
55+
});
56+
} else if (device.deviceType === DeviceType.Mouse) {
57+
const mouse: DeviceSource<DeviceType.Mouse> = deviceSourceManager.getDeviceSource(device.deviceType, device.deviceSlot)!;
58+
mouse.onInputChangedObservable.add(mouseEvent => {
59+
if (mouse.getInput(PointerInput.LeftClick)) {
60+
handlePointerInput(mouseEvent.inputIndex, mouseEvent.previousState, mouseEvent.currentState);
5161
}
52-
})
62+
});
5363
}
54-
})
64+
});
5565

5666
const transformContainer = new TransformNode("Transform Container", scene);
5767
transformContainer.parent = rootNode;

Apps/Playground/package-lock.json

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Apps/Playground/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
1313
},
1414
"dependencies": {
15-
"@babylonjs/core": "^5.0.0-alpha.6",
16-
"@babylonjs/loaders": "^5.0.0-alpha.6",
15+
"@babylonjs/core": "^5.0.0-alpha.9",
16+
"@babylonjs/loaders": "^5.0.0-alpha.9",
1717
"@babylonjs/react-native": "file:../../Modules/@babylonjs/react-native",
1818
"@react-native-community/slider": "4.0.0-rc.2",
1919
"logkitty": "^0.7.1",

Modules/@babylonjs/react-native/android/src/main/cpp/BabylonNativeInterop.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ extern "C" JNIEXPORT void JNICALL Java_com_babylonreactnative_BabylonNativeInter
7777
Babylon::UpdateView(windowPtr, width, height);
7878
}
7979

80-
extern "C" JNIEXPORT void JNICALL Java_com_babylonreactnative_BabylonNativeInterop_00024BabylonNative_setPointerButtonState(JNIEnv* env, jclass obj, jint pointerId, jint buttonId, jboolean isDown, jint x, jint y)
80+
extern "C" JNIEXPORT void JNICALL Java_com_babylonreactnative_BabylonNativeInterop_00024BabylonNative_setTouchButtonState(JNIEnv* env, jclass obj, jint pointerId, jboolean isDown, jint x, jint y)
8181
{
82-
Babylon::SetPointerButtonState(static_cast<uint32_t>(pointerId), static_cast<uint32_t>(buttonId), isDown, static_cast<uint32_t>(x), static_cast<uint32_t>(y));
82+
Babylon::SetTouchButtonState(static_cast<uint32_t>(pointerId), isDown, static_cast<uint32_t>(x), static_cast<uint32_t>(y));
8383
}
8484

85-
extern "C" JNIEXPORT void JNICALL Java_com_babylonreactnative_BabylonNativeInterop_00024BabylonNative_setPointerPosition(JNIEnv* env, jclass obj, jint pointerId, jint x, jint y)
85+
extern "C" JNIEXPORT void JNICALL Java_com_babylonreactnative_BabylonNativeInterop_00024BabylonNative_setTouchPosition(JNIEnv* env, jclass obj, jint pointerId, jint x, jint y)
8686
{
87-
Babylon::SetPointerPosition(static_cast<uint32_t>(pointerId), static_cast<uint32_t>(x), static_cast<uint32_t>(y));
87+
Babylon::SetTouchPosition(static_cast<uint32_t>(pointerId), static_cast<uint32_t>(x), static_cast<uint32_t>(y));
8888
}

Modules/@babylonjs/react-native/android/src/main/java/com/babylonreactnative/BabylonNativeInterop.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ private static class BabylonNative {
2424
public static native void pause();
2525
public static native void resume();
2626
public static native void updateView(Surface surface);
27-
public static native void setPointerButtonState(int pointerId, int buttonId, boolean isDown, int x, int y);
28-
public static native void setPointerPosition(int pointerId, int x, int y);
27+
public static native void setTouchButtonState(int pointerId, boolean isDown, int x, int y);
28+
public static native void setTouchPosition(int pointerId, int x, int y);
2929
}
3030

3131
private static LifecycleEventListener lifeCycleEventListener;
@@ -98,13 +98,13 @@ public static void reportMotionEvent(MotionEvent motionEvent) {
9898
int pointerId = motionEvent.getPointerId(pointerIndex);
9999
int x = (int)motionEvent.getX(pointerIndex);
100100
int y = (int)motionEvent.getY(pointerIndex);
101-
BabylonNative.setPointerButtonState(pointerId, 0, isPointerDown, x, y);
101+
BabylonNative.setTouchButtonState(pointerId, isPointerDown, x, y);
102102
} else if (isPointerMove) {
103103
for (int pointerIndex = 0; pointerIndex < motionEvent.getPointerCount(); pointerIndex++) {
104104
int pointerId = motionEvent.getPointerId(pointerIndex);
105105
int x = (int)motionEvent.getX(pointerIndex);
106106
int y = (int)motionEvent.getY(pointerIndex);
107-
BabylonNative.setPointerPosition(pointerId, x, y);
107+
BabylonNative.setTouchPosition(pointerId, x, y);
108108
}
109109
}
110110
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ + (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches eve
7979
} else {
8080
[activeTouches replaceObjectAtIndex:pointerId withObject:touch];
8181
}
82-
Babylon::SetPointerButtonState(static_cast<uint32_t>(pointerId), 0, true, x, y);
82+
Babylon::SetTouchButtonState(static_cast<uint32_t>(pointerId), true, x, y);
8383
break;
8484
}
8585

8686
case UITouchPhaseMoved: {
8787
NSUInteger pointerId = [activeTouches indexOfObject:touch];
88-
Babylon::SetPointerPosition(static_cast<uint32_t>(pointerId), x, y);
88+
Babylon::SetTouchPosition(static_cast<uint32_t>(pointerId), x, y);
8989
break;
9090
}
9191

9292
case UITouchPhaseEnded:
9393
case UITouchPhaseCancelled: {
9494
NSUInteger pointerId = [activeTouches indexOfObject:touch];
9595
[activeTouches replaceObjectAtIndex:pointerId withObject:[NSNull null]];
96-
Babylon::SetPointerButtonState(static_cast<uint32_t>(pointerId), 0, false, x, y);
96+
Babylon::SetTouchButtonState(static_cast<uint32_t>(pointerId), false, x, y);
9797
break;
9898
}
9999

0 commit comments

Comments
 (0)