Tanoda
MouseDragBehaviour.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEditor;
5using UnityEngine;
7using UnityEngine.UI;
9
10public class MouseDragBehaviour : MonoBehaviour, IInitializePotentialDragHandler, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler,
11 IPointerClickHandler, IPointerUpHandler
12{
13 //Draggable Component
14 public event Action<PointerEventData> OnBeginDragHandler;
15 public event Action<PointerEventData> OnDragHandler;
16 public event Action<PointerEventData, bool> OnEndDragHandler;
17 public bool FollowCursor { get; set; } = true;
18 public Vector3 StartPosition;
19 public bool CanDrag { get; set; } = true;
20 bool succes = false;
21 private RectTransform rectTransform;
22 private Canvas canvas;
23 GameObject collector;
24 Transform OriginalParent;
25 DropArea dropArea = null;
26 DropArea prevDropArea = null;
27 float width;
28 float height;
29 RectTransform rt;
30 GameObject ActionSlot;
31 GameObject droppedItem;
32 Reserved reserved;
33 Reserved reservedGO;
34 String action;
35 GameObject clone;
36 ActionObject[] ActionSlotChildren;
37 bool containPosition = false;
38 GameObject PreviousGO;
39 string[] containAction;
40 //Draggable Component
41 private Vector2 lastMousePosition;
42 private bool dragged;
43 List<GameObject> inputsList = new List<GameObject>();
45 List<String> line = new List<String>();
46 GameObject lineObject;
47 List<String> line2 = new List<String>();
48 GameObject lineObject2;
50 ReferenceSaver prevrs;
51 GameObject content;
52
57
58 //Draggable Component
59 private void Awake()
60 {
61 rectTransform = GetComponent<RectTransform>();
62 canvas = GetComponentInParent<Canvas>();
63 content = GameObject.Find("Content");
64 }
65 public void Start()
66 {
67 OriginalParent = content.transform;
68 action = gameObject.GetComponent<ActionObject>().action;
69 containAction = new string[] { "Waiter", "GameObject", "Weld", "Led", "Start",
70 "Sound", "Move","Position", "BeltSetter", "ShowHide", "Deactivate", "Script"};
71
72 //inputsList = new List<GameObject>();
73 Instance = this;
74 }
75 //Draggable Component
76
77 public void OnBeginDrag(PointerEventData eventData)
78 {
79 dragged = true;
80 lastMousePosition = eventData.position;
81
82 //Draggable Component
83 if (!CanDrag)
84 {
85 return;
86 }
87 var results = new List<RaycastResult>();
88 EventSystem.current.RaycastAll(eventData, results);
89
90 if (gameObject.transform.parent.name.Contains("Content"))
91 {
92 prevDropArea = null;
93 }
94 else
95 {
96 foreach (var result in results)
97 {
98 prevDropArea = result.gameObject.GetComponent<DropArea>();
99 dropArea = result.gameObject.GetComponent<DropArea>();
100 if (prevDropArea != null)
101 {
102 succes = true;
103 break;
104 }
105 }
106 }
107
108
109 if (dropArea != null)
110 {
111 collector = dropArea.GetComponentInParent<CollectorAction>().gameObject;
112 rt = (RectTransform)collector.transform;
113 width = rt.rect.width;
114 height = rt.rect.height;
115 ActionSlot = GameObject.Find("ActionSlot");
116 if (OriginalParent != null)
117 {
118 gameObject.transform.parent = OriginalParent;
119 }
120
121 if (GameObjectNeeded())
122 {
123 rs = dropArea.transform.parent.GetComponent<ReferenceSaver>();
124 rs.Reference.transform.localScale = new Vector3(0, 0, 0);
125 ActionSlotChildren = ActionSlot.GetComponentsInChildren<ActionObject>();
126 rs.transform.Find("ExtendGO").gameObject.SetActive(false);
127 foreach (var a in ActionSlotChildren)
128 {
129 if (a.action.Contains("Position") ||
130 a.action.Contains("Hold") ||
131 a.action.Contains("Move"))
132 {
133 containPosition = true;
134 break;
135 }
136 }
137 if (!containPosition)
138 {
139 rt.sizeDelta = new Vector2(width = 520, height);
140 }
141 }
142 if (action.Contains("GameObject"))
143 {
144 RowCircle(dropArea);
145 }
146 //if (!action.Contains("GameObject"))
147 //{
148 // inputs.Remove(gameObject);
149 //}
150 }
151
152 OnBeginDragHandler?.Invoke(eventData);
153 //Draggable Component
154 }
155
160 public void OnDrag(PointerEventData eventData)
161 {
162 var currentMousePosition = eventData.position;
163 var diff = currentMousePosition - lastMousePosition;
164 var rect = GetComponent<RectTransform>();
165
166 var newPosition = rect.position + new Vector3(diff.x, diff.y, transform.position.z);
167 var oldPos = rect.position;
168 rect.position = newPosition;
169 //if (!IsRectTransformInsideSreen(rect))
170 //{
171 // rect.position = oldPos;
172 //}
173 try
174 {
175 if (prevDropArea == null)
176 {
177 LineDrawer.Instance.ReDraw(gameObject.name);
178 }
179
180 }
181 catch (Exception e)
182 {
183 Console.WriteLine(e);
184 }
185
186
187 lastMousePosition = currentMousePosition;
188 }
189
194 public void OnEndDrag(PointerEventData eventData)
195 {
196
197 if (!CanDrag)
198 {
199 return;
200 }
201
202 var results = new List<RaycastResult>();
203 EventSystem.current.RaycastAll(eventData, results);
204
205 foreach (var result in results)
206 {
207 dropArea = result.gameObject.GetComponent<DropArea>();
208 droppedItem = result.gameObject;
209
210 if (dropArea != null)
211 {
212 succes = true;
213 break;
214 }
215 }
216
217 if (dropArea != null && succes)
218 {
219 if (dropArea.Accepts(this) && !droppedItem.GetComponent<Reserved>())
220 {
221 dropArea.Drop(this);
222 OnEndDragHandler?.Invoke(eventData, true);
223 OriginalParent = gameObject.transform.parent;
224 ReParent();
225 droppedItem.AddComponent<Reserved>();
226 reserved = droppedItem.GetComponent<Reserved>();
227 collector = dropArea.GetComponentInParent<CollectorAction>().gameObject;
228 rs = dropArea.transform.parent.GetComponent<ReferenceSaver>();
229 if (GameObjectNeeded())
230 {
232 rs.transform.Find("ExtendGO").gameObject.SetActive(true);
233 }
234 if (!GameObjectNeeded())
235 {
236 StartCoroutine(makeConnection(this.gameObject, collector));
237 StartCoroutine(getConnection(this.gameObject, false, true));
238 }
239 if (action.Contains("GameObject"))
240 {
241 FullCircle(dropArea);
242 }
243 return;
244 }
245 if (dropArea.Accepts(this) && droppedItem.GetComponent<Reserved>())
246 {
247 foreach (var c in containAction)
248 {
249 if (dropArea.transform.parent.gameObject.GetComponentInChildren<ActionObject>().action.Contains(c))
250 {
251 PreviousGO = dropArea.transform.parent.gameObject.GetComponentInChildren<ActionObject>().gameObject;
252 break;
253 }
254 }
255
256 if (!prevDropArea && !action.Contains("Collector"))
257 {
258 collector = dropArea.GetComponentInParent<CollectorAction>().gameObject;
259 var Acopy = dropArea.GetComponentInParent<CollectorAction>().addItems._AddButton();
260 DropArea newArea = Acopy.transform.Find("ASlot").GetComponent<DropArea>();
261 newArea.Drop(this);
262 OnEndDragHandler?.Invoke(eventData, true);
263 gameObject.transform.parent = newArea.transform.parent;
264 newArea.gameObject.AddComponent<Reserved>();
265 if (GameObjectNeeded())
266 {
267 rs = newArea.transform.parent.GetComponent<ReferenceSaver>();
268 rs.transform.Find("ExtendGO").gameObject.SetActive(true);
270 }
271 if (!GameObjectNeeded())
272 {
273 StartCoroutine(makeConnection(this.gameObject, collector));
274 StartCoroutine(getConnection(this.gameObject, false, true));
275 }
276 }
277 if (prevDropArea)
278 {
279 dropArea.Drop(this);
280 MouseDragBehaviour prevDraggable = PreviousGO.GetComponent<MouseDragBehaviour>();
281 prevDropArea.Drop(prevDraggable);
282 PreviousGO.transform.parent = prevDropArea.transform.parent;
283 OnEndDragHandler?.Invoke(eventData, true);
284 OriginalParent = gameObject.transform.parent;
285 ReParent();
286 line = Controller.Instance.GetOutputConnectionsToObject(prevDraggable.name);
287 lineObject = dropArea.transform.parent.transform.Find(line[0]).gameObject;
288 lineObject.transform.parent = prevDropArea.transform.parent;
290 lineObject2 = prevDropArea.transform.parent.transform.Find(line2[0]).gameObject;
291 lineObject2.transform.parent = dropArea.transform.parent;
292 if (!prevDropArea.gameObject.GetComponent<Reserved>())
293 {
294 prevDropArea.gameObject.AddComponent<Reserved>();
295 }
296
297 rs = dropArea.transform.parent.GetComponent<ReferenceSaver>();
298 prevrs = prevDropArea.transform.parent.GetComponent<ReferenceSaver>();
299 DropArea GoDropArea = rs.Reference.transform.Find("GOSlot").GetComponent<DropArea>();
300 DropArea PrevGoDropArea = prevrs.Reference.transform.Find("GOSlot").GetComponent<DropArea>();
301 MouseDragBehaviour GODraggable = null;
302 MouseDragBehaviour GOprevDraggable = null;
303 if (rs.Reference.GetComponentInChildren<ActionObject>() && rs.Reference.GetComponentInChildren<ActionObject>().action.Contains("GameObject"))
304 {
305 GODraggable = rs.Reference.GetComponentInChildren<MouseDragBehaviour>();
306 }
307
308 if (prevrs.Reference.GetComponentInChildren<ActionObject>() && prevrs.Reference.GetComponentInChildren<ActionObject>().action.Contains("GameObject"))
309 {
310 GOprevDraggable = prevrs.Reference.GetComponentInChildren<MouseDragBehaviour>();
311
312 }
313 RowCircle(GoDropArea);
314 RowCircle(PrevGoDropArea);
315 if (GODraggable)
316 {
317 PrevGoDropArea.Drop(GODraggable);
318 GODraggable.transform.parent = PrevGoDropArea.transform.parent;
319 GODraggable.transform.localScale = new Vector3(1, 1, 1);
320 FullCircle(PrevGoDropArea);
321 line = Controller.Instance.GetOutputConnectionsToObject(GODraggable.name);
322 lineObject = GoDropArea.transform.parent.transform.Find(line[0]).gameObject;
323 lineObject.transform.parent = PrevGoDropArea.transform.parent;
324 }
325
326 if (GOprevDraggable)
327 {
328 GoDropArea.Drop(GOprevDraggable);
329 GOprevDraggable.transform.parent = GoDropArea.transform.parent;
330 GOprevDraggable.transform.localScale = new Vector3(1, 1, 1);
331 FullCircle(GoDropArea);
332 line = Controller.Instance.GetOutputConnectionsToObject(GOprevDraggable.name);
333 lineObject = PrevGoDropArea.transform.parent.transform.Find(line[0]).gameObject;
334 lineObject.transform.parent = GoDropArea.transform.parent;
335 }
336
337 rs.transform.Find("ExtendGO").gameObject.SetActive(false);
338 prevrs.transform.Find("ExtendGO").gameObject.SetActive(false);
339
340 if (GameObjectNeeded())
341 {
342 rs.transform.Find("ExtendGO").gameObject.SetActive(true);
343 }
344 if (PreviousGO.GetComponent<ActionObject>().action.Contains("Position") ||
345 PreviousGO.GetComponent<ActionObject>().action.Contains("Hold") ||
346 PreviousGO.GetComponent<ActionObject>().action.Contains("Move"))
347 {
348 prevrs.transform.Find("ExtendGO").gameObject.SetActive(true);
349 }
350 }
351 }
352 }
353
354 if (!droppedItem.GetComponent<Reserved>())
355 {
356 OnEndDragHandler?.Invoke(eventData, false);
357 }
358 if (dropArea == null && prevDropArea != null)
359 {
360 reserved = prevDropArea.GetComponent<Reserved>();
361 reservedGO = prevDropArea.transform.parent.GetComponent<ReferenceSaver>().Reference.GetComponentInChildren<Reserved>();
362 Destroy(reserved);
363 Destroy(reservedGO);
364 if (rs)
365 {
366 Controller.Instance.RemoveConnection(rs.GetComponentInChildren<UILineRenderer>().name);
367 if (rs.Reference.GetComponentInChildren<ActionObject>())
368 {
369 Controller.Instance.RemoveAction(rs.Reference.GetComponentInChildren<ActionObject>().name);
370 Controller.Instance.RemoveConnection(rs.Reference.GetComponentInChildren<UILineRenderer>().name);
371 }
372 }
373 else
374 {
375 return;
376 }
377
378 }
379 }
380 public void OnInitializePotentialDrag(PointerEventData eventData)
381 {
382 //StartPosition = rectTransform.localPosition;
383 }
384
390 private bool IsRectTransformInsideSreen(RectTransform rectTransform)
391 {
392 var isInside = false;
393 var corners = new Vector3[4];
394 rectTransform.GetWorldCorners(corners);
395 var visibleCorners = 0;
396 var rect = new Rect(0, 0, Screen.width, Screen.height);
397 foreach (var corner in corners)
398 if (rect.Contains(corner))
399 visibleCorners++;
400 if (visibleCorners == 4) isInside = true;
401 return isInside;
402 }
403
404 public void OnPointerClick(PointerEventData eventData)
405 {
406 if (dragged)
407 {
408 dragged = false;
409 return;
410 }
411
413 var allNode = FindObjectsOfType<MouseDragBehaviour>();
414 foreach (var node in allNode) Destroy(node.GetComponent<Outline>());
415
416 gameObject.AddComponent<Outline>();
417 }
418
419 public void OnPointerDown(PointerEventData eventData)
420 {
421 }
422
423
424 public void OnPointerUp(PointerEventData eventData)
425 {
426 }
427
428
429 public void RowCircle(DropArea dropArea)
430 {
431 Sprite RowCircle = Resources.Load<Sprite>("T_ReticleOpen");
432 ReferenceSaver rsgo = dropArea.transform.parent.GetComponent<ReferenceSaver>();
433 rsgo.Reference.transform.Find("ExtendGO").transform.Find("ExtendButton").gameObject.GetComponent<Image>().sprite = RowCircle;
434 }
435 public void FullCircle(DropArea dropArea)
436 {
437 Sprite FullCircle = Resources.Load<Sprite>("T_ReticleClosed");
438 ReferenceSaver rsgo = dropArea.transform.parent.GetComponent<ReferenceSaver>();
439 rsgo.Reference.transform.Find("ExtendGO").transform.Find("ExtendButton").gameObject.GetComponent<Image>().sprite = FullCircle;
440 }
441 public void ReParent()
442 {
443 var hits = GetEventSystemRaycastResults();
444 var good = false;
445 foreach (var hit in hits)
446 {
447 if (hit.gameObject == gameObject)
448 {
449 good = true;
450 break;
451 }
452 }
453 if (action.Contains("GameObject"))
454 {
455 gameObject.transform.parent = droppedItem.transform.parent;
456 }
457 else
458 {
459 gameObject.transform.parent = droppedItem.transform.parent;
460 }
461 }
462
463 static List<RaycastResult> GetEventSystemRaycastResults()
464 {
465 PointerEventData eventData = new PointerEventData(EventSystem.current) { position = Input.mousePosition };
466 List<RaycastResult> raysastResults = new List<RaycastResult>();
467 EventSystem.current.RaycastAll(eventData, raysastResults);
468 return raysastResults;
469 }
470
471 public List<GameObject> InputList()
472 {
473 return inputsList;
474 }
475 public GameObject addedGameObject()
476 {
477 return gameObject;
478 }
479
480 IEnumerator makeConnection(GameObject StartGameObject, GameObject EndGameObject)
481 {
482 yield return null;
483 Controller.Instance.StartConnection(StartGameObject.GetComponent<ActionObject>().outPuts[0].name, StartGameObject.name);
484 Controller.Instance.EndConnection(EndGameObject.GetComponent<ActionObject>().inPuts[0].name, EndGameObject.name);
485 }
486
487 IEnumerator getConnection(GameObject ConnectedGO, bool visible, bool parent)
488 {
489 yield return null;
490 line = Controller.Instance.GetOutputConnectionsToObject(ConnectedGO.name);
491 lineObject = OriginalParent.Find(line[0]).gameObject;
492
493 if (parent)
494 {
495 lineObject.transform.parent = ConnectedGO.transform.parent;
496 }
497 else
498 {
499 lineObject.transform.parent = collector.transform;
500 }
501 if (visible)
502 {
503 lineObject.transform.localScale = new Vector3(1, 1, 1);
504 }
505 else
506 {
507 lineObject.GetComponent<UILineRenderer>().enabled = false;
508 }
509 lineObject.transform.localScale = new Vector3(1, 1, 1);
510 }
511 public void refreshConnectionPos(GameObject ConnectedGO)
512 {
513 var diff = new Vector2(0,0);
514 var rect = ConnectedGO.GetComponent<RectTransform>();
515 var newPosition = rect.position + new Vector3(diff.x, diff.y, transform.position.z);
516 var oldPos = rect.position;
517 rect.position = newPosition;
518
519 try
520 {
521 LineDrawer.Instance.ReDraw(ConnectedGO.name);
522 }
523 catch (Exception e)
524 {
525 Console.WriteLine(e);
526 }
527 }
528 private bool GameObjectNeeded()
529 {
530 return action.Contains("Move") || action.Contains("Position") || action.Contains("Hold");
531 }
532 public bool IsConnectionHidden()
533 {
534 line = Controller.Instance.GetOutputConnectionsToObject(gameObject.name);
535 lineObject = OriginalParent.Find(line[0]).gameObject;
536 line2 = Controller.Instance.GetInputConnectionsToObject(gameObject.name);
537 lineObject2 = OriginalParent.Find(line[0]).gameObject;
538 return lineObject.GetComponent<UILineRenderer>().enabled;
539 }
540
541 public void AddGameObject()
542 {
543
545 if (!clone)
546 {
547 return;
548 }
549 MouseDragBehaviour NewGODraggable = clone.GetComponent<MouseDragBehaviour>();
550 DropArea NewGoDropArea = rs.Reference.transform.Find("GOSlot").GetComponent<DropArea>();
551 NewGoDropArea.Drop(NewGODraggable);
552 clone.transform.parent = NewGoDropArea.transform.parent;
553 clone.transform.localPosition = new Vector3(0, 0, 0);
554 clone.transform.localScale = new Vector3(1, 1, 1);
555 FullCircle(NewGoDropArea);
556 StartCoroutine(makeConnection(clone, this.gameObject));
557 StartCoroutine(getConnection(clone, true, true));
558 //if (Controller.Instance.GetOutputConnectionsToObject(this.gameObject.name).Count == 0)
559 //{
560 StartCoroutine(makeConnection(this.gameObject, collector));
561 StartCoroutine(getConnection(this.gameObject, false, true));
562 refreshConnectionPos(this.gameObject);
563 //}
564 }
565
566}
System.Drawing.Image Image
Definition: TestScript.cs:37
string action
Definition: ActionObject.cs:32
List< GameObject > inPuts
Definition: ActionObject.cs:18
List< GameObject > outPuts
Definition: ActionObject.cs:19
List< string > GetOutputConnectionsToObject(string id)
Definition: Controller.cs:899
void EndConnection(string inputID, string actionID)
Definition: Controller.cs:760
void RemoveConnection(string connectionId)
Definition: Controller.cs:836
GameObject AddAction(GameObject obj, string id)
Definition: Controller.cs:545
List< string > GetInputConnectionsToObject(string id)
Definition: Controller.cs:888
List< GameObject > actionPrefabs
Definition: Controller.cs:26
static Controller Instance
Definition: Controller.cs:16
void StartConnection(string outputID, string actionID)
Definition: Controller.cs:753
void RemoveAction(string id)
Definition: Controller.cs:727
void ClickedOn(string id)
Definition: Controller.cs:975
void Drop(MouseDragBehaviour draggable)
Definition: DropArea.cs:15
bool Accepts(MouseDragBehaviour draggable)
Definition: DropArea.cs:10
static LineDrawer Instance
Definition: LineDrawer.cs:16
void ReDraw(string id)
Definition: LineDrawer.cs:235
Action< PointerEventData > OnBeginDragHandler
void OnPointerUp(PointerEventData eventData)
void RowCircle(DropArea dropArea)
void OnBeginDrag(PointerEventData eventData)
void OnEndDrag(PointerEventData eventData)
This method will be called at the end of mouse drag
void OnInitializePotentialDrag(PointerEventData eventData)
Action< PointerEventData > OnDragHandler
void FullCircle(DropArea dropArea)
GameObject addedGameObject()
static MouseDragBehaviour Instance
void OnDrag(PointerEventData eventData)
This method will be called during the mouse drag
List< GameObject > InputList()
Action< PointerEventData, bool > OnEndDragHandler
void OnPointerDown(PointerEventData eventData)
void refreshConnectionPos(GameObject ConnectedGO)
void OnPointerClick(PointerEventData eventData)
GameObject Reference
Credit Erdener Gonenc - @PixelEnvision.