summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Menu/PortLegendInfo.cs
blob: 9f76a512adca3e196552dd72ac28cfcf27c7f74b (plain)
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
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

namespace AmplifyShaderEditor
{
	class NodeDescriptionInfo
	{
		public bool FoldoutValue;
		public string Category;
		public string[,] Contents;
	}

	public sealed class PortLegendInfo : EditorWindow
	{
		private const string NoASEWindowWarning = "Please Open the ASE to get access to shortcut info";
		private const float PixelSeparator = 5;
		private const string EditorShortcutsTitle = "Editor Shortcuts";
		private const string MenuShortcutsTitle = "Menu Shortcuts";
		private const string NodesShortcutsTitle = "Nodes Shortcuts";
		private const string PortShortcutsTitle = "Port Shortcuts";
		private const string PortLegendTitle = "Port Legend";
		private const string NodesDescTitle = "Node Info";
		private const string CompatibleAssetsTitle = "Compatible Assets";

		private const string KeyboardUsageTemplate = "[{0}] - {1}";
		private const string m_lockedStr = "Locked Port";

		private const float WindowSizeX = 350;
		private const float WindowSizeY = 300;
		private const float WindowPosX = 5;
		private const float WindowPosY = 5;

		private int TitleLabelWidth = 150;
		private Rect m_availableArea;

		private bool m_portAreaFoldout = true;
		private bool m_editorShortcutAreaFoldout = true;
		private bool m_menuShortcutAreaFoldout = true;
		private bool m_nodesShortcutAreaFoldout = true;
		private bool m_nodesDescriptionAreaFoldout = true;
		private bool m_compatibleAssetsFoldout = true;

		private Vector2 m_currentScrollPos;

		private GUIStyle m_portStyle;
		private GUIStyle m_labelStyleBold;
		private GUIStyle m_labelStyle;

		private GUIStyle m_nodeInfoLabelStyleBold;
		private GUIStyle m_nodeInfoLabelStyle;

		private GUIStyle m_nodeInfoFoldoutStyle;

		private GUIContent m_content = new GUIContent( "Helper", "Shows helper info for ASE users" );
		private bool m_init = true;

		private List<ShortcutItem> m_editorShortcuts = null;
		private List<ShortcutItem> m_nodesShortcuts = null;
		private List<NodeDescriptionInfo> m_nodeDescriptionsInfo = null;
		private List<string[]> m_compatibleAssetsInfo = null;

		public static PortLegendInfo OpenWindow()
		{
			PortLegendInfo currentWindow = ( PortLegendInfo ) PortLegendInfo.GetWindow( typeof( PortLegendInfo ), false );
			currentWindow.minSize = new Vector2( WindowSizeX, WindowSizeY );
			currentWindow.maxSize = new Vector2( WindowSizeX * 2, 2 * WindowSizeY ); ;
			currentWindow.wantsMouseMove = true;
			return currentWindow;
		}

		public void Init()
		{
			m_init = false;
			wantsMouseMove = false;
			titleContent = m_content;
			m_portStyle = new GUIStyle( UIUtils.GetCustomStyle( CustomStyle.PortEmptyIcon ) );
			m_portStyle.alignment = TextAnchor.MiddleLeft;
			m_portStyle.imagePosition = ImagePosition.ImageOnly;
			m_portStyle.margin = new RectOffset( 5, 0, 5, 0 );

			m_labelStyleBold = new GUIStyle( UIUtils.InputPortLabel );
			m_labelStyleBold.fontStyle = FontStyle.Bold;
			m_labelStyleBold.fontSize = ( int ) ( Constants.TextFieldFontSize );


			m_labelStyle = new GUIStyle( UIUtils.InputPortLabel );
			m_labelStyle.clipping = TextClipping.Overflow;
			m_labelStyle.imagePosition = ImagePosition.TextOnly;
			m_labelStyle.contentOffset = new Vector2( -10, 0 );
			m_labelStyle.fontSize = ( int ) ( Constants.TextFieldFontSize );

			m_nodeInfoLabelStyleBold = new GUIStyle( UIUtils.InputPortLabel );
			m_nodeInfoLabelStyleBold.fontStyle = FontStyle.Bold;
			m_nodeInfoLabelStyleBold.fontSize = ( int ) ( Constants.TextFieldFontSize );

			m_nodeInfoLabelStyle = new GUIStyle( UIUtils.InputPortLabel );
			m_nodeInfoLabelStyle.clipping = TextClipping.Clip;
			m_nodeInfoLabelStyle.imagePosition = ImagePosition.TextOnly;
			m_nodeInfoLabelStyle.fontSize = ( int ) ( Constants.TextFieldFontSize );
			

			m_nodeInfoFoldoutStyle = new GUIStyle( ( GUIStyle ) "foldout" );
			m_nodeInfoFoldoutStyle.fontStyle = FontStyle.Bold;

			if ( !EditorGUIUtility.isProSkin )
			{
				m_labelStyleBold.normal.textColor = m_labelStyle.normal.textColor = Color.black;
				m_nodeInfoLabelStyleBold.normal.textColor = m_labelStyle.normal.textColor = Color.black;
				m_nodeInfoLabelStyle.normal.textColor = m_labelStyle.normal.textColor = Color.black;
			}

			m_availableArea = new Rect( WindowPosX, WindowPosY, WindowSizeX - 2 * WindowPosX, WindowSizeY - 2 * WindowPosY );
		}

