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
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
224
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Base class for all UI components that should be derived from when creating new widget types.
/// </summary>

[ExecuteInEditMode]
[AddComponentMenu("NGUI/UI/NGUI Widget")]
public class UIWidget : MonoBehaviour
{
	/// <summary>
	/// List of all the active widgets currently present in the scene.
	/// </summary>

	static public BetterList<UIWidget> list = new BetterList<UIWidget>();

	public enum Pivot
	{
		TopLeft,
		Top,
		TopRight,
		Left,
		Center,
		Right,
		BottomLeft,
		Bottom,
		BottomRight,
	}

	// Cached and saved values
	[HideInInspector][SerializeField] protected Color mColor = Color.white;
	[HideInInspector][SerializeField] protected Pivot mPivot = Pivot.Center;
	[HideInInspector][SerializeField] protected int mWidth = 100;
	[HideInInspector][SerializeField] protected int mHeight = 100;
	[HideInInspector][SerializeField] protected int mDepth = 0;

	protected Vector4 mDrawRegion = new Vector4(0f, 0f, 1f, 1f);

	/// <summary>
	/// Draw region alters how the widget looks without modifying the widget's rectangle.
	/// A region is made up of 4 relative values (0-1 range). The order is Left (X), Bottom (Y), Right (Z) and Top (W).
	/// To have a widget's left edge be 30% from the left side, set X to 0.3. To have the widget's right edge be 30%
	/// from the right hand side, set Z to 0.7.
	/// </summary>

	public Vector4 drawRegion
	{
		get
		{
			return mDrawRegion;
		}
		set
		{
			if (mDrawRegion != value)
			{
				mDrawRegion = value;
				if (autoResizeBoxCollider) ResizeCollider();
				MarkAsChanged();
			}
		}
	}

	/// <summary>
	/// If set to 'true', the box collider's dimensions will be adjusted to always match the widget whenever it resizes.
	/// </summary>

	public bool autoResizeBoxCollider = false;
	
	protected GameObject mGo;
	protected Transform mTrans;
	protected UIPanel mPanel;

	protected bool mChanged = true;
	protected bool mPlayMode = true;

	bool mStarted = false;
	Vector3 mDiffPos;
	Quaternion mDiffRot;
	Vector3 mDiffScale;
	Matrix4x4 mLocalToPanel;
	bool mVisibleByPanel = true;
	float mLastAlpha = 0f;

	/// <summary>
	/// Internal usage -- draw call that's drawing the widget.
	/// </summary>

	[HideInInspector]
	[System.NonSerialized]
	public UIDrawCall drawCall;

	// Widget's generated geometry
	protected UIGeometry mGeom = new UIGeometry();
	protected Vector3[] mCorners = new Vector3[4];

	/// <summary>
	/// Whether the widget is visible.
	/// </summary>

	public bool isVisible { get { return mVisibleByPanel && finalAlpha > 0.001f; } }

	/// <summary>
	/// Widget's width in pixels.
	/// </summary>

	public int width
	{
		get
		{
			return mWidth;
		}
		set
		{
			int min = minWidth;
			if (value < min) value = min;

			if (mWidth != value)
			{
				mWidth = value;
				if (autoResizeBoxCollider) ResizeCollider();
				MarkAsChanged();
			}
		}
	}

	/// <summary>
	/// Widget's height in pixels.
	/// </summary>

	public int height
	{
		get
		{
			return mHeight;
		}
		set
		{
			int min = minHeight;
			if (value < min) value = min;

			if (mHeight != value)
			{
				mHeight = value;
				if (autoResizeBoxCollider) ResizeCollider();
				MarkAsChanged();
			}
		}
	}

	/// <summary>
	/// Color used by the widget.
	/// </summary>

	public Color color
	{
		get
		{
			return mColor;
		}
		set
		{
			if (mColor != value)
			{
				mColor = value;
				mChanged = true;
			}
		}
	}

	/// <summary>
	/// Widget's alpha -- a convenience method.
	/// </summary>

	public float alpha
	{
		get
		{
			return mColor.a;
		}
		set
		{
			Color c = mColor;

			if (c.a != value)
			{
				c.a = value;
				color = c;
			}
		}
	}

