Draggable.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
  7. public Transform parent = null;
  8. public Transform placeholderParent = null;
  9. public GameObject placeholder = null;
  10. public void OnDrag(PointerEventData eventData) {
  11. if (!eventData.pointerDrag.gameObject.name.Contains("NewQuestion")) {
  12. return;
  13. }
  14. this.transform.position = eventData.position;
  15. if (placeholder.transform.parent != placeholderParent) {
  16. placeholder.transform.SetParent(placeholderParent);
  17. }
  18. int newSiblingIndex = placeholderParent.childCount;
  19. for (int i = 0; i < placeholderParent.childCount; i++) {
  20. if (this.transform.position.x < placeholderParent.GetChild(i).position.x) {
  21. newSiblingIndex = i;
  22. if (placeholder.transform.GetSiblingIndex() < newSiblingIndex) {
  23. newSiblingIndex--;
  24. }
  25. break;
  26. }
  27. }
  28. if (placeholderParent.gameObject.name.Contains("AnswerLine")) {
  29. int cardBefore = newSiblingIndex - 1;
  30. int cardAfter = newSiblingIndex + 1;
  31. if (placeholder.transform.GetSiblingIndex() == cardAfter) {
  32. cardAfter++;
  33. }
  34. QuestionCard questionCardBefore = null;
  35. QuestionCard questionCardAfter = null;
  36. string answerTextBefore = "";
  37. string answerTextAfter = "";
  38. if (cardBefore < 0) {
  39. answerTextBefore = "Before";
  40. } else {
  41. questionCardBefore = placeholderParent.GetChild(cardBefore).GetComponent<QuestionCard>();
  42. if (questionCardBefore == null) {
  43. if (cardBefore - 1 >= 0) {
  44. questionCardBefore = placeholderParent.GetChild(cardBefore - 1).GetComponent<QuestionCard>();
  45. }
  46. }
  47. if (questionCardBefore != null) {
  48. answerTextBefore = questionCardBefore.GetAnswerText().text;
  49. } else {
  50. answerTextBefore = "Before";
  51. }
  52. }
  53. if (cardAfter >= placeholderParent.childCount) {
  54. answerTextAfter = "After";
  55. } else {
  56. questionCardAfter = placeholderParent.GetChild(cardAfter).GetComponent<QuestionCard>();
  57. answerTextAfter += questionCardAfter.GetAnswerText().text;
  58. }
  59. if (answerTextAfter.Equals("After")) {
  60. this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextAfter + " " + answerTextBefore;
  61. } else if (answerTextBefore.Equals("Before")) {
  62. this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " " + answerTextAfter;
  63. } else {
  64. this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " - " + answerTextAfter;
  65. }
  66. }
  67. placeholder.transform.SetSiblingIndex(newSiblingIndex);
  68. }
  69. public void OnBeginDrag(PointerEventData eventData) {
  70. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  71. d.GetComponent<CanvasGroup>().alpha = 0.8f;
  72. placeholder = new GameObject();
  73. placeholder.transform.SetParent(this.transform.parent);
  74. LayoutElement le = placeholder.AddComponent<LayoutElement>();
  75. le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
  76. le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
  77. le.flexibleHeight = this.GetComponent<LayoutElement>().flexibleHeight;
  78. le.flexibleWidth = this.GetComponent<LayoutElement>().flexibleWidth;
  79. le.minHeight = this.GetComponent<LayoutElement>().minHeight;
  80. le.minWidth = this.GetComponent<LayoutElement>().minWidth;
  81. placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
  82. parent = this.transform.parent;
  83. placeholderParent = parent;
  84. this.transform.SetParent(this.transform.parent);
  85. GetComponent<CanvasGroup>().blocksRaycasts = false;
  86. }
  87. public void OnEndDrag(PointerEventData eventData) {
  88. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  89. d.GetComponent<CanvasGroup>().alpha = 1f;
  90. this.transform.SetParent(parent);
  91. this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
  92. GetComponent<CanvasGroup>().blocksRaycasts = true;
  93. Destroy(placeholder);
  94. }
  95. }