/// <summary> /// Set a new cursor for this <see cref="Microsoft.MixedReality.Toolkit.Input.IMixedRealityPointer"/> /// </summary> /// <remarks>This <see href="https://docs.unity3d.com/ScriptReference/GameObject.html">GameObject</see> must have a <see cref="Microsoft.MixedReality.Toolkit.Input.IMixedRealityCursor"/> attached to it.</remarks> /// <param name="newCursor">The new cursor</param> public virtual void SetCursor(GameObject newCursor = null) { if (cursorInstance != null) { if (Application.isEditor) { DestroyImmediate(cursorInstance); } else { Destroy(cursorInstance); } cursorInstance = newCursor; } if (cursorInstance == null && cursorPrefab != null) { cursorInstance = Instantiate(cursorPrefab, transform); } if (cursorInstance != null) { cursorInstance.name = $"{Handedness}_{name}_Cursor"; BaseCursor oldC = BaseCursor as BaseCursor; if (oldC != null && enabled) { oldC.VisibleSourcesCount--; } BaseCursor = cursorInstance.GetComponent <IMixedRealityCursor>(); BaseCursor newC = BaseCursor as BaseCursor; if (newC != null && enabled) { newC.VisibleSourcesCount++; } if (BaseCursor != null) { BaseCursor.DefaultCursorDistance = DefaultPointerExtent; BaseCursor.Pointer = this; BaseCursor.SetVisibilityOnSourceDetected = setCursorVisibilityOnSourceDetected; if (disableCursorOnStart) { BaseCursor.SetVisibility(false); } } else { Debug.LogError($"No IMixedRealityCursor component found on {cursorInstance.name}"); } } }
private void TryDisablingBasedOnTimer() { timeoutTimer += Time.unscaledDeltaTime; if (timeoutTimer >= hideTimeout) { timeoutTimer = 0.0f; BaseCursor?.SetVisibility(false); isDisabled = true; } }
protected override void OnDisable() { base.OnDisable(); MixedRealityToolkit.TeleportSystem?.Unregister(gameObject); IsHoldPressed = false; IsSelectPressed = false; HasSelectPressedOnce = false; BaseCursor?.SetVisibility(false); }
/// <summary> /// Set a new cursor for this <see cref="Microsoft.MixedReality.Toolkit.Input.IMixedRealityPointer"/> /// </summary> /// <remarks>This <see href="https://docs.unity3d.com/ScriptReference/GameObject.html">GameObject</see> must have a <see cref="Microsoft.MixedReality.Toolkit.Input.IMixedRealityCursor"/> attached to it.</remarks> /// <param name="newCursor">The new cursor</param> public virtual void SetCursor(GameObject newCursor = null) { using (SetCursorPerfMarker.Auto()) { // Destroy the old cursor and replace it with the new one if a new cursor was provided if (cursorInstance != null && newCursor != null) { DestroyCursorInstance(); cursorInstance = newCursor; } if (cursorInstance == null && cursorPrefab != null) { // We spawn the cursor at the same level as this pointer by setting its parent to be the same as the pointer's // In the future, the pointer will not be responsible for instantiating the cursor, so we'll avoid making this assumption about the hierarchy cursorInstance = Instantiate(cursorPrefab, transform.parent); isCursorInstantiatedFromPrefab = true; } if (cursorInstance != null) { cursorInstance.name = $"{name}_Cursor"; BaseCursor oldC = BaseCursor as BaseCursor; if (oldC != null && enabled) { oldC.VisibleSourcesCount--; } BaseCursor = cursorInstance.GetComponent <IMixedRealityCursor>(); BaseCursor newC = BaseCursor as BaseCursor; if (newC != null && enabled) { newC.VisibleSourcesCount++; } if (BaseCursor != null) { BaseCursor.DefaultCursorDistance = DefaultPointerExtent; BaseCursor.Pointer = this; BaseCursor.SetVisibilityOnSourceDetected = setCursorVisibilityOnSourceDetected; if (disableCursorOnStart) { BaseCursor.SetVisibility(false); } } else { Debug.LogError($"No IMixedRealityCursor component found on {cursorInstance.name}"); } } } }
/// <inheritdoc /> public override void OnPostSceneQuery() { base.OnPostSceneQuery(); // This ensures that we actually hide the finger cursor if the poke pointer is off BaseCursor?.SetVisibility(IsInteractionEnabled); if (!IsActive) { return; } if (Result?.CurrentPointerTarget != null && closestProximityTouchable != null) { // Start position of the ray is offset by TouchableDistance, subtract to get distance between surface and pointer position. float distToFront = Vector3.Distance(Result.StartPoint, Result.Details.Point) - touchableDistance; bool newIsDown = (distToFront < 0.0f); bool newIsUp = (distToFront > closestProximityTouchable.DebounceThreshold); if (newIsDown) { TryRaisePokeDown(); } else if (currentTouchableObjectDown != null) { if (newIsUp) { TryRaisePokeUp(); } else { TryRaisePokeDown(); } } } if (!IsNearObject) { line.endColor = line.startColor = new Color(1, 1, 1, 0.25f); } else if (currentTouchableObjectDown == null) { line.endColor = line.startColor = new Color(1, 1, 1, 0.75f); } else { line.endColor = line.startColor = new Color(0, 0, 1, 0.75f); } PreviousPosition = Position; }
/// <inheritdoc /> public override void OnPostSceneQuery() { base.OnPostSceneQuery(); bool isEnabled = IsInteractionEnabled; LineBase.enabled = isEnabled; if (BaseCursor != null) { BaseCursor.SetVisibility(isEnabled); } PostUpdateLineRenderers(); }
/// <inheritdoc /> public override void OnInputDown(InputEventData eventData) { cursorWasDisabledOnDown = isDisabled; if (cursorWasDisabledOnDown) { BaseCursor?.SetVisibility(true); transform.rotation = CameraCache.Main.transform.rotation; } else { base.OnInputDown(eventData); } }
protected override void OnDisable() { if (IsSelectPressed && MixedRealityToolkit.InputSystem != null) { MixedRealityToolkit.InputSystem.RaisePointerUp(this, pointerAction, Handedness); } base.OnDisable(); IsHoldPressed = false; IsSelectPressed = false; HasSelectPressedOnce = false; BaseCursor?.SetVisibility(false); }
private void Update() { if (!hideCursorWhenInactive || isDisabled) { return; } timeoutTimer += Time.unscaledDeltaTime; if (timeoutTimer >= hideTimeout) { timeoutTimer = 0.0f; BaseCursor?.SetVisibility(false); isDisabled = true; } }
/// <inheritdoc /> public override void OnPostSceneQuery() { using (OnPostSceneQueryPerfMarker.Auto()) { base.OnPostSceneQuery(); bool isEnabled = IsInteractionEnabled; lineDataProvider.enabled = isEnabled; if (BaseCursor != null) { BaseCursor.SetVisibility(isEnabled); } PostUpdateLineRenderers(); } }
private void TryEnablingBaseOnMovement(float mouseX, float mouseY) { if (Mathf.Abs(mouseX) >= movementThresholdToUnHide || Mathf.Abs(mouseY) >= movementThresholdToUnHide) { if (isDisabled) { BaseCursor?.SetVisibility(true); transform.rotation = CameraCache.Main.transform.rotation; isDisabled = false; } } if (!isDisabled) { timeoutTimer = 0.0f; } }
/// <inheritdoc /> public override void OnPreSceneQuery() { TryDisablingBasedOnTimer(); bool visible = IsInteractionEnabled && !IsInactiveAndDisabled; BaseCursor?.SetVisibility(visible); UpdateSystemCursor(visible); Ray ray = new Ray(transform.position, transform.rotation * Vector3.forward); Rays[0].CopyRay(ray, PointerExtent); if (MixedRealityRaycaster.DebugEnabled) { Debug.DrawRay(ray.origin, ray.direction * PointerExtent, Color.red); } base.OnPreSceneQuery(); }
private void UpdateMouseRotation(Vector3 mouseDeltaRotation) { if (mouseDeltaRotation.magnitude >= movementThresholdToUnHide) { if (isDisabled) { // if cursor was hidden reset to center BaseCursor?.SetVisibility(true); transform.rotation = CameraCache.Main.transform.rotation; } isDisabled = false; } if (!isDisabled) { timeoutTimer = 0.0f; } transform.Rotate(mouseDeltaRotation, Space.World); }
protected override void OnDisable() { if (IsSelectPressed && InputSystem != null) { InputSystem.RaisePointerUp(this, pointerAction, Handedness); } base.OnDisable(); IsHoldPressed = false; IsSelectPressed = false; HasSelectPressedOnce = false; BaseCursor?.SetVisibility(false); BaseCursor c = BaseCursor as BaseCursor; if (c != null) { c.VisibleSourcesCount--; } }
/// <inheritdoc /> public override void OnPostSceneQuery() { base.OnPostSceneQuery(); Gradient lineColor = LineColorNoTarget; if (!IsActive) { lineBase.enabled = false; BaseCursor?.SetVisibility(false); return; } lineBase.enabled = true; BaseCursor?.SetVisibility(true); // The distance the ray travels through the world before it hits something. Measured in world-units (as opposed to normalized distance). float clearWorldLength; // Used to ensure the line doesn't extend beyond the cursor float cursorOffsetWorldLength = (BaseCursor != null) ? BaseCursor.SurfaceCursorDistance : 0; // If we hit something if (Result?.CurrentPointerTarget != null) { clearWorldLength = Result.Details.RayDistance; lineColor = LineColorValid; } else { clearWorldLength = DefaultPointerExtent; lineColor = IsSelectPressed ? LineColorSelected : LineColorNoTarget; } if (IsFocusLocked) { lineColor = LineColorLockFocus; } int maxClampLineSteps = LineCastResolution; foreach (BaseMixedRealityLineRenderer lineRenderer in lineRenderers) { // Renderers are enabled by default if line is enabled lineRenderer.enabled = true; maxClampLineSteps = Mathf.Max(maxClampLineSteps, lineRenderer.LineStepCount); lineRenderer.LineColor = lineColor; } // If focus is locked, we're sticking to the target // So don't clamp the world length if (IsFocusLocked && IsTargetPositionLockedOnFocusLock) { float cursorOffsetLocalLength = LineBase.GetNormalizedLengthFromWorldLength(cursorOffsetWorldLength); LineBase.LineEndClamp = 1 - cursorOffsetLocalLength; } else { // Otherwise clamp the line end by the clear distance float clearLocalLength = lineBase.GetNormalizedLengthFromWorldLength(clearWorldLength - cursorOffsetWorldLength, maxClampLineSteps); lineBase.LineEndClamp = clearLocalLength; } }
/// <inheritdoc /> public override void OnPostSceneQuery() { if (IsSelectPressed) { MixedRealityToolkit.InputSystem.RaisePointerDragged(this, MixedRealityInputAction.None, Handedness); } Gradient lineColor = LineColorNoTarget; BaseMixedRealityLineRenderer contextRenderer = null; if (!IsActive) { LineBase.enabled = false; BaseCursor?.SetVisibility(false); return; } contextRenderer = lineRendererNoTarget; LineBase.enabled = true; BaseCursor?.SetVisibility(true); float clearWorldLength; float cursorOffsetWorldLength = (BaseCursor != null) ? BaseCursor.SurfaceCursorDistance : 0; // If we hit something if (Result?.CurrentPointerTarget != null) { clearWorldLength = Result.Details.RayDistance; lineColor = LineColorValid; contextRenderer = lineRendererSelected; } else { clearWorldLength = DefaultPointerExtent; lineColor = IsSelectPressed ? LineColorSelected : LineColorNoTarget; contextRenderer = IsSelectPressed ? lineRendererSelected : lineRendererNoTarget; } if (IsFocusLocked) { lineColor = LineColorLockFocus; contextRenderer = lineRendererSelected; } int maxClampLineSteps = LineCastResolution; foreach (BaseMixedRealityLineRenderer lineRenderer in LineRenderers) { // Otherwise, enable the renderer we chose if (lineRenderer == contextRenderer) { lineRenderer.enabled = true; maxClampLineSteps = Mathf.Max(maxClampLineSteps, lineRenderer.LineStepCount); } else { lineRenderer.enabled = false; } // Set colors on all line renderers regardless of context lineRenderer.LineColor = lineColor; } // If focus and target point is locked, we're sticking to the target // So don't clamp the world length if (IsFocusLocked && IsTargetPositionLockedOnFocusLock) { float cursorOffsetLocalLength = LineBase.GetNormalizedLengthFromWorldLength(cursorOffsetWorldLength); LineBase.LineEndClamp = 1 - cursorOffsetLocalLength; } else { // Otherwise clamp the line end by the clear distance float clearLocalLength = LineBase.GetNormalizedLengthFromWorldLength(clearWorldLength - cursorOffsetWorldLength, maxClampLineSteps); LineBase.LineEndClamp = clearLocalLength; } }
/// <inheritdoc /> protected override void SetVisibility(bool visible) { base.SetVisibility(visible); BaseCursor?.SetVisibility(visible); }
/// <inheritdoc /> public virtual void OnTeleportRequest(TeleportEventData eventData) { // Only turn off pointers that aren't making the request. IsTeleportRequestActive = true; BaseCursor?.SetVisibility(false); }
/// <inheritdoc /> public virtual void OnTeleportStarted(TeleportEventData eventData) { // Turn off all pointers while we teleport. IsTeleportRequestActive = true; BaseCursor?.SetVisibility(false); }
/// <inheritdoc /> public virtual void OnTeleportCanceled(TeleportEventData eventData) { // Turn all our pointers back on. IsTeleportRequestActive = false; BaseCursor?.SetVisibility(true); }