From d1d9dcd11faed58b0c01f29a95aaa9257c1db7cf Mon Sep 17 00:00:00 2001 From: Richard Marcus <richard.marcus@fau.de> Date: Fri, 13 Dec 2019 21:24:05 +0100 Subject: [PATCH] refactoring, line functionality isnt back yet, but it compiles again --- Assets/FactManager.cs | 124 ++++++++++++++---- .../InteractionEngine/CommunicationEvents.cs | 38 ++++-- Assets/InteractionEngine/Fact.cs | 18 ++- Assets/InteractionEngine/FactObject.cs | 14 +- Assets/InteractionEngine/FactSpawner.cs | 67 ++++++---- Assets/InteractionEngine/MMTInterface.cs | 8 +- Assets/{ => InteractionEngine}/ShinyThings.cs | 47 ++++--- .../ShinyThings.cs.meta | 0 Assets/InteractionEngine/SmartMenu.cs | 3 +- Assets/InteractionEngine/WorldCursor.cs | 6 +- Assets/TreeWorld.unity | 10 +- 11 files changed, 227 insertions(+), 108 deletions(-) rename Assets/{ => InteractionEngine}/ShinyThings.cs (64%) rename Assets/{ => InteractionEngine}/ShinyThings.cs.meta (100%) diff --git a/Assets/FactManager.cs b/Assets/FactManager.cs index 5362ae4c..5f7d12e8 100644 --- a/Assets/FactManager.cs +++ b/Assets/FactManager.cs @@ -1,18 +1,76 @@ using System.Collections; using System.Collections.Generic; +using TMPro; using UnityEngine; using static CommunicationEvents; public class FactManager : MonoBehaviour { + + + + public GameObject SmartMenu; + private Stack<int> NextEmptyStack = new Stack<int>(); + // Start is called before the first frame update void Start() { CommunicationEvents.ToolModeChangedEvent.AddListener(OnToolModeChanged); + CommunicationEvents.TriggerEvent.AddListener(OnHit); + + CommunicationEvents.RemoveFactEvent.AddListener(DeleteFact);//we also need the listener here at the moment so we can react to UI delete events + + NextEmptyStack.Push(0); + + } + + + void AddLineFact(int pid1, int pid2, int id) + { + Facts.Insert(id, new LineFact + { + Id = id, + Pid1 = pid1, + Pid2 = pid2 + }); + } + + void AddAngleFact(int pid1, int pid2, int pid3, int id) + { + Facts.Insert(id, new AngleFact + { + Id = id, + Pid1 = pid1, + Pid2 = pid2, + Pid3 = pid3 + }); + } + + + PointFact AddPointFact(RaycastHit hit, int id) + { + + Facts.Insert(id, new PointFact + { + Id = id, + Point = hit.point + }); + + return Facts[id] as PointFact; } + void DeleteFact(Fact fact) + { + + NextEmptyStack.Push(fact.Id); + Facts.RemoveAt(fact.Id); + } + + // Update is called once per frame void Update() { + + //Je nachdem ob erster oder der zweite Punkt angeklickt wurde behandeln //Wenn erster Punkt einen Point-Collider erwischt hat: @@ -27,12 +85,12 @@ void Update() //LayerMask for Points int layerMask = 1 << LayerMask.NameToLayer("Point"); //only hit Point - + /* //Wenn bereits der erste Punkt markiert wurde if (this.lineRendererActivated) //instead: bool variable.... { //If a second Point was Hit - if (Physics.Raycast(ray, out Hit, 30f, layerMask)) + if (Physics.Raycast(ray, out Hit, 30f, layerMask)) //instead: another hitevent, refer to OnHit { //Event for Creating the Line Vector3 point1 = this.linePositions[0]; @@ -72,7 +130,7 @@ void Update() } } - + */ } @@ -80,12 +138,20 @@ void Update() public int GetFirstEmptyID() { - for (int i = 0; i < Facts.Length; ++i) - { - if (Facts[i] == "") - return i; - } - return Facts.Length - 1; + /* 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); + + + return id; + } @@ -96,51 +162,55 @@ public void OnToolModeChanged(ToolMode ActiveToolMode) case ToolMode.MarkPointMode: //If MarkPointMode is activated we want to have the ability to mark the point //everywhere, independent of already existing facts - foreach (GameObject GameObjectFact in GameObjectFacts) + foreach (Fact fact in Facts) { - GameObjectFact.GetComponentInChildren<Collider>().enabled = false; + 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 - foreach (GameObject GameObjectFact in GameObjectFacts) + foreach (Fact fact in Facts) { - if (GameObjectFact.layer == LayerMask.NameToLayer("Line") || GameObjectFact.layer == LayerMask.NameToLayer("Angle")) + GameObject gO = fact.Representation; + if (gO.layer == LayerMask.NameToLayer("Line") || gO.layer == LayerMask.NameToLayer("Angle")) { - GameObjectFact.GetComponentInChildren<Collider>().enabled = false; + gO.GetComponentInChildren<Collider>().enabled = false; } - else if (GameObjectFact.layer == LayerMask.NameToLayer("Point")) + else if (gO.layer == LayerMask.NameToLayer("Point")) { - GameObjectFact.GetComponentInChildren<Collider>().enabled = true; + 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 - foreach (GameObject GameObjectFact in GameObjectFacts) + foreach (Fact fact in Facts) { - if (GameObjectFact.layer == LayerMask.NameToLayer("Point") || GameObjectFact.layer == LayerMask.NameToLayer("Angle")) + GameObject gO = fact.Representation; + if (gO.layer == LayerMask.NameToLayer("Point") || gO.layer == LayerMask.NameToLayer("Angle")) { - GameObjectFact.GetComponentInChildren<Collider>().enabled = false; + gO.GetComponentInChildren<Collider>().enabled = false; } - else if (GameObjectFact.layer == LayerMask.NameToLayer("Line")) + else if (gO.layer == LayerMask.NameToLayer("Line")) { - GameObjectFact.GetComponentInChildren<Collider>().enabled = true; + 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 - foreach (GameObject GameObjectFact in GameObjectFacts) + foreach (Fact fact in Facts) { - GameObjectFact.GetComponentInChildren<Collider>().enabled = true; + GameObject gO = fact.Representation; + gO.GetComponentInChildren<Collider>().enabled = true; } break; case ToolMode.ExtraMode: - foreach (GameObject GameObjectFact in GameObjectFacts) + foreach (Fact fact in Facts) { } @@ -168,14 +238,14 @@ public void OnHit(RaycastHit hit) { char letter = hit.transform.gameObject.GetComponentInChildren<TextMeshPro>().text.ToCharArray()[0]; int id = letter - 65; - CommunicationEvents.RemoveEvent.Invoke(id); + CommunicationEvents.RemoveFactEvent.Invoke(Facts[id]); } } else { - - CommunicationEvents.AddPointEvent.Invoke(hit, GetFirstEmptyID()); + PointFact fact = AddPointFact(hit, GetFirstEmptyID()); + CommunicationEvents.AddFactEvent.Invoke(fact); } } diff --git a/Assets/InteractionEngine/CommunicationEvents.cs b/Assets/InteractionEngine/CommunicationEvents.cs index 706e4666..717e0732 100644 --- a/Assets/InteractionEngine/CommunicationEvents.cs +++ b/Assets/InteractionEngine/CommunicationEvents.cs @@ -6,23 +6,32 @@ public static class CommunicationEvents { - public class PointEvent : UnityEvent<RaycastHit,int> - { + /* public class PointEvent : UnityEvent<RaycastHit,int> + { - } + } - public class LineEvent : UnityEvent<int, int, int> { + public class LineEvent : UnityEvent<int, int, int> { - } + } + + + + public class FactEvent : UnityEvent<int> + { + }*/ public class HitEvent : UnityEvent<RaycastHit> { } - public class FactEvent : UnityEvent<int> + public class FactEvent : UnityEvent<Fact> { } + + + public class MouseOverFactEvent : UnityEvent<Transform> { @@ -33,16 +42,21 @@ public class ToolModeEvent : UnityEvent<ToolMode> { } public static HitEvent TriggerEvent = new HitEvent(); - public static MouseOverFactEvent HighlightEvent = new MouseOverFactEvent(); - public static MouseOverFactEvent EndHighlightEvent = new MouseOverFactEvent(); + public static ToolModeEvent ToolModeChangedEvent = new ToolModeEvent(); - public static PointEvent AddPointEvent = new PointEvent(); - public static LineEvent AddLineEvent = new LineEvent(); - public static FactEvent RemoveEvent = new FactEvent(); + /* + public static FactEvent AddPointEvent = new FactEvent(); + public static FactEvent AddLineEvent = new FactEvent(); + public static FactEvent AddAngleEvent = new FactEvent(); + */ + public static FactEvent AddFactEvent = new FactEvent(); + public static FactEvent RemoveFactEvent = new FactEvent(); + public static ToolMode ActiveToolMode { get; set; } public static List<Fact> Facts = new List<Fact>(); - + // public static MouseOverFactEvent HighlightEvent = new MouseOverFactEvent(); + //public static MouseOverFactEvent EndHighlightEvent = new MouseOverFactEvent(); } diff --git a/Assets/InteractionEngine/Fact.cs b/Assets/InteractionEngine/Fact.cs index fd6f5e65..c3434a99 100644 --- a/Assets/InteractionEngine/Fact.cs +++ b/Assets/InteractionEngine/Fact.cs @@ -6,8 +6,22 @@ public class Fact public GameObject Representation; } - +//I am not sure if we ever need to attach these to an object, so one script for all for now... public class PointFact : Fact { public Vector3 Point; -} \ No newline at end of file + public Vector3 Normal; +} +public class LineFact : Fact +{ + public int Pid1, Pid2; +} +public class AngleFact : Fact +{ + public int Pid1, Pid2, Pid3; +} +public class OnLineFact : Fact +{ + public int Pid1, Pid2, Pid3; +} + diff --git a/Assets/InteractionEngine/FactObject.cs b/Assets/InteractionEngine/FactObject.cs index 31b6adf9..8d983829 100644 --- a/Assets/InteractionEngine/FactObject.cs +++ b/Assets/InteractionEngine/FactObject.cs @@ -4,16 +4,8 @@ public class FactObject : MonoBehaviour { + //object that can represent arbitrary facts + //used to access entry in global Fact Collection public int Id; - // Start is called before the first frame update - void Start() - { - - } - - // Update is called once per frame - void Update() - { - - } + } diff --git a/Assets/InteractionEngine/FactSpawner.cs b/Assets/InteractionEngine/FactSpawner.cs index ce32db93..dde6a215 100644 --- a/Assets/InteractionEngine/FactSpawner.cs +++ b/Assets/InteractionEngine/FactSpawner.cs @@ -9,40 +9,48 @@ public class FactSpawner : MonoBehaviour { private GameObject FactRepresentation; - - //Variables for highlighting Facts where the cursor moves over - public Material defaultMaterial; - public Material highlightMaterial; - - public GameObject SmartMenu; - void Start() { - CommunicationEvents.HighlightEvent.AddListener(OnMouseOverFact); - CommunicationEvents.EndHighlightEvent.AddListener(OnMouseOverFactEnd); - CommunicationEvents.TriggerEvent.AddListener(OnHit); - - CommunicationEvents.AddPointEvent.AddListener(SpawnPoint); - CommunicationEvents.AddLineEvent.AddListener(SpawnLine); - CommunicationEvents.RemoveEvent.AddListener(DeletePoint); + + AddFactEvent.AddListener(FactAction); + RemoveFactEvent.AddListener(DeleteObject); //Default FactRepresenation = Sphere-Prefab for Points this.FactRepresentation = (GameObject) Resources.Load("Prefabs/Sphere", typeof(GameObject)); } + public void FactAction(Fact fact) + { + + switch (fact) + { + case PointFact pointFact: + SpawnPoint(pointFact); + break; + case LineFact lineFact: + SpawnLine(lineFact); + break; + + } + } - public void SpawnPoint(RaycastHit hit, int id) + public void SpawnPoint(PointFact fact) { + + + PointFact pointFact = fact; this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Sphere", typeof(GameObject)); - Debug.Log(id); + GameObject point = GameObject.Instantiate(FactRepresentation); - point.transform.position = hit.point; - point.transform.up = hit.normal; - string letter = ((Char)(64+id+1)).ToString(); + point.transform.position = pointFact.Point; + point.transform.up = pointFact.Normal; + string letter = ((Char)(64+fact.Id+1)).ToString(); point.GetComponentInChildren<TextMeshPro>().text = letter; - point.GetComponent<FactObject>().Id = id; + point.GetComponent<FactObject>().Id = fact.Id; + pointFact.Representation = point; + //If a new Point was spawned -> We are in MarkPointMode -> Then we want the collider to be disabled //Hint: Thats why by now, if we mark a Point in an other mode than MarkPointMode, the //Collider will be set disabled @@ -52,16 +60,19 @@ public void SpawnPoint(RaycastHit hit, int id) } - public void DeletePoint(int id) + public void DeleteObject(Fact fact) { - GameObject point = Facts[id].Representation; + Debug.Log("delete obj"); + GameObject point = fact.Representation; GameObject.Destroy(point); } - public void SpawnLine(int pid1, int pid2, int id) { + public void SpawnLine(LineFact lineFact) { + - Vector3 point1 = (Facts[pid1] as PointFact).Point; + Vector3 point1 = (Facts[lineFact.Pid1] as PointFact).Point; + Vector3 point2 = (Facts[lineFact.Pid2] as PointFact).Point; //Change FactRepresentation to Line this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Line2", typeof(GameObject)); GameObject line = GameObject.Instantiate(FactRepresentation); @@ -76,14 +87,14 @@ public void SpawnLine(int pid1, int pid2, int id) { line.transform.localScale = v3T; line.transform.rotation = Quaternion.FromToRotation(Vector3.up, point2 - point1); - string letter = ((Char)(64 + id + 1)).ToString(); + string letter = ((Char)(64 + lineFact.Id + 1)).ToString(); line.GetComponentInChildren<TextMeshPro>().text = letter; - line.GetComponent<FactObject>().Id = id; + line.GetComponent<FactObject>().Id = lineFact.Id; //If a new Line was spawned -> We are in CreateLineMode -> Then we want the collider to be disabled if (CommunicationEvents.ActiveToolMode != ToolMode.ExtraMode) line.GetComponentInChildren<BoxCollider>().enabled = false; - Facts[id] = letter; - GameObjectFacts[id] = line; + lineFact.Representation = line; + } diff --git a/Assets/InteractionEngine/MMTInterface.cs b/Assets/InteractionEngine/MMTInterface.cs index 37a25b95..d76c0d52 100644 --- a/Assets/InteractionEngine/MMTInterface.cs +++ b/Assets/InteractionEngine/MMTInterface.cs @@ -27,12 +27,12 @@ void Start() mmtServerProcess = Process.Start(mmtServerProcessInfo); */ - CommunicationEvents.AddPointEvent.AddListener(AddFactToMMT); - CommunicationEvents.RemoveEvent.AddListener(RemoveFactFromMMT); + // CommunicationEvents.AddPointEvent.AddListener(AddFactToMMT); + // CommunicationEvents.RemoveEvent.AddListener(RemoveFactFromMMT); } - + /* void AddFactToMMT(RaycastHit hit, int id ) { //send new fact to MMT @@ -43,6 +43,6 @@ void RemoveFactFromMMT( int id) { UnityEngine.Debug.Log("remove fact"+ id); } - + */ } diff --git a/Assets/ShinyThings.cs b/Assets/InteractionEngine/ShinyThings.cs similarity index 64% rename from Assets/ShinyThings.cs rename to Assets/InteractionEngine/ShinyThings.cs index 65504113..95e15361 100644 --- a/Assets/ShinyThings.cs +++ b/Assets/InteractionEngine/ShinyThings.cs @@ -9,6 +9,9 @@ public class ShinyThings : MonoBehaviour //Attributes for Highlighting of Facts when Mouse-Over private string selectableTag = "Selectable"; private Transform lastFactSelection; + public Material defaultMaterial; + public Material highlightMaterial; + //Attributes for simulating the drawing of a line public LineRenderer lineRenderer; @@ -20,6 +23,8 @@ public class ShinyThings : MonoBehaviour void Start() { if(Cursor == null)Cursor = GetComponent<WorldCursor>(); + // CommunicationEvents.HighlightEvent.AddListener(OnMouseOverFact); + // CommunicationEvents.EndHighlightEvent.AddListener(OnMouseOverFactEnd); } // Update is called once per frame @@ -28,28 +33,36 @@ void Update() //SELECTION-HIGHLIGHTING-PART //Check if a Fact was Hit - RaycastHit Hit = Cursor.Hit; - - Transform selection = Hit.transform; + RaycastHit Hit = Cursor.Hit; - //Set the last Fact unselected - if (this.lastFactSelection != null) + if (Hit.transform != null) { - //Invoke the EndHighlightEvent that will be handled in FactSpawner - CommunicationEvents.EndHighlightEvent.Invoke(this.lastFactSelection); - this.lastFactSelection = null; - } + Transform selection = Hit.transform; - //Set the Fact that was Hit as selected - if (selection.CompareTag(selectableTag)) - { - //Invoke the HighlightEvent that will be handled in FactSpawner - this.lastFactSelection = selection; - CommunicationEvents.HighlightEvent.Invoke(selection); + //Set the last Fact unselected + if (this.lastFactSelection != null) + { + //Invoke the EndHighlightEvent that will be handled in FactSpawner + // CommunicationEvents.EndHighlightEvent.Invoke(this.lastFactSelection); + OnMouseOverFactEnd(lastFactSelection); + this.lastFactSelection = null; + } + + //Set the Fact that was Hit as selected + if (selection.CompareTag(selectableTag)) + { + //Invoke the HighlightEvent that will be handled in FactSpawner + this.lastFactSelection = selection; + //CommunicationEvents.HighlightEvent.Invoke(selection); + OnMouseOverFact(lastFactSelection); + } + //SELECTION-HIGHLIGHTING-PART-END } - //SELECTION-HIGHLIGHTING-PART-END + + } + public void OnMouseOverFactEnd(Transform selection) { Renderer selectionRenderer; @@ -90,7 +103,7 @@ void DeactivateLineRenderer() //Updates the second-point of the Line when First Point was selected in LineMode void UpdateLineRenderer(Vector3 currentPosition) { - if (this.ActiveToolMode == ToolMode.CreateLineMode) + // if (this.ActiveToolMode == ToolMode.CreateLineMode) { if (this.lineRendererActivated) { diff --git a/Assets/ShinyThings.cs.meta b/Assets/InteractionEngine/ShinyThings.cs.meta similarity index 100% rename from Assets/ShinyThings.cs.meta rename to Assets/InteractionEngine/ShinyThings.cs.meta diff --git a/Assets/InteractionEngine/SmartMenu.cs b/Assets/InteractionEngine/SmartMenu.cs index 48c6821a..e092ef90 100644 --- a/Assets/InteractionEngine/SmartMenu.cs +++ b/Assets/InteractionEngine/SmartMenu.cs @@ -7,7 +7,8 @@ public class SmartMenu : MonoBehaviour public void DestroyObject() { - CommunicationEvents.RemoveEvent.Invoke(transform.parent.GetComponent<FactObject>().Id); + + CommunicationEvents.RemoveFactEvent.Invoke(CommunicationEvents.Facts[transform.parent.GetComponent<FactObject>().Id]); } diff --git a/Assets/InteractionEngine/WorldCursor.cs b/Assets/InteractionEngine/WorldCursor.cs index 1a3a94dc..9c676296 100644 --- a/Assets/InteractionEngine/WorldCursor.cs +++ b/Assets/InteractionEngine/WorldCursor.cs @@ -61,10 +61,12 @@ void Update() //Check if left Mouse-Button was pressed and handle it void CheckMouseButtons(Ray ray) { + if (Input.GetMouseButtonDown(0)) { - - CommunicationEvents.TriggerEvent.Invoke(Hit); + if (EventSystem.current.IsPointerOverGameObject()) return; //this prevents rays from shooting through ui + + CommunicationEvents.TriggerEvent.Invoke(Hit); } } diff --git a/Assets/TreeWorld.unity b/Assets/TreeWorld.unity index 72bf4cc5..363eef32 100644 --- a/Assets/TreeWorld.unity +++ b/Assets/TreeWorld.unity @@ -1133,10 +1133,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 5c41d44ed1851e14089a3b6e37cba740, type: 3} m_Name: m_EditorClassIdentifier: - defaultMaterial: {fileID: 2100000, guid: 8ae9adf4dc782964387385c1e8c0eb72, type: 2} - highlightMaterial: {fileID: 2100000, guid: c7daa82e15f0cf04d92d0f41ce84f9df, type: 2} - SmartMenu: {fileID: 5601740127768851631, guid: e693bf633c633d243b0254d117ec3893, - type: 3} --- !u!120 &1661088668 LineRenderer: m_ObjectHideFlags: 0 @@ -1244,6 +1240,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 1ed2627334ef0e44ebe98f8b9ce4bc25, type: 3} m_Name: m_EditorClassIdentifier: + Cursor: {fileID: 1661088666} + defaultMaterial: {fileID: 2100000, guid: 8ae9adf4dc782964387385c1e8c0eb72, type: 2} + highlightMaterial: {fileID: 2100000, guid: c7daa82e15f0cf04d92d0f41ce84f9df, type: 2} + lineRenderer: {fileID: 1661088668} --- !u!114 &1661088670 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1256,6 +1256,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c75823b1712c4914b987f0e1ba8a5cba, type: 3} m_Name: m_EditorClassIdentifier: + SmartMenu: {fileID: 5601740127768851631, guid: e693bf633c633d243b0254d117ec3893, + type: 3} --- !u!1 &1675643434 GameObject: m_ObjectHideFlags: 0 -- GitLab