Subversion Repository Public Repository

Nextrek

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
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"PlayerCursor.cs"
 * 
 *	This script is adapted from the code found here:
 *	http://forum.unity3d.com/threads/71979-Drawing-lines-in-the-editor
 * 
 */



using UnityEngine;
using System.Collections;

public class DrawStraightLine
{

	static Texture2D _aaLineTex = null;
	static Texture2D _lineTex = null;


	static Texture2D adLineTex
	{
		get
		{
			if (!_aaLineTex)
			{
				_aaLineTex = new Texture2D(1, 3, TextureFormat.ARGB32, true);
				_aaLineTex.SetPixel(0, 0, new Color(1, 1, 1, 0));
				_aaLineTex.SetPixel(0, 1, Color.white);
				_aaLineTex.SetPixel(0, 2, new Color(1, 1, 1, 0));
				_aaLineTex.Apply();
			}
			return _aaLineTex;
		}
	}


	static Texture2D lineTex
	{
		get
		{
			if (!_lineTex)
			{
				_lineTex = new Texture2D(1, 1, TextureFormat.ARGB32, true);
				_lineTex.SetPixel(0, 1, Color.white);
				_lineTex.Apply();
			}
			return _lineTex;
		}
	}


	static void DrawLineMac (Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
	{
		Color savedColor = GUI.color;
		Matrix4x4 savedMatrix = GUI.matrix;

		float oldWidth = width;

		if (antiAlias)
		{
			width *= 3;
		}

		float angle = Vector3.Angle (pointB - pointA, Vector2.right) * (pointA.y <= pointB.y?1:-1);
		float m = (pointB - pointA).magnitude;
 
		if (m > 0.01f)
		{
			Vector3 dz = new Vector3(pointA.x, pointA.y, 0);
			Vector3 offset = new Vector3((pointB.x - pointA.x) * 0.5f, (pointB.y - pointA.y) * 0.5f, 0f);
 
			Vector3 tmp = Vector3.zero;

			if (antiAlias)
			{
				tmp = new Vector3 (-oldWidth * 1.5f * Mathf.Sin(angle * Mathf.Deg2Rad), oldWidth * 1.5f * Mathf.Cos (angle * Mathf.Deg2Rad));
			}
			else
			{
				tmp = new Vector3 (-oldWidth * 0.5f * Mathf.Sin(angle * Mathf.Deg2Rad), oldWidth * 0.5f * Mathf.Cos (angle * Mathf.Deg2Rad));
			}

			GUI.color = color;
			GUI.matrix = translationMatrix (dz) * GUI.matrix;
			GUIUtility.ScaleAroundPivot (new Vector2 (m, width), new Vector2(-0.5f, 0));
			GUI.matrix = translationMatrix (-dz) * GUI.matrix;
			GUIUtility.RotateAroundPivot (angle, Vector2.zero);
			GUI.matrix = translationMatrix (dz  - tmp - offset) * GUI.matrix;
 
			GUI.DrawTexture(new Rect(0, 0, 1, 1), antiAlias ? adLineTex :  lineTex);
		}

		GUI.matrix = savedMatrix;

		GUI.color = savedColor;
	}


	static void DrawLineWindows (Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
	{

		Color savedColor = GUI.color;
 		Matrix4x4 savedMatrix = GUI.matrix;

 		if (antiAlias)
		{
			width *= 3;
		}

		float angle = Vector3.Angle (pointB - pointA, Vector2.right) * (pointA.y <= pointB.y ? 1 : -1);
		float m = (pointB - pointA).magnitude;

		Vector3 dz = new Vector3(pointA.x, pointA.y, 0);
		GUI.color = color;
		GUI.matrix = translationMatrix(dz) * GUI.matrix;
		GUIUtility.ScaleAroundPivot(new Vector2(m, width), new Vector2(-0.5f, 0));
		GUI.matrix = translationMatrix(-dz) * GUI.matrix;
		GUIUtility.RotateAroundPivot(angle, new Vector2(0, 0));
		GUI.matrix = translationMatrix(dz + new Vector3(width / 2, -m / 2) * Mathf.Sin(angle * Mathf.Deg2Rad)) * GUI.matrix;
 
		GUI.DrawTexture(new Rect(0, 0, 1, 1), !antiAlias ? lineTex : adLineTex);
		GUI.matrix = savedMatrix;
		GUI.color = savedColor;
	}


	public static void Draw (Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
	{
		if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer)
		{
			DrawLineWindows (pointA, pointB, color, width, antiAlias);
		}
		else
		{
			DrawLineMac (pointA, pointB, color, width, antiAlias);
		}
	}
	
	
	public static void DrawBox (Rect rect, Color color, float width, bool antiAlias)
	{
		Draw (new Vector2 (rect.x - 1, rect.y - 1), new Vector2 (rect.x + rect.width + 1, rect.y - 1), color, 1f, false);
		Draw (new Vector2 (rect.x - 1, rect.y - 1), new Vector2 (rect.x - 1, rect.y + rect.height + 1), color, 1f, false);
		Draw (new Vector2 (rect.x + rect.width + 1, rect.y - 1), new Vector2 (rect.x + rect.width + 1, rect.y + rect.height + 1), color, 1f, false);
		Draw (new Vector2 (rect.x - 1, rect.y + rect.height + 1), new Vector2 (rect.x + rect.width + 1, rect.y + rect.height + 1), color, 1f, false);
	}


	private static Matrix4x4 translationMatrix(Vector3 v)
	{
		return Matrix4x4.TRS(v, Quaternion.identity, Vector3.one);
	}

}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Static/DrawStraightLine.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000