Skip to content
Snippets Groups Projects
ScrollDetails.cs 10.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • using System.Collections;
    using System.Collections.Generic;
    
    using UnityEngine;
    
    using TMPro;
    
    using UnityEngine.Networking;
    
    using Newtonsoft.Json;
    
    using static CommunicationEvents;
    
    
    public class ScrollDetails : MonoBehaviour
    {
    
    Richard Marcus's avatar
    Richard Marcus committed
        public WorldCursor cursor;
    
        public GameObject parameterDisplayPrefab;
    
        public Scroll scroll;
    
    
        public int x_Start;
        public int y_Start;
        public int y_Paece_Between_Items;
    
    
    Richard Marcus's avatar
    Richard Marcus committed
        public static List<GameObject> ParameterDisplays;
    
        private static List<Scroll.ScrollAssignment> LatestCompletions;
    
        private static List<Fact> LatestRenderedHints;
    
        public Vector3 GetPosition(int i)
        {
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            return new Vector3(x_Start, y_Start + i * (-y_Paece_Between_Items), 0f);
    
        // Start is called before the first frame update
        void Start()
        {
    
    Richard Marcus's avatar
    Richard Marcus committed
            if (cursor == null) cursor = GameObject.FindObjectOfType<WorldCursor>();
    
    Richard Marcus's avatar
    Richard Marcus committed
    
    
            ScrollFactHintEvent.AddListener(animateHint);
    
            NewAssignmentEvent.AddListener(newAssignmentTrigger);
    
    BenniHome's avatar
    BenniHome committed
        public void setScroll(Scroll s)
        {
    
            Transform originalScroll = gameObject.transform.GetChild(1).transform;
            Transform originalScrollView = originalScroll.GetChild(1);
            Transform originalViewport = originalScrollView.GetChild(0);
    
            this.scroll = s;
    
            originalScroll.GetChild(0).GetComponent<TextMeshProUGUI>().text = s.description;
    
    
            //Clear all current ScrollFacts
    
            for (int i = 0; i < originalViewport.GetChild(0).childCount; i++) {
                GameObject.Destroy(originalViewport.GetChild(0).transform.GetChild(i).gameObject);
            }
    
    Richard Marcus's avatar
    Richard Marcus committed
            ParameterDisplays = new List<GameObject>();
            for (int i = 0; i < s.requiredFacts.Count; i++)
    
    BenniHome's avatar
    BenniHome committed
            {
    
                var originalObj = Instantiate(parameterDisplayPrefab, Vector3.zero, Quaternion.identity, transform);
                var originalScrollFact = originalObj.transform.GetChild(0).GetComponent<RenderedScrollFact>();
                originalScrollFact.ID = i;
                originalScrollFact.Label = s.requiredFacts[i].label;
    
                originalScrollFact.factUri = s.requiredFacts[i].@ref.uri;
    
    
                originalObj.transform.SetParent(originalViewport.GetChild(0));
    
                ParameterDisplays.Add(originalObj);
    
        public void magicButtonTrigger() {
            StartCoroutine(magicButton());
        }
    
        IEnumerator magicButton()
    
    BenniHome's avatar
    BenniHome committed
        {
    
            //Non blocking wait till sendView() is finished
            yield return sendView("/scroll/apply");
    
    BenniHome's avatar
    BenniHome committed
            {
    
                Debug.Log("DAS HAT NICHT GEKLAPPT");
                //TODO: hier ne Art PopUp, wo drin steht, dass das nicht geklappt hat
                PushoutFactFailEvent.Invoke(null);
            }
            else
            {
                Scroll.ScrollApplicationInfo pushout = JsonConvert.DeserializeObject<Scroll.ScrollApplicationInfo>(currentMmtAnswer);
                readPushout(pushout.acquiredFacts);
    
    John Schihada's avatar
    John Schihada committed
    
    
        public void newAssignmentTrigger() {
            StartCoroutine(newAssignment());
    
        IEnumerator newAssignment()
    
            //Non blocking wait till sendView() is finished
            yield return sendView("/scroll/dynamic");
    
                Debug.Log("DAS HAT NICHT GEKLAPPT");
            }
            else
            {
                Scroll.ScrollDynamicInfo scrollDynamicInfo = JsonConvert.DeserializeObject<Scroll.ScrollDynamicInfo>(currentMmtAnswer);
                processScrollDynamicInfo(scrollDynamicInfo);
    
    BenniHome's avatar
    BenniHome committed
        {
    
            string body = prepareScrollAssignments();
    
    BenniHome's avatar
    BenniHome committed
    
    
            UnityWebRequest www = UnityWebRequest.Put(ServerAdress + endpoint, body);
    
    BenniHome's avatar
    BenniHome committed
            www.method = UnityWebRequest.kHttpVerbPOST;
    
            www.SetRequestHeader("Content-Type", "application/json");
    
            var async = www.SendWebRequest();
    
            while (!async.isDone) {
                //Non blocking wait for one frame, for letting the game do other things
                yield return null;
            }
    
    BenniHome's avatar
    BenniHome committed
    
    
            if (www.result == UnityWebRequest.Result.ConnectionError 
             || www.result == UnityWebRequest.Result.ProtocolError)
    
    BenniHome's avatar
    BenniHome committed
            {
                Debug.Log(www.error);
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
            }
    
                string answer = www.downloadHandler.text;
    
    Benjamin Bösl's avatar
    Benjamin Bösl committed
        }
    
        private string prepareScrollAssignments()
        {
            Fact tempFact;
    
            List<Scroll.ScrollAssignment> assignmentList = new List<Scroll.ScrollAssignment>();
    
    
            for (int i = 0; i < ParameterDisplays.Count; i++)
            {
    
                Scroll.ScrollAssignment listEntry = new Scroll.ScrollAssignment();
    
                tempFact = ParameterDisplays[i].GetComponentInChildren<DropHandling>().currentFact;
                if (tempFact != null)
                {
    
                    listEntry.fact = new Scroll.UriReference(this.scroll.requiredFacts[i].@ref.uri);
    
                    listEntry.assignment = new JSONManager.OMS(tempFact.Id);
    
                    assignmentList.Add(listEntry);
    
                }
            }
    
            Scroll.FilledScroll filledScroll = new Scroll.FilledScroll(this.scroll.@ref, assignmentList);
            return Scroll.ToJSON(filledScroll);
        }
    
    
        private void readPushout(List<Scroll.ScrollFact> pushoutFacts)
        {
            FactManager factManager = cursor.GetComponent<FactManager>();
    
    
            if(pushoutFacts.Count == 0)
                PushoutFactFailEvent.Invoke(null);
    
    
            bool samestep = false;
            for (int i = 0; i < pushoutFacts.Count; i++, samestep = true)
    
            {
                Fact newFact = ParsingDictionary.parseFactDictionary[pushoutFacts[i].getType()].Invoke(pushoutFacts[i]);
    
                if (newFact != null)
                {
    
                    PushoutFactEvent.Invoke(FactManager.AddFactIfNotFound(newFact, out bool exists, samestep));
    
                }
                else {
                    Debug.Log("Parsing on pushout-fact returned null -> One of the dependent facts does not exist");
                }
    
            }
        }
    
        public void processScrollDynamicInfo(Scroll.ScrollDynamicInfo scrollDynamicInfo) {
    
    John Schihada's avatar
    John Schihada committed
    
            if (scrollDynamicInfo.completions.Count != 0)
                LatestCompletions = scrollDynamicInfo.completions[0];
            else
                LatestCompletions = new List<Scroll.ScrollAssignment>();
    
            LatestRenderedHints = new List<Fact>();
    
            List<string> hintUris = new List<string>();
    
            foreach (Scroll.ScrollAssignment currentCompletion in LatestCompletions) {
    
                hintUris.Add(currentCompletion.fact.uri);
    
            //Update Scroll, process data for later hints and update Uri-List for which hints are available
            hintUris = processRenderedScroll(scrollDynamicInfo.rendered, hintUris);
    
            //Show that Hint is available for ScrollParameter
            HintAvailableEvent.Invoke(hintUris);
    
        public List<string> processRenderedScroll(Scroll rendered, List<string> hintUris)
    
        {
            Transform scroll = gameObject.transform.GetChild(1).transform;
    
    
            //Update scroll-description
    
            scroll.GetChild(0).GetComponent<TextMeshProUGUI>().text = rendered.description;
    
            for (int i = 0; i < rendered.requiredFacts.Count; i++)
            {
    
                //Update ScrollParameter label
    
                var obj = ParameterDisplays.Find(x => x.transform.GetChild(0).GetComponent<RenderedScrollFact>().factUri.Equals(rendered.requiredFacts[i].@ref.uri));
                obj.transform.GetChild(0).GetComponent<RenderedScrollFact>().Label = rendered.requiredFacts[i].label;
    
    
                //Check Hint Informations
                //If ScrollFact is assigned -> No Hint
                if (obj.transform.GetChild(0).GetComponent<DropHandling>().currentFact == null) {
                    Fact currentFact = ParsingDictionary.parseFactDictionary[rendered.requiredFacts[i].getType()].Invoke(rendered.requiredFacts[i]);
                    //If the fact could not be parsed -> Therefore not all dependent Facts exist -> No Hint
                    //AND if fact has no dependent facts -> No Hint
                    if (currentFact != null && currentFact.hasDependentFacts())
                    {
                        //Hint available for abstract-problem uri
    
                        LatestRenderedHints.Add(currentFact);
                    }
                }
    
        public void animateHint(GameObject scrollParameter, string scrollParameterUri) {
    
            Scroll.ScrollAssignment suitableCompletion = LatestCompletions.Find(x => x.fact.uri.Equals(scrollParameterUri) );
    
            if (suitableCompletion != null)
            {
    
                if (LevelFacts.ContainsKey(suitableCompletion.assignment.uri))
    
                    fact = LevelFacts[suitableCompletion.assignment.uri];
    
    John Schihada's avatar
    John Schihada committed
                    scrollParameter.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger();
    
                    //Animate Fact in FactPanel
                    AnimateExistingFactEvent.Invoke(fact);
                    //Animate factRepresentation in game
    
    John Schihada's avatar
    John Schihada committed
                    fact.Representation.GetComponentInChildren<MeshRendererHintAnimation>().AnimationTrigger();
    
            else if (LatestRenderedHints.Exists(x => x.Id.Equals(scrollParameterUri)))
    
                fact = LatestRenderedHints.Find(x => x.Id.Equals(scrollParameterUri));
                var factId = fact.Id;
    
    
                //If there is an equal existing fact -> Animate that fact AND ScrollParameter
    
                    Fact existingFact = LevelFacts[factId];
    
    
                    //Animate ScrollParameter
    
    John Schihada's avatar
    John Schihada committed
                    scrollParameter.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger();
    
                    //Animate Fact in FactPanel
                    AnimateExistingFactEvent.Invoke(existingFact);
    
                    //Animate factRepresentation in game if Fact has a Representation
                    if(existingFact.Representation != null)
                        existingFact.Representation.GetComponentInChildren<MeshRendererHintAnimation>().AnimationTrigger();
    
                }
                //If not -> Generate a Fact-Representation with such dependent facts
                else
                {
                    //Animate ScrollParameter
    
    John Schihada's avatar
    John Schihada committed
                    scrollParameter.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger();
    
                    //Generate new FactRepresentation and animate it
                    AnimateNonExistingFactEvent.Invoke(fact);
                }
            }
    
        }
    
        public void animateScrollParameter(string label)
        {
            var obj = ParameterDisplays.Find(x => x.transform.GetChild(0).GetComponent<RenderedScrollFact>().Label == label);
            obj.GetComponentInChildren<Animator>().SetTrigger("animateHint");
        }