	/// <summary>
	/// Widget's final alpha, after taking the panel's alpha into account.
	/// </summary>

	public float finalAlpha
	{
		get
		{
			if (mPanel == null) CreatePanel();
			return (mPanel != null) ? mColor.a * mPanel.finalAlpha : mColor.a;
		}
	}

	/// <summary>
	/// Change the pivot point and do not attempt to keep the widget in the same place by adjusting its transform.
	/// </summary>

	public Pivot rawPivot
	{
		get
		{
			return mPivot;
		}
		set
		{
			if (mPivot != value)
			{
				mPivot = value;
				if (autoResizeBoxCollider) ResizeCollider();
				MarkAsChanged();
			}
		}
	}

	/// <summary>
	/// Set or get the value that specifies where the widget's pivot point should be.
	/// </summary>

	public Pivot pivot
	{
		get
		{
			return mPivot;
		}
		set
		{
			if (mPivot != value)
			{
				Vector3 before = worldCorners[0];

				mPivot = value;
				mChanged = true;

				Vector3 after = worldCorners[0];

				Transform t = cachedTransform;
				Vector3 pos = t.position;
				float z = t.localPosition.z;
				pos.x += (before.x - after.x);
				pos.y += (before.y - after.y);
				cachedTransform.position = pos;

				pos = cachedTransform.localPosition;
				pos.x = Mathf.Round(pos.x);
				pos.y = Mathf.Round(pos.y);
				pos.z = z;
				cachedTransform.localPosition = pos;
			}
		}
	}
	
	/// <summary>
	/// Depth controls the rendering order -- lowest to highest.
	/// </summary>

	public int depth
	{
		get
		{
			return mDepth;
		}
		set
		{
			if (mDepth != value)
			{
				RemoveFromPanel();
				mDepth = value;
#if UNITY_EDITOR
				UnityEditor.EditorUtility.SetDirty(this);
#endif
				UIPanel.RebuildAllDrawCalls(true);
			}
		}
	}

	/// <summary>
	/// Raycast depth order on widgets takes the depth of their panel into consideration.
	/// This functionality is used to determine the "final" depth of the widget for drawing and raycasts.
	/// </summary>

	public int raycastDepth
	{
		get
		{
			if (mPanel == null) CreatePanel();
			return (mPanel != null) ? mDepth + mPanel.depth * 1000 : mDepth;
		}
	}

	/// <summary>
	/// Local space corners of the widget. The order is bottom-left, top-left, top-right, bottom-right.
	/// </summary>

	public virtual Vector3[] localCorners
	{
		get
		{
			Vector2 offset = pivotOffset;

			float x0 = -offset.x * mWidth;
			float y0 = -offset.y * mHeight;
			float x1 = x0 + mWidth;
			float y1 = y0 + mHeight;

			mCorners[0] = new Vector3(x0, y0);
			mCorners[1] = new Vector3(x0, y1);
			mCorners[2] = new Vector3(x1, y1);
			mCorners[3] = new Vector3(x1, y0);

			return mCorners;
		}
	}

	/// <summary>
	/// Local width and height of the widget in pixels.
	/// </summary>

	public virtual Vector2 localSize
	{
		get
		{
			Vector3[] cr = localCorners;
			return cr[2] - cr[0];
		}
	}

	/// <summary>
	/// World-space corners of the widget. The order is bottom-left, top-left, top-right, bottom-right.
	/// </summary>

	public virtual Vector3[] worldCorners
	{
		get
		{
			Vector2 offset = pivotOffset;

			float x0 = -offset.x * mWidth;
			float y0 = -offset.y * mHeight;
			float x1 = x0 + mWidth;
			float y1 = y0 + mHeight;

			Transform wt = cachedTransform;

			mCorners[0] = wt.TransformPoint(x0, y0, 0f);
			mCorners[1] = wt.TransformPoint(x0, y1, 0f);
			mCorners[2] = wt.TransformPoint(x1, y1, 0f);
			mCorners[3] = wt.TransformPoint(x1, y0, 0f);

			return mCorners;
		}
	}

