Skip to content
Snippets Groups Projects
Commit fe9328a8 authored by John Schihada's avatar John Schihada
Browse files

Refactoring completed

parent d1d9dcd1
No related branches found
No related tags found
No related merge requests found
...@@ -5,57 +5,68 @@ ...@@ -5,57 +5,68 @@
using static CommunicationEvents; using static CommunicationEvents;
public class FactManager : MonoBehaviour public class FactManager : MonoBehaviour
{ {
public GameObject SmartMenu; public GameObject SmartMenu;
private Stack<int> NextEmptyStack = new Stack<int>(); private Stack<int> NextEmptyStack = new Stack<int>();
//Variable for LineMode distinction
public bool lineModeFirstPointSelected = false;
public Fact firstPointSelected = null;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
CommunicationEvents.ToolModeChangedEvent.AddListener(OnToolModeChanged); CommunicationEvents.ToolModeChangedEvent.AddListener(OnToolModeChanged);
CommunicationEvents.TriggerEvent.AddListener(OnHit); 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 //We also need the listener here at the moment so we can react to UI delete events in ExtraMode -> Delete-Button
CommunicationEvents.RemoveFactEvent.AddListener(DeleteFact);
NextEmptyStack.Push(0); NextEmptyStack.Push(0);
} }
// Update is called once per frame
void Update()
{
}
void AddLineFact(int pid1, int pid2, int id) PointFact AddPointFact(RaycastHit hit, int id)
{ {
Facts.Insert(id, new LineFact
Facts.Insert(id, new PointFact
{ {
Id = id, Id = id,
Pid1 = pid1, Point = hit.point,
Pid2 = pid2 Normal = hit.normal
}); });
return Facts[id] as PointFact;
} }
void AddAngleFact(int pid1, int pid2, int pid3, int id) LineFact AddLineFact(int pid1, int pid2, int id)
{ {
Facts.Insert(id, new AngleFact Facts.Insert(id, new LineFact
{ {
Id = id, Id = id,
Pid1 = pid1, Pid1 = pid1,
Pid2 = pid2, Pid2 = pid2
Pid3 = pid3
}); });
}
return Facts[id] as LineFact;
}
PointFact AddPointFact(RaycastHit hit, int id) AngleFact AddAngleFact(int pid1, int pid2, int pid3, int id)
{ {
Facts.Insert(id, new AngleFact
Facts.Insert(id, new PointFact
{ {
Id = id, Id = id,
Point = hit.point Pid1 = pid1,
Pid2 = pid2,
Pid3 = pid3
}); });
return Facts[id] as PointFact; return Facts[id] as AngleFact;
} }
void DeleteFact(Fact fact) void DeleteFact(Fact fact)
...@@ -63,78 +74,9 @@ void DeleteFact(Fact fact) ...@@ -63,78 +74,9 @@ void DeleteFact(Fact fact)
NextEmptyStack.Push(fact.Id); NextEmptyStack.Push(fact.Id);
Facts.RemoveAt(fact.Id); Facts.RemoveAt(fact.Id);
CommunicationEvents.RemoveFactEvent.Invoke(fact);
} }
// 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:
//Linie aktivieren und Cursor folgen
//Wenn erster Punkt keinen Point-Collider erwischt hat:
//Nichts tun -> Evtl Hint einblenden
//Wenn zweiter Punkt einen Point-Collider erwischt hat:
//Event senden um GameObject-Line zu erzeugen
//Wenn zweiter Punkt keinen Point-Collider erwischt hat:
//Linie deaktivieren -> Evtl Hint einblenden
//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)) //instead: another hitevent, refer to OnHit
{
//Event for Creating the Line
Vector3 point1 = this.linePositions[0];
Vector3 point2 = Hit.transform.gameObject.transform.position;
this.DeactivateLineRenderer();
CommunicationEvents.AddLineEvent.Invoke(point1, point2);
break;
}
//If no Point was hit
else
{
//TODO: Hint that only a line can be drawn between already existing points
this.DeactivateLineRenderer();
}
}
//Wenn der erste Punkt noch nicht markiert wurde
else
{
//Check if a Point was hit
if (Physics.Raycast(ray, out Hit, 30f, layerMask))
{
//Set LineRenderer activated
this.lineRendererActivated = true;
//Add the position of the hit Point for the start of the Line
Vector3 temp = Hit.transform.gameObject.transform.position;
//temp += Vector3.up;
linePositions.Add(temp);
//The second point is the same point at the moment
linePositions.Add(temp);
this.lineRenderer.SetPosition(0, linePositions[0]);
this.lineRenderer.SetPosition(1, linePositions[1]);
}
else
{
//TODO: Hint that only a line can be drawn between already existing points
}
}
*/
}
public int GetFirstEmptyID() public int GetFirstEmptyID()
{ {
...@@ -157,6 +99,7 @@ public int GetFirstEmptyID() ...@@ -157,6 +99,7 @@ public int GetFirstEmptyID()
public void OnToolModeChanged(ToolMode ActiveToolMode) public void OnToolModeChanged(ToolMode ActiveToolMode)
{ {
switch (ActiveToolMode) switch (ActiveToolMode)
{ {
case ToolMode.MarkPointMode: case ToolMode.MarkPointMode:
...@@ -210,45 +153,89 @@ public void OnToolModeChanged(ToolMode ActiveToolMode) ...@@ -210,45 +153,89 @@ public void OnToolModeChanged(ToolMode ActiveToolMode)
} }
break; break;
case ToolMode.ExtraMode: case ToolMode.ExtraMode:
foreach (Fact fact in Facts) /*foreach (Fact fact in Facts)
{ {
} }
*/
break; break;
} }
} }
public void OnHit(RaycastHit hit) public void OnHit(RaycastHit hit)
{ {
Debug.Log(CommunicationEvents.ActiveToolMode);
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Point"))
{
//hit existing point, so delete it
if (CommunicationEvents.ActiveToolMode == ToolMode.ExtraMode)
{
var menu = GameObject.Instantiate(SmartMenu);
menu.GetComponent<Canvas>().worldCamera = Camera.main;
menu.transform.SetParent(hit.transform);
menu.transform.localPosition = Vector3.up - Camera.main.transform.forward;
}
else
{
char letter = hit.transform.gameObject.GetComponentInChildren<TextMeshPro>().text.ToCharArray()[0];
int id = letter - 65;
CommunicationEvents.RemoveFactEvent.Invoke(Facts[id]);
}
} switch (ActiveToolMode)
else
{ {
PointFact fact = AddPointFact(hit, GetFirstEmptyID()); //If Left-Mouse-Button was pressed in MarkPointMode
CommunicationEvents.AddFactEvent.Invoke(fact); 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];
if (this.lineModeFirstPointSelected)
{
//Event for end of line-rendering in "ShinyThings"
CommunicationEvents.StopLineRendererEvent.Invoke(null);
//Create LineFact
CommunicationEvents.AddFactEvent.Invoke(this.AddLineFact(this.firstPointSelected.Id, tempFact.Id, this.GetFirstEmptyID()));
this.lineModeFirstPointSelected = false;
this.firstPointSelected = null;
}
else {
//Activate LineRenderer for preview
this.lineModeFirstPointSelected = true;
this.firstPointSelected = tempFact;
//Event for start line-rendering in "ShinyThings"
CommunicationEvents.StartLineRendererEvent.Invoke(this.firstPointSelected);
}
}
//If no Point was hit
else {
if (this.lineModeFirstPointSelected)
{
//Deactivate LineRendering and first point selection
this.lineModeFirstPointSelected = false;
this.firstPointSelected = null;
//Event for end of line-rendering in "ShinyThings"
CommunicationEvents.StopLineRendererEvent.Invoke(null);
}
//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:
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
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Point") || hit.transform.gameObject.layer == LayerMask.NameToLayer("Line") || hit.transform.gameObject.layer == LayerMask.NameToLayer("Angle")){
//Search for the suitable fact from the List
this.DeleteFact(Facts[hit.transform.GetComponent<FactObject>().Id]);
}
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<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;
}
}
} }
...@@ -29,9 +29,6 @@ public class FactEvent : UnityEvent<Fact> ...@@ -29,9 +29,6 @@ public class FactEvent : UnityEvent<Fact>
{ {
} }
public class MouseOverFactEvent : UnityEvent<Transform> public class MouseOverFactEvent : UnityEvent<Transform>
{ {
...@@ -41,6 +38,13 @@ public class ToolModeEvent : UnityEvent<ToolMode> { ...@@ -41,6 +38,13 @@ public class ToolModeEvent : UnityEvent<ToolMode> {
} }
public class ShinyEvent : UnityEvent<Fact> {
}
public static HitEvent TriggerEvent = new HitEvent(); public static HitEvent TriggerEvent = new HitEvent();
public static ToolModeEvent ToolModeChangedEvent = new ToolModeEvent(); public static ToolModeEvent ToolModeChangedEvent = new ToolModeEvent();
...@@ -52,11 +56,21 @@ public class ToolModeEvent : UnityEvent<ToolMode> { ...@@ -52,11 +56,21 @@ public class ToolModeEvent : UnityEvent<ToolMode> {
public static FactEvent AddFactEvent = new FactEvent(); public static FactEvent AddFactEvent = new FactEvent();
public static FactEvent RemoveFactEvent = new FactEvent(); public static FactEvent RemoveFactEvent = new FactEvent();
//public static MouseOverFactEvent HighlightEvent = new MouseOverFactEvent();
//public static MouseOverFactEvent EndHighlightEvent = new MouseOverFactEvent();
public static ShinyEvent StartLineRendererEvent = new ShinyEvent();
public static ShinyEvent StopLineRendererEvent = new ShinyEvent();
//------------------------------------------------------------------------------------
//-------------------------------Global Variables-------------------------------------
//Global ActiveToolMode
public static ToolMode ActiveToolMode { get; set; } public static ToolMode ActiveToolMode { get; set; }
//Global List of Facts
public static List<Fact> Facts = new List<Fact>(); public static List<Fact> Facts = new List<Fact>();
// public static MouseOverFactEvent HighlightEvent = new MouseOverFactEvent();
//public static MouseOverFactEvent EndHighlightEvent = new MouseOverFactEvent();
} }
using UnityEngine; using UnityEngine;
public class Fact public abstract class Fact
{ {
public int Id; public int Id;
public GameObject Representation; public GameObject Representation;
...@@ -14,14 +14,17 @@ public class PointFact : Fact ...@@ -14,14 +14,17 @@ public class PointFact : Fact
} }
public class LineFact : Fact public class LineFact : Fact
{ {
//Id's of the 2 Point-Facts that are connected
public int Pid1, Pid2; public int Pid1, Pid2;
} }
public class AngleFact : Fact public class AngleFact : Fact
{ {
//Id's of the 3 Point-Facts, where Pid2 is the point, where the angle is
public int Pid1, Pid2, Pid3; public int Pid1, Pid2, Pid3;
} }
public class OnLineFact : Fact public class OnLineFact : Fact
{ {
//Id's of the 3 Point-Facs that are on one line
public int Pid1, Pid2, Pid3; public int Pid1, Pid2, Pid3;
} }
...@@ -38,18 +38,16 @@ public void FactAction(Fact fact) ...@@ -38,18 +38,16 @@ public void FactAction(Fact fact)
public void SpawnPoint(PointFact fact) public void SpawnPoint(PointFact fact)
{ {
PointFact pointFact = fact;
this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Sphere", typeof(GameObject)); this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Sphere", typeof(GameObject));
GameObject point = GameObject.Instantiate(FactRepresentation); GameObject point = GameObject.Instantiate(FactRepresentation);
point.transform.position = pointFact.Point; point.transform.position = fact.Point;
point.transform.up = pointFact.Normal; point.transform.up = fact.Normal;
string letter = ((Char)(64+fact.Id+1)).ToString(); string letter = ((Char)(64+fact.Id+1)).ToString();
point.GetComponentInChildren<TextMeshPro>().text = letter; point.GetComponentInChildren<TextMeshPro>().text = letter;
point.GetComponent<FactObject>().Id = fact.Id; point.GetComponent<FactObject>().Id = fact.Id;
pointFact.Representation = point; fact.Representation = point;
//If a new Point was spawned -> We are in MarkPointMode -> Then we want the collider to be disabled //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 //Hint: Thats why by now, if we mark a Point in an other mode than MarkPointMode, the
...@@ -63,14 +61,13 @@ public void SpawnPoint(PointFact fact) ...@@ -63,14 +61,13 @@ public void SpawnPoint(PointFact fact)
public void DeleteObject(Fact fact) public void DeleteObject(Fact fact)
{ {
Debug.Log("delete obj"); Debug.Log("delete obj");
GameObject point = fact.Representation; GameObject factRepresentation = fact.Representation;
GameObject.Destroy(point); GameObject.Destroy(factRepresentation);
} }
public void SpawnLine(LineFact lineFact) { public void SpawnLine(LineFact lineFact) {
Vector3 point1 = (Facts[lineFact.Pid1] as PointFact).Point; Vector3 point1 = (Facts[lineFact.Pid1] as PointFact).Point;
Vector3 point2 = (Facts[lineFact.Pid2] as PointFact).Point; Vector3 point2 = (Facts[lineFact.Pid2] as PointFact).Point;
//Change FactRepresentation to Line //Change FactRepresentation to Line
......
...@@ -17,18 +17,17 @@ public class ShinyThings : MonoBehaviour ...@@ -17,18 +17,17 @@ public class ShinyThings : MonoBehaviour
public LineRenderer lineRenderer; public LineRenderer lineRenderer;
private List<Vector3> linePositions = new List<Vector3>(); private List<Vector3> linePositions = new List<Vector3>();
private bool lineRendererActivated; private bool lineRendererActivated;
//Visual helpers
// Start is called before the first frame update // Start is called before the first frame update
void Start() public void Start()
{ {
if(Cursor == null)Cursor = GetComponent<WorldCursor>(); if(Cursor == null)Cursor = GetComponent<WorldCursor>();
// CommunicationEvents.HighlightEvent.AddListener(OnMouseOverFact); CommunicationEvents.StartLineRendererEvent.AddListener(ActivateLineRenderer);
// CommunicationEvents.EndHighlightEvent.AddListener(OnMouseOverFactEnd); CommunicationEvents.StopLineRendererEvent.AddListener(DeactivateLineRenderer);
} }
// Update is called once per frame // Update is called once per frame
void Update() public void Update()
{ {
//SELECTION-HIGHLIGHTING-PART //SELECTION-HIGHLIGHTING-PART
//Check if a Fact was Hit //Check if a Fact was Hit
...@@ -57,12 +56,27 @@ void Update() ...@@ -57,12 +56,27 @@ void Update()
OnMouseOverFact(lastFactSelection); OnMouseOverFact(lastFactSelection);
} }
//SELECTION-HIGHLIGHTING-PART-END //SELECTION-HIGHLIGHTING-PART-END
//LineRendering-Part
if (this.lineRendererActivated)
UpdateLineRenderer(Hit.point);
} }
} }
public void OnMouseOverFact(Transform selection)
{
Renderer selectionRenderer;
selectionRenderer = selection.GetComponent<Renderer>();
if (selectionRenderer != null)
{
//Set the Material of the Fact, where the mouse is over, to a special one
selectionRenderer.material = highlightMaterial;
}
}
public void OnMouseOverFactEnd(Transform selection) public void OnMouseOverFactEnd(Transform selection)
{ {
Renderer selectionRenderer; Renderer selectionRenderer;
...@@ -78,20 +92,28 @@ public void OnMouseOverFactEnd(Transform selection) ...@@ -78,20 +92,28 @@ public void OnMouseOverFactEnd(Transform selection)
} }
} }
public void OnMouseOverFact(Transform selection) public void ActivateLineRenderer(Fact startFact) {
{ //Set LineRenderer activated
Renderer selectionRenderer; this.lineRendererActivated = true;
//Add the position of the Fact for the start of the Line
linePositions.Add(startFact.Representation.transform.position);
//The second point is the same point at the moment
linePositions.Add(startFact.Representation.transform.position);
this.lineRenderer.SetPosition(0, linePositions[0]);
this.lineRenderer.SetPosition(1, linePositions[1]);
selectionRenderer = selection.GetComponent<Renderer>();
if (selectionRenderer != null)
{
//Set the Material of the Fact, where the mouse is over, to a special one
selectionRenderer.material = highlightMaterial;
}
} }
//Updates the second-point of the Line when First Point was selected in LineMode
public void UpdateLineRenderer(Vector3 currentPosition)
{
this.linePositions[1] = currentPosition;
this.lineRenderer.SetPosition(1, this.linePositions[1]);
}
void DeactivateLineRenderer() //Deactivate LineRenderer so that no Line gets drawn when Cursor changes
public void DeactivateLineRenderer(Fact startFact)
{ {
//Reset the first points //Reset the first points
this.lineRenderer.SetPosition(0, Vector3.zero); this.lineRenderer.SetPosition(0, Vector3.zero);
...@@ -100,17 +122,4 @@ void DeactivateLineRenderer() ...@@ -100,17 +122,4 @@ void DeactivateLineRenderer()
this.linePositions.Clear(); this.linePositions.Clear();
this.lineRendererActivated = false; this.lineRendererActivated = false;
} }
//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.lineRendererActivated)
{
this.linePositions[1] = currentPosition;
this.lineRenderer.SetPosition(1, this.linePositions[1]);
}
}
}
} }
...@@ -4,25 +4,20 @@ ...@@ -4,25 +4,20 @@
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using static CommunicationEvents;
public class WorldCursor : MonoBehaviour public class WorldCursor : MonoBehaviour
{ {
public RaycastHit Hit; public RaycastHit Hit;
private Camera Cam; private Camera Cam;
private ToolMode ActiveToolMode{get; set;}
void Start() void Start()
{ {
Cam = Camera.main; Cam = Camera.main;
//Set MarkPointMode as the default ActiveToolMode //Set MarkPointMode as the default ActiveToolMode
this.ActiveToolMode = ToolMode.ExtraMode;//ToolMode.MarkPointMode; ActiveToolMode = ToolMode.ExtraMode;//ToolMode.MarkPointMode;
CommunicationEvents.ToolModeChangedEvent.Invoke(this.ActiveToolMode); CommunicationEvents.ToolModeChangedEvent.Invoke(ActiveToolMode);
//TODO: we probably can configure these things to automatically trigger when the variable is changed...
CommunicationEvents.ActiveToolMode = this.ActiveToolMode;
//redundant for now, but we probably want to have the activetool mode available globally
} }
// Update is called once per frame // Update is called once per frame
...@@ -55,9 +50,6 @@ void Update() ...@@ -55,9 +50,6 @@ void Update()
} }
//Deactivate LineRenderer so that no Line gets drawn when Cursor changes
//Check if left Mouse-Button was pressed and handle it //Check if left Mouse-Button was pressed and handle it
void CheckMouseButtons(Ray ray) void CheckMouseButtons(Ray ray)
{ {
...@@ -71,20 +63,19 @@ void CheckMouseButtons(Ray ray) ...@@ -71,20 +63,19 @@ void CheckMouseButtons(Ray ray)
} }
} }
//Checks if the ToolMode was switched by User, and handle it
void CheckToolModeSelection() { void CheckToolModeSelection() {
if (Input.GetButtonDown("ToolMode")) { if (Input.GetButtonDown("ToolMode")) {
//Change the ActiveToolMode dependent on which Mode was selected //Change the ActiveToolMode dependent on which Mode was selected
if ((int)this.ActiveToolMode == Enum.GetNames(typeof(ToolMode)).Length - 1) if ((int)ActiveToolMode == Enum.GetNames(typeof(ToolMode)).Length - 1)
{ {
this.ActiveToolMode = 0; ActiveToolMode = 0;
} }
else { else {
this.ActiveToolMode++; ActiveToolMode++;
} }
CommunicationEvents.ActiveToolMode = this.ActiveToolMode;
//Invoke the Handler for the Facts //Invoke the Handler for the Facts
CommunicationEvents.ToolModeChangedEvent.Invoke(this.ActiveToolMode); CommunicationEvents.ToolModeChangedEvent.Invoke(ActiveToolMode);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment