Explorar o código

Liten uppdatering till drag and drop

Axel Nordh %!s(int64=7) %!d(string=hai) anos
pai
achega
3ba8264ac1

+ 50 - 15
Assets/Scripts/Draggable.cs

@@ -6,36 +6,71 @@ using UnityEngine.UI;
 
 public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
 
-    public Vector3 startPosition;
     public Transform parent = null;
     public Transform placeholderParent = null;
-    GameObject placeholder = 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).transform.position.x) {
+            if (this.transform.position.x < placeholderParent.GetChild(i).position.x) {
                 newSiblingIndex = i;
-                if (parent.transform.GetSiblingIndex() < newSiblingIndex) {
+                if (placeholder.transform.GetSiblingIndex() < newSiblingIndex) {
                     newSiblingIndex--;
                 }
                 break;
             }
         }
 
-        placeholderParent.transform.SetSiblingIndex(newSiblingIndex);
+        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) {
+                    questionCardBefore = placeholderParent.GetChild(cardBefore - 1).GetComponent<QuestionCard>();
+                }
+                answerTextBefore = questionCardBefore.GetAnswerText().text;
+            }
+
+            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) {
-        startPosition = this.transform.position;
-
         placeholder = new GameObject();
         placeholder.transform.SetParent(this.transform.parent);
         LayoutElement le = placeholder.AddComponent<LayoutElement>();
@@ -50,18 +85,18 @@ public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDra
 
         parent = this.transform.parent;
         placeholderParent = parent;
+        this.transform.SetParent(this.transform.parent);
+
         GetComponent<CanvasGroup>().blocksRaycasts = false;
     }
 
     public void OnEndDrag(PointerEventData eventData) {
-        if (parent == null) {
-            this.transform.position = startPosition;
-            this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
-        } else {
-            this.transform.SetParent(parent);
-        }
+        this.transform.SetParent(parent);
+        this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
         GetComponent<CanvasGroup>().blocksRaycasts = true;
 
         Destroy(placeholder);
+        //Destroy(this);
+        // Show Modal and get yes/no confirm on placement, create new QuestionCard at newSiblingIndex.
     }
 }

+ 2 - 1
Assets/Scripts/DropZone.cs

@@ -28,9 +28,10 @@ public class DropZone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPoin
             return;
         }
 
+        /*
         Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
         if (d != null && d.placeholderParent == this.transform) {
             d.placeholderParent = d.parent;
-        }
+        }*/
     }
 }

+ 1 - 6
Assets/Scripts/NewQuestionScript.cs

@@ -18,14 +18,9 @@ public class NewQuestionScript : MonoBehaviour {
         // if new game, create one question card to start with
         GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
         questionCard = question.GetComponent<NewQuestion>();
-        PrepareQuestion(questionCard);
+        questionCard.PrepareQuestion("NewQuestionText", "NewAnswerText");
         questionCard.GetQuestion(false);
 
         questionCard.transform.SetParent(contentPanel);
     }
-
-    public void PrepareQuestion(QuestionCard qc) {
-        qc.questionText = GameObject.Find("NewQuestionText").GetComponent<Text>();
-        qc.answerText = GameObject.Find("NewAnswerText").GetComponent<Text>();
-    }
 }

+ 34 - 10
Assets/Scripts/QuestionCard.cs

