-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialPointerReticle.cs
186 lines (155 loc) · 6.13 KB
/
SpatialPointerReticle.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Shapes;
using Cuboid.Utils;
namespace Cuboid.Input
{
/// <summary>
/// The SpatialPointerReticle determines how it should display the reticle depending on the
/// current EventData's eventData.enterObjectType.
/// </summary>
public class SpatialPointerReticle : MonoBehaviour
{
private SpatialPointerReticleData _data = new SpatialPointerReticleData();
public SpatialPointerReticleData Data
{
get => _data;
set
{
if (_data == value) { return; } // don't update when it has already been loaded
_data = value;
OnDataChanged(_data);
}
}
// Custom image
[SerializeField] private Image _customReticleImage;
private GameObject _customReticle;
// Default reticle (to disable when a custom image has been added)
[SerializeField] private GameObject _defaultReticle;
[SerializeField] private Disc _currentTransformDisc;
[SerializeField] private Disc _targetTransformDisc;
[SerializeField] private Transform _currentTransform;
[SerializeField] private Transform _targetTransform;
[SerializeField] private Line _line;
private void Awake()
{
}
private void Start()
{
if (_customReticleImage == null)
{
// Try to get the image in the game object
_customReticleImage = GetComponentInChildren<Image>();
}
_customReticle = _customReticleImage.gameObject;
}
private void UpdateRotation()
{
if (Data.updateRotation != null)
{
_targetTransform.localRotation = Data.updateRotation.Invoke();
}
else
{
if (Data.Billboard)
{
// Rotate towards the camera (don't use the supplied rotation)
Vector3 cameraPosition = Camera.main.transform.position;
Vector3 position = _targetTransform.position;
Vector3 delta = position - cameraPosition;
_targetTransform.localRotation = Quaternion.LookRotation(delta, Vector3.up);
}
else
{
_targetTransform.localRotation = Data.Rotation;
}
}
}
private void LateUpdate()
{
if (Data == null) { return; }
UpdateScale();
UpdateRotation();
}
private void UpdateScale()
{
if (Data.ReticleImage != null)
{
_customReticle.transform.localScale = ConstantScaleOnScreen.GetConstantScale(_customReticle.transform.position, Data.ReferenceSizeAtOneMeterDistance);
}
}
private void UpdateReticleVisibility(bool visible, bool spatial, bool showCurrentSpatialPointerPosition)
{
_targetTransform.gameObject.SetActive(visible);
_currentTransform.gameObject.SetActive(visible && spatial && showCurrentSpatialPointerPosition);
_line.gameObject.SetActive(visible && spatial && showCurrentSpatialPointerPosition);
}
public void OnSpatialPointerEventDataUpdated(SpatialPointerEventData eventData)
{
bool pressed = eventData.pointerPress != null || eventData.outsideUIPointerPress == true;
Color color = pressed ? Data.PressedColor : Data.Color;
bool visible = false;
if (eventData.outsideUIPointerEnter)
{
visible = eventData.outsideUIValidPointerPosition;
}
else
{
visible = eventData.pointerEnter != null;
}
//if (eventData.invalidRaycastWasIntercepted)
//{
// visible = false;
//}
// also show the current spatial pointer position when the outside UI has been entered and registered.
//bool showCurrentSpatialPointerPosition = (pressed && eventData.outsideUIPressRaycastResult.isValid) || eventData.outsideUIPointerEnter;
bool showCurrentSpatialPointerPosition = false;
if (eventData.outsideUIPointerEnter)
{
// otherwise, only show if valid
showCurrentSpatialPointerPosition = pressed && eventData.outsideUIPointerDrag && eventData.outsideUIPressRaycastResult.isValid;
}
else
{
showCurrentSpatialPointerPosition = pressed && eventData.pointerDrag != null;
}
UpdateReticleVisibility(
visible,
true,//eventData.enterObjectType != SpatialPointerEventData.ObjectType.UI,
showCurrentSpatialPointerPosition);
UpdateScale();
// set the reticle color based on whether the user is pressing
_customReticleImage.color = color;
_currentTransformDisc.Color = color;
_targetTransformDisc.Color = color;
Vector3 current = eventData.spatialPosition;
Vector3 target = eventData.spatialTargetPosition;
_currentTransform.position = current;
_targetTransform.position = target;
_line[0] = current;
_line[1] = target;
}
private void SetReticleImageVisibility(bool showReticleImage)
{
_customReticle.SetActive(showReticleImage);
_defaultReticle.SetActive(!showReticleImage);
}
private void OnDataChanged(SpatialPointerReticleData data)
{
bool showReticleImage = data.ReticleImage != null;
if (showReticleImage)
{
_customReticleImage.sprite = data.ReticleImage;
}
UpdateScale();
UpdateRotation();
SetReticleImageVisibility(showReticleImage);
}
}
}