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

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

namespace AmplifyShaderEditor
{
	[Serializable]
	public class DuplicatePreventionBuffer
	{
		private const string VectorNameStr = "Vector ";
		private const string TextureSampleNameStr = "Texture Sample ";
		private const string MatrixNameStr = "Matrix ";
		private const string IntNameStr = "Int ";
		private const string FloatNameStr = "Float ";
		private const string ColorNameStr = "Color ";
		
		[SerializeField]
		private int[] m_availableUVChannelsArray = { -1, -1, -1, -1 };
		private string[] m_availableUVChannelsNamesArray = { "null",
															"null",
															"null",
															"null" };

		private Dictionary<string, int> m_availablePropertyNames = new Dictionary<string, int>();
		private Dictionary<string, int> m_availableUniformNames = new Dictionary<string, int>();
		private Dictionary<string, int> m_availableLocalVariableNames = new Dictionary<string, int>();

		public void ReleaseAllUVChannels()
		{
			for ( int i = 0; i < m_availableUVChannelsArray.Length; i++ )
			{
				m_availableUVChannelsArray[ i ] = -1;
			}
		}

		public bool RegisterUVChannel( int nodeId, int channelId, string name )
		{
			if ( channelId < 0 ||
					channelId > ( m_availableUVChannelsArray.Length - 1 ) ||
					m_availableUVChannelsArray[ channelId ] >= 0 )
			{
				return false;
			}

			m_availableUVChannelsArray[ channelId ] = nodeId;
			m_availableUVChannelsNamesArray[ channelId ] = name;
			return true;
		}


		public bool ReleaseUVChannel( int nodeId, int channelId )
		{
			if ( channelId < 0 ||
				channelId > ( m_availableUVChannelsArray.Length - 1 ) )
			{
				return false;
			}

			if ( m_availableUVChannelsArray[ channelId ] == nodeId )
			{
				m_availableUVChannelsArray[ channelId ] = -1;
				return true;
			}
			return false;
		}

		public int RegisterFirstAvailableChannel( int nodeId , string name)
		{
			for ( int i = 0; i < m_availableUVChannelsArray.Length; i++ )
			{
				if ( m_availableUVChannelsArray[ i ] == -1 )
				{
					m_availableUVChannelsArray[ i ] = nodeId;
					m_availableUVChannelsNamesArray[ i ] = name;
					return i;
				}
			}
			return -1;
		}

		public bool IsChannelAvailable( int channelId )
		{
			if ( channelId < 0 ||
				channelId > ( m_availableUVChannelsArray.Length - 1 ) )
			{
				return false;
			}

			return ( m_availableUVChannelsArray[ channelId ] < 0 );
		}

		public int GetFirstOccupiedChannel()
		{
			for ( int i = 0; i < 4; i++ )
			{
				if ( m_availableUVChannelsArray[ i ] > -1 )
					return i;
			}
			return -1;
		}

		public string GetChannelName( int channelId )
		{
			if ( channelId < 0 ||
				channelId > ( m_availableUVChannelsArray.Length - 1 ) )
			{
				return string.Empty;
			}

			return m_availableUVChannelsNamesArray[ channelId ] ;
		}

		public void SetChannelName( int channelId , string name )
		{
			if ( channelId < 0 ||
				channelId > ( m_availableUVChannelsArray.Length - 1 ) )
			{
				return;
			}
			 m_availableUVChannelsNamesArray[ channelId ] = name;
		}

		public bool RegisterLocalVariableName( int nodeId, string name )
		{
			if ( name.Length == 0 )
				return false;

			if ( m_availableLocalVariableNames.ContainsKey( name ) )
			{
				if ( m_availableLocalVariableNames[ name ] > -1 )
				{
					return false;
				}
				else
				{
					m_availableLocalVariableNames[ name ] = nodeId;
					return true;
				}
			}

			m_availableLocalVariableNames.Add( name, nodeId );
			return true;
		}

		public int CheckUniformNameOwner( string name )
		{
			if ( name.Length == 0 )
				return -1;

			if ( m_availableUniformNames.ContainsKey( name ) )
			{
				return m_availableUniformNames[ name ];
			}

			return -1;
		}

		public bool RegisterUniformName( int nodeId, string name )
		{
			if ( name.Length == 0 )
				return false;

			if ( m_availableUniformNames.ContainsKey( name ) )
			{
				if ( m_availableUniformNames[ name ] > -1 )
				{
					return false;
				}
				else
				{
					m_availableUniformNames[ name ] = nodeId;
					return true;
				}
			}
			
			m_availableUniformNames.Add( name, nodeId );
			return true;
		}

