/// <summary> /// This method is called by Unity whenever a touch event is processed. Override this method with a custom implementation to process touch events yourself. /// </summary> /// <param name="pointerEvent">Event data relating to the touch event, such as position and ID to be passed to the touch event destination object.</param> /// <param name="pressed">This is true for the first frame of a touch event, and false thereafter. This can therefore be used to determine the instant a touch event occurred.</param> /// <param name="released">This is true only for the last frame of a touch event.</param> /// <remarks> /// This method can be overridden in derived classes to change how touch press events are handled. /// </remarks> protected void ProcessTouchPress(RayEventData pointerEvent, bool pressed, bool released) { var currentOverGo = pointerEvent.pointerCurrentRaycast.gameObject; // PointerDown notification if (pressed) { BeginPointerDown(pointerEvent, currentOverGo); if (pointerEvent.pointerEnter != currentOverGo) { // send a pointer enter to the touched element if it isn't the one to select... HandlePointerExitAndEnter(pointerEvent, currentOverGo); pointerEvent.pointerEnter = currentOverGo; } EndPointerDown(pointerEvent, currentOverGo); } // PointerUp notification if (released) { OnPointerUp(pointerEvent, currentOverGo); // send exit events as we need to simulate this on touch up on touch device ExecuteEvents.ExecuteHierarchy(pointerEvent.pointerEnter, pointerEvent, ExecuteEvents.pointerExitHandler); pointerEvent.pointerEnter = null; m_InputPointerEvent = pointerEvent; } }
protected void OnPointerUp(RayEventData rayEvent, GameObject currentOverGo) { if (pointerUp != null && currentOverGo != null) { pointerUp(currentOverGo, rayEvent); } var pressedObject = rayEvent.pointerPress; ExecuteEvents.Execute(pressedObject, rayEvent, ExecuteRayEvents.pointerUpHandler); ExecuteEvents.Execute(pressedObject, rayEvent, ExecuteEvents.pointerUpHandler); // see if we mouse up on the same element that we clicked on... var pointerClickHandler = ExecuteEvents.GetEventHandler <IPointerClickHandler>(currentOverGo); // PointerClick and Drop events var draggedObject = rayEvent.pointerDrag; if (pressedObject == pointerClickHandler && rayEvent.eligibleForClick) { ExecuteEvents.Execute(pressedObject, rayEvent, ExecuteEvents.pointerClickHandler); } else if (draggedObject != null && rayEvent.dragging) { ExecuteEvents.ExecuteHierarchy(currentOverGo, rayEvent, ExecuteEvents.dropHandler); } // see if we mouse up on the same element that we clicked on... var rayClickHandler = ExecuteEvents.GetEventHandler <IRayClickHandler>(currentOverGo); // PointerClick and Drop events if (pressedObject == rayClickHandler && rayEvent.eligibleForClick) { if (clicked != null && pressedObject != null) { clicked(pressedObject, rayEvent); } ExecuteEvents.Execute(pressedObject, rayEvent, ExecuteRayEvents.pointerClickHandler); } rayEvent.eligibleForClick = false; rayEvent.pointerPress = null; rayEvent.rawPointerPress = null; if (draggedObject != null && rayEvent.dragging) { if (dragEnded != null) { dragEnded(draggedObject, rayEvent); } ExecuteEvents.Execute(draggedObject, rayEvent, ExecuteRayEvents.endDragHandler); ExecuteEvents.Execute(draggedObject, rayEvent, ExecuteEvents.endDragHandler); } rayEvent.dragging = false; rayEvent.pointerDrag = null; }
/// <summary> /// Copy one RayEventData to another. /// </summary> protected void CopyFromTo(RayEventData @from, RayEventData @to) { @to.position = @from.position; @to.delta = @from.delta; @to.scrollDelta = @from.scrollDelta; @to.pointerCurrentRaycast = @from.pointerCurrentRaycast; @to.pointerEnter = @from.pointerEnter; }
public void OnDragStarted(GameObject gameObject, RayEventData eventData) { var droppable = ComponentUtils <IDroppable> .GetComponent(gameObject); if (droppable != null) { m_Droppables[eventData.rayOrigin] = droppable; } }
void BeginPointerDown(RayEventData rayEvent, GameObject currentOverGo) { rayEvent.eligibleForClick = true; rayEvent.delta = Vector2.zero; rayEvent.dragging = false; rayEvent.useDragThreshold = true; rayEvent.pointerPressRaycast = rayEvent.pointerCurrentRaycast; rayEvent.pressPosition = rayEvent.position; DeselectIfSelectionChanged(currentOverGo, rayEvent); }
void OnRayExited(GameObject gameObject, RayEventData eventData) { if (gameObject && gameObject != m_ModuleParent) { k_TooltipList.Clear(); gameObject.GetComponents(k_TooltipList); foreach (var tooltip in k_TooltipList) { HideTooltip(tooltip); } } }
void OnRayHovering(GameObject gameObject, RayEventData eventData) { if (gameObject == m_ModuleParent) { return; } k_TooltipList.Clear(); gameObject.GetComponents(k_TooltipList); foreach (var tooltip in k_TooltipList) { ShowTooltip(tooltip); } }
public void OnRayEntered(GameObject gameObject, RayEventData eventData) { var dropReceiver = ComponentUtils <IDropReceiver> .GetComponent(gameObject); if (dropReceiver != null) { var rayOrigin = eventData.rayOrigin; if (dropReceiver.CanDrop(GetCurrentDropObject(rayOrigin))) { dropReceiver.OnDropHoverStarted(); m_HoverObjects[rayOrigin] = gameObject; SetCurrentDropReceiver(rayOrigin, dropReceiver); } } }
void ReleaseMouse(RayEventData pointerEvent, GameObject currentOverGo) { OnPointerUp(pointerEvent, currentOverGo); // redo pointer enter / exit to refresh state // so that if we moused over something that ignored it before // due to having pressed on something else // it now gets it. if (currentOverGo != pointerEvent.pointerEnter) { HandlePointerExitAndEnter(pointerEvent, null); HandlePointerExitAndEnter(pointerEvent, currentOverGo); } m_InputPointerEvent = pointerEvent; }
/// <summary> /// Search the cache for currently active pointers, return true if found. /// </summary> /// <param name="id">Touch ID</param> /// <param name="data">Found data</param> /// <param name="create">If not found should it be created</param> /// <returns>True if pointer is found.</returns> protected bool GetRayData(int id, out RayEventData data, bool create) { if (!m_RayData.TryGetValue(id, out data) && create) { var mainCamera = CameraUtils.GetMainCamera(); data = new RayEventData(eventSystem) { pointerId = id, camera = mainCamera, rayOrigin = mainCamera.transform }; m_RayData.Add(id, data); return(true); } return(false); }
public override void UpdateModule() { if (!eventSystem.isFocused && ShouldIgnoreEventsOnNoFocus()) { if (m_InputPointerEvent != null && m_InputPointerEvent.pointerDrag != null && m_InputPointerEvent.dragging) { ReleaseMouse(m_InputPointerEvent, m_InputPointerEvent.pointerCurrentRaycast.gameObject); } m_InputPointerEvent = null; return; } m_LastMousePosition = m_MousePosition; m_MousePosition = input.mousePosition; }
public void OnDragEnded(GameObject gameObject, RayEventData eventData) { var droppable = ComponentUtils <IDroppable> .GetComponent(gameObject); if (droppable != null) { var rayOrigin = eventData.rayOrigin; m_Droppables.Remove(rayOrigin); var dropReceiver = GetCurrentDropReceiver(rayOrigin); var dropObject = droppable.GetDropObject(); if (dropReceiver != null && dropReceiver.CanDrop(dropObject)) { dropReceiver.ReceiveDrop(droppable.GetDropObject()); } } }
public void OnRayExited(GameObject gameObject, RayEventData eventData) { if (!gameObject) { return; } var dropReceiver = ComponentUtils <IDropReceiver> .GetComponent(gameObject); if (dropReceiver != null) { var rayOrigin = eventData.rayOrigin; if (m_HoverObjects.Remove(rayOrigin)) { dropReceiver.OnDropHoverEnded(); SetCurrentDropReceiver(rayOrigin, null); } } }
RayEventData GetTempEventDataClone(RayEventData eventData) { var clone = m_TempRayEvent; clone.rayOrigin = eventData.rayOrigin; clone.camera = eventData.camera; clone.position = eventData.position; clone.delta = eventData.delta; clone.node = eventData.node; clone.hovered.Clear(); clone.hovered.AddRange(eventData.hovered); clone.pressPosition = eventData.pressPosition; clone.pointerEnter = eventData.pointerEnter; clone.pointerPress = eventData.pointerPress; clone.pointerDrag = eventData.pointerDrag; clone.pointerPressRaycast = eventData.pointerPressRaycast; clone.pointerCurrentRaycast = eventData.pointerCurrentRaycast; clone.pointerLength = eventData.pointerLength; clone.useDragThreshold = eventData.useDragThreshold; return(clone); }
/// <summary> /// Remove the RayEventData from the cache. /// </summary> protected void RemoveRayData(RayEventData data) { m_RayData.Remove(data.pointerId); }
protected override void OnEnable() { base.OnEnable(); m_TempRayEvent = new RayEventData(eventSystem); }
void EndPointerDown(RayEventData rayEvent, GameObject currentOverGo) { if (pointerDown != null && currentOverGo != null) { pointerDown(currentOverGo, rayEvent); } // search for the control that will receive the press // if we can't find a press handler set the press // handler to be what would receive a click. var newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, rayEvent, ExecuteRayEvents.pointerDownHandler); if (newPressed == null) { newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, rayEvent, ExecuteEvents.pointerDownHandler); } // didnt find a press handler... search for a click handler if (newPressed == null) { newPressed = ExecuteEvents.GetEventHandler <IPointerClickHandler>(currentOverGo); } if (newPressed == null) { newPressed = ExecuteEvents.GetEventHandler <IRayClickHandler>(currentOverGo); } var time = Time.unscaledTime; if (newPressed == rayEvent.lastPress) { var diffTime = time - rayEvent.clickTime; if (diffTime < 0.3f) { ++rayEvent.clickCount; } else { rayEvent.clickCount = 1; } } else { rayEvent.clickCount = 1; } rayEvent.pointerPress = newPressed; rayEvent.rawPointerPress = currentOverGo; rayEvent.clickTime = time; // Save the drag handler as well var draggedObject = ExecuteEvents.GetEventHandler <IDragHandler>(currentOverGo); if (draggedObject == null) { draggedObject = ExecuteEvents.GetEventHandler <IRayDragHandler>(currentOverGo); } rayEvent.pointerDrag = draggedObject; if (rayEvent.pointerDrag != null) { ExecuteEvents.Execute(draggedObject, rayEvent, ExecuteEvents.initializePotentialDrag); } m_InputPointerEvent = rayEvent; }
/// <summary> /// Process the drag for the current frame with the given pointer event. /// </summary> protected virtual void ProcessDrag(RayEventData rayEvent, bool useWorldPosition = false) { var draggedObject = rayEvent.pointerDrag; if (Cursor.lockState == CursorLockMode.Locked || draggedObject == null) { return; } Vector3 pressPosition; Vector3 currentPosition; float threshold; if (useWorldPosition) { pressPosition = rayEvent.pointerPressRaycast.worldPosition; currentPosition = rayEvent.pointerCurrentRaycast.worldPosition; threshold = m_DragThreshold * this.GetViewerScale(); } else { pressPosition = rayEvent.pressPosition; currentPosition = rayEvent.position; threshold = eventSystem.pixelDragThreshold; } if (!rayEvent.dragging && ShouldStartDrag(pressPosition, currentPosition, threshold, rayEvent.useDragThreshold)) { if (dragStarted != null) { dragStarted(draggedObject, rayEvent); } ExecuteEvents.Execute(draggedObject, rayEvent, ExecuteRayEvents.beginDragHandler); ExecuteEvents.Execute(draggedObject, rayEvent, ExecuteEvents.beginDragHandler); rayEvent.dragging = true; } // Drag notification if (rayEvent.dragging) { // Before doing drag we should cancel any pointer down state // And clear selection! if (rayEvent.pointerPress != draggedObject) { ExecuteEvents.Execute(rayEvent.pointerPress, rayEvent, ExecuteRayEvents.pointerUpHandler); ExecuteEvents.Execute(rayEvent.pointerPress, rayEvent, ExecuteEvents.pointerUpHandler); rayEvent.eligibleForClick = false; rayEvent.pointerPress = null; rayEvent.rawPointerPress = null; } if (dragging != null) { dragging(draggedObject, rayEvent); } ExecuteEvents.Execute(draggedObject, rayEvent, ExecuteRayEvents.dragHandler); ExecuteEvents.Execute(draggedObject, rayEvent, ExecuteEvents.dragHandler); } }
protected void HandlePointerExitAndEnter(RayEventData eventData, GameObject newEnterTarget, bool exitOnly = false) { // Cache properties before executing base method, so we can complete additional ray events later var cachedEventData = GetTempEventDataClone(eventData); // This will modify the event data (new target will be set) base.HandlePointerExitAndEnter(eventData, newEnterTarget); var pointerEnter = cachedEventData.pointerEnter; if (newEnterTarget == null || pointerEnter == null) { for (var i = 0; i < cachedEventData.hovered.Count; ++i) { var hovered = cachedEventData.hovered[i]; ExecuteEvents.Execute(hovered, eventData, ExecuteEvents.pointerExitHandler); ExecuteEvents.Execute(hovered, eventData, ExecuteRayEvents.rayExitHandler); if (rayExited != null) { rayExited(hovered, eventData); } } if (newEnterTarget == null) { return; } } if (!exitOnly) { // if we have not changed hover target if (newEnterTarget && pointerEnter == newEnterTarget) { var transform = newEnterTarget.transform; while (transform != null) { var hovered = transform.gameObject; ExecuteEvents.Execute(hovered, cachedEventData, ExecuteRayEvents.rayHoverHandler); if (rayHovering != null) { rayHovering(hovered, cachedEventData); } transform = transform.parent; } return; } } var commonRoot = FindCommonRoot(pointerEnter, newEnterTarget); // and we already an entered object from last time if (pointerEnter != null) { // send exit handler call to all elements in the chain // until we reach the new target, or null! var transform = pointerEnter.transform; while (transform != null) { // if we reach the common root break out! if (commonRoot != null && commonRoot.transform == transform) { break; } var hovered = transform.gameObject; ExecuteEvents.Execute(hovered, cachedEventData, ExecuteEvents.pointerExitHandler); ExecuteEvents.Execute(hovered, cachedEventData, ExecuteRayEvents.rayExitHandler); if (rayExited != null) { rayExited(hovered, cachedEventData); } transform = transform.parent; } } if (!exitOnly) { // now issue the enter call up to but not including the common root cachedEventData.pointerEnter = newEnterTarget; var transform = newEnterTarget.transform; while (transform != null && transform.gameObject != commonRoot) { var hovered = transform.gameObject; ExecuteEvents.Execute(hovered, cachedEventData, ExecuteEvents.pointerEnterHandler); ExecuteEvents.Execute(hovered, cachedEventData, ExecuteRayEvents.rayEnterHandler); if (rayEntered != null) { rayEntered(hovered, cachedEventData); } transform = transform.parent; } } }
/// <summary> /// Process movement for the current frame with the given pointer event. /// </summary> protected virtual void ProcessMove(RayEventData pointerEvent) { var targetGO = (Cursor.lockState == CursorLockMode.Locked ? null : pointerEvent.pointerCurrentRaycast.gameObject); HandlePointerExitAndEnter(pointerEvent, targetGO); }
public void SetButtonState(PointerEventData.InputButton button, PointerEventData.FramePressState stateForMouseButton, RayEventData data) { var toModify = GetButtonState(button); toModify.eventData.buttonState = stateForMouseButton; toModify.eventData.buttonData = data; }
public void ProcessInput(ActionMapInput input, ConsumeControlDelegate consumeControl) { if (!(m_RayOrigin.gameObject.activeSelf || eventData.pointerDrag) || !m_Proxy.active) { return; } var preProcessRaycastSource = m_Owner.preProcessRaycastSource; if (preProcessRaycastSource != null) { preProcessRaycastSource(m_RayOrigin); } if (eventData == null) { eventData = new RayEventData(m_Owner.eventSystem); } m_Owner.GetRayIntersection(this); // Check all currently running raycasters var currentRaycast = eventData.pointerCurrentRaycast; m_Owner.m_CurrentFocusedGameObject = currentRaycast.gameObject; eventData.node = m_Node; eventData.rayOrigin = m_RayOrigin; eventData.pointerLength = m_Owner.GetPointerLength(m_RayOrigin); var uiActions = (UIActions)input; var select = uiActions.select; if (m_IsValid != null && !m_IsValid(this)) { currentRaycast.gameObject = null; eventData.pointerCurrentRaycast = currentRaycast; m_Owner.HandlePointerExitAndEnter(eventData, null, true); // Send only exit events if (select.wasJustReleased) { m_Owner.OnSelectReleased(this); } HideScrollFeedback(); return; } if (currentRaycast.gameObject) { if (select.wasJustPressed) { m_Owner.OnSelectPressed(this); consumeControl(select); } } if (select.wasJustReleased) { m_Owner.OnSelectReleased(this); } m_Owner.ProcessMove(eventData); m_Owner.ProcessDrag(eventData, true); var verticalScroll = uiActions.verticalScroll; var horizontalScroll = uiActions.horizontalScroll; if (eventData.dragging) { consumeControl(verticalScroll); consumeControl(horizontalScroll); } // Send scroll events if (currentObject) { var hasScrollHandler = ExecuteEvents.GetEventHandler <IScrollHandler>(currentObject); if (hasScrollHandler) { var verticalScrollValue = verticalScroll.value; var horizontalScrollValue = horizontalScroll.value; if (!Mathf.Approximately(verticalScrollValue, 0f) || !Mathf.Approximately(horizontalScrollValue, 0f)) { consumeControl(verticalScroll); consumeControl(horizontalScroll); eventData.scrollDelta = new Vector2(horizontalScrollValue, verticalScrollValue); ExecuteEvents.ExecuteHierarchy(currentObject, eventData, ExecuteEvents.scrollHandler); } if (m_ScrollFeedback.Count == 0) { ShowScrollFeedback(); } } } }