| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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;
- 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<QuestionCard>();
- if (questionCardBefore == null) {
- if (cardBefore - 1 >= 0) {
- questionCardBefore = placeholderParent.GetChild(cardBefore - 1).GetComponent<QuestionCard>();
- }
- }
- if (questionCardBefore != null) {
- answerTextBefore = questionCardBefore.GetAnswerText().text;
- } else {
- answerTextBefore = "Before";
- }
- }
- if (cardAfter >= placeholderParent.childCount) {
- answerTextAfter = "After";
- } else {
- questionCardAfter = placeholderParent.GetChild(cardAfter).GetComponent<QuestionCard>();
- answerTextAfter += questionCardAfter.GetAnswerText().text;
- }
- if (answerTextAfter.Equals("After")) {
- this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextAfter + " " + answerTextBefore;
- } else if (answerTextBefore.Equals("Before")) {
- this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " " + answerTextAfter;
- } else {
- this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " - " + answerTextAfter;
- }
- }
- placeholder.transform.SetSiblingIndex(newSiblingIndex);
- }
- public void OnBeginDrag(PointerEventData eventData) {
- placeholder = new GameObject();
- placeholder.transform.SetParent(this.transform.parent);
- LayoutElement le = placeholder.AddComponent<LayoutElement>();
- le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
- le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
- le.flexibleHeight = this.GetComponent<LayoutElement>().flexibleHeight;
- le.flexibleWidth = this.GetComponent<LayoutElement>().flexibleWidth;
- le.minHeight = this.GetComponent<LayoutElement>().minHeight;
- le.minWidth = this.GetComponent<LayoutElement>().minWidth;
- placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
- parent = this.transform.parent;
- placeholderParent = parent;
- this.transform.SetParent(this.transform.parent);
- GetComponent<CanvasGroup>().blocksRaycasts = false;
- }
- public void OnEndDrag(PointerEventData eventData) {
- this.transform.SetParent(parent);
- this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
- GetComponent<CanvasGroup>().blocksRaycasts = true;
- Destroy(placeholder);
- }
- }
|