Draggable.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. string before = LocalizationManager.Instance.GetText("BEFORE");
  32. string after = LocalizationManager.Instance.GetText("AFTER");
  33. if (placeholder.transform.GetSiblingIndex() == cardAfter) {
  34. cardAfter++;
  35. }
  36. QuestionCard questionCardBefore = null;
  37. QuestionCard questionCardAfter = null;
  38. string answerTextBefore = "";
  39. string answerTextAfter = "";
  40. if (cardBefore < 0) {
  41. answerTextBefore = before;
  42. } else {
  43. questionCardBefore = placeholderParent.GetChild(cardBefore).GetComponent<QuestionCard>();
  44. if (questionCardBefore == null) {
  45. if (cardBefore - 1 >= 0) {
  46. questionCardBefore = placeholderParent.GetChild(cardBefore - 1).GetComponent<QuestionCard>();
  47. }
  48. }
  49. if (questionCardBefore != null) {
  50. answerTextBefore = questionCardBefore.GetAnswerText().text;
  51. } else {
  52. answerTextBefore = before;
  53. }
  54. }
  55. if (cardAfter >= placeholderParent.childCount) {
  56. answerTextAfter = after;
  57. } else {
  58. questionCardAfter = placeholderParent.GetChild(cardAfter).GetComponent<QuestionCard>();
  59. answerTextAfter += questionCardAfter.GetAnswerText().text;
  60. }
  61. if (answerTextAfter.Equals(after)) {
  62. this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextAfter + " " + answerTextBefore;
  63. } else if (answerTextBefore.Equals(before)) {
  64. this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " " + answerTextAfter;
  65. } else {
  66. this.GetComponent<NewQuestion>().GetAnswerText().text = answerTextBefore + " - " + answerTextAfter;
  67. }
  68. }
  69. placeholder.transform.SetSiblingIndex(newSiblingIndex);
  70. }
  71. public void OnBeginDrag(PointerEventData eventData) {
  72. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  73. d.GetComponent<CanvasGroup>().alpha = 0.8f;
  74. placeholder = new GameObject();
  75. placeholder.transform.SetParent(this.transform.parent);
  76. LayoutElement le = placeholder.AddComponent<LayoutElement>();
  77. le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
  78. le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
  79. le.flexibleHeight = this.GetComponent<LayoutElement>().flexibleHeight;
  80. le.flexibleWidth = this.GetComponent<LayoutElement>().flexibleWidth;
  81. le.minHeight = this.GetComponent<LayoutElement>().minHeight;
  82. le.minWidth = this.GetComponent<LayoutElement>().minWidth;
  83. placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
  84. parent = this.transform.parent;
  85. placeholderParent = parent;
  86. this.transform.SetParent(this.transform.parent);
  87. GetComponent<CanvasGroup>().blocksRaycasts = false;
  88. }
  89. public void OnEndDrag(PointerEventData eventData) {
  90. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  91. d.GetComponent<CanvasGroup>().alpha = 1f;
  92. this.transform.SetParent(parent);
  93. this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
  94. GetComponent<CanvasGroup>().blocksRaycasts = true;
  95. Destroy(placeholder);
  96. }
  97. }