@@ -7,13 +7,13 @@ using UnityEngine.UI;
 
 public class QuestionCard : MonoBehaviour
 {
-    public Text questionText;
-    public Text answerText;
+    private Text questionText;
+    private Text answerText;
 
-    private string questionString = "";
-    private string answerString = "";
-    private string idString = "";
-    private string categoryString = "";
+    public string questionString = "";
+    public string answerString = "";
+    public string idString = "";
+    public string categoryString = "";
 
     private Color32 unsafeColor = new Color32(255, 1, 1, 150);
     private Color32 safeColor = new Color32(1, 255, 1, 75);
@@ -36,15 +36,24 @@ public class QuestionCard : MonoBehaviour
     {
     }
 
-    public void PrepareQuestion()
+    public QuestionCard() {
+
+    }
+
+    public QuestionCard(string answer, string question) {
+        answerText.text = answer;
+        questionText.text = question;
+    }
+
+    public void PrepareQuestion(string questionTextName, string answerTextName)
     {
-        questionText = GameObject.Find("QuestionText").GetComponent<Text>();
-        answerText = GameObject.Find("AnswerText").GetComponent<Text>();
+        questionText = GameObject.Find(questionTextName).GetComponent<Text>();
+        answerText = GameObject.Find(answerTextName).GetComponent<Text>();
     }
 
     private IEnumerator GetQuestionData(bool showAnswer)
     {
-        UnityWebRequest www = UnityWebRequest.Get("nordh.xyz/narKampen/dbAccess.php");
+        UnityWebRequest www = UnityWebRequest.Get("nordh.xyz/narKampen/dbFiles/dbAccess.php");
 
         yield return www.SendWebRequest();
 
@@ -85,4 +94,19 @@ public class QuestionCard : MonoBehaviour
     {
         this.GetComponent<Image>().color = safeColor;
     }
+
+    public void SetQuestionText(string text) {
+        this.questionText.text = text;
+    }
+    public void SetAnswerText(string text) {
+        this.answerText.text = text;
+    }
+
+    public Text GetQuestionText() {
+        return this.questionText;
+    }
+
+    public Text GetAnswerText() {
+        return this.answerText;
+    }
 }

+ 22 - 32
Assets/Scripts/ScrollViewScript.cs

@@ -1,9 +1,10 @@
-using System.Collections;
+using System;
+using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
-public class ScrollViewScript : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
+public class ScrollViewScript : MonoBehaviour, IDropHandler
 {
     public GameObject prefab;
     public Transform contentPanel;
@@ -19,47 +20,36 @@ public class ScrollViewScript : MonoBehaviour, IDropHandler, IPointerEnterHandle
         // if new game, create one question card to start with
         GameObject question = Instantiate(prefab, new Vector2(0,0), Quaternion.identity) as GameObject;
         QuestionCard questionCard = question.GetComponent<QuestionCard>();
-        questionCard.PrepareQuestion();
+        questionCard.PrepareQuestion("QuestionText", "AnswerText");
         questionCard.GetQuestion(true);
         questionCard.SetQuestionSafe();
 
+        questionCard.transform.SetSiblingIndex(0);
         questionCard.transform.SetParent(contentPanel);
     }
-    /*
-    public void OnDrop(PointerEventData eventData) {
-        Debug.Log(eventData.pointerDrag + " dropped on " + gameObject.name);
-        Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
-        if (d != null) {
-            // Kontrollera om man släppt kortet på en korrekt possition, 
-            d.parent = contentPanel;
-        }
-    } */
+
     public void OnDrop(PointerEventData eventData) {
         Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
-        if (d != null) {
-            d.placeholderParent = this.transform;
-        }
-    }
-
-    public void OnPointerEnter(PointerEventData eventData) {
-        if (eventData.pointerDrag == null) {
+        if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
             return;
         }
+        // Popup modal asking are you sure here or at onDragEnd?
+        GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
+        QuestionCard questionCard = question.GetComponent<QuestionCard>();
+        questionCard.PrepareQuestion("QuestionText", "AnswerText");
 
-        Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
-        if (d != null) {
-            d.placeholderParent = this.transform;
-        }
-    }
+        NewQuestion nq = d.GetComponent<NewQuestion>();
 
-    public void OnPointerExit(PointerEventData eventData) {
-        if (eventData.pointerDrag == null) {
-            return;
-        }
+        questionCard.SetAnswerText(nq.answerString);
+        questionCard.SetQuestionText(nq.questionString);
+        questionCard.questionString = nq.questionString;
+        questionCard.answerString = nq.answerString;
+        questionCard.categoryString = nq.categoryString;
+        questionCard.idString = nq.idString;
 
-        Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
-        if (d != null && d.placeholderParent == this.transform) {
-            d.placeholderParent = d.parent;
-        }
+
+        questionCard.transform.SetParent(contentPanel);
+        questionCard.transform.SetSiblingIndex(d.placeholder.transform.GetSiblingIndex());
     }
+
 }

