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
using UnityEngine;
using UnityEditor;

/// <summary>
/// This editor helper class makes it easy to create and show a context menu.
/// It ensures that it's possible to add multiple items with the same name.
/// </summary>

public static class NGUIContextMenu
{
	public delegate UIWidget AddFunc (GameObject go);

	static BetterList<string> mEntries = new BetterList<string>();
	static GenericMenu mMenu;

	/// <summary>
	/// Clear the context menu list.
	/// </summary>

	static public void Clear ()
	{
		mEntries.Clear();
		mMenu = null;
	}

	/// <summary>
	/// Add a new context menu entry.
	/// </summary>

	static public void AddItem (string item, bool isChecked, GenericMenu.MenuFunction2 callback, object param)
	{
		if (callback != null)
		{
			if (mMenu == null) mMenu = new GenericMenu();
			int count = 0;

			for (int i = 0; i < mEntries.size; ++i)
			{
				string str = mEntries[i];
				if (str == item) ++count;
			}
			mEntries.Add(item);

			if (count > 0) item += " [" + count + "]";
			mMenu.AddItem(new GUIContent(item), isChecked, callback, param);
		}
		else AddDisabledItem(item);
	}

	/// <summary>
	/// Wrapper function called by the menu that in turn calls the correct callback.
	/// </summary>

	static void AddChild (object obj)
	{
		AddFunc func = obj as AddFunc;
		UIWidget widget = func(Selection.activeGameObject);
		if (widget != null) Selection.activeGameObject = widget.gameObject;
	}

	/// <summary>
	/// Add a new context menu entry.
	/// </summary>

	static void AddChildWidget (string item, bool isChecked, AddFunc callback)
	{
		if (callback != null)
		{
			if (mMenu == null) mMenu = new GenericMenu();
			int count = 0;

			for (int i = 0; i < mEntries.size; ++i)
			{
				string str = mEntries[i];
				if (str == item) ++count;
			}
			mEntries.Add(item);

			if (count > 0) item += " [" + count + "]";
			mMenu.AddItem(new GUIContent(item), isChecked, AddChild, callback);
		}
		else AddDisabledItem(item);
	}

	/// <summary>
	/// Wrapper function called by the menu that in turn calls the correct callback.
	/// </summary>

	static void AddSibling (object obj)
	{
		AddFunc func = obj as AddFunc;
		UIWidget widget = func(Selection.activeTransform.parent.gameObject);
		if (widget != null) Selection.activeGameObject = widget.gameObject;
	}

	/// <summary>
	/// Add a new context menu entry.
	/// </summary>

	static void AddSiblingWidget (string item, bool isChecked, AddFunc callback)
	{
		if (callback != null)
		{
			if (mMenu == null) mMenu = new GenericMenu();
			int count = 0;

			for (int i = 0; i < mEntries.size; ++i)
			{
				string str = mEntries[i];
				if (str == item) ++count;
			}
			mEntries.Add(item);

			if (count > 0) item += " [" + count + "]";
			mMenu.AddItem(new GUIContent(item), isChecked, AddSibling, callback);
		}
		else AddDisabledItem(item);
	}

	/// <summary>
	/// Add commonly NGUI context menu options.
	/// </summary>

