File size: 10,579 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#if UNITY_INPUT_SYSTEM_ENABLE_UI
using UnityEngine.EventSystems;

namespace UnityEngine.InputSystem.UI
{
    // A pointer is identified by a single unique integer ID. It has an associated position and the ability to press
    // on up to three buttons. It can also scroll.
    //
    // There's a single ExtendedPointerEventData instance allocated for the pointer which is used to retain the pointer's
    // event state. As part of that state is specific to button presses, each button retains a partial copy of press-specific
    // event information.
    //
    // A pointer can operate in 2D (mouse, pen, touch) or 3D (tracked). For 3D, screen-space 2D positions are derived
    // via raycasts based on world-space position and orientation.
    internal struct PointerModel
    {
        public bool changedThisFrame;
        public UIPointerType pointerType => eventData.pointerType;

        public Vector2 screenPosition
        {
            get => m_ScreenPosition;
            set
            {
                if (m_ScreenPosition != value)
                {
                    m_ScreenPosition = value;
                    changedThisFrame = true;
                }
            }
        }

        public Vector3 worldPosition
        {
            get => m_WorldPosition;
            set
            {
                if (m_WorldPosition != value)
                {
                    m_WorldPosition = value;
                    changedThisFrame = true;
                }
            }
        }

        public Quaternion worldOrientation
        {
            get => m_WorldOrientation;
            set
            {
                if (m_WorldOrientation != value)
                {
                    m_WorldOrientation = value;
                    changedThisFrame = true;
                }
            }
        }

        public Vector2 scrollDelta
        {
            get => m_ScrollDelta;
            set
            {
                if (m_ScrollDelta != value)
                {
                    changedThisFrame = true;
                    m_ScrollDelta = value;
                }
            }
        }

        public float pressure
        {
            get => m_Pressure;
            set
            {
                if (m_Pressure != value)
                {
                    changedThisFrame = true;
                    m_Pressure = value;
                }
            }
        }

        public float azimuthAngle
        {
            get => m_AzimuthAngle;
            set
            {
                if (m_AzimuthAngle != value)
                {
                    changedThisFrame = true;
                    m_AzimuthAngle = value;
                }
            }
        }

        public float altitudeAngle
        {
            get => m_AltitudeAngle;
            set
            {
                if (m_AltitudeAngle != value)
                {
                    changedThisFrame = true;
                    m_AltitudeAngle = value;
                }
            }
        }

        public float twist
        {
            get => m_Twist;
            set
            {
                if (m_Twist != value)
                {
                    changedThisFrame = true;
                    m_Twist = value;
                }
            }
        }

        public Vector2 radius
        {
            get => m_Radius;
            set
            {
                if (m_Radius != value)
                {
                    changedThisFrame = true;
                    m_Radius = value;
                }
            }
        }

        public ButtonState leftButton;
        public ButtonState rightButton;
        public ButtonState middleButton;
        public ExtendedPointerEventData eventData;

        public PointerModel(ExtendedPointerEventData eventData)
        {
            this.eventData = eventData;

            changedThisFrame = false;

            leftButton = default; leftButton.OnEndFrame();
            rightButton = default; rightButton.OnEndFrame();
            middleButton = default; middleButton.OnEndFrame();

            m_ScreenPosition = default;
            m_ScrollDelta = default;
            m_WorldOrientation = default;
            m_WorldPosition = default;

            m_Pressure = default;
            m_AzimuthAngle = default;
            m_AltitudeAngle = default;
            m_Twist = default;
            m_Radius = default;
        }

        public void OnFrameFinished()
        {
            changedThisFrame = false;
            scrollDelta = default;
            leftButton.OnEndFrame();
            rightButton.OnEndFrame();
            middleButton.OnEndFrame();
        }

        private Vector2 m_ScreenPosition;
        private Vector2 m_ScrollDelta;
        private Vector3 m_WorldPosition;
        private Quaternion m_WorldOrientation;

        private float m_Pressure;
        private float m_AzimuthAngle;
        private float m_AltitudeAngle;
        private float m_Twist;
        private Vector2 m_Radius;

        public void CopyTouchOrPenStateFrom(PointerEventData eventData)
        {
#if UNITY_2021_1_OR_NEWER
            pressure = eventData.pressure;
            azimuthAngle = eventData.azimuthAngle;
            altitudeAngle = eventData.altitudeAngle;
            twist = eventData.twist;
            radius = eventData.radius;
#endif
        }

