Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PanelRaycaster.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine.EventSystems;
  5. namespace UnityEngine.UIElements
  6. {
  7. #if PACKAGE_UITOOLKIT
  8. /// <summary>
  9. /// A derived BaseRaycaster to raycast against UI Toolkit panel instances at runtime.
  10. /// </summary>
  11. [AddComponentMenu("UI Toolkit/Panel Raycaster (UI Toolkit)")]
  12. public class PanelRaycaster : BaseRaycaster, IRuntimePanelComponent
  13. {
  14. private BaseRuntimePanel m_Panel;
  15. /// <summary>
  16. /// The panel that this component relates to. If panel is null, this component will have no effect.
  17. /// Will be set to null automatically if panel is Disposed from an external source.
  18. /// </summary>
  19. public IPanel panel
  20. {
  21. get => m_Panel;
  22. set
  23. {
  24. var newPanel = (BaseRuntimePanel)value;
  25. if (m_Panel != newPanel)
  26. {
  27. UnregisterCallbacks();
  28. m_Panel = newPanel;
  29. RegisterCallbacks();
  30. }
  31. }
  32. }
  33. void RegisterCallbacks()
  34. {
  35. if (m_Panel != null)
  36. {
  37. m_Panel.destroyed += OnPanelDestroyed;
  38. }
  39. }
  40. void UnregisterCallbacks()
  41. {
  42. if (m_Panel != null)
  43. {
  44. m_Panel.destroyed -= OnPanelDestroyed;
  45. }
  46. }
  47. void OnPanelDestroyed()
  48. {
  49. panel = null;
  50. }
  51. private GameObject selectableGameObject => m_Panel?.selectableGameObject;
  52. public override int sortOrderPriority => (int)(m_Panel?.sortingPriority ?? 0f);
  53. public override int renderOrderPriority => ConvertFloatBitsToInt(m_Panel?.sortingPriority ?? 0f);
  54. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  55. {
  56. if (m_Panel == null)
  57. return;
  58. var eventPosition = Display.RelativeMouseAt(eventData.position);
  59. var displayIndex = m_Panel.targetDisplay;
  60. var originalEventPosition = eventPosition;
  61. if (eventPosition != Vector3.zero)
  62. {
  63. // We support multiple display and display identification based on event position.
  64. int eventDisplayIndex = (int)eventPosition.z;
  65. // Discard events that are not part of this display so the user does not interact with multiple displays at once.
  66. if (eventDisplayIndex != displayIndex)
  67. return;
  68. }
  69. else
  70. {
  71. // The multiple display system is not supported on all platforms, when it is not supported the returned position
  72. // will be all zeros so when the returned index is 0 we will default to the event data to be safe.
  73. eventPosition = eventData.position;
  74. #if UNITY_EDITOR
  75. if (Display.activeEditorGameViewTarget != displayIndex)
  76. return;
  77. eventPosition.z = Display.activeEditorGameViewTarget;
  78. #endif
  79. // We don't really know in which display the event occurred. We will process the event assuming it occurred in our display.
  80. }
  81. var position = eventPosition;
  82. var delta = eventData.delta;
  83. float h = Screen.height;
  84. if (displayIndex > 0 && displayIndex < Display.displays.Length)
  85. {
  86. h = Display.displays[displayIndex].systemHeight;
  87. }
  88. position.y = h - position.y;
  89. delta.y = -delta.y;
  90. var eventSystem = UIElementsRuntimeUtility.activeEventSystem as EventSystem;
  91. var pointerId = eventSystem.currentInputModule.ConvertUIToolkitPointerId(eventData);
  92. var capturingElement = m_Panel.GetCapturingElement(pointerId);
  93. if (capturingElement is VisualElement ve && ve.panel != m_Panel)
  94. return;
  95. var capturingPanel = PointerDeviceState.GetPressedButtons(pointerId) != 0 ?
  96. PointerDeviceState.GetPlayerPanelWithSoftPointerCapture(pointerId) :
  97. null;
  98. if (capturingPanel != null && capturingPanel != m_Panel)
  99. return;
  100. if (capturingElement == null && capturingPanel == null)
  101. {
  102. if (!m_Panel.ScreenToPanel(position, delta, out var panelPosition, out _))
  103. return;
  104. var pick = m_Panel.Pick(panelPosition);
  105. if (pick == null)
  106. return;
  107. }
  108. resultAppendList.Add(new RaycastResult
  109. {
  110. gameObject = selectableGameObject,
  111. module = this,
  112. screenPosition = eventPosition,
  113. displayIndex = m_Panel.targetDisplay,
  114. });
  115. }
  116. public override Camera eventCamera => null;
  117. [StructLayout(LayoutKind.Explicit, Size = sizeof(int))]
  118. private struct FloatIntBits
  119. {
  120. [FieldOffset(0)]
  121. public float f;
  122. [FieldOffset(0)]
  123. public int i;
  124. }
  125. private static int ConvertFloatBitsToInt(float f)
  126. {
  127. FloatIntBits bits = new FloatIntBits {f = f};
  128. return bits.i;
  129. }
  130. }
  131. #endif
  132. }