Skip to content

Commit c8e255f

Browse files
Merge pull request #204 from michaelfarnsworth/add-pointer-interactions
Add PointerDown and PointerUp Handling
2 parents a9eeffb + 66f6f4f commit c8e255f

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

GettingStartedGuides/HoloLens2_GettingStarted/HoloLens2GetStartedApp/Assets/Scenes/SampleScene.unity

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ RectTransform:
248248
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
249249
m_LocalPosition: {x: 0, y: 0, z: 0}
250250
m_LocalScale: {x: 1, y: 1, z: 1}
251+
m_ConstrainProportionsScale: 0
251252
m_Children: []
252253
m_Father: {fileID: 143832582}
253254
m_RootOrder: 1
@@ -284,6 +285,7 @@ RectTransform:
284285
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
285286
m_LocalPosition: {x: 0, y: 0, z: 0}
286287
m_LocalScale: {x: 1, y: 1, z: 1}
288+
m_ConstrainProportionsScale: 0
287289
m_Children:
288290
- {fileID: 1895741750}
289291
- {fileID: 97853871}
@@ -337,6 +339,7 @@ RectTransform:
337339
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
338340
m_LocalPosition: {x: 0, y: 0, z: 0}
339341
m_LocalScale: {x: 1, y: 1, z: 1}
342+
m_ConstrainProportionsScale: 0
340343
m_Children: []
341344
m_Father: {fileID: 2092252165}
342345
m_RootOrder: 0
@@ -471,6 +474,7 @@ Transform:
471474
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
472475
m_LocalPosition: {x: 0, y: 0, z: 0}
473476
m_LocalScale: {x: 1, y: 1, z: 1}
477+
m_ConstrainProportionsScale: 0
474478
m_Children:
475479
- {fileID: 963194228}
476480
m_Father: {fileID: 0}
@@ -565,6 +569,7 @@ Transform:
565569
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
566570
m_LocalPosition: {x: 0, y: 3, z: 0}
567571
m_LocalScale: {x: 1, y: 1, z: 1}
572+
m_ConstrainProportionsScale: 0
568573
m_Children: []
569574
m_Father: {fileID: 0}
570575
m_RootOrder: 0
@@ -609,6 +614,7 @@ Transform:
609614
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
610615
m_LocalPosition: {x: 0, y: 0, z: 0}
611616
m_LocalScale: {x: 1, y: 1, z: 1}
617+
m_ConstrainProportionsScale: 0
612618
m_Children: []
613619
m_Father: {fileID: 0}
614620
m_RootOrder: 1
@@ -695,6 +701,7 @@ Transform:
695701
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
696702
m_LocalPosition: {x: 0, y: 0, z: 0}
697703
m_LocalScale: {x: 1, y: 1, z: 1}
704+
m_ConstrainProportionsScale: 0
698705
m_Children: []
699706
m_Father: {fileID: 340263451}
700707
m_RootOrder: 0
@@ -734,6 +741,7 @@ MonoBehaviour:
734741
m_Script: {fileID: 11500000, guid: 7a21b486d0bb44444b1418aaa38b44de, type: 3}
735742
m_Name:
736743
m_EditorClassIdentifier:
744+
m_SendPointerHoverToParent: 1
737745
m_HorizontalAxis: Horizontal
738746
m_VerticalAxis: Vertical
739747
m_SubmitButton: Submit
@@ -790,6 +798,7 @@ RectTransform:
790798
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
791799
m_LocalPosition: {x: 0, y: 0, z: 1.5}
792800
m_LocalScale: {x: 0.001, y: 0.001, z: 0.001}
801+
m_ConstrainProportionsScale: 0
793802
m_Children:
794803
- {fileID: 1187233980}
795804
- {fileID: 2092252165}
@@ -1180,6 +1189,7 @@ RectTransform:
11801189
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
11811190
m_LocalPosition: {x: 0, y: 0, z: -5}
11821191
m_LocalScale: {x: 1, y: 1, z: 1}
1192+
m_ConstrainProportionsScale: 0
11831193
m_Children:
11841194
- {fileID: 143832582}
11851195
m_Father: {fileID: 1054508623}
@@ -1219,6 +1229,7 @@ RectTransform:
12191229
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
12201230
m_LocalPosition: {x: 0, y: 0, z: -5}
12211231
m_LocalScale: {x: 1, y: 1, z: 1}
1232+
m_ConstrainProportionsScale: 0
12221233
m_Children:
12231234
- {fileID: 1959170144}
12241235
m_Father: {fileID: 1054508623}
@@ -1352,6 +1363,7 @@ Transform:
13521363
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
13531364
m_LocalPosition: {x: 0, y: 0, z: 0}
13541365
m_LocalScale: {x: 1, y: 1, z: 1}
1366+
m_ConstrainProportionsScale: 0
13551367
m_Children:
13561368
- {fileID: 1054508623}
13571369
m_Father: {fileID: 0}
@@ -1503,6 +1515,7 @@ RectTransform:
15031515
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
15041516
m_LocalPosition: {x: 0, y: 0, z: 0}
15051517
m_LocalScale: {x: 1, y: 1, z: 1}
1518+
m_ConstrainProportionsScale: 0
15061519
m_Children: []
15071520
m_Father: {fileID: 143832582}
15081521
m_RootOrder: 0
@@ -1540,6 +1553,7 @@ RectTransform:
15401553
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
15411554
m_LocalPosition: {x: 0, y: 0, z: 0}
15421555
m_LocalScale: {x: 1, y: 1, z: 1}
1556+
m_ConstrainProportionsScale: 0
15431557
m_Children: []
15441558
m_Father: {fileID: 1306366453}
15451559
m_RootOrder: 0
@@ -1673,6 +1687,21 @@ MonoBehaviour:
16731687
BackButton: {fileID: 2092252166}
16741688
GoButton: {fileID: 1306366454}
16751689
URLField: {fileID: 1187233977}
1690+
Collider: {fileID: 2082689498}
1691+
--- !u!64 &2082689498
1692+
MeshCollider:
1693+
m_ObjectHideFlags: 0
1694+
m_CorrespondingSourceObject: {fileID: 0}
1695+
m_PrefabInstance: {fileID: 0}
1696+
m_PrefabAsset: {fileID: 0}
1697+
m_GameObject: {fileID: 2082689487}
1698+
m_Material: {fileID: 0}
1699+
m_IsTrigger: 0
1700+
m_Enabled: 1
1701+
serializedVersion: 4
1702+
m_Convex: 0
1703+
m_CookingOptions: 30
1704+
m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0}
16761705
--- !u!1 &2092252164
16771706
GameObject:
16781707
m_ObjectHideFlags: 0
@@ -1702,6 +1731,7 @@ RectTransform:
17021731
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
17031732
m_LocalPosition: {x: 0, y: 0, z: -5}
17041733
m_LocalScale: {x: 1, y: 1, z: 1}
1734+
m_ConstrainProportionsScale: 0
17051735
m_Children:
17061736
- {fileID: 165517765}
17071737
m_Father: {fileID: 1054508623}