+ 28 - 27
Assets/narKampen.unity

@@ -422,8 +422,8 @@ MonoBehaviour:
   m_TargetGraphic: {fileID: 1133529224}
   m_HandleRect: {fileID: 1133529223}
   m_Direction: 0
-  m_Value: 0.5
-  m_Size: 0.9999999
+  m_Value: 0
+  m_Size: 1
   m_NumberOfSteps: 0
   m_OnValueChanged:
     m_PersistentCalls:
@@ -914,7 +914,6 @@ GameObject:
   - component: {fileID: 1400291477}
   - component: {fileID: 1400291479}
   - component: {fileID: 1400291478}
-  - component: {fileID: 1400291480}
   m_Layer: 5
   m_Name: NewQuestionPanel
   m_TagString: Untagged
@@ -1035,18 +1034,6 @@ MonoBehaviour:
   m_ChildForceExpandHeight: 1
   m_ChildControlWidth: 0
   m_ChildControlHeight: 1
---- !u!114 &1400291480
-MonoBehaviour:
-  m_ObjectHideFlags: 0
-  m_CorrespondingSourceObject: {fileID: 0}
-  m_PrefabInstance: {fileID: 0}
-  m_PrefabAsset: {fileID: 0}
-  m_GameObject: {fileID: 1400291473}
-  m_Enabled: 1
-  m_EditorHideFlags: 0
-  m_Script: {fileID: 11500000, guid: e8b2fc3684a5cca4e9de0bbec931fc27, type: 3}
-  m_Name: 
-  m_EditorClassIdentifier: 
 --- !u!114 &1423700018 stripped
 MonoBehaviour:
   m_CorrespondingSourceObject: {fileID: 4606992419449489139, guid: 952f6c50eed555c448854a8a8fdb799e,
@@ -1118,8 +1105,9 @@ GameObject:
   - component: {fileID: 1715970007}
   - component: {fileID: 1715970009}
   - component: {fileID: 1715970008}
+  - component: {fileID: 1715970010}
   m_Layer: 5
-  m_Name: Content
+  m_Name: AnswerLine
   m_TagString: Untagged
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0
@@ -1141,7 +1129,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 1, y: 1}
-  m_AnchoredPosition: {x: -0.000038369853, y: 0}
+  m_AnchoredPosition: {x: 0.0000098118135, y: 0}
   m_SizeDelta: {x: 0, y: 0}
   m_Pivot: {x: 0, y: 1}
 --- !u!114 &1715970008
@@ -1167,20 +1155,33 @@ MonoBehaviour:
   m_GameObject: {fileID: 1715970006}
   m_Enabled: 1
   m_EditorHideFlags: 0
-  m_Script: {fileID: -405508275, guid: f70555f144d8491a825f0804e09c671c, type: 3}
+  m_Script: {fileID: -2095666955, guid: f70555f144d8491a825f0804e09c671c, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
   m_Padding:
-    m_Left: 1
-    m_Right: 1
-    m_Top: 5
-    m_Bottom: 15
+    m_Left: 0
+    m_Right: 0
+    m_Top: 0
+    m_Bottom: 0
   m_ChildAlignment: 4
-  m_Spacing: 10
-  m_ChildForceExpandWidth: 0
-  m_ChildForceExpandHeight: 1
-  m_ChildControlWidth: 0
-  m_ChildControlHeight: 1
+  m_StartCorner: 0
+  m_StartAxis: 0
+  m_CellSize: {x: 100, y: 200}
+  m_Spacing: {x: 5, y: 0}
+  m_Constraint: 2
+  m_ConstraintCount: 1
+--- !u!114 &1715970010
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1715970006}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: e8b2fc3684a5cca4e9de0bbec931fc27, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
 --- !u!1 &1754055316
 GameObject:
   m_ObjectHideFlags: 0