	static public void AddCommonItems (GameObject target)
	{
		if (target != null)
		{
			UIWidget widget = target.GetComponent<UIWidget>();

			string myName = string.Format("Selected {0}", (widget != null) ? NGUITools.GetTypeName(widget) : "Object");

			AddItem(myName + "/Bring to Front", false,
				delegate(object obj) { NGUITools.BringForward(Selection.activeGameObject); }, null);

			AddItem(myName + "/Push to Back", false,
				delegate(object obj) { NGUITools.PushBack(Selection.activeGameObject); }, null);

			AddItem(myName + "/Nudge Forward", false,
				delegate(object obj) { NGUITools.AdjustDepth(Selection.activeGameObject, 1); }, null);

			AddItem(myName + "/Nudge Back", false,
				delegate(object obj) { NGUITools.AdjustDepth(Selection.activeGameObject, -1); }, null);

			if (widget != null)
			{
				NGUIContextMenu.AddSeparator(myName + "/");

				AddItem(myName + "/Make Pixel-Perfect", false, OnMakePixelPerfect, Selection.activeTransform);

				if (target.GetComponent<BoxCollider>() != null)
				{
					AddItem(myName + "/Reset Collider Size", false, OnBoxCollider, target);
				}
			}

			NGUIContextMenu.AddSeparator(myName + "/");
			AddItem(myName + "/Delete", false, OnDelete, target);
			NGUIContextMenu.AddSeparator("");

			if (Selection.activeTransform.parent != null && widget != null)
			{
				AddChildWidget("Create/Sprite/Child", false, NGUISettings.AddSprite);
				AddChildWidget("Create/Label/Child", false, NGUISettings.AddLabel);
				AddChildWidget("Create/Invisible Widget/Child", false, NGUISettings.AddWidget);
				AddChildWidget("Create/Simple Texture/Child", false, NGUISettings.AddTexture);
#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
				AddChildWidget("Create/Unity 2D Sprite/Child", false, NGUISettings.Add2DSprite);
#endif
				AddSiblingWidget("Create/Sprite/Sibling", false, NGUISettings.AddSprite);
				AddSiblingWidget("Create/Label/Sibling", false, NGUISettings.AddLabel);
				AddSiblingWidget("Create/Invisible Widget/Sibling", false, NGUISettings.AddWidget);
				AddSiblingWidget("Create/Simple Texture/Sibling", false, NGUISettings.AddTexture);
#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
				AddSiblingWidget("Create/Unity 2D Sprite/Sibling", false, NGUISettings.Add2DSprite);
#endif
			}
			else
			{
				AddChildWidget("Create/Sprite", false, NGUISettings.AddSprite);
				AddChildWidget("Create/Label", false, NGUISettings.AddLabel);
				AddChildWidget("Create/Invisible Widget", false, NGUISettings.AddWidget);
				AddChildWidget("Create/Simple Texture", false, NGUISettings.AddTexture);
#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
				AddChildWidget("Create/Unity 2D Sprite", false, NGUISettings.Add2DSprite);
#endif
			}

			NGUIContextMenu.AddSeparator("Create/");

			AddItem("Create/Anchor", false, AddChild<UIAnchor>, target);
			AddItem("Create/Panel", false, AddPanel, target);
			AddItem("Create/Scroll View", false, AddScrollView, target);
			AddItem("Create/Grid", false, AddChild<UIGrid>, target);
			AddItem("Create/Table", false, AddChild<UITable>, target);

			if (target.GetComponent<UIPanel>() != null)
			{
				if (target.GetComponent<UIScrollView>() == null)
				{
					AddItem("Attach/Scroll View", false, delegate(object obj) { target.AddComponent<UIScrollView>(); }, null);
					NGUIContextMenu.AddSeparator("Attach/");
				}
			}
			else if (target.collider == null)
			{
				AddItem("Attach/Box Collider", false, delegate(object obj) { NGUITools.AddWidgetCollider(target); }, null);
				NGUIContextMenu.AddSeparator("Attach/");
			}

			if (target.collider != null)
			{
				if (target.GetComponent<UIDragScrollView>() == null)
				{
					for (int i = 0; i < UIPanel.list.size; ++i)
					{
						UIPanel pan = UIPanel.list[i];
						if (pan.clipping == UIDrawCall.Clipping.None) continue;
	
						UIScrollView dr = pan.GetComponent<UIScrollView>();
						if (dr == null) continue;

						AddItem("Attach/Drag Scroll View", false, delegate(object obj)
							{ target.AddComponent<UIDragScrollView>().scrollView = dr; }, null);
						
						NGUIContextMenu.AddSeparator("Attach/");
						break;
					}
				}

				AddItem("Attach/Button Script", false, delegate(object obj) { target.AddComponent<UIButton>(); }, null);
				AddItem("Attach/Toggle Script", false, delegate(object obj) { target.AddComponent<UIToggle>(); }, null);
				AddItem("Attach/Slider Script", false, delegate(object obj) { target.AddComponent<UISlider>(); }, null);
				AddItem("Attach/Scroll Bar Script", false, delegate(object obj) { target.AddComponent<UIScrollBar>(); }, null);
				AddItem("Attach/Progress Bar Script", false, delegate(object obj) { target.AddComponent<UISlider>(); }, null);
				AddItem("Attach/Popup List Script", false, delegate(object obj) { target.AddComponent<UIPopupList>(); }, null);
				AddItem("Attach/Input Field Script", false, delegate(object obj) { target.AddComponent<UIInput>(); }, null);
				NGUIContextMenu.AddSeparator("Attach/");
				if (target.GetComponent<UIAnchor>() == null) AddItem("Attach/Anchor Script", false, delegate(object obj) { target.AddComponent<UIAnchor>(); }, null);
				if (target.GetComponent<UIStretch>() == null) AddItem("Attach/Stretch Script", false, delegate(object obj) { target.AddComponent<UIStretch>(); }, null);
				AddItem("Attach/Key Binding Script", false, delegate(object obj) { target.AddComponent<UIKeyBinding>(); }, null);

				NGUIContextMenu.AddSeparator("Attach/");

				AddItem("Attach/Play Tween Script", false, delegate(object obj) { target.AddComponent<UIPlayTween>(); }, null);
				AddItem("Attach/Play Animation Script", false, delegate(object obj) { target.AddComponent<UIPlayAnimation>(); }, null);
				AddItem("Attach/Play Sound Script", false, delegate(object obj) { target.AddComponent<UIPlaySound>(); }, null);
			}

			if (widget != null)
			{
				AddMissingItem<TweenAlpha>(target, "Tween/Alpha");
				AddMissingItem<TweenColor>(target, "Tween/Color");
				AddMissingItem<TweenWidth>(target, "Tween/Width");
				AddMissingItem<TweenHeight>(target, "Tween/Height");
			}
			else if (target.GetComponent<UIPanel>() != null)
			{
				AddMissingItem<TweenAlpha>(target, "Tween/Alpha");
			}

			NGUIContextMenu.AddSeparator("Tween/");

			AddMissingItem<TweenPosition>(target, "Tween/Position");
			AddMissingItem<TweenRotation>(target, "Tween/Rotation");
			AddMissingItem<TweenScale>(target, "Tween/Scale");
			AddMissingItem<TweenTransform>(target, "Tween/Transform");

			if (target.GetComponent<AudioSource>() != null)
				AddMissingItem<TweenVolume>(target, "Tween/Volume");

			if (target.GetComponent<Camera>() != null)
			{
				AddMissingItem<TweenFOV>(target, "Tween/Field of View");
				AddMissingItem<TweenOrthoSize>(target, "Tween/Orthographic Size");
			}
		}
	}