        // State related to pressing and releasing individual bodies. Retains those parts of
        // PointerInputEvent that are specific to presses and releases.
        public struct ButtonState
        {
            private bool m_IsPressed;
            private PointerEventData.FramePressState m_FramePressState;
            private float m_PressTime;

            public bool isPressed
            {
                get => m_IsPressed;
                set
                {
                    if (m_IsPressed != value)
                    {
                        m_IsPressed = value;

                        if (m_FramePressState == PointerEventData.FramePressState.NotChanged && value)
                            m_FramePressState = PointerEventData.FramePressState.Pressed;
                        else if (m_FramePressState == PointerEventData.FramePressState.NotChanged && !value)
                            m_FramePressState = PointerEventData.FramePressState.Released;
                        else if (m_FramePressState == PointerEventData.FramePressState.Pressed && !value)
                            m_FramePressState = PointerEventData.FramePressState.PressedAndReleased;
                    }
                }
            }

            /// <summary>
            /// When we "release" a button other than through user interaction (e.g. through focus switching),
            /// we don't want this to count as an actual release that ends up clicking. This flag will cause
            /// generated events to have <c>eligibleForClick</c> to be false.
            /// </summary>
            public bool ignoreNextClick
            {
                get => m_IgnoreNextClick;
                set => m_IgnoreNextClick = value;
            }

            public float pressTime
            {
                get => m_PressTime;
                set => m_PressTime = value;
            }

            public bool clickedOnSameGameObject
            {
                get => m_ClickedOnSameGameObject;
                set => m_ClickedOnSameGameObject = value;
            }

            public bool wasPressedThisFrame => m_FramePressState == PointerEventData.FramePressState.Pressed ||
            m_FramePressState == PointerEventData.FramePressState.PressedAndReleased;
            public bool wasReleasedThisFrame => m_FramePressState == PointerEventData.FramePressState.Released ||
            m_FramePressState == PointerEventData.FramePressState.PressedAndReleased;

            private RaycastResult m_PressRaycast;
            private GameObject m_PressObject;
            private GameObject m_RawPressObject;
            private GameObject m_LastPressObject;
            private GameObject m_DragObject;
            private Vector2 m_PressPosition;
            private float m_ClickTime; // On Time.unscaledTime timeline, NOT input event time.
            private int m_ClickCount;
            private bool m_Dragging;
            private bool m_ClickedOnSameGameObject;
            private bool m_IgnoreNextClick;

            public void CopyPressStateTo(PointerEventData eventData)
            {
                eventData.pointerPressRaycast = m_PressRaycast;
                eventData.pressPosition = m_PressPosition;
                eventData.clickCount = m_ClickCount;
                eventData.clickTime = m_ClickTime;
                // We can't set lastPress directly. Old input module uses three different event instances, one for each
                // button. We share one instance and just switch press states. Set pointerPress twice to get the lastPress
                // we need.
                //
                // NOTE: This does *NOT* quite work as stated in the docs. pointerPress is nulled out on button release which
                //       will set lastPress as a side-effect. This means that lastPress will only be non-null while no press is
                //       going on and will *NOT* refer to the last pressed object when a new object has been pressed on.
                eventData.pointerPress = m_LastPressObject;
                eventData.pointerPress = m_PressObject;
                eventData.rawPointerPress = m_RawPressObject;
                eventData.pointerDrag = m_DragObject;
                eventData.dragging = m_Dragging;

                if (ignoreNextClick)
                    eventData.eligibleForClick = false;
            }

            public void CopyPressStateFrom(PointerEventData eventData)
            {
                m_PressRaycast = eventData.pointerPressRaycast;
                m_PressObject = eventData.pointerPress;
                m_RawPressObject = eventData.rawPointerPress;
                m_LastPressObject = eventData.lastPress;
                m_PressPosition = eventData.pressPosition;
                m_ClickTime = eventData.clickTime;
                m_ClickCount = eventData.clickCount;
                m_DragObject = eventData.pointerDrag;
                m_Dragging = eventData.dragging;
            }

            public void OnEndFrame()
            {
                m_FramePressState = PointerEventData.FramePressState.NotChanged;
            }
        }
    }
}
#endif