Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
UIDragPanel.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3using UnityEngine.EventSystems;
4
5public class UIDragPanel : MonoBehaviour, IPointerDownHandler, IEventSystemHandler, IPointerUpHandler, IDragHandler, IChangeResolution, IInitializePotentialDragHandler
6{
7 public static bool dragging;
8
10
12
13 public RectTransform target;
14
15 public RectTransform bound;
16
17 public RectTransform container;
18
19 public bool axisY = true;
20
21 public bool axisX = true;
22
23 public bool enable = true;
24
25 public bool autoAnchor;
26
27 public bool clamp = true;
28
29 public Action onDrag;
30
31 private void Awake()
32 {
33 if (target == null)
34 {
35 target = base.transform.parent as RectTransform;
36 }
37 if (bound == null)
38 {
39 bound = target;
40 }
41 }
42
43 public void SetTarget(RectTransform r)
44 {
45 target = (bound = r);
46 container = target.parent as RectTransform;
47 }
48
49 public void OnChangeResolution()
50 {
51 }
52
53 public void OnPointerDown(PointerEventData data)
54 {
55 if (enable && data.button == PointerEventData.InputButton.Left)
56 {
57 if (container == null)
58 {
59 container = target.parent as RectTransform;
60 }
61 originalPanelLocalPosition = target.localPosition;
62 RectTransformUtility.ScreenPointToLocalPointInRectangle(container, data.position, data.pressEventCamera, out originalLocalPointerPosition);
63 dragging = true;
64 }
65 }
66
67 public void OnPointerUp(PointerEventData data)
68 {
69 if (enable && data.button == PointerEventData.InputButton.Left)
70 {
71 if (axisY && axisX && autoAnchor)
72 {
73 target.SetAnchor();
74 }
75 dragging = false;
76 }
77 }
78
79 public void OnInitializePotentialDrag(PointerEventData ped)
80 {
81 ped.useDragThreshold = false;
82 }
83
84 public void OnDrag(PointerEventData data)
85 {
86 if (enable && data.button == PointerEventData.InputButton.Left && !(target == null) && !(container == null))
87 {
88 if (RectTransformUtility.ScreenPointToLocalPointInRectangle(container, data.position, data.pressEventCamera, out var localPoint))
89 {
90 Vector3 vector = localPoint - originalLocalPointerPosition;
91 target.localPosition = originalPanelLocalPosition + vector;
92 }
93 if (clamp)
94 {
96 }
97 if (onDrag != null)
98 {
99 onDrag();
100 }
101 }
102 }
103
104 private void ClampToWindow()
105 {
106 Vector3 localPosition = target.localPosition;
107 Vector3 vector = container.rect.min - bound.rect.min;
108 Vector3 vector2 = container.rect.max - bound.rect.max;
109 if (axisX)
110 {
111 localPosition.x = (int)Mathf.Clamp(localPosition.x, vector.x - 20f, vector2.x + 20f);
112 }
113 if (axisY)
114 {
115 localPosition.y = (int)Mathf.Clamp(localPosition.y, vector.y - 20f, vector2.y + 20f);
116 }
117 target.localPosition = localPosition;
118 }
119}
void SetTarget(RectTransform r)
Definition: UIDragPanel.cs:43
void ClampToWindow()
Definition: UIDragPanel.cs:104
void OnChangeResolution()
Definition: UIDragPanel.cs:49
void OnDrag(PointerEventData data)
Definition: UIDragPanel.cs:84
bool autoAnchor
Definition: UIDragPanel.cs:25
RectTransform bound
Definition: UIDragPanel.cs:15
Action onDrag
Definition: UIDragPanel.cs:29
Vector3 originalPanelLocalPosition
Definition: UIDragPanel.cs:11
void Awake()
Definition: UIDragPanel.cs:31
RectTransform target
Definition: UIDragPanel.cs:13
void OnPointerUp(PointerEventData data)
Definition: UIDragPanel.cs:67
void OnInitializePotentialDrag(PointerEventData ped)
Definition: UIDragPanel.cs:79
static bool dragging
Definition: UIDragPanel.cs:7
RectTransform container
Definition: UIDragPanel.cs:17
Vector2 originalLocalPointerPosition
Definition: UIDragPanel.cs:9
void OnPointerDown(PointerEventData data)
Definition: UIDragPanel.cs:53