		void DrawPort( WirePortDataType type )
		{
			EditorGUILayout.BeginHorizontal();
			{
				GUI.color = UIUtils.GetColorForDataType( type, false );
				GUILayout.Box( string.Empty, m_portStyle, GUILayout.Width( UIUtils.PortsSize.x ), GUILayout.Height( UIUtils.PortsSize.y ) );
				GUI.color = Color.white;
				EditorGUILayout.LabelField( UIUtils.GetNameForDataType( type ), m_labelStyle );
			}
			EditorGUILayout.EndHorizontal();
			EditorGUILayout.Separator();
		}

		void OnGUI()
		{
			if ( !UIUtils.Initialized || UIUtils.CurrentWindow == null )
			{
				EditorGUILayout.LabelField( NoASEWindowWarning );
				return;
			}

			if ( m_init )
			{
				Init();
			}

			TitleLabelWidth = (int)(this.position.width * 0.42f);

			KeyCode key = Event.current.keyCode;
			if ( key == ShortcutsManager.ScrollUpKey )
			{
				m_currentScrollPos.y -= 10;
				if ( m_currentScrollPos.y < 0 )
				{
					m_currentScrollPos.y = 0;
				}
				Event.current.Use();
			}

			if ( key == ShortcutsManager.ScrollDownKey )
			{
				m_currentScrollPos.y += 10;
				Event.current.Use();
			}

			if ( Event.current.type == EventType.MouseDrag && Event.current.button > 0 )
			{
				m_currentScrollPos.x += Constants.MenuDragSpeed * Event.current.delta.x;
				if ( m_currentScrollPos.x < 0 )
				{
					m_currentScrollPos.x = 0;
				}

				m_currentScrollPos.y += Constants.MenuDragSpeed * Event.current.delta.y;
				if ( m_currentScrollPos.y < 0 )
				{
					m_currentScrollPos.y = 0;
				}
			}

			m_availableArea = new Rect( WindowPosX, WindowPosY, position.width - 2 * WindowPosX, position.height - 2 * WindowPosY );
			GUILayout.BeginArea( m_availableArea );
			{
				if ( GUILayout.Button( "Wiki Page" ) )
				{
					Application.OpenURL( Constants.HelpURL );
				}

				m_currentScrollPos = GUILayout.BeginScrollView( m_currentScrollPos );
				{
					EditorGUILayout.BeginVertical();
					{
						NodeUtils.DrawPropertyGroup( ref m_portAreaFoldout, PortLegendTitle, DrawPortInfo );
						float currLabelWidth = EditorGUIUtility.labelWidth;
						EditorGUIUtility.labelWidth = 1;
						NodeUtils.DrawPropertyGroup( ref m_editorShortcutAreaFoldout, EditorShortcutsTitle, DrawEditorShortcuts );
						NodeUtils.DrawPropertyGroup( ref m_menuShortcutAreaFoldout, MenuShortcutsTitle, DrawMenuShortcuts );
						NodeUtils.DrawPropertyGroup( ref m_nodesShortcutAreaFoldout, NodesShortcutsTitle, DrawNodesShortcuts );
						NodeUtils.DrawPropertyGroup( ref m_compatibleAssetsFoldout, CompatibleAssetsTitle, DrawCompatibleAssets );
						NodeUtils.DrawPropertyGroup( ref m_nodesDescriptionAreaFoldout, NodesDescTitle, DrawNodeDescriptions );
						EditorGUIUtility.labelWidth = currLabelWidth;
					}
					EditorGUILayout.EndVertical();
				}
				GUILayout.EndScrollView();
			}
			GUILayout.EndArea();

		}

