You need to sign in or sign up before continuing.
Newer
Older
using TMPro;
using UnityEngine;
using static CommunicationEvents;
public class FactManager : MonoBehaviour
{
public GameObject SmartMenu;
private Stack<int> NextEmptyStack = new Stack<int>();
//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 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()
{
}
{
Id = id,
});
}
{
Id = id,
Pid1 = pid1,
});
AngleFact AddAngleFact(int pid1, int pid2, int pid3, int id)
{
Id = id,
});
public void DeleteFact(Fact fact)
John Schihada
committed
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);
}
}
/* 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;
}
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
foreach (Fact fact in 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
foreach (Fact fact in Facts)
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
foreach (Fact fact in Facts)
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
foreach (Fact fact in Facts)
GameObject gO = fact.Representation;
gO.GetComponentInChildren<Collider>().enabled = true;
}
break;
case ToolMode.ExtraMode:
break;
}
}
public void OnHit(RaycastHit hit)
{
//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];
//Event for end of line-drawing in "ShinyThings"
CommunicationEvents.StopLineDrawingEvent.Invoke(null);
CommunicationEvents.AddFactEvent.Invoke(this.AddLineFact(this.lineModeFirstPointSelected.Id, tempFact.Id, this.GetFirstEmptyID()));
this.lineModeIsFirstPointSelected = false;
this.lineModeFirstPointSelected = null;
else
{
//Activate LineDrawing for preview
this.lineModeIsFirstPointSelected = true;
this.lineModeFirstPointSelected = tempFact;
//Event for start line-drawing in "ShinyThings"
CommunicationEvents.StartLineDrawingEvent.Invoke(this.lineModeFirstPointSelected);
else
{
if (this.lineModeIsFirstPointSelected)
//Deactivate LineDrawing and first point selection
this.lineModeIsFirstPointSelected = false;
this.lineModeFirstPointSelected = null;
//Event for end of line-drawing in "ShinyThings"
CommunicationEvents.StopLineDrawingEvent.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:
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//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
//TODO: CommunicationEvents.AddFactEvent.Invoke(this.AddAngleFact());
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
{
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
}
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"))
{
John Schihada
committed
this.DeleteFact(Facts.Find(x => x.Id == 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<SmartMenu>().FactManager = this;
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;