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

Added Line-Drawing Functionality - Still a problem with the TextMeshPro-Component of the line

parent 6fe7a0ad
Branches
No related tags found
No related merge requests found
Showing
with 331 additions and 18 deletions
...@@ -9,6 +9,11 @@ public class PointEvent : UnityEvent<RaycastHit,int> ...@@ -9,6 +9,11 @@ public class PointEvent : UnityEvent<RaycastHit,int>
{ {
} }
public class LineEvent : UnityEvent<Vector3, Vector3> {
}
public class HitEvent : UnityEvent<RaycastHit> public class HitEvent : UnityEvent<RaycastHit>
{ {
...@@ -30,7 +35,8 @@ public class ToolModeEvent : UnityEvent<ToolMode> { ...@@ -30,7 +35,8 @@ public class ToolModeEvent : UnityEvent<ToolMode> {
public static MouseOverFactEvent HighlightEvent = new MouseOverFactEvent(); public static MouseOverFactEvent HighlightEvent = new MouseOverFactEvent();
public static MouseOverFactEvent EndHighlightEvent = new MouseOverFactEvent(); public static MouseOverFactEvent EndHighlightEvent = new MouseOverFactEvent();
public static ToolModeEvent ToolModeChangedEvent = new ToolModeEvent(); public static ToolModeEvent ToolModeChangedEvent = new ToolModeEvent();
public static PointEvent AddEvent = new PointEvent(); public static PointEvent AddPointEvent = new PointEvent();
public static LineEvent AddLineEvent = new LineEvent();
public static FactEvent RemoveEvent = new FactEvent(); public static FactEvent RemoveEvent = new FactEvent();
public static ToolMode ActiveToolMode { get; set; } public static ToolMode ActiveToolMode { get; set; }
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
public class FactSpawner : MonoBehaviour public class FactSpawner : MonoBehaviour
{ {
public GameObject FactRepresentation; private GameObject FactRepresentation;
public string[] Facts = new String[100]; public string[] Facts = new String[100];
public GameObject[] GameObjectFacts = new GameObject[100]; public GameObject[] GameObjectFacts = new GameObject[100];
...@@ -22,10 +22,12 @@ void Start() ...@@ -22,10 +22,12 @@ void Start()
CommunicationEvents.EndHighlightEvent.AddListener(OnMouseOverFactEnd); CommunicationEvents.EndHighlightEvent.AddListener(OnMouseOverFactEnd);
CommunicationEvents.TriggerEvent.AddListener(OnHit); CommunicationEvents.TriggerEvent.AddListener(OnHit);
CommunicationEvents.ToolModeChangedEvent.AddListener(OnToolModeChanged); CommunicationEvents.ToolModeChangedEvent.AddListener(OnToolModeChanged);
CommunicationEvents.AddEvent.AddListener(SpawnFact); CommunicationEvents.AddPointEvent.AddListener(SpawnPoint);
CommunicationEvents.AddLineEvent.AddListener(SpawnLine);
CommunicationEvents.RemoveEvent.AddListener(DeletePoint); CommunicationEvents.RemoveEvent.AddListener(DeletePoint);
//Default FactRepresenation = Sphere-Prefab for Points
this.FactRepresentation = (GameObject) Resources.Load("Prefabs/Sphere", typeof(GameObject));
} }
...@@ -41,13 +43,9 @@ public int GetFirstEmptyID() ...@@ -41,13 +43,9 @@ public int GetFirstEmptyID()
} }
public void SpawnFact(RaycastHit hit, int id) {
SpawnPoint(hit, id);
}
public void SpawnPoint(RaycastHit hit, int id) public void SpawnPoint(RaycastHit hit, int id)
{ {
this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Sphere", typeof(GameObject));
Debug.Log(id); Debug.Log(id);
GameObject point = GameObject.Instantiate(FactRepresentation); GameObject point = GameObject.Instantiate(FactRepresentation);
point.transform.position = hit.point; point.transform.position = hit.point;
...@@ -72,6 +70,33 @@ public void DeletePoint(int id) ...@@ -72,6 +70,33 @@ public void DeletePoint(int id)
Facts[id] = ""; Facts[id] = "";
} }
public void SpawnLine(Vector3 point1, Vector3 point2) {
int id = GetFirstEmptyID();
Debug.Log(id);
//Change FactRepresentation to Line
this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Line2", typeof(GameObject));
GameObject line = GameObject.Instantiate(FactRepresentation);
//Place the Line in the centre of the two points
//and change scale and rotation, so that the two points are connected by the line
line.transform.position = Vector3.Lerp(point1, point2, 0.5f);
var v3T = line.transform.localScale;
v3T.y = (point2 - point1).magnitude;
//x and z of the line/Cube-GameObject here hard coded = ratio of sphere-prefab
v3T.x = 0.1f;
v3T.z = 0.1f;
line.transform.localScale = v3T;
line.transform.rotation = Quaternion.FromToRotation(Vector3.up, point2 - point1);
string letter = ((Char)(64 + id + 1)).ToString();
line.GetComponentInChildren<TextMeshPro>().text = letter;
line.GetComponent<FactObject>().Id = 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;
}
public void OnMouseOverFactEnd(Transform selection) public void OnMouseOverFactEnd(Transform selection)
{ {
Renderer selectionRenderer; Renderer selectionRenderer;
...@@ -122,7 +147,7 @@ public void OnHit(RaycastHit hit) ...@@ -122,7 +147,7 @@ public void OnHit(RaycastHit hit)
else else
{ {
CommunicationEvents.AddEvent.Invoke(hit, GetFirstEmptyID()); CommunicationEvents.AddPointEvent.Invoke(hit, GetFirstEmptyID());
} }
} }
......
...@@ -27,7 +27,7 @@ void Start() ...@@ -27,7 +27,7 @@ void Start()
mmtServerProcess = Process.Start(mmtServerProcessInfo); mmtServerProcess = Process.Start(mmtServerProcessInfo);
*/ */
CommunicationEvents.AddEvent.AddListener(AddFactToMMT); CommunicationEvents.AddPointEvent.AddListener(AddFactToMMT);
CommunicationEvents.RemoveEvent.AddListener(RemoveFactFromMMT); CommunicationEvents.RemoveEvent.AddListener(RemoveFactFromMMT);
......
...@@ -11,9 +11,14 @@ public class WorldCursor : MonoBehaviour ...@@ -11,9 +11,14 @@ public class WorldCursor : MonoBehaviour
private Camera Cam; private Camera Cam;
private ToolMode ActiveToolMode{get; set;} private ToolMode ActiveToolMode{get; set;}
//Attributes for Highlighting of Facts when Mouse-Over
private string selectableTag = "Selectable"; private string selectableTag = "Selectable";
private Transform lastFactSelection; private Transform lastFactSelection;
//Attributes for simulating the drawing of a line
public LineRenderer lineRenderer;
private List<Vector3> linePositions = new List<Vector3>();
private bool lineRendererActivated;
void Start() void Start()
{ {
...@@ -64,7 +69,9 @@ void Update() ...@@ -64,7 +69,9 @@ void Update()
} }
//SELECTION-HIGHLIGHTING-PART-END //SELECTION-HIGHLIGHTING-PART-END
CheckMouseButtons(); CheckMouseButtons(ray);
UpdateLineRenderer(transform.position);
} }
else else
...@@ -78,15 +85,112 @@ void Update() ...@@ -78,15 +85,112 @@ void Update()
} }
void CheckMouseButtons() //Deactivate LineRenderer so that no Line gets drawn when Cursor changes
void DeactivateLineRenderer()
{ {
//Reset the first points
//send HitEvent this.lineRenderer.SetPosition(0, Vector3.zero);
if (Input.GetMouseButtonDown(0)){ this.lineRenderer.SetPosition(1, Vector3.zero);
if (EventSystem.current.IsPointerOverGameObject()) return; if (linePositions.Count > 0)
CommunicationEvents.TriggerEvent.Invoke(Hit); this.linePositions.Clear();
this.lineRendererActivated = false;
}
//Check if left Mouse-Button was pressed and handle it
void CheckMouseButtons(Ray ray)
{
if (Input.GetMouseButtonDown(0))
{
switch (this.ActiveToolMode)
{
case ToolMode.MarkPointMode:
//send HitEvent
CommunicationEvents.TriggerEvent.Invoke(Hit);
break;
case ToolMode.ExtraMode:
//send HitEvent
CommunicationEvents.TriggerEvent.Invoke(Hit);
break;
case ToolMode.DeleteMode:
//send HitEvent
CommunicationEvents.TriggerEvent.Invoke(Hit);
break;
case ToolMode.CreateLineMode:
//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)
{
//If a second Point was Hit
if (Physics.Raycast(ray, out Hit, 30f, layerMask))
{
//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
}
}
break;
}
} }
}
//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]);
}
}
} }
void CheckToolModeSelection() { void CheckToolModeSelection() {
......
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Crosshair
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
fileFormatVersion: 2
guid: a09c41e07a71f7145bde02d9edc5c747
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: LineMaterial
m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 4000
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.990566, g: 0.97702986, b: 0, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
fileFormatVersion: 2
guid: a8a7bf60a30970f469a9c9d3ae2de6ef
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: f89bd8b5ca144814885dc5929e10c1ec
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment