forked from Facepunch/Rust.Community
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunityEntity.UI.Slot.cs
More file actions
132 lines (97 loc) · 3.97 KB
/
CommunityEntity.UI.Slot.cs
File metadata and controls
132 lines (97 loc) · 3.97 KB
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
using Object = UnityEngine.Object;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Facepunch;
using Facepunch.Extend;
using System.IO;
#if CLIENT
public partial class CommunityEntity
{
public class DraggableSlot : UIBehaviour, IDropHandler
{
#region Config
// the filter tag for the slot. if both Draggable & slot have a filter tag that dont match the slot is denied
public string filter = null;
#endregion
#region Values
public Draggable content;
public Transform canvas;
private bool _initialized;
#endregion
#region Core
// call to initialize the Draggable, marking it as ready
public void Init()
{
content = GetComponentInChildren<Draggable>();
canvas = GetComponentInParent<Canvas>().transform;
_initialized = true;
}
// to support swapping
public void OnDrop(PointerEventData eventData)
{
if (!_initialized)
return;
var draggedObj = eventData.pointerDrag.GetComponent<Draggable>();
if (draggedObj.ShouldDie())
return;
if (draggedObj == content)
return;
// prevent sending the DragRPC regardless. because the player intended to attach it, not drag it.
if (!draggedObj.dropAnywhere)
draggedObj.wasSnapped = true;
// if the dragging object is on seperate canvases, dont parent
if (draggedObj.canvas != canvas)
return;
if (!FitsIntoSlot(draggedObj, this))
return;
// cant swap because the draggable's position is too far away
if (draggedObj.scaledMaxDistance > 0f && Vector2.Distance(draggedObj.lastDropPosition, transform.position) > draggedObj.scaledMaxDistance)
return;
// if swapping would violate the draggable's constraints
if (draggedObj.limitToParent && !draggedObj.parentWorldRect.Contains(eventData.pointerCurrentRaycast.screenPosition))
return;
if (content != null)
{
// cant swap because the draggable's position is too far away from the current content's anchor
if (content.scaledMaxDistance > 0f && Vector2.Distance(draggedObj.lastDropPosition, content.anchor) > content.scaledMaxDistance)
return;
// incase a resize occured
content.TryRefreshParentBounds();
// if swapping would violate the current content's constraints
if (content.limitToParent && !content.parentWorldRect.Contains(draggedObj.lastDropPosition))
return;
if (draggedObj.slot != null && !FitsIntoSlot(content, draggedObj.slot))
return;
Draggable.Swap(draggedObj, content);
return;
}
if (draggedObj.slot != null)
draggedObj.slot.content = null;
content = draggedObj;
draggedObj.slot = this;
draggedObj.realParent = this.transform;
draggedObj.lastDropPosition = transform.position;
if (draggedObj.dropAnywhere)
draggedObj.rt.position = this.transform.position;
draggedObj.offset = draggedObj.lastDropPosition - draggedObj.anchor;
draggedObj.wasSnapped = true;
Draggable.SendDropRPC(draggedObj.gameObject.name, draggedObj.slot?.gameObject.name, null, null);
}
#endregion
#region Helpers
// checks filters
public static bool FitsIntoSlot(Draggable drag, DraggableSlot slot)
{
if (slot.filter == null)
return true;
return drag.filter == slot.filter;
}
#endregion
}
}
#endif