Skip to content
Snippets Groups Projects
FactManager.cs 13 KiB
Newer Older
  • Learn to ignore specific revisions
  • John Schihada's avatar
    John Schihada committed
    using System;
    using System.Collections;
    
    using System.Collections.Generic;
    
    using UnityEngine;
    using static CommunicationEvents;
    public class FactManager : MonoBehaviour
    {
    
        public GameObject SmartMenu;
        private Stack<int> NextEmptyStack = new Stack<int>();
    
    
    John Schihada's avatar
    John Schihada committed
        //Variables for LineMode distinction
        public bool lineModeIsFirstPointSelected = false;
        public Fact lineModeFirstPointSelected = null;
    
        //Variables for AngleMode distinction
        public bool angleModeIsFirstLineSelected = false;
        public Fact angleModeFirstLineSelected = null;
    
        // Start is called before the first frame update
        void Start()
        {
            CommunicationEvents.ToolModeChangedEvent.AddListener(OnToolModeChanged);
    
            CommunicationEvents.TriggerEvent.AddListener(OnHit);
    
    
            //We dont want to have this here anymore...
            //CommunicationEvents.RemoveFactEvent.AddListener(DeleteFact);
    
    John Schihada's avatar
    John Schihada committed
        // Update is called once per frame
        void Update()
        {
    
        }
    
    John Schihada's avatar
    John Schihada committed
        PointFact AddPointFact(RaycastHit hit, int id)
    
    John Schihada's avatar
    John Schihada committed
    
            Facts.Insert(id, new PointFact
    
    John Schihada's avatar
    John Schihada committed
                Point = hit.point,
                Normal = hit.normal
    
    John Schihada's avatar
    John Schihada committed
            return Facts.Find(x => x.Id == id) as PointFact;
    
    John Schihada's avatar
    John Schihada committed
        LineFact AddLineFact(int pid1, int pid2, int id)
    
    John Schihada's avatar
    John Schihada committed
           Facts.Insert(id, new LineFact
    
    John Schihada's avatar
    John Schihada committed
                Pid2 = pid2
    
    John Schihada's avatar
    John Schihada committed
            return Facts.Find(x => x.Id == id) as LineFact;
    
    John Schihada's avatar
    John Schihada committed
        }
    
    John Schihada's avatar
    John Schihada committed
        AngleFact AddAngleFact(int pid1, int pid2, int pid3, int id)
    
    John Schihada's avatar
    John Schihada committed
            Facts.Insert(id, new AngleFact
    
    John Schihada's avatar
    John Schihada committed
                Pid1 = pid1,
                Pid2 = pid2,
                Pid3 = pid3
    
    John Schihada's avatar
    John Schihada committed
            return Facts.Find(x => x.Id == id) as AngleFact;
    
            if (Facts.Contains(fact)) {
                NextEmptyStack.Push(fact.Id);
                //Facts.RemoveAt(fact.Id);
                Facts.Remove(Facts.Find(x => x.Id == fact.Id));
                CommunicationEvents.RemoveFactEvent.Invoke(fact);
            }
    
        public int GetFirstEmptyID()
        {
    
    
            /* for (int i = 0; i < Facts.Length; ++i)
             {
                 if (Facts[i] == "")
                     return i;
             }
             return Facts.Length - 1;*/
    
            int id = NextEmptyStack.Pop();
            if (NextEmptyStack.Count == 0)
                NextEmptyStack.Push(id + 1);
    
    
            Debug.Log("place fact at " + id);
    
    
        }
    
        public void OnToolModeChanged(ToolMode ActiveToolMode)
        {
    
            //We need to do this somehwere...
            CommunicationEvents.ActiveToolMode = ActiveToolMode;
    
            switch (ActiveToolMode)
            {
                case ToolMode.MarkPointMode:
                    //If MarkPointMode is activated we want to have the ability to mark the point
                    //everywhere, independent of already existing facts
    
                       GameObject gO = fact.Representation;
                       gO.GetComponentInChildren<Collider>().enabled = false;
    
                    }
                    break;
                case ToolMode.CreateLineMode:
                    //If CreateLineMode is activated we want to have the ability to select points for the Line
                    //but we don't want to have the ability to select Lines or Angles
    
                        GameObject gO = fact.Representation;
                        if (gO.layer == LayerMask.NameToLayer("Line") || gO.layer == LayerMask.NameToLayer("Angle"))
    
                            gO.GetComponentInChildren<Collider>().enabled = false;
    
                        else if (gO.layer == LayerMask.NameToLayer("Point"))
    
                            gO.GetComponentInChildren<Collider>().enabled = true;
    
                        }
                    }
                    break;
                case ToolMode.CreateAngleMode:
                    //If CreateAngleMode is activated we want to have the ability to select Lines for the Angle
                    //but we don't want to have the ability to select Points or Angles
    
                        GameObject gO = fact.Representation;
                        if (gO.layer == LayerMask.NameToLayer("Point") || gO.layer == LayerMask.NameToLayer("Angle"))
    
                            gO.GetComponentInChildren<Collider>().enabled = false;
    
                        else if (gO.layer == LayerMask.NameToLayer("Line"))
    
                            gO.GetComponentInChildren<Collider>().enabled = true;
    
                        }
                    }
                    break;
                case ToolMode.DeleteMode:
                    //If DeleteMode is activated we want to have the ability to delete every Fact
                    //independent of the concrete type of fact
    
                        GameObject gO = fact.Representation;
                        gO.GetComponentInChildren<Collider>().enabled = true;
    
                    }
                    break;
                case ToolMode.ExtraMode:
    
    John Schihada's avatar
    John Schihada committed
                    /*foreach (Fact fact in Facts)
    
    John Schihada's avatar
    John Schihada committed
                    */
    
                    break;
            }
        }
    
        public void OnHit(RaycastHit hit)
        {
    
    John Schihada's avatar
    John Schihada committed
            switch (ActiveToolMode)
    
    John Schihada's avatar
    John Schihada committed
                //If Left-Mouse-Button was pressed in MarkPointMode
                case ToolMode.MarkPointMode:
                    CommunicationEvents.AddFactEvent.Invoke(this.AddPointFact(hit, this.GetFirstEmptyID()));
                    break;
                //If Left-Mouse-Button was pressed in CreateLineMode
                case ToolMode.CreateLineMode:
                    //Check if an existing Point was hit
                    if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Point"))
                    {
                        Fact tempFact = Facts[hit.transform.GetComponent<FactObject>().Id];
    
    John Schihada's avatar
    John Schihada committed
                        if (this.lineModeIsFirstPointSelected)
    
    John Schihada's avatar
    John Schihada committed
                            //Event for end of line-drawing in "ShinyThings"
                            CommunicationEvents.StopLineDrawingEvent.Invoke(null);
    
    John Schihada's avatar
    John Schihada committed
                            //Create LineFact
    
    John Schihada's avatar
    John Schihada committed
                            CommunicationEvents.AddFactEvent.Invoke(this.AddLineFact(this.lineModeFirstPointSelected.Id, tempFact.Id, this.GetFirstEmptyID()));
                            this.lineModeIsFirstPointSelected = false;
                            this.lineModeFirstPointSelected = null;
    
    John Schihada's avatar
    John Schihada committed
                        else
                        {
                            //Activate LineDrawing for preview
                            this.lineModeIsFirstPointSelected = true;
                            this.lineModeFirstPointSelected = tempFact;
                            //Event for start line-drawing in "ShinyThings"
                            CommunicationEvents.StartLineDrawingEvent.Invoke(this.lineModeFirstPointSelected);
    
    John Schihada's avatar
    John Schihada committed
                        }
                    }
                    //If no Point was hit
    
    John Schihada's avatar
    John Schihada committed
                    else
                    {
                        if (this.lineModeIsFirstPointSelected)
    
    John Schihada's avatar
    John Schihada committed
                            //Deactivate LineDrawing and first point selection
                            this.lineModeIsFirstPointSelected = false;
                            this.lineModeFirstPointSelected = null;
                            //Event for end of line-drawing in "ShinyThings"
                            CommunicationEvents.StopLineDrawingEvent.Invoke(null);
    
    John Schihada's avatar
    John Schihada committed
                        //TODO: Hint that only a line can be drawn between already existing points
                    }
                    break;
                //If Left-Mouse-Button was pressed in CreateAngleMode
                case ToolMode.CreateAngleMode:
    
    John Schihada's avatar
    John Schihada committed
                    //Check if an existing Line was hit
                    if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Line"))
                    {
                        Fact tempFact = Facts[hit.transform.GetComponent<FactObject>().Id];
    
                        if (this.angleModeIsFirstLineSelected)
                        {
                            //Event for end of line-rendering in "ShinyThings"
                            CommunicationEvents.StopCurveDrawingEvent.Invoke(null);
                            //Create AngleFact
    
                            //Check if selected Lines are the same -> if true -> cancel
                            if (!(angleModeFirstLineSelected.Id == tempFact.Id))
                            {
                                //Check if selected Lines have a common Point = id2 for AngleFact
                                if (((LineFact)angleModeFirstLineSelected).Pid1 == ((LineFact)tempFact).Pid1)
                                {
                                    CommunicationEvents.AddFactEvent.Invoke(this.AddAngleFact(((LineFact)angleModeFirstLineSelected).Pid2, ((LineFact)tempFact).Pid1, ((LineFact)tempFact).Pid2, GetFirstEmptyID()));
                                }
                                else if (((LineFact)angleModeFirstLineSelected).Pid1 == ((LineFact)tempFact).Pid2)
                                {
                                    CommunicationEvents.AddFactEvent.Invoke(this.AddAngleFact(((LineFact)angleModeFirstLineSelected).Pid2, ((LineFact)tempFact).Pid2, ((LineFact)tempFact).Pid1, GetFirstEmptyID()));
                                }
                                else if (((LineFact)angleModeFirstLineSelected).Pid2 == ((LineFact)tempFact).Pid1)
                                {
                                    CommunicationEvents.AddFactEvent.Invoke(this.AddAngleFact(((LineFact)angleModeFirstLineSelected).Pid1, ((LineFact)tempFact).Pid1, ((LineFact)tempFact).Pid2, GetFirstEmptyID()));
                                }
                                else if (((LineFact)angleModeFirstLineSelected).Pid2 == ((LineFact)tempFact).Pid2)
                                {
                                    CommunicationEvents.AddFactEvent.Invoke(this.AddAngleFact(((LineFact)angleModeFirstLineSelected).Pid1, ((LineFact)tempFact).Pid2, ((LineFact)tempFact).Pid1, GetFirstEmptyID()));
                                }
                                else
                                {
                                    //TODO: Hint that the selected Lines have no common point
                                }
                            }
    
    
    John Schihada's avatar
    John Schihada committed
                            this.angleModeIsFirstLineSelected = false;
                            this.angleModeFirstLineSelected = null;
                        }
                        else
                        {
                            //Activate CurveDrawing for preview
                            this.angleModeIsFirstLineSelected = true;
                            this.angleModeFirstLineSelected = tempFact;
                            //Event for start line-rendering in "ShinyThings"
                            CommunicationEvents.StartCurveDrawingEvent.Invoke(this.angleModeFirstLineSelected);
                        }
                    }
                    else
                    {
    
                        //TODO: If Point was hit: Angle Drawing with Selecting 3 Points
    
    John Schihada's avatar
    John Schihada committed
                        if (this.angleModeIsFirstLineSelected)
                        {
                            //Deactivate CurveDrawing and first line selection
                            this.angleModeIsFirstLineSelected = false;
                            this.angleModeFirstLineSelected = null;
                            //Event for end of line-drawing in "ShinyThings"
                            CommunicationEvents.StopCurveDrawingEvent.Invoke(null);
                        }
    
                        //TODO: Hint that only a curve can be drawn between already existing lines
                    }
    
    John Schihada's avatar
    John Schihada committed
                    break;
                //If Left-Mouse-Button was pressed in DeleteMode
                case ToolMode.DeleteMode:
                    //Search for the Fact that was hit
                    //If the hit GameObject was a Point/Line/Angle
    
    John Schihada's avatar
    John Schihada committed
                    if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Point") || hit.transform.gameObject.layer == LayerMask.NameToLayer("Line") || hit.transform.gameObject.layer == LayerMask.NameToLayer("Angle"))
                    {
    
    John Schihada's avatar
    John Schihada committed
                        //Search for the suitable fact from the List
    
                        this.DeleteFact(Facts.Find(x => x.Id == hit.transform.GetComponent<FactObject>().Id));
    
    John Schihada's avatar
    John Schihada committed
                    }
                    break;
                //If Left-Mouse-Button was pressed in ExtraMode
                case ToolMode.ExtraMode:
                    if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Point")) {
                        var menu = GameObject.Instantiate(SmartMenu);
    
                        menu.GetComponent<SmartMenu>().FactManager = this;
    
    John Schihada's avatar
    John Schihada committed
                        menu.GetComponent<Canvas>().worldCamera = Camera.main;
                        menu.transform.SetParent(hit.transform);
                        menu.transform.localPosition = Vector3.up - Camera.main.transform.forward;
                    }
                    else
                    {
                        PointFact fact = AddPointFact(hit, GetFirstEmptyID());
                        CommunicationEvents.AddFactEvent.Invoke(fact);
                    }
                    break;