Tanoda
DragAndDropRawCell.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
4using System.Collections;
5using static DragAndDropCell;
6
10[RequireComponent(typeof(RawImage))]
11public class DragAndDropRawCell : MonoBehaviour, IDropHandler
12{
13 [Tooltip("Functional type of this cell")]
14 public CellType cellType = CellType.Swap; // Special type of this cell
15 [Tooltip("Sprite color for empty cell")]
16 public Color empty = new Color(); // Sprite color for empty cell
17 [Tooltip("Sprite color for filled cell")]
18 public Color full = new Color(); // Sprite color for filled cell
19 [Tooltip("This cell has unlimited amount of items")]
20 public bool unlimitedSource = false; // Item from this cell will be cloned on drag start
21
22 public bool ignoreColor = false;
23
24 internal DragAndDropItem myDadItem; // Item of this DaD cell
25
26 void OnEnable()
27 {
28 DragAndDropItem.OnItemDragStartEvent += OnAnyItemDragStart; // Handle any item drag start
29 DragAndDropItem.OnItemDragEndEvent += OnAnyItemDragEnd; // Handle any item drag end
31 StartCoroutine(LateOnEnabled());
32 }
33
34 IEnumerator LateOnEnabled()
35 {
36 yield return new WaitForEndOfFrame();
38 }
39
40 void OnDisable()
41 {
42 DragAndDropItem.OnItemDragStartEvent -= OnAnyItemDragStart;
43 DragAndDropItem.OnItemDragEndEvent -= OnAnyItemDragEnd;
44 StopAllCoroutines(); // Stop all coroutines if there is any
45 }
46
51 private void OnAnyItemDragStart(DragAndDropItem item)
52 {
54 if (myDadItem != null)
55 {
56 myDadItem.MakeRaycast(false); // Disable item's raycast for correct drop handling
57 if (myDadItem == item) // If item dragged from this cell
58 {
59 // Check cell's type
60 switch (cellType)
61 {
62 case CellType.DropOnly:
63 DragAndDropItem.icon.SetActive(false); // Item can not be dragged. Hide icon
64 break;
65 }
66 }
67 }
68 }
69
74 private void OnAnyItemDragEnd(DragAndDropItem item)
75 {
77 if (myDadItem != null)
78 {
79 myDadItem.MakeRaycast(true); // Enable item's raycast
80 }
82 }
83
88 public void OnDrop(PointerEventData data)
89 {
90 if (DragAndDropItem.icon != null)
91 {
94 if (DragAndDropItem.icon.activeSelf == true) // If icon inactive do not need to drop item into cell
95 {
96 if ((item != null) && (sourceCell != this))
97 {
98 DropEventDescriptor desc = new DropEventDescriptor();
99 switch (cellType) // Check this cell's type
100 {
101 case CellType.Swap: // Item in destination cell can be swapped
102 UpdateMyItem();
103 switch (sourceCell.cellType)
104 {
105 case CellType.Swap: // Item in source cell can be swapped
106 // Fill event descriptor
107 desc.item = item;
108 desc.sourceCell = sourceCell;
109 desc.destinationCell = this;
110 SendRequest(desc); // Send drop request
111 StartCoroutine(NotifyOnDragEnd(desc)); // Send notification after drop will be finished
112 if (desc.permission == true) // If drop permitted by application
113 {
114 if (myDadItem != null) // If destination cell has item
115 {
116 // Fill event descriptor
117 DropEventDescriptor descAutoswap = new DropEventDescriptor();
118 descAutoswap.item = myDadItem;
119 descAutoswap.sourceCell = this;
120 descAutoswap.destinationCell = sourceCell;
121 SendRequest(descAutoswap); // Send drop request
122 StartCoroutine(NotifyOnDragEnd(descAutoswap)); // Send notification after drop will be finished
123 if (descAutoswap.permission == true) // If drop permitted by application
124 {
125 SwapItems(sourceCell, this); // Swap items between cells
126 }
127 else
128 {
129 PlaceItem(item); // Delete old item and place dropped item into this cell
130 }
131 }
132 else
133 {
134 PlaceItem(item); // Place dropped item into this empty cell
135 }
136 }
137 break;
138 default: // Item in source cell can not be swapped
139 // Fill event descriptor
140 desc.item = item;
141 desc.sourceCell = sourceCell;
142 desc.destinationCell = this;
143 SendRequest(desc); // Send drop request
144 StartCoroutine(NotifyOnDragEnd(desc)); // Send notification after drop will be finished
145 if (desc.permission == true) // If drop permitted by application
146 {
147 PlaceItem(item); // Place dropped item into this cell
148 }
149 break;
150 }
151 break;
152 case CellType.DropOnly: // Item only can be dropped into destination cell
153 // Fill event descriptor
154 desc.item = item;
155 desc.sourceCell = sourceCell;
156 desc.destinationCell = this;
157 SendRequest(desc); // Send drop request
158 StartCoroutine(NotifyOnDragEnd(desc)); // Send notification after drop will be finished
159 if (desc.permission == true) // If drop permitted by application
160 {
161 PlaceItem(item); // Place dropped item in this cell
162 }
163 break;
164 default:
165 break;
166 }
167 }
168 }
169 //if (item != null)
170 //{
171 //if (item.GetComponentInParent<DragAndDropCell>() == null) // If item have no cell after drop
172 //{
173 //Destroy(item.gameObject); // Destroy it
174 //}
175 //}
176 UpdateMyItem();
178 //sourceCell.UpdateMyItem();
179 //sourceCell.UpdateBackgroundState();
180 }
181 }
182
187 private void PlaceItem(DragAndDropItem item)
188 {
189 if (item != null)
190 {
191 DestroyItem(); // Remove current item from this cell
192 myDadItem = null;
193 DragAndDropCell cell = item.GetComponentInParent<DragAndDropCell>();
194 if (cell != null)
195 {
196 if (cell.unlimitedSource == true)
197 {
198 string itemName = item.name;
199 item = Instantiate(item); // Clone item from source cell
200 item.name = itemName;
201 }
202 }
203 item.transform.SetParent(transform, false);
204 item.transform.localPosition = Vector3.zero;
205 item.MakeRaycast(true);
206 myDadItem = item;
207 }
209 }
210
214 private void DestroyItem()
215 {
216 UpdateMyItem();
217 if (myDadItem != null)
218 {
219 DropEventDescriptor desc = new DropEventDescriptor();
220 // Fill event descriptor
221 desc.triggerType = TriggerType.ItemWillBeDestroyed;
222 desc.item = myDadItem;
223 desc.sourceCell = this;
224 desc.destinationCell = this;
225 SendNotification(desc); // Notify application about item destruction
226 if (myDadItem != null)
227 {
228 Destroy(myDadItem.gameObject);
229 }
230 }
231 myDadItem = null;
233 }
234
239 private void SendNotification(DropEventDescriptor desc)
240 {
241 if (desc != null)
242 {
243 // Send message with DragAndDrop info to parents GameObjects
244 gameObject.SendMessageUpwards("OnSimpleDragAndDropEvent", desc, SendMessageOptions.DontRequireReceiver);
245 }
246 }
247
253 private bool SendRequest(DropEventDescriptor desc)
254 {
255 bool result = false;
256 if (desc != null)
257 {
258 desc.triggerType = TriggerType.DropRequest;
259 desc.permission = true;
260 SendNotification(desc);
261 result = desc.permission;
262 }
263 return result;
264 }
265
271 private IEnumerator NotifyOnDragEnd(DropEventDescriptor desc)
272 {
273 // Wait end of drag operation
274 while (DragAndDropItem.draggedItem != null)
275 {
276 yield return new WaitForEndOfFrame();
277 }
278 desc.triggerType = TriggerType.DropEventEnd;
279 SendNotification(desc);
280 }
281
287 {
288 if (ignoreColor)
289 return;
290
291 RawImage bg = GetComponent<RawImage>();
292 if (bg != null)
293 {
294 bg.color = myDadItem != null ? full : empty;
295 }
296 }
297
301 public void UpdateMyItem()
302 {
303 myDadItem = GetComponentInChildren<DragAndDropItem>();
304 }
305
311 {
312 return myDadItem;
313 }
314
319 public void AddItem(DragAndDropItem newItem)
320 {
321 if (newItem != null)
322 {
323 PlaceItem(newItem);
324 DropEventDescriptor desc = new DropEventDescriptor();
325 // Fill event descriptor
326 desc.triggerType = TriggerType.ItemAdded;
327 desc.item = newItem;
328 desc.sourceCell = this;
329 desc.destinationCell = this;
330 SendNotification(desc);
331 }
332 }
333
337 public void RemoveItem()
338 {
339 DestroyItem();
340 }
341
347 public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
348 {
349 if ((firstCell != null) && (secondCell != null))
350 {
351 DragAndDropItem firstItem = firstCell.GetItem(); // Get item from first cell
352 DragAndDropItem secondItem = secondCell.GetItem(); // Get item from second cell
353 // Swap items
354 if (firstItem != null)
355 {
356 firstItem.transform.SetParent(secondCell.transform, false);
357 firstItem.transform.localPosition = Vector3.zero;
358 firstItem.MakeRaycast(true);
359 }
360 if (secondItem != null)
361 {
362 secondItem.transform.SetParent(firstCell.transform, false);
363 secondItem.transform.localPosition = Vector3.zero;
364 secondItem.MakeRaycast(true);
365 }
366 // Update states
367 firstCell.UpdateMyItem();
368 secondCell.UpdateMyItem();
369 firstCell.UpdateBackgroundState();
370 secondCell.UpdateBackgroundState();
371 }
372 }
373}
UnityEngine.Color Color
Definition: TestScript.cs:32
Every item's cell must contain this script
void UpdateBackgroundState()
Change cell's sprite color on item put/remove.
DragAndDropItem GetItem()
Get item from this cell
void UpdateMyItem()
Updates my item
Drag and Drop item.
static DragEvent OnItemDragEndEvent
static DragAndDropCell sourceCell
static DragAndDropItem draggedItem
void MakeRaycast(bool condition)
Enable item's raycast.
static GameObject icon
static DragEvent OnItemDragStartEvent
Every item's cell must contain this script
void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
Swap items between two cells
void OnDrop(PointerEventData data)
Item is dropped in this cell
void UpdateBackgroundState()
Change cell's sprite color on item put/remove.
void RemoveItem()
Manualy delete item from this cell
void UpdateMyItem()
Updates my item
DragAndDropItem GetItem()
Get item from this cell
void AddItem(DragAndDropItem newItem)
Manualy add item into this cell