	/// <summary>
	/// Local space region where the actual drawing will take place.
	/// X = left, Y = bottom, Z = right, W = top.
	/// </summary>

	public virtual Vector4 drawingDimensions
	{
		get
		{
			Vector2 offset = pivotOffset;

			float x0 = -offset.x * mWidth;
			float y0 = -offset.y * mHeight;
			float x1 = x0 + mWidth;
			float y1 = y0 + mHeight;

			return new Vector4(
				mDrawRegion.x == 0f ? x0 : Mathf.Lerp(x0, x1, mDrawRegion.x),
				mDrawRegion.y == 0f ? y0 : Mathf.Lerp(y0, y1, mDrawRegion.y),
				mDrawRegion.z == 1f ? x1 : Mathf.Lerp(x0, x1, mDrawRegion.z),
				mDrawRegion.w == 1f ? y1 : Mathf.Lerp(y0, y1, mDrawRegion.w));
		}
	}

	/// <summary>
	/// Whether the widget has some geometry that can be drawn.
	/// </summary>

	public bool hasVertices { get { return mGeom != null && mGeom.hasVertices; } }

	/// <summary>
	/// Helper function that calculates the relative offset based on the current pivot.
	/// X = 0 (left) to 1 (right)
	/// Y = 0 (bottom) to 1 (top)
	/// </summary>

	public Vector2 pivotOffset { get { return NGUIMath.GetPivotOffset(pivot); } }

	/// <summary>
	/// Game object gets cached for speed. Can't simply return 'mGo' set in Awake because this function may be called on a prefab.
	/// </summary>

	public GameObject cachedGameObject { get { if (mGo == null) mGo = gameObject; return mGo; } }

	/// <summary>
	/// Transform gets cached for speed. Can't simply return 'mTrans' set in Awake because this function may be called on a prefab.
	/// </summary>

	public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }

	/// <summary>
	/// Material used by the widget.
	/// </summary>

	public virtual Material material
	{
		get
		{
			return null;
		}
		set
		{
			throw new System.NotImplementedException(GetType() + " has no material setter");
		}
	}

	/// <summary>
	/// Texture used by the widget.
	/// </summary>

	public virtual Texture mainTexture
	{
		get
		{
			Material mat = material;
			return (mat != null) ? mat.mainTexture : null;
		}
		set
		{
			throw new System.NotImplementedException(GetType() + " has no mainTexture setter");
		}
	}

	/// <summary>
	/// Shader is used to create a dynamic material if the widget has no material to work with.
	/// </summary>

	public virtual Shader shader
	{
		get
		{
			Material mat = material;
			return (mat != null) ? mat.shader : null;
		}
		set
		{
			throw new System.NotImplementedException(GetType() + " has no shader setter");
		}
	}

	/// <summary>
	/// Returns the UI panel responsible for this widget.
	/// </summary>

	public UIPanel panel { get { if (mPanel == null) CreatePanel(); return mPanel; } set { mPanel = value; } }

	/// <summary>
	/// Do not use this, it's obsolete.
	/// </summary>

	[System.Obsolete("There is no relative scale anymore. Widgets now have width and height instead")]
	public Vector2 relativeSize { get { return Vector2.one; } }

	/// <summary>
	/// Convenience function that returns 'true' if the widget has a box collider.
	/// </summary>

	public bool hasBoxCollider
	{
		get
		{
			BoxCollider box = collider as BoxCollider;
			return (box != null);
		}
	}

	/// <summary>
	/// Adjust the widget's collider size to match the widget's dimensions.
	/// </summary>

	public void ResizeCollider ()
	{
		if (NGUITools.IsActive(this))
		{
			BoxCollider box = collider as BoxCollider;
			if (box != null) NGUITools.UpdateWidgetCollider(box, true);
		}
	}

	/// <summary>
	/// Static widget comparison function used for depth sorting.
	/// </summary>

	static public int CompareFunc (UIWidget left, UIWidget right)
	{
		int val = UIPanel.CompareFunc(left.mPanel, right.mPanel);

		if (val == 0)
		{
			if (left.mDepth < right.mDepth) return -1;
			if (left.mDepth > right.mDepth) return 1;

			Material leftMat = left.material;
			Material rightMat = right.material;

			if (leftMat == rightMat) return 0;
			if (leftMat != null) return -1;
			if (rightMat != null) return 1;
			return (leftMat.GetInstanceID() < rightMat.GetInstanceID()) ? -1 : 1;
		}
		return val;
	}

	/// <summary>
	/// Calculate the widget's bounds, optionally making them relative to the specified transform.
	/// </summary>

	public Bounds CalculateBounds () { return CalculateBounds(null); }

	/// <summary>
	/// Calculate the widget's bounds, optionally making them relative to the specified transform.
	/// </summary>

	public Bounds CalculateBounds (Transform relativeParent)
	{
		if (relativeParent == null)
		{
			Vector3[] corners = localCorners;
			Bounds b = new Bounds(corners[0], Vector3.zero);
			for (int j = 1; j < 4; ++j) b.Encapsulate(corners[j]);
			return b;
		}
		else
		{
			Matrix4x4 toLocal = relativeParent.worldToLocalMatrix;
			Vector3[] corners = worldCorners;
			Bounds b = new Bounds(toLocal.MultiplyPoint3x4(corners[0]), Vector3.zero);
			for (int j = 1; j < 4; ++j) b.Encapsulate(toLocal.MultiplyPoint3x4(corners[j]));
			return b;
		}
	}

	/// <summary>
	/// Mark the widget as changed so that the geometry can be rebuilt.
	/// </summary>

	void SetDirty ()
	{
		if (drawCall != null)
		{
			drawCall.isDirty = true;
		}
		else if (isVisible && hasVertices)
		{
			drawCall = UIPanel.InsertWidget(this);
		}
	}

	/// <summary>
	/// Remove this widget from the panel.
	/// </summary>

	protected void RemoveFromPanel ()
	{
		UIPanel.RemoveWidget(this);
		mPanel = null;
		list.Remove(this);
#if UNITY_EDITOR
		mOldTex = null;
		mOldShader = null;
#endif
	}

