-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handle.cs
176 lines (151 loc) · 4.76 KB
/
Handle.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
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cuboid.Utils;
using Cuboid.Input;
namespace Cuboid
{
/// <summary>
/// Generic data that is the same for each Handle
/// </summary>
public class HandleData
{
/// <summary>
/// The index is used for identifying which of the handles is being pressed
/// when modifying using the tool class.
/// </summary>
public int Index;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class Handle<T> : MonoBehaviour,
ISpatialPointerEnterHandler,
ISpatialPointerExitHandler,
ISpatialPointerDownHandler,
ISpatialPointerUpHandler,
ISpatialDragHandler,
ISpatialBeginDragHandler,
ISpatialEndDragHandler,
ISpatialPointerConfiguration
where T : HandleData
{
[SerializeField] private Vector3 _referenceScale = Vector3.one;
private T _data;
public T Data
{
get => _data;
set
{
_data = value;
}
}
private bool _hovered = false;
public bool Hovered
{
get => _hovered;
private set
{
_hovered = value;
UpdateHoverPressedAppearance();
}
}
private bool _pressed = false;
public bool Pressed
{
get => _pressed;
private set
{
_pressed = value;
UpdateHoverPressedAppearance();
}
}
[Header("Configuration")]
[SerializeField] private bool _useDefaultConfiguration = true;
[SerializeField] private SpatialPointerConfiguration _configuration;
public SpatialPointerConfiguration Configuration
{
get => _useDefaultConfiguration ? SpatialPointerConfiguration.Default : _configuration;
set => _configuration = value;
}
/// <summary>
///
/// </summary>
/// <param name="newPosition"></param>
/// <param name="data"></param>
public delegate void PositionUpdated(Vector3 pressedPosition, Vector3 newPosition, T data);
public Action HandleBeginDrag;
public Action HandleEndDrag;
/// <summary>
/// Should be listened to by the tool etc.
/// </summary>
public PositionUpdated positionUpdated;
public Action<SpatialPointerEventData, T> HandleDrag;
protected virtual void Awake()
{
UpdateScale();
UpdateHoverPressedAppearance();
}
private void Start()
{
UpdateHoverPressedAppearance();
}
private void UpdateScale()
{
transform.localScale = ConstantScaleOnScreen.GetConstantScale(transform.position, _referenceScale);
}
/// <summary>
/// Can be overridden, updates scale
/// </summary>
protected virtual void LateUpdate()
{
UpdateScale();
}
protected virtual void UpdateHoverPressedAppearance()
{
}
void ISpatialPointerEnterHandler.OnSpatialPointerEnter(SpatialPointerEventData eventData)
{
Hovered = true;
}
void ISpatialPointerExitHandler.OnSpatialPointerExit(SpatialPointerEventData eventData)
{
Hovered = false;
}
void ISpatialPointerDownHandler.OnSpatialPointerDown(SpatialPointerEventData eventData)
{
Pressed = true;
}
void ISpatialPointerUpHandler.OnSpatialPointerUp(SpatialPointerEventData eventData)
{
Pressed = false;
}
void ISpatialDragHandler.OnSpatialDrag(SpatialPointerEventData eventData)
{
UpdatePosition(eventData);
HandleDrag?.Invoke(eventData, Data);
}
private Vector3 _raycastOffset = Vector3.zero;
private Vector3 _pressedPosition = Vector3.zero;
private void UpdatePosition(SpatialPointerEventData eventData)
{
positionUpdated?.Invoke(_pressedPosition, eventData.spatialPosition + _raycastOffset, Data);
}
void ISpatialBeginDragHandler.OnSpatialBeginDrag(SpatialPointerEventData eventData)
{
_pressedPosition = transform.position;
_raycastOffset = _pressedPosition - eventData.spatialPosition;
HandleBeginDrag?.Invoke();
}
void ISpatialEndDragHandler.OnSpatialEndDrag(SpatialPointerEventData eventData)
{
HandleEndDrag?.Invoke();
}
}
}