	/// <summary>
	/// Helper function.
	/// </summary>

	static void AddMissingItem<T> (GameObject target, string name) where T : MonoBehaviour
	{
		if (target.GetComponent<T>() == null)
			AddItem(name, false, delegate(object obj) { target.AddComponent<T>(); }, null);
	}

	/// <summary>
	/// Helper function for menu creation.
	/// </summary>

	static void AddChild<T> (object obj) where T : MonoBehaviour
	{
		GameObject go = obj as GameObject;
		T t = NGUITools.AddChild<T>(go);
		Selection.activeGameObject = t.gameObject;
	}

	/// <summary>
	/// Helper function for menu creation.
	/// </summary>

	static void AddPanel (object obj)
	{
		GameObject go = obj as GameObject;
		if (go.GetComponent<UIWidget>() != null) go = go.transform.parent.gameObject;
		UIPanel panel = NGUISettings.AddPanel(go);
		Selection.activeGameObject = panel.gameObject;
	}

	/// <summary>
	/// Helper function for menu creation.
	/// </summary>

	static void AddScrollView (object obj)
	{
		GameObject go = obj as GameObject;
		if (go.GetComponent<UIWidget>() != null) go = go.transform.parent.gameObject;
		go.name = "Scroll View";
		UIPanel panel = NGUISettings.AddPanel(go);
		panel.clipping = UIDrawCall.Clipping.SoftClip;
		panel.gameObject.AddComponent<UIScrollView>();
		Selection.activeGameObject = panel.gameObject;
	}

	/// <summary>
	/// Add help options based on the components present on the specified game object.
	/// </summary>

	static public void AddHelp (GameObject go, bool addSeparator)
	{
		MonoBehaviour[] comps = Selection.activeGameObject.GetComponents<MonoBehaviour>();

		bool addedSomething = false;

		for (int i = 0; i < comps.Length; ++i)
		{
			System.Type type = comps[i].GetType();
			string url = NGUIHelp.GetHelpURL(type);
			
			if (url != null)
			{
				if (addSeparator)
				{
					addSeparator = false;
					AddSeparator("");
				}

				AddItem("Help/" + type, false, delegate(object obj) { Application.OpenURL(url); }, null);
				addedSomething = true;
			}
		}

		if (addedSomething) AddSeparator("Help/");
		AddItem("Help/All Topics", false, delegate(object obj) { NGUIHelp.Show(); }, null);
	}

	static void OnHelp (object obj) { NGUIHelp.Show(obj); }
	static void OnMakePixelPerfect (object obj) { NGUITools.MakePixelPerfect(obj as Transform); }
	static void OnBoxCollider (object obj) { NGUITools.AddWidgetCollider(obj as GameObject); }
	static void OnDelete (object obj)
	{
		GameObject go = obj as GameObject;
		Selection.activeGameObject = go.transform.parent.gameObject;
#if UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
		NGUITools.Destroy(go);
#else
		Undo.DestroyObjectImmediate(go);
#endif
	}

	/// <summary>
	/// Add a new disabled context menu entry.
	/// </summary>

	static public void AddDisabledItem (string item)
	{
		if (mMenu == null) mMenu = new GenericMenu();
		mMenu.AddDisabledItem(new GUIContent(item));
	}

	/// <summary>
	/// Add a separator to the menu.
	/// </summary>

	static public void AddSeparator (string path)
	{
		if (mMenu == null) mMenu = new GenericMenu();

		// For some weird reason adding separators on OSX causes the entire menu to be disabled. Wtf?
		if (Application.platform != RuntimePlatform.OSXEditor)
			mMenu.AddSeparator(path);
	}

	/// <summary>
	/// Show the context menu with all the added items.
	/// </summary>

	static public void Show ()
	{
		if (mMenu != null)
		{
			mMenu.ShowAsContext();
			mMenu = null;
			mEntries.Clear();
		}
	}
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Scripts/Editor/NGUIContextMenu.cs

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