#if UNITY_EDITOR
	Texture mOldTex;
	Shader mOldShader;

	/// <summary>
	/// This callback is sent inside the editor notifying us that some property has changed.
	/// </summary>

	protected virtual void OnValidate()
	{
		mChanged = true;

		// Prior to NGUI 2.7.0 width and height was specified as transform's local scale
		if ((mWidth == 100 || mWidth == minWidth) &&
			(mHeight == 100 || mHeight == minHeight) && cachedTransform.localScale.magnitude > 8f)
		{
			UpgradeFrom265();
			cachedTransform.localScale = Vector3.one;
		}

		if (mWidth < minWidth) mWidth = minWidth;
		if (mHeight < minHeight) mHeight = minHeight;
		if (autoResizeBoxCollider) ResizeCollider();

		// If the texture is changing, we need to make sure to rebuild the draw calls
		if (mOldTex != mainTexture || mOldShader != shader)
		{
			mOldTex = mainTexture;
			mOldShader = shader;
			UIPanel.RemoveWidget(this);
			drawCall = UIPanel.InsertWidget(this);
		}
	}
#endif

	/// <summary>
	/// Only sets the local flag, does not notify the panel.
	/// In most cases you will want to use MarkAsChanged() instead.
	/// </summary>

	public void MarkAsChangedLite () { mChanged = true; }

	/// <summary>
	/// Tell the panel responsible for the widget that something has changed and the buffers need to be rebuilt.
	/// </summary>

	public virtual void MarkAsChanged ()
	{
		if (this == null) return;
		mChanged = true;
#if UNITY_EDITOR
		UnityEditor.EditorUtility.SetDirty(this);
#endif
		// If we're in the editor, update the panel right away so its geometry gets updated.
		if (mPanel != null && enabled && NGUITools.GetActive(gameObject) && !Application.isPlaying)
		{
			SetDirty();
			CheckLayer();
#if UNITY_EDITOR
			// Mark the panel as dirty so it gets updated
			if (material != null) UnityEditor.EditorUtility.SetDirty(mPanel.gameObject);
#endif
		}
	}

	/// <summary>
	/// Ensure we have a panel referencing this widget.
	/// </summary>

	public void CreatePanel ()
	{
		if (mStarted && mPanel == null && enabled && NGUITools.GetActive(gameObject))
		{
			mPanel = UIPanel.Find(cachedTransform, mStarted);

			if (mPanel != null)
			{
				int rd = raycastDepth;
				bool inserted = false;

				// Try to insert this widget at the appropriate location within the list
				for (int i = 0; i < list.size; ++i)
				{
					if (list[i].raycastDepth > rd)
					{
						list.Insert(i, this);
						inserted = true;
						break;
					}
				}

				// Add this widget to the end of the list if it's not already there
				if (!inserted) list.Add(this);

				CheckLayer();
				mChanged = true;
				drawCall = UIPanel.InsertWidget(this);
			}
		}
	}

	/// <summary>
	/// Check to ensure that the widget resides on the same layer as its panel.
	/// </summary>

	public void CheckLayer ()
	{
		if (mPanel != null && mPanel.gameObject.layer != gameObject.layer)
		{
			Debug.LogWarning("You can't place widgets on a layer different than the UIPanel that manages them.\n" +
				"If you want to move widgets to a different layer, parent them to a new panel instead.", this);
			gameObject.layer = mPanel.gameObject.layer;
		}
	}

	/// <summary>
	/// Checks to ensure that the widget is still parented to the right panel.
	/// </summary>

	public void ParentHasChanged ()
	{
		if (mPanel != null)
		{
			UIPanel p = UIPanel.Find(cachedTransform);

			if (mPanel != p)
			{
				RemoveFromPanel();
				CreatePanel();
			}
		}
	}

	/// <summary>
	/// Remember whether we're in play mode.
	/// </summary>

	protected virtual void Awake ()
	{
		mGo = gameObject;
		mPlayMode = Application.isPlaying;
	}

	/// <summary>
	/// Mark the widget and the panel as having been changed.
	/// </summary>

	protected virtual void OnEnable ()
	{
		mChanged = true;
		RemoveFromPanel();

		// Prior to NGUI 2.7.0 width and height was specified as transform's local scale
		if (mWidth == 100 && mHeight == 100 && cachedTransform.localScale.magnitude > 8f)
		{
			UpgradeFrom265();
			cachedTransform.localScale = Vector3.one;
#if UNITY_EDITOR
			UnityEditor.EditorUtility.SetDirty(this);
#endif
		}
	}

	/// <summary>
	/// Facilitates upgrading from NGUI 2.6.5 or earlier versions.
	/// </summary>

	protected virtual void UpgradeFrom265 ()
	{
		Vector3 scale = cachedTransform.localScale;
		mWidth = Mathf.Abs(Mathf.RoundToInt(scale.x));
		mHeight = Mathf.Abs(Mathf.RoundToInt(scale.y));
		if (GetComponent<BoxCollider>() != null)
			NGUITools.AddWidgetCollider(gameObject, true);
	}

	/// <summary>
	/// Set the depth, call the virtual start function, and sure we have a panel to work with.
	/// </summary>

	void Start ()
	{
		mStarted = true;
		OnStart();
		CreatePanel();
	}

	/// <summary>
	/// Ensure that we have a panel to work with. The reason the panel isn't added in OnEnable()
	/// is because OnEnable() is called right after Awake(), which is a problem when the widget
	/// is brought in on a prefab object as it happens before it gets parented.
	/// </summary>

	public virtual void Update ()
	{
		// Ensure we have a panel to work with by now
		if (mPanel == null) CreatePanel();
#if UNITY_EDITOR
		else if (!Application.isPlaying) ParentHasChanged();
#endif
	}

	/// <summary>
	/// Mark the UI as changed when returning from paused state.
	/// </summary>

	void OnApplicationPause (bool paused) { if (!paused) MarkAsChanged(); }

	/// <summary>
	/// Clear references.
	/// </summary>

	protected virtual void OnDisable () { RemoveFromPanel(); }

	/// <summary>
	/// Unregister this widget.
	/// </summary>

	void OnDestroy () { RemoveFromPanel(); }

