public void detachAnnotation(MouseTrackerAnnotation annotation) { _TrackedAnnotation trackedAnnotation = this._findAnnotation(annotation); foreach (int deviceId in trackedAnnotation.activeDevices) { if (annotation.onExit != null) { annotation.onExit( PointerExitEvent.fromMouseEvent(this._lastMouseEvent[deviceId])); } #if UNITY_EDITOR this.detachDragFromEditorAnnotation(annotation, deviceId); #endif } this._trackedAnnotations.Remove(annotation); }
public static void _dispatchDeviceCallbacks( HashSet <MouseTrackerAnnotation> lastAnnotations, HashSet <MouseTrackerAnnotation> nextAnnotations, PointerEvent previousEvent = null, PointerEvent unhandledEvent = null ) { D.assert(lastAnnotations != null); D.assert(nextAnnotations != null); var latestEvent = unhandledEvent ?? previousEvent; D.assert(latestEvent != null); IEnumerable <MouseTrackerAnnotation> exitingAnnotations = new List <MouseTrackerAnnotation>(); var exiting = new List <MouseTrackerAnnotation>(); foreach (var lastAnnotation in lastAnnotations) { if (!nextAnnotations.Contains(item: lastAnnotation)) { exiting.Add(item: lastAnnotation); } } exitingAnnotations = exiting; foreach (var annotation in exitingAnnotations) { if (annotation.onExit != null) { annotation.onExit(PointerExitEvent.fromMouseEvent(hover: latestEvent)); } } IEnumerable <MouseTrackerAnnotation> enteringAnnotations = new List <MouseTrackerAnnotation>(); var entering = new List <MouseTrackerAnnotation>(); foreach (var nextAnnotation in nextAnnotations) { if (!lastAnnotations.Contains(item: nextAnnotation)) { entering.Add(item: nextAnnotation); } } entering.ToList().Reverse(); enteringAnnotations = entering; foreach (var annotation in enteringAnnotations) { if (annotation.onEnter != null) { annotation.onEnter(PointerEnterEvent.fromMouseEvent(hover: latestEvent)); } } if (unhandledEvent is PointerHoverEvent) { var lastHoverPosition = previousEvent is PointerHoverEvent ? previousEvent.position : null; var pointerHasMoved = lastHoverPosition == null || lastHoverPosition != unhandledEvent.position; nextAnnotations.ToList().Reverse(); var hoveringAnnotations = pointerHasMoved ? nextAnnotations : enteringAnnotations; foreach (var annotation in hoveringAnnotations) { if (annotation.onHover != null) { annotation.onHover((PointerHoverEvent)unhandledEvent); } } } }
/// Tells interested objects that a mouse has entered, exited, or moved, given /// a callback to fetch the [MouseTrackerAnnotation] associated with a global /// offset. /// /// This is called from a post-frame callback when the layer tree has been /// updated, right after rendering the frame. /// /// This function is only public to allow for proper testing of the /// MouseTracker. Do not call in other contexts. public void collectMousePositions() { void exitAnnotation(_TrackedAnnotation trackedAnnotation, int deviceId) { if (trackedAnnotation.activeDevices.Contains(deviceId)) { if (trackedAnnotation.annotation?.onExit != null) { trackedAnnotation.annotation.onExit( PointerExitEvent.fromMouseEvent(this._lastMouseEvent[deviceId])); } trackedAnnotation.activeDevices.Remove(deviceId); } } void exitAllDevices(_TrackedAnnotation trackedAnnotation) { if (trackedAnnotation.activeDevices.isNotEmpty()) { HashSet <int> deviceIds = new HashSet <int>(trackedAnnotation.activeDevices); foreach (int deviceId in deviceIds) { exitAnnotation(trackedAnnotation, deviceId); } } } if (!this.mouseIsConnected) { foreach (var annotation in this._trackedAnnotations.Values) { exitAllDevices(annotation); } return; } foreach (int deviceId in this._lastMouseEvent.Keys) { PointerEvent lastEvent = this._lastMouseEvent[deviceId]; MouseTrackerAnnotation hit = this.annotationFinder(lastEvent.position); if (hit == null) { foreach (_TrackedAnnotation trackedAnnotation in this._trackedAnnotations.Values) { exitAnnotation(trackedAnnotation, deviceId); } return; } _TrackedAnnotation hitAnnotation = this._findAnnotation(hit); //enter if (!hitAnnotation.activeDevices.Contains(deviceId)) { hitAnnotation.activeDevices.Add(deviceId); if (hitAnnotation.annotation?.onEnter != null) { hitAnnotation.annotation.onEnter(PointerEnterEvent.fromMouseEvent(lastEvent)); } } //hover if (hitAnnotation.annotation?.onHover != null && lastEvent is PointerHoverEvent) { hitAnnotation.annotation.onHover(lastEvent as PointerHoverEvent); } //leave foreach (_TrackedAnnotation trackedAnnotation in this._trackedAnnotations.Values) { if (hitAnnotation == trackedAnnotation) { continue; } if (trackedAnnotation.activeDevices.Contains(deviceId)) { if (trackedAnnotation.annotation?.onExit != null) { trackedAnnotation.annotation.onExit( PointerExitEvent.fromHoverEvent((PointerHoverEvent)lastEvent)); } trackedAnnotation.activeDevices.Remove(deviceId); } } } }