Ejemplo n.º 1
0
        public void collectMousePositions()
        {
            void exitAnnotation(_TrackedAnnotation trackedAnnotation, int deviceId)
            {
                if (trackedAnnotation.annotation?.onExit != null &&
                    trackedAnnotation.activeDevices.Contains(deviceId))
                {
                    trackedAnnotation.annotation.onExit(PointerExitEvent.fromHoverEvent(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.fromHoverEvent(lastEvent));
                    }
                }

                //hover
                if (hitAnnotation.annotation?.onHover != null)
                {
                    hitAnnotation.annotation.onHover(PointerHoverEvent.fromHoverEvent(lastEvent));
                }

                //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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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);
                    }
                }
            }
        }