		void DrawPortInfo()
		{
			Color originalColor = GUI.color;

			DrawPort( WirePortDataType.OBJECT );
			DrawPort( WirePortDataType.INT );
			DrawPort( WirePortDataType.FLOAT );
			DrawPort( WirePortDataType.FLOAT2 );
			DrawPort( WirePortDataType.FLOAT3 );
			DrawPort( WirePortDataType.FLOAT4 );
			DrawPort( WirePortDataType.COLOR );
			DrawPort( WirePortDataType.SAMPLER2D );
			DrawPort( WirePortDataType.FLOAT3x3 );
			DrawPort( WirePortDataType.FLOAT4x4 );

			EditorGUILayout.BeginHorizontal();
			{
				GUI.color = Constants.LockedPortColor;
				GUILayout.Box( string.Empty, m_portStyle, GUILayout.Width( UIUtils.PortsSize.x ), GUILayout.Height( UIUtils.PortsSize.y ) );
				GUI.color = Color.white;
				EditorGUILayout.LabelField( m_lockedStr, m_labelStyle );
			}
			EditorGUILayout.EndHorizontal();

			GUI.color = originalColor;
		}

		public void DrawEditorShortcuts()
		{
			AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
			if ( window != null )
			{
				if ( m_editorShortcuts == null )
				{
					m_editorShortcuts = window.ShortcutManagerInstance.AvailableEditorShortcutsList;
				}

				EditorGUI.indentLevel--;
				int count = m_editorShortcuts.Count;
				for ( int i = 0; i < count; i++ )
				{
					DrawItem( m_editorShortcuts[ i ].Name, m_editorShortcuts[ i ].Description );
				}
				DrawItem( "Q", "Alternative Pan modifier" );
				DrawItem( "Ctrl + F", "Find nodes" );
				DrawItem( "LMB Drag", "Box selection" );
				DrawItem( "MMB/RMB Drag", "Camera pan" );
				DrawItem( "Alt + MMB/RMB Drag", "Zoom graph" );
				DrawItem( "Shift/Ctrl + Node Select", "Add/Remove from selection" );
				DrawItem( "Shift + Node Drag", "Node move with offset" );
				DrawItem( "Ctrl + Node Drag", "Node move with snap" );
				DrawItem( "MMB/RMB + Drag Panel", "Scroll panel" );
				DrawItem( "Alt + LMB Drag", "Additive box selection" );
				DrawItem( "Alt + Shift + Drag", "Subtractive box selection" );
				DrawItem( "Alt + Node Drag", "Auto-(Dis)Connect node on existing wire connection" );
				EditorGUI.indentLevel++;

			}
			else
			{
				EditorGUILayout.LabelField( NoASEWindowWarning );
			}
		}

		public void DrawMenuShortcuts()
		{
			AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
			if ( window != null )
			{
				EditorGUI.indentLevel--;
				DrawItem( ShortcutsManager.ScrollUpKey.ToString(), "Scroll Up Menu" );
				DrawItem( ShortcutsManager.ScrollDownKey.ToString(), "Scroll Down Menu" );
				DrawItem( "RMB Drag", "Scroll Menu" );
				EditorGUI.indentLevel++;
			}
			else
			{
				EditorGUILayout.LabelField( NoASEWindowWarning );
			}
		}

		void DrawItem( string name, string description )
		{
			GUILayout.BeginHorizontal();
			GUILayout.Label( name, m_labelStyleBold , GUILayout.Width( TitleLabelWidth ) );
			GUILayout.Label( description, m_labelStyle );
			GUILayout.EndHorizontal();
			GUILayout.Space( PixelSeparator );
		}