#if UNITY_EDITOR
	static int mHandles = -1;

	/// <summary>
	/// Whether widgets will show handles with the Move Tool, or just the View Tool.
	/// </summary>

	static public bool showHandlesWithMoveTool
	{
		get
		{
			if (mHandles == -1)
			{
				mHandles = UnityEditor.EditorPrefs.GetInt("NGUI Handles", 1);
			}
			return (mHandles == 1);
		}
		set
		{
			int val = value ? 1 : 0;

			if (mHandles != val)
			{
				mHandles = val;
				UnityEditor.EditorPrefs.SetInt("NGUI Handles", mHandles);
			}
		}
	}

	/// <summary>
	/// Whether the widget should have some form of handles shown.
	/// </summary>

	static public bool showHandles
	{
		get
		{
			if (showHandlesWithMoveTool)
			{
				return UnityEditor.Tools.current == UnityEditor.Tool.Move;
			}
			return UnityEditor.Tools.current == UnityEditor.Tool.View;
		}
	}

	/// <summary>
	/// Whether the widget can be resized using drag handles.
	/// </summary>

	public virtual bool canResize { get { return GetComponent<UIStretch>() == null; } }

	/// <summary>
	/// Draw some selectable gizmos.
	/// </summary>

	void OnDrawGizmos ()
	{
		if (isVisible && NGUITools.IsActive(this))
		{
			if (UnityEditor.Selection.activeGameObject == gameObject && showHandles) return;

			Color outline = new Color(1f, 1f, 1f, 0.2f);

			Vector2 offset = pivotOffset;
			Vector3 center = new Vector3(mWidth * (0.5f - offset.x), mHeight * (0.5f - offset.y), -mDepth * 0.25f);
			Vector3 size = new Vector3(mWidth, mHeight, 1f);

			// Draw the gizmo
			Gizmos.matrix = cachedTransform.localToWorldMatrix;
			Gizmos.color = (UnityEditor.Selection.activeGameObject == cachedTransform) ? Color.white : outline;
			Gizmos.DrawWireCube(center, size);

			// Make the widget selectable
			size.z = 0.01f;
			Gizmos.color = Color.clear;
			Gizmos.DrawCube(center, size);
		}
	}