		public void DumpUniformNames()
		{
			string val = "CONTENTS\n";
			foreach ( KeyValuePair<string, int> kvp in m_availableUniformNames )
			{
				val += ( "key " + kvp.Key + " : value " + kvp.Value + "\n" );
			}
		}

		public void DumpLocalVariableNames()
		{
			string val = "CONTENTS\n";
			foreach ( KeyValuePair<string, int> kvp in m_availableLocalVariableNames )
			{
				val += ( "key " + kvp.Key + " : value " + kvp.Value + "\n" );
			}
		}


		public bool ReleaseUniformName( int nodeId, string name )
		{
			if ( !string.IsNullOrEmpty(name) && name.Length == 0 )
				return false;

			if ( m_availableUniformNames.ContainsKey( name ) )
			{
				if ( m_availableUniformNames[ name ] == nodeId )
				{
					m_availableUniformNames.Remove( name );
					return true;
				}
			}
			return false;
		}

		public bool ReleaseLocalVariableName( int nodeId, string name )
		{
			if ( name.Length == 0 )
				return false;

			if ( m_availableLocalVariableNames.ContainsKey( name ) )
			{
				if ( m_availableLocalVariableNames[ name ] == nodeId )
				{
					m_availableLocalVariableNames.Remove( name );
					return true;
				}
			}
			return false;
		}

		public void ReleaseAllUniformNames()
		{
			m_availableUniformNames.Clear();
		}

		public void ReleaseAllLocalVariableNames()
		{
			m_availableLocalVariableNames.Clear();
		}

		public void GetFirstAvailableName( int nodeId, WirePortDataType type , out string outProperty , out string outInspector, bool useCustomPrefix = false, string customPrefix = null)
		{
			string name = string.Empty;
			if ( useCustomPrefix && customPrefix != null )
			{
				name = customPrefix;
			}
			else
			{
				switch ( type )
				{
					case WirePortDataType.OBJECT:
					case WirePortDataType.FLOAT:
					{
						name = FloatNameStr;
					}
					break;
					case WirePortDataType.INT:
					{
						name = IntNameStr;
					}
					break;
					case WirePortDataType.FLOAT2:
					case WirePortDataType.FLOAT3:
					case WirePortDataType.FLOAT4:
					{
						name = VectorNameStr;
					}
					break;
					case WirePortDataType.FLOAT3x3:
					case WirePortDataType.FLOAT4x4:
					{
						name = MatrixNameStr;
					}
					break;
					case WirePortDataType.COLOR:
					{
						name = ColorNameStr;
					}
					break;
				}
			}

			int count = 0;
			bool foundName = false;
			while ( !foundName )
			{
				string inspectorName = name + count;
				string propertyName =  UIUtils.GeneratePropertyName( inspectorName , PropertyType.Property );
				
				if ( IsUniformNameAvailable( propertyName ) )
				{
					outInspector = inspectorName;
					outProperty = propertyName;
					RegisterUniformName( nodeId, propertyName );
					return;
				}
				count += 1;
			}
			outProperty = string.Empty;
			outInspector = string.Empty;
			UIUtils.ShowMessage( "Could not find a valid name " + MessageSeverity.Warning );
		}

		public bool IsUniformNameAvailable( string name )
		{
			if ( m_availableUniformNames.ContainsKey( name ) && m_availableUniformNames[ name ] > -1 )
				return false;
			return true;
		}

		public bool IsLocalvariableNameAvailable( string name )
		{
			if ( m_availableLocalVariableNames.ContainsKey( name ) && m_availableLocalVariableNames[ name ] > -1 )
				return false;
			return true;
		}

		public bool GetPropertyName( int nodeId, string name )
		{
			if ( m_availablePropertyNames.ContainsKey( name ) )
			{
				if ( m_availablePropertyNames[ name ] > -1 )
				{
					return false;
				}
				else
				{
					m_availablePropertyNames[ name ] = nodeId;
					return true;
				}
			}

			m_availablePropertyNames.Add( name, nodeId );
			return true;
		}


		public bool ReleasePropertyName( int nodeId, string name )
		{
			if ( m_availablePropertyNames.ContainsKey( name ) )
			{
				if ( m_availablePropertyNames[ name ] == nodeId )
				{
					m_availablePropertyNames[ name ] = -1;
					return true;
				}
			}
			return false;
		}

		public void ReleaseAllPropertyNames()
		{
			m_availablePropertyNames.Clear();
		}

		public bool IsPropertyNameAvailable( string name )
		{
			if ( m_availablePropertyNames.ContainsKey( name ) && m_availablePropertyNames[ name ] > -1 )
				return false;
			return true;
		}

		public void ReleaseAllData()
		{
			ReleaseAllUVChannels();
			ReleaseAllUniformNames();
			ReleaseAllPropertyNames();
			ReleaseAllLocalVariableNames();
		}
	}
}