-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathInteractionDetector.cs
More file actions
121 lines (103 loc) · 4.44 KB
/
InteractionDetector.cs
File metadata and controls
121 lines (103 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.XR.Interaction.Toolkit.Interactors;
using UnityEngine.XR.Interaction.Toolkit.UI;
namespace MixedReality.Toolkit.Input
{
/// <summary>
/// Basic implementation of a <see cref="IInteractionModeDetector"/>,
/// which reports the specified hover and select modes whenever the associated
/// interactor has a valid hover or select target.
/// </summary>
[AddComponentMenu("MRTK/Input/Interaction Detector")]
public class InteractionDetector : MonoBehaviour, IInteractionModeDetector
{
[SerializeField]
[Tooltip("The interactor to listen to.")]
private XRBaseInteractor interactor;
/// <summary>
/// The interactor to listen to.
/// </summary>
public XRBaseInteractor Interactor
{
get => interactor;
set => interactor = value;
}
[SerializeField]
[Tooltip("Should this detector set a mode when the specified interactor has a hover target?")]
private bool detectHover;
/// <summary>
/// Should this detector set a mode when the specified interactor has a hover target?
/// </summary>
public bool DetectHover
{
get => detectHover;
set => detectHover = value;
}
[SerializeField]
[FormerlySerializedAs("farHoverMode")]
[Tooltip("The interaction mode to be set when the interactor is hovering over an interactable.")]
private InteractionMode modeOnHover;
/// <summary>
/// The interaction mode to be set when the interactor is hovering over an interactable.
/// </summary>
public InteractionMode ModeOnHover
{
get => modeOnHover;
set => modeOnHover = value;
}
[SerializeField]
[Tooltip("Should this detector set a mode when the specified interactor has a selection?")]
private bool detectSelect;
/// <summary>
/// Should this detector set a mode when the specified interactor has a selection?
/// </summary>
public bool DetectSelect
{
get => detectSelect;
set => detectSelect = value;
}
[SerializeField]
[FormerlySerializedAs("farSelectMode")]
[Tooltip("The interaction mode to be set when the interactor is selecting an interactable.")]
private InteractionMode modeOnSelect;
/// <summary>
/// The interaction mode to be set when the interactor is selecting an interactable.
/// </summary>
public InteractionMode ModeOnSelect
{
get => modeOnSelect;
set => modeOnSelect = value;
}
/// <inheritdoc />
public InteractionMode ModeOnDetection => interactor.hasSelection ? modeOnSelect : modeOnHover;
[SerializeField]
[FormerlySerializedAs("Controllers")]
[FormerlySerializedAs("controllers")]
[Tooltip("List of GameObjects which represent the interactor groups that this interaction mode detector has jurisdiction over. Interaction modes will be set on all specified groups.")]
private List<GameObject> interactorGroups;
/// <inheritdoc />
[Obsolete("This function has been deprecated in version 4.0.0 and will be removed in a future version. Please use GetInteractorGroups instead.")]
public List<GameObject> GetControllers() => GetInteractorGroups();
/// <inheritdoc />
public List<GameObject> GetInteractorGroups() => interactorGroups;
/// <inheritdoc />
public bool IsModeDetected()
{
bool isDetected = (interactor.hasHover && detectHover) || (interactor.hasSelection && detectSelect);
if (interactor is XRRayInteractor rayInteractor)
{
isDetected |= rayInteractor.TryGetUIModel(out TrackedDeviceModel model) && model.currentRaycast.isValid && (detectHover || (model.select && detectSelect));
}
else if (interactor is NearFarInteractor nearFarInteractor)
{
isDetected |= nearFarInteractor.TryGetUIModel(out TrackedDeviceModel model) && model.currentRaycast.isValid && (detectHover || (model.select && detectSelect));
}
return isDetected;
}
}
}