using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public Transform parent = null; public Transform placeholderParent = null; public GameObject placeholder = null; public void OnDrag(PointerEventData eventData) { if (!eventData.pointerDrag.gameObject.name.Contains("NewQuestion")) { return; } this.transform.position = eventData.position; if (placeholder.transform.parent != placeholderParent) { placeholder.transform.SetParent(placeholderParent); } int newSiblingIndex = placeholderParent.childCount; for (int i = 0; i < placeholderParent.childCount; i++) { if (this.transform.position.x < placeholderParent.GetChild(i).position.x) { newSiblingIndex = i; if (placeholder.transform.GetSiblingIndex() < newSiblingIndex) { newSiblingIndex--; } break; } } if (placeholderParent.gameObject.name.Contains("AnswerLine")) { int cardBefore = newSiblingIndex - 1; int cardAfter = newSiblingIndex + 1; string before = LocalizationManager.Instance.GetText("BEFORE"); string after = LocalizationManager.Instance.GetText("AFTER"); if (placeholder.transform.GetSiblingIndex() == cardAfter) { cardAfter++; } QuestionCard questionCardBefore = null; QuestionCard questionCardAfter = null; string answerTextBefore = ""; string answerTextAfter = ""; if (cardBefore < 0) { answerTextBefore = before; } else { questionCardBefore = placeholderParent.GetChild(cardBefore).GetComponent(); if (questionCardBefore == null) { if (cardBefore - 1 >= 0) { questionCardBefore = placeholderParent.GetChild(cardBefore - 1).GetComponent(); } } if (questionCardBefore != null) { answerTextBefore = questionCardBefore.GetAnswerText().text; } else { answerTextBefore = before; } } if (cardAfter >= placeholderParent.childCount) { answerTextAfter = after; } else { questionCardAfter = placeholderParent.GetChild(cardAfter).GetComponent(); answerTextAfter += questionCardAfter.GetAnswerText().text; } if (answerTextAfter.Equals(after)) { this.GetComponent().GetAnswerText().text = answerTextAfter + " " + answerTextBefore; } else if (answerTextBefore.Equals(before)) { this.GetComponent().GetAnswerText().text = answerTextBefore + " " + answerTextAfter; } else { this.GetComponent().GetAnswerText().text = answerTextBefore + " - " + answerTextAfter; } } placeholder.transform.SetSiblingIndex(newSiblingIndex); } public void OnBeginDrag(PointerEventData eventData) { Draggable d = eventData.pointerDrag.GetComponent(); d.GetComponent().alpha = 0.8f; placeholder = new GameObject(); placeholder.transform.SetParent(this.transform.parent); LayoutElement le = placeholder.AddComponent(); le.preferredHeight = this.GetComponent().preferredHeight; le.preferredWidth = this.GetComponent().preferredWidth; le.flexibleHeight = this.GetComponent().flexibleHeight; le.flexibleWidth = this.GetComponent().flexibleWidth; le.minHeight = this.GetComponent().minHeight; le.minWidth = this.GetComponent().minWidth; placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex()); parent = this.transform.parent; placeholderParent = parent; this.transform.SetParent(this.transform.parent); GetComponent().blocksRaycasts = false; } public void OnEndDrag(PointerEventData eventData) { Draggable d = eventData.pointerDrag.GetComponent(); d.GetComponent().alpha = 1f; this.transform.SetParent(parent); this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex()); GetComponent().blocksRaycasts = true; Destroy(placeholder); } }