A game about forced loneliness, made by TACStudios
1using System.Collections;
2using System;
3using UnityEngine;
4using UnityEngine.UIElements;
5
6namespace UnityEditor.Rendering.LookDev
7{
8 class DropArea
9 {
10 readonly Type[] k_AcceptedTypes;
11 bool droppable;
12
13 public DropArea(Type[] acceptedTypes, VisualElement area, Action<UnityEngine.Object, Vector2> OnDrop)
14 {
15 k_AcceptedTypes = acceptedTypes;
16 area.RegisterCallback<DragPerformEvent>(evt => Drop(evt, OnDrop));
17 area.RegisterCallback<DragEnterEvent>(evt => DragEnter(evt));
18 area.RegisterCallback<DragLeaveEvent>(evt => DragLeave(evt));
19 area.RegisterCallback<DragExitedEvent>(evt => DragExit(evt));
20 area.RegisterCallback<DragUpdatedEvent>(evt => DragUpdate(evt));
21 }
22
23 void DragEnter(DragEnterEvent evt)
24 {
25 droppable = false;
26 foreach (UnityEngine.Object obj in DragAndDrop.objectReferences)
27 {
28 if (!IsInAcceptedTypes(obj.GetType()))
29 continue;
30
31 droppable = true;
32 evt.StopPropagation();
33 return;
34 }
35 }
36
37 void DragLeave(DragLeaveEvent evt)
38 {
39 foreach (UnityEngine.Object obj in DragAndDrop.objectReferences)
40 {
41 if (!IsInAcceptedTypes(obj.GetType()))
42 continue;
43
44 DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
45 evt.StopPropagation();
46 return;
47 }
48 }
49
50 void DragExit(DragExitedEvent evt)
51 {
52 foreach (UnityEngine.Object obj in DragAndDrop.objectReferences)
53 {
54 if (!IsInAcceptedTypes(obj.GetType()))
55 continue;
56
57 DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
58 evt.StopPropagation();
59 return;
60 }
61 }
62
63 void DragUpdate(DragUpdatedEvent evt)
64 {
65 foreach (UnityEngine.Object obj in DragAndDrop.objectReferences)
66 {
67 if (!IsInAcceptedTypes(obj.GetType()))
68 continue;
69
70 DragAndDrop.visualMode = droppable ? DragAndDropVisualMode.Link : DragAndDropVisualMode.Rejected;
71 evt.StopPropagation();
72 }
73 }
74
75 void Drop(DragPerformEvent evt, Action<UnityEngine.Object, Vector2> OnDrop)
76 {
77 bool atLeastOneAccepted = false;
78 foreach (UnityEngine.Object obj in DragAndDrop.objectReferences)
79 {
80 if (!IsInAcceptedTypes(obj.GetType()))
81 continue;
82
83 OnDrop.Invoke(obj, evt.localMousePosition);
84 atLeastOneAccepted = true;
85 }
86 if (atLeastOneAccepted)
87 {
88 DragAndDrop.AcceptDrag();
89 evt.StopPropagation();
90 }
91 }
92
93 bool IsInAcceptedTypes(Type testedType)
94 {
95 foreach (Type type in k_AcceptedTypes)
96 {
97 if (testedType.IsAssignableFrom(type))
98 return true;
99 }
100 return false;
101 }
102 }
103}