		public void DrawNodesShortcuts()
		{
			AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
			if ( window != null )
			{
				if ( m_nodesShortcuts == null || m_nodesShortcuts.Count == 0 )
				{
					m_nodesShortcuts = window.ShortcutManagerInstance.AvailableNodesShortcutsList;
				}

				EditorGUI.indentLevel--;
				int count = m_nodesShortcuts.Count;
				for ( int i = 0; i < count; i++ )
				{
					DrawItem( m_nodesShortcuts[ i ].Name, m_nodesShortcuts[ i ].Description );
				}
				EditorGUI.indentLevel++;
			}
			else
			{
				EditorGUILayout.LabelField( NoASEWindowWarning );
			}
		}
		string CreateCompatibilityString( string source )
		{
			string[] split = source.Split( '.' );
			if ( split != null && split.Length > 1 )
			{
				return split[ 1 ];
			}
			else
			{
				return source;
			}
		}
		public void DrawCompatibleAssets()
		{
			AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
			if ( window != null )
			{
				if ( m_compatibleAssetsInfo == null )
				{
					m_compatibleAssetsInfo = new List<string[]>();
					List<ContextMenuItem> items = window.ContextMenuInstance.MenuItems;
					int count = items.Count;
					for ( int i = 0; i < count; i++ )
					{
						if ( items[ i ].NodeAttributes != null && items[ i ].NodeAttributes.CastType != null )
						{
							string types = string.Empty;
							if ( items[ i ].NodeAttributes.CastType.Length > 1 )
							{
								for ( int j = 0; j < items[ i ].NodeAttributes.CastType.Length; j++ )
								{
									types += CreateCompatibilityString( items[ i ].NodeAttributes.CastType[ j ].ToString() );


									if ( j < items[ i ].NodeAttributes.CastType.Length - 1 )
									{
										types += ", ";
									}
								}
							}
							else
							{
								types = CreateCompatibilityString( items[ i ].NodeAttributes.CastType[ 0 ].ToString() );
							}
							m_compatibleAssetsInfo.Add( new string[] { items[ i ].NodeAttributes.Name + ":   ", types } );
						}
					}
				}
				EditorGUI.indentLevel--;
				int nodeCount = m_compatibleAssetsInfo.Count;
				for ( int j = 0; j < nodeCount; j++ )
				{
					DrawItem( m_compatibleAssetsInfo[ j ][ 0 ], m_compatibleAssetsInfo[ j ][ 1 ] );
				}
				EditorGUI.indentLevel++;
			}
			else
			{
				EditorGUILayout.LabelField( NoASEWindowWarning );
			}
		}

		public void DrawNodeDescriptions()
		{
			AmplifyShaderEditorWindow window = UIUtils.CurrentWindow;
			if ( window != null )
			{
				if ( m_nodeDescriptionsInfo == null )
				{
					//fetch node info
					m_nodeDescriptionsInfo = new List<NodeDescriptionInfo>();
					Dictionary<string, PaletteFilterData> nodeData = window.CurrentPaletteWindow.BuildFullList();
					var enumerator = nodeData.GetEnumerator();
					while ( enumerator.MoveNext() )
					{
						List<ContextMenuItem> nodes = enumerator.Current.Value.Contents;
						int count = nodes.Count;

						NodeDescriptionInfo currInfo = new NodeDescriptionInfo();
						currInfo.Contents = new string[ count, 2 ];
						currInfo.Category = enumerator.Current.Key;

						for ( int i = 0; i < count; i++ )
						{
							currInfo.Contents[ i, 0 ] = nodes[ i ].Name + ':';
							currInfo.Contents[ i, 1 ] = nodes[ i ].Description;
						}
						m_nodeDescriptionsInfo.Add( currInfo );
					}
				}

				//draw
				{
					GUILayout.Space( 5 );
					int count = m_nodeDescriptionsInfo.Count;
					EditorGUI.indentLevel--;
					for ( int i = 0; i < count; i++ )
					{
						m_nodeDescriptionsInfo[ i ].FoldoutValue = EditorGUILayout.Foldout( m_nodeDescriptionsInfo[ i ].FoldoutValue, m_nodeDescriptionsInfo[ i ].Category, m_nodeInfoFoldoutStyle );
						if ( m_nodeDescriptionsInfo[ i ].FoldoutValue )
						{
							EditorGUI.indentLevel++;
							int nodeCount = m_nodeDescriptionsInfo[ i ].Contents.GetLength( 0 );
							for ( int j = 0; j < nodeCount; j++ )
							{
								GUILayout.Label( m_nodeDescriptionsInfo[ i ].Contents[ j, 0 ], m_nodeInfoLabelStyleBold );
								GUILayout.Label( m_nodeDescriptionsInfo[ i ].Contents[ j, 1 ], m_nodeInfoLabelStyle );
								GUILayout.Space( PixelSeparator );
							}
							EditorGUI.indentLevel--;
						}
						GUILayout.Space( PixelSeparator );
					}
					EditorGUI.indentLevel++;
				}
			}
			else
			{
				EditorGUILayout.LabelField( NoASEWindowWarning );
			}
		}

		private void OnDestroy()
		{
			m_nodesShortcuts = null;
			m_editorShortcuts = null;
			m_portStyle = null;
			m_labelStyle = null;
			m_labelStyleBold = null;
			m_nodeInfoLabelStyle = null;
			m_nodeInfoLabelStyleBold = null;
			m_nodeInfoFoldoutStyle = null;
			m_init = false;

			if ( m_nodeDescriptionsInfo != null )
			{
				m_nodeDescriptionsInfo.Clear();
				m_nodeDescriptionsInfo = null;
			}

			if( m_compatibleAssetsInfo != null )
			{
				m_compatibleAssetsInfo.Clear();
				m_compatibleAssetsInfo = null;
			}
		}
	}
}