GettingStartedGuides/HoloLens2_GettingStarted/HoloLens2GetStartedApp/Assets/WebViewBrowser.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
using TMPro;
55
using System;
66

7-
public class WebViewBrowser : MonoBehaviour
7+
using Microsoft.MixedReality.Toolkit.Input;
8+
9+
public class WebViewBrowser : MonoBehaviour, IMixedRealityPointerHandler
810
{
911
// Declare UI elements: back button, go button, and URL input field
1012
public Button BackButton;
1113
public Button GoButton;
1214
public TMP_InputField URLField;
15+
public MeshCollider Collider;
16+
private IWebView _webView;
1317

1418
private void Start()
1519
{
1620
// Get the WebView component attached to the game object
1721
var webViewComponent = gameObject.GetComponent<WebView>();
1822
webViewComponent.GetWebViewWhenReady((IWebView webView) =>
1923
{
24+
_webView = webView;
25+
2026
// If the WebView supports browser history, enable the back button
2127
if (webView is IWithBrowserHistory history)
2228
{
@@ -52,4 +58,55 @@ private void CanGoBack(bool value)
5258
{
5359
BackButton.enabled = value;
5460
}
61+
62+
public void OnPointerDown(MixedRealityPointerEventData eventData)
63+
{
64+
TranslateToWebViewMouseEvent(eventData, WebViewMouseEventData.EventType.MouseDown);
65+
}
66+
67+
public void OnPointerUp(MixedRealityPointerEventData eventData)
68+
{
69+
TranslateToWebViewMouseEvent(eventData, WebViewMouseEventData.EventType.MouseUp);
70+
}
71+
72+
private void TranslateToWebViewMouseEvent(MixedRealityPointerEventData eventData, WebViewMouseEventData.EventType evenType)
73+
{
74+
var hitCoord = NormalizeWorldPoint(eventData.Pointer.Result.Details.Point);
75+
76+
hitCoord.x *= _webView.Width;
77+
hitCoord.y *= _webView.Height;
78+
79+
var mouseEventsWebView = _webView as IWithMouseEvents;
80+
WebViewMouseEventData mouseEvent = new WebViewMouseEventData
81+
{
82+
X = (int)hitCoord.x,
83+
Y = (int)hitCoord.y,
84+
Device = WebViewMouseEventData.DeviceType.Pointer,
85+
Type = evenType,
86+
Button = WebViewMouseEventData.MouseButton.ButtonLeft,
87+
TertiaryAxisDeviceType = WebViewMouseEventData.TertiaryAxisDevice.PointingDevice
88+
};
89+
90+
mouseEventsWebView.MouseEvent(mouseEvent);
91+
}
92+
93+
private Vector2 NormalizeWorldPoint(Vector3 worldPoint)
94+
{
95+
// Convert the world point to our control's local space.
96+
Vector3 localPoint = transform.InverseTransformPoint(worldPoint);
97+
98+
var boundsSize = Collider.sharedMesh.bounds.size;
99+
var boundsExtents = Collider.sharedMesh.bounds.max;
100+
101+
// Adjust the point to be based on a 0,0 origin.
102+
var uvTouchPoint = new Vector2((localPoint.x + boundsExtents.x), -1.0f * (localPoint.y - boundsExtents.y));
103+
104+
// Normalize the point so it can be mapped to the WebView's texture.
105+
var normalizedPoint = new Vector2(uvTouchPoint.x / boundsSize.x, uvTouchPoint.y / boundsSize.y);
106+
107+
return normalizedPoint;
108+
}
109+
110+
public void OnPointerClicked(MixedRealityPointerEventData eventData) { }
111+
public void OnPointerDragged(MixedRealityPointerEventData eventData) { }
55112
}

0 commit comments

Comments
 (0)