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

Refactoring completed

parent d1d9dcd1
Branches
Tags
No related merge requests found
......@@ -5,57 +5,68 @@
using static CommunicationEvents;
public class FactManager : MonoBehaviour
{
public GameObject SmartMenu;
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
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
//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);
}
// 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,
Pid1 = pid1,
Pid2 = pid2
Point = hit.point,
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,
Pid1 = pid1,
Pid2 = pid2,
Pid3 = pid3
Pid2 = pid2
});
}
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 PointFact
Facts.Insert(id, new AngleFact
{
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)
......@@ -63,78 +74,9 @@ void DeleteFact(Fact fact)
NextEmptyStack.Push(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()
{
......@@ -157,6 +99,7 @@ public int GetFirstEmptyID()
public void OnToolModeChanged(ToolMode ActiveToolMode)
{
switch (ActiveToolMode)
{
case ToolMode.MarkPointMode:
......@@ -210,45 +153,89 @@ public void OnToolModeChanged(ToolMode ActiveToolMode)
}
break;
case ToolMode.ExtraMode:
foreach (Fact fact in Facts)
/*foreach (Fact fact in Facts)
{
}
*/
break;
}
}
public void OnHit(RaycastHit hit)
{
Debug.Log(CommunicationEvents.ActiveToolMode);
switch (ActiveToolMode)
{
//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"))
{
//hit existing point, so delete it
if (CommunicationEvents.ActiveToolMode == ToolMode.ExtraMode)
Fact tempFact = Facts[hit.transform.GetComponent<FactObject>().Id];
if (this.lineModeFirstPointSelected)
{
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;
//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
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)
{
char letter = hit.transform.gameObject.GetComponentInChildren<TextMeshPro>().text.ToCharArray()[0];
int id = letter - 65;
CommunicationEvents.RemoveFactEvent.Invoke(Facts[id]);
//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;
}
}
}
......@@ -30,9 +30,6 @@ public class FactEvent : UnityEvent<Fact>
}
public class MouseOverFactEvent : UnityEvent<Transform>
{
......@@ -41,6 +38,13 @@ public class ToolModeEvent : UnityEvent<ToolMode> {
}
public class ShinyEvent : UnityEvent<Fact> {
}
public static HitEvent TriggerEvent = new HitEvent();
public static ToolModeEvent ToolModeChangedEvent = new ToolModeEvent();
......@@ -52,11 +56,21 @@ public class ToolModeEvent : UnityEvent<ToolMode> {
public static FactEvent AddFactEvent = 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; }
//Global List of Facts
public static List<Fact> Facts = new List<Fact>();
// public static MouseOverFactEvent HighlightEvent = new MouseOverFactEvent();
//public static MouseOverFactEvent EndHighlightEvent = new MouseOverFactEvent();
}
using UnityEngine;
public class Fact
public abstract class Fact
{
public int Id;
public GameObject Representation;
......@@ -14,14 +14,17 @@ public class PointFact : Fact
}
public class LineFact : Fact
{
//Id's of the 2 Point-Facts that are connected
public int Pid1, Pid2;
}
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 class OnLineFact : Fact
{
//Id's of the 3 Point-Facs that are on one line
public int Pid1, Pid2, Pid3;
}
......@@ -39,17 +39,15 @@ public void FactAction(Fact fact)
public void SpawnPoint(PointFact fact)
{
PointFact pointFact = fact;
this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Sphere", typeof(GameObject));
GameObject point = GameObject.Instantiate(FactRepresentation);
point.transform.position = pointFact.Point;
point.transform.up = pointFact.Normal;
point.transform.position = fact.Point;
point.transform.up = fact.Normal;
string letter = ((Char)(64+fact.Id+1)).ToString();
point.GetComponentInChildren<TextMeshPro>().text = letter;
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
//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)
public void DeleteObject(Fact fact)
{
Debug.Log("delete obj");
GameObject point = fact.Representation;
GameObject.Destroy(point);
GameObject factRepresentation = fact.Representation;
GameObject.Destroy(factRepresentation);
}
public void SpawnLine(LineFact lineFact) {
Vector3 point1 = (Facts[lineFact.Pid1] as PointFact).Point;
Vector3 point2 = (Facts[lineFact.Pid2] as PointFact).Point;
//Change FactRepresentation to Line
......
......@@ -17,18 +17,17 @@ public class ShinyThings : MonoBehaviour
public LineRenderer lineRenderer;
private List<Vector3> linePositions = new List<Vector3>();
private bool lineRendererActivated;
//Visual helpers
// Start is called before the first frame update
void Start()
public void Start()
{
if(Cursor == null)Cursor = GetComponent<WorldCursor>();
// CommunicationEvents.HighlightEvent.AddListener(OnMouseOverFact);
// CommunicationEvents.EndHighlightEvent.AddListener(OnMouseOverFactEnd);
CommunicationEvents.StartLineRendererEvent.AddListener(ActivateLineRenderer);
CommunicationEvents.StopLineRendererEvent.AddListener(DeactivateLineRenderer);
}
// Update is called once per frame
void Update()
public void Update()
{
//SELECTION-HIGHLIGHTING-PART
//Check if a Fact was Hit
......@@ -57,11 +56,26 @@ void Update()
OnMouseOverFact(lastFactSelection);
}
//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)
{
......@@ -78,20 +92,28 @@ public void OnMouseOverFactEnd(Transform selection)
}
}
public void OnMouseOverFact(Transform selection)
{
Renderer selectionRenderer;
public void ActivateLineRenderer(Fact startFact) {
//Set LineRenderer activated
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
this.lineRenderer.SetPosition(0, Vector3.zero);
......@@ -100,17 +122,4 @@ void DeactivateLineRenderer()
this.linePositions.Clear();
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 @@
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using static CommunicationEvents;
public class WorldCursor : MonoBehaviour
{
public RaycastHit Hit;
private Camera Cam;
private ToolMode ActiveToolMode{get; set;}
void Start()
{
Cam = Camera.main;
//Set MarkPointMode as the default ActiveToolMode
this.ActiveToolMode = ToolMode.ExtraMode;//ToolMode.MarkPointMode;
CommunicationEvents.ToolModeChangedEvent.Invoke(this.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
ActiveToolMode = ToolMode.ExtraMode;//ToolMode.MarkPointMode;
CommunicationEvents.ToolModeChangedEvent.Invoke(ActiveToolMode);
}
// Update is called once per frame
......@@ -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
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() {
if (Input.GetButtonDown("ToolMode")) {
//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 {
this.ActiveToolMode++;
ActiveToolMode++;
}
CommunicationEvents.ActiveToolMode = this.ActiveToolMode;
//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.
Please register or to comment