#endif // UNITY_EDITOR

#if UNITY_3_5 || UNITY_4_0
	Vector3 mOldPos;
	Quaternion mOldRot;
	Vector3 mOldScale;
#endif

	/// <summary>
	/// Whether the transform has changed since the last time it was checked.
	/// </summary>

	bool HasTransformChanged ()
	{
#if UNITY_3_5 || UNITY_4_0
		Transform t = cachedTransform;
		
		if (t.position != mOldPos || t.rotation != mOldRot || t.lossyScale != mOldScale)
		{
			mOldPos = t.position;
			mOldRot = t.rotation;
			mOldScale = t.lossyScale;
			return true;
		}
#else
		if (cachedTransform.hasChanged)
		{
		    mTrans.hasChanged = false;
		    return true;
		}
#endif
		return false;
	}

	bool mForceVisible = false;
	Vector3 mOldV0;
	Vector3 mOldV1;

	/// <summary>
	/// Update the widget and fill its geometry if necessary. Returns whether something was changed.
	/// </summary>

	public bool UpdateGeometry (bool forceVisible)
	{
		bool hasMatrix = false;
		float final = finalAlpha;
		bool visibleByAlpha = (final > 0.001f);
		bool visibleByPanel = forceVisible || mVisibleByPanel;
		bool moved = false;

		// Check to see if the widget has moved relative to the panel that manages it
		if (HasTransformChanged())
		{
#if UNITY_EDITOR
			if (!mPanel.widgetsAreStatic || !Application.isPlaying)
#else
			if (!mPanel.widgetsAreStatic)
#endif
			{
				mLocalToPanel = mPanel.worldToLocal * cachedTransform.localToWorldMatrix;
				hasMatrix = true;

				Vector2 offset = pivotOffset;

				float x0 = -offset.x * mWidth;
				float y0 = -offset.y * mHeight;
				float x1 = x0 + mWidth;
				float y1 = y0 + mHeight;

				Transform wt = cachedTransform;

				Vector3 v0 = wt.TransformPoint(x0, y0, 0f);
				Vector3 v1 = wt.TransformPoint(x1, y1, 0f);

				v0 = mPanel.worldToLocal.MultiplyPoint3x4(v0);
				v1 = mPanel.worldToLocal.MultiplyPoint3x4(v1);

				if (Vector3.SqrMagnitude(mOldV0 - v0) > 0.000001f ||
					Vector3.SqrMagnitude(mOldV1 - v1) > 0.000001f)
				{
					moved = true;
					mOldV0 = v0;
					mOldV1 = v1;
				}
			}

			// Is the widget visible by the panel?
			if (visibleByAlpha || mForceVisible != forceVisible)
			{
				mForceVisible = forceVisible;
				visibleByPanel = forceVisible || mPanel.IsVisible(this);
			}
		}
		else if (visibleByAlpha && mForceVisible != forceVisible)
		{
			mForceVisible = forceVisible;
			visibleByPanel = mPanel.IsVisible(this);
		}

		// Is the visibility changing?
		if (mVisibleByPanel != visibleByPanel)
		{
			mVisibleByPanel = visibleByPanel;
			mChanged = true;
		}

		// Has the alpha changed?
		if (mVisibleByPanel && mLastAlpha != final) mChanged = true;
		mLastAlpha = final;

		if (mChanged)
		{
			mChanged = false;

			if (isVisible && shader != null)
			{
				bool hadVertices = mGeom.hasVertices;
				mGeom.Clear();
				OnFill(mGeom.verts, mGeom.uvs, mGeom.cols);

				if (mGeom.hasVertices)
				{
					// Want to see what's being filled? Uncomment this line.
					//Debug.Log("Fill " + name + " (" + Time.time + ")");

					if (!hasMatrix) mLocalToPanel = mPanel.worldToLocal * cachedTransform.localToWorldMatrix;
					mGeom.ApplyTransform(mLocalToPanel);
					return true;
				}
				return hadVertices;
			}
			else if (mGeom.hasVertices)
			{
				mGeom.Clear();
				return true;
			}
		}
		else if (moved && mGeom.hasVertices)
		{
			if (!hasMatrix) mLocalToPanel = mPanel.worldToLocal * cachedTransform.localToWorldMatrix;
			mGeom.ApplyTransform(mLocalToPanel);
			return true;
		}
		return false;
	}

	/// <summary>
	/// Append the local geometry buffers to the specified ones.
	/// </summary>

	public void WriteToBuffers (BetterList<Vector3> v, BetterList<Vector2> u, BetterList<Color32> c, BetterList<Vector3> n, BetterList<Vector4> t)
	{
		mGeom.WriteToBuffers(v, u, c, n, t);
	}

	/// <summary>
	/// Make the widget pixel-perfect.
	/// </summary>

	virtual public void MakePixelPerfect ()
	{
		Vector3 pos = cachedTransform.localPosition;
		pos.z = Mathf.Round(pos.z);
		pos.x = Mathf.Round(pos.x);
		pos.y = Mathf.Round(pos.y);
		cachedTransform.localPosition = pos;

		Vector3 ls = cachedTransform.localScale;
		cachedTransform.localScale = new Vector3(Mathf.Sign(ls.x), Mathf.Sign(ls.y), 1f);
	}

	/// <summary>
	/// Minimum allowed width for this widget.
	/// </summary>

	virtual public int minWidth { get { return 2; } }

	/// <summary>
	/// Minimum allowed height for this widget.
	/// </summary>

	virtual public int minHeight { get { return 2; } }

	/// <summary>
	/// Dimensions of the sprite's border, if any.
	/// </summary>

	virtual public Vector4 border { get { return Vector4.zero; } }

	/// <summary>
	/// Virtual Start() functionality for widgets.
	/// </summary>

	virtual protected void OnStart () { }

	/// <summary>
	/// Virtual function called by the UIPanel that fills the buffers.
	/// </summary>

	virtual public void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) { }
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Scripts/Internal/UIWidget.cs

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