CanvasPointerInputDetector.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Reflection;
  18. using UnityEngine;
  19. using UnityEngine.EventSystems;
  20. using UnityEngine.UI;
  21. #if VUPLEX_MRTK
  22. using Microsoft.MixedReality.Toolkit.Input;
  23. #endif
  24. namespace Vuplex.WebView {
  25. [HelpURL("https://developer.vuplex.com/webview/IPointerInputDetector")]
  26. public class CanvasPointerInputDetector : DefaultPointerInputDetector {
  27. Canvas _cachedCanvas;
  28. RectTransform _cachedRectTransform;
  29. protected override Vector2 _convertToNormalizedPoint(PointerEventData pointerEventData) {
  30. var canvas = _getCanvas();
  31. var camera = canvas == null || canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
  32. Vector2 localPoint;
  33. RectTransformUtility.ScreenPointToLocalPointInRectangle(_getRectTransform(), pointerEventData.position, camera, out localPoint);
  34. return _convertToNormalizedPoint(localPoint);
  35. }
  36. protected override Vector2 _convertToNormalizedPoint(Vector3 worldPosition) {
  37. var localPoint = _getRectTransform().InverseTransformPoint(worldPosition);
  38. return _convertToNormalizedPoint(localPoint);
  39. }
  40. Vector2 _convertToNormalizedPoint(Vector2 localPoint) {
  41. var normalizedPoint = Rect.PointToNormalized(_getRectTransform().rect, localPoint);
  42. normalizedPoint.y = 1 - normalizedPoint.y;
  43. return normalizedPoint;
  44. }
  45. Canvas _getCanvas() {
  46. // Note: If the instance is moved from one Canvas to another,
  47. // the old Canvas will still be cached.
  48. if (_cachedCanvas == null) {
  49. _cachedCanvas = GetComponentInParent<Canvas>();
  50. }
  51. return _cachedCanvas;
  52. }
  53. RectTransform _getRectTransform() {
  54. if (_cachedRectTransform == null) {
  55. _cachedRectTransform = GetComponent<RectTransform>();
  56. }
  57. return _cachedRectTransform;
  58. }
  59. protected override bool _positionIsZero(PointerEventData eventData) {
  60. return eventData.position == Vector2.zero;
  61. }
  62. // Code specific to Microsoft's Mixed Reality Toolkit.
  63. #if VUPLEX_MRTK
  64. void Start() {
  65. // Add a NearInteractionTouchable script to allow touch interactions
  66. // to trigger the IMixedRealityPointerHandler methods.
  67. var touchable = gameObject.AddComponent<NearInteractionTouchableUnityUI>();
  68. touchable.EventsToReceive = TouchableEventType.Pointer;
  69. }
  70. #endif
  71. }
  72. }