1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
using System.Collections.Generic;
using UnityEngine;
using TMPro;
namespace JoystickLab
{
struct LineProp
{
public GameObject point;
public GameObject distanceText;
public LineProp(GameObject point, GameObject distanceText)
{
this.point = point;
this.distanceText = distanceText;
}
public void SetEnable(bool enabled)
{
if (distanceText != null)
{
distanceText.SetActive(enabled);
}
point.SetActive(enabled);
}
}
public class LineRendererDrawing : SingleToneManager<LineRendererDrawing>
{
public Material lineMaterial;
public Material dottedLineMaterial;
//public TextMesh distanceText;
public GameObject distanceTextButton;
public UnitConverter distanceUIBox;
private List<Transform> clickPoints;
private List<Vector3> clickPositions;
public float lineWidth;
GameObject textObj = null;
LineRenderer lineRenderer;
private Stack<LineProp> pointStack;
private bool isDisCrete;
public bool IsDisCrete
{
private get { return isDisCrete; }
set
{
isDisCrete = value;
textObj.SetActive(false);
}
}
// Use this for initialization
void Start()
{
//lineRenderer = GetComponent<LineRenderer>();
clickPoints = new List<Transform>();
lineRenderer = new LineRenderer();
pointStack = new Stack<LineProp>();
textObj = Instantiate(distanceTextButton.gameObject, Vector3.zero, Quaternion.identity);
textObj.name = "DottedDistanceLabel";
textObj.SetActive(false);
}
public float DrawLine(GameObject point, bool done)
{
float distance = 0;
int lastIndex = clickPoints.Count - 1;
Transform startpoint = null;
Transform endPoint = null;
if (!done && clickPoints.Count > 0)
{
//Debug.Log("Dotted");
lineRenderer.material = dottedLineMaterial;
lineRenderer.textureMode = LineTextureMode.Tile;
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, clickPoints[lastIndex].position);
lineRenderer.SetPosition(1, point.transform.position);
startpoint = clickPoints[lastIndex];
endPoint = point.transform;
}
if (done) // done == true when user click
{
//Debug.Log("Straight");
clickPoints.Add(point.transform);
pointStack.Push(new LineProp(point, textObj));
point.transform.name = clickPoints.Count.ToString();
//print("The Pushed Point is = "+ point.name+ " and the text is = "+textObj.name);
lastIndex = clickPoints.Count - 1;
if (lineRenderer != null)
{
lineRenderer.material = lineMaterial;
}
lineRenderer = point.AddComponent<LineRenderer>();
lineRenderer.material = lineMaterial;
lineRenderer.startWidth = lineRenderer.endWidth = lineWidth;
if (clickPoints.Count >= 2)
{
startpoint = clickPoints[lastIndex];
endPoint = clickPoints[lastIndex - 1];
if (IsDisCrete)
{
ClearPoints();
}
}
}
if (startpoint != null && endPoint != null)
{
Vector3 resultant = endPoint.position - startpoint.position;
distance = Vector3.Magnitude(resultant);
float angel = Mathf.Atan2(resultant.z, resultant.x) * Mathf.Rad2Deg;
DrawLength(endPoint.position, startpoint.position, distance, angel, done);
}
return distance;
}
void DrawLength(Vector3 pointOne, Vector3 pointTwo, float distance, float yAngle, bool done)
{
string unit;
string finalOutputText;
Vector3 point = (pointOne + pointTwo) / 2;
point.y += 0.02f;
if (done)
{
textObj = Instantiate(distanceTextButton.gameObject, point, Quaternion.identity);
textObj.name = clickPoints.Count.ToString() + "th Label";
if (pointStack.Count > 0)
{
LineProp l = pointStack.Pop();
l.distanceText = textObj;
pointStack.Push(l);
}
}
else if (!done)
{
if (distance * 100 > 5f && pointStack.Count>0)
{
textObj.SetActive(true);
}
else
{
textObj.SetActive(false);
}
textObj.transform.position = point;
}
finalOutputText = processDistance(distance, out unit);
textObj.GetComponentInChildren<TextMeshProUGUI>().text = finalOutputText + " <size=2>" + unit;
Vector3 prevAngle = textObj.transform.eulerAngles;
prevAngle.y = -yAngle;
prevAngle.x = 90;
textObj.transform.eulerAngles = prevAngle;
}
public void ClearPoints()
{
clickPoints.Clear();
}
public void Undo()
{
if (pointStack.Count > 0)
{
pointStack.Pop().SetEnable(false);
textObj.SetActive(false);
}
ClearPoints();
}
private string processDistance(float distance, out string unit)
{
float d = distance * 100;
unit = "cm";
if (d > 100)
{
d = distance;
unit = "m";
}
return d.ToString("F1");
}
public void ShowDistanceBox(string output)
{
int leftarrowIndex = output.IndexOf('<');
int rightarrawIndex = output.IndexOf('>');
string ans = output.Substring(0,leftarrowIndex-1);
print("leng"+output.Length);
string unit = output.Substring(rightarrawIndex+1, output.Length - rightarrawIndex-1);
distanceUIBox.output = ans+ " <size=80>" +unit;
distanceUIBox.gameObject.SetActive(true);
}
public void HideDistanceBox()
{
distanceUIBox.gameObject.SetActive